1818
1919#if DEVICE_FLASH
2020#include "flash_api.h"
21- #include "flash_data.h"
2221#include "platform/mbed_critical.h"
2322
2423static uint32_t GetSector (uint32_t Address );
25- static uint32_t GetSectorSize (uint32_t Sector );
2624static uint32_t GetSectorBase (uint32_t SectorId , uint32_t BanksId );
2725
2826int32_t flash_init (flash_t * obj )
@@ -79,7 +77,7 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address)
7977 EraseInitStruct .Sector = GetSector (address );
8078 EraseInitStruct .NbSectors = 1 ;
8179
82- if (address < ADDR_FLASH_SECTOR_0_BANK2 ) {
80+ if (address < FLASH_BANK2_BASE ) {
8381 EraseInitStruct .Banks = FLASH_BANK_1 ;
8482 } else {
8583 EraseInitStruct .Banks = FLASH_BANK_2 ;
@@ -91,11 +89,11 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address)
9189
9290#if defined(DUAL_CORE )
9391#if defined(CORE_CM7 )
94- SCB_CleanInvalidateDCache_by_Addr ((uint32_t * )GetSectorBase (EraseInitStruct .Sector , EraseInitStruct .Banks ), GetSectorSize ( EraseInitStruct . Sector ) );
92+ SCB_CleanInvalidateDCache_by_Addr ((uint32_t * )GetSectorBase (EraseInitStruct .Sector , EraseInitStruct .Banks ), FLASH_SECTOR_SIZE );
9593 SCB_InvalidateICache ();
9694#endif /* CORE_CM7 */
9795#else /* DUAL_CORE */
98- SCB_CleanInvalidateDCache_by_Addr ((uint32_t * )GetSectorBase (EraseInitStruct .Sector , EraseInitStruct .Banks ), GetSectorSize ( EraseInitStruct . Sector ) );
96+ SCB_CleanInvalidateDCache_by_Addr ((uint32_t * )GetSectorBase (EraseInitStruct .Sector , EraseInitStruct .Banks ), FLASH_SECTOR_SIZE );
9997 SCB_InvalidateICache ();
10098#endif /* DUAL_CORE */
10199
@@ -119,11 +117,6 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
119117 return -1 ;
120118 }
121119
122- if ((size % 32 ) != 0 ) {
123- /* H7 flash devices can only be programmed 256bits/32 bytes at a time */
124- return -1 ;
125- }
126-
127120#if defined(DUAL_CORE )
128121 while (LL_HSEM_1StepLock (HSEM , CFG_HW_FLASH_SEMID )) {
129122 }
@@ -138,8 +131,8 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
138131 StartAddress = address ;
139132 while ((address < (StartAddress + size )) && (status == 0 )) {
140133 if (HAL_FLASH_Program (FLASH_TYPEPROGRAM_FLASHWORD , address , (uint32_t )data ) == HAL_OK ) {
141- address = address + 32 ;
142- data = data + 32 ;
134+ address = address + flash_get_page_size ( obj ) ;
135+ data = data + flash_get_page_size ( obj ) ;
143136 } else {
144137 status = -1 ;
145138 }
@@ -168,12 +161,12 @@ uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address)
168161 if ((address >= (FLASH_BASE + FLASH_SIZE )) || (address < FLASH_BASE )) {
169162 return MBED_FLASH_INVALID_SIZE ;
170163 }
171- return (GetSectorSize ( GetSector ( address )) );
164+ return (FLASH_SECTOR_SIZE );
172165}
173166
174167uint32_t flash_get_page_size (const flash_t * obj )
175168{
176- return (32 );
169+ return (FLASH_NB_32BITWORD_IN_FLASHWORD * 4 );
177170}
178171
179172uint32_t flash_get_start_address (const flash_t * obj )
@@ -195,43 +188,15 @@ static uint32_t GetSector(uint32_t Address)
195188{
196189 uint32_t sector = 0 ;
197190
198- if (((Address < ADDR_FLASH_SECTOR_1_BANK1 ) && (Address >= ADDR_FLASH_SECTOR_0_BANK1 )) || \
199- ((Address < ADDR_FLASH_SECTOR_1_BANK2 ) && (Address >= ADDR_FLASH_SECTOR_0_BANK2 ))) {
200- sector = FLASH_SECTOR_0 ;
201- } else if (((Address < ADDR_FLASH_SECTOR_2_BANK1 ) && (Address >= ADDR_FLASH_SECTOR_1_BANK1 )) || \
202- ((Address < ADDR_FLASH_SECTOR_2_BANK2 ) && (Address >= ADDR_FLASH_SECTOR_1_BANK2 ))) {
203- sector = FLASH_SECTOR_1 ;
204- } else if (((Address < ADDR_FLASH_SECTOR_3_BANK1 ) && (Address >= ADDR_FLASH_SECTOR_2_BANK1 )) || \
205- ((Address < ADDR_FLASH_SECTOR_3_BANK2 ) && (Address >= ADDR_FLASH_SECTOR_2_BANK2 ))) {
206- sector = FLASH_SECTOR_2 ;
207- } else if (((Address < ADDR_FLASH_SECTOR_4_BANK1 ) && (Address >= ADDR_FLASH_SECTOR_3_BANK1 )) || \
208- ((Address < ADDR_FLASH_SECTOR_4_BANK2 ) && (Address >= ADDR_FLASH_SECTOR_3_BANK2 ))) {
209- sector = FLASH_SECTOR_3 ;
210- } else if (((Address < ADDR_FLASH_SECTOR_5_BANK1 ) && (Address >= ADDR_FLASH_SECTOR_4_BANK1 )) || \
211- ((Address < ADDR_FLASH_SECTOR_5_BANK2 ) && (Address >= ADDR_FLASH_SECTOR_4_BANK2 ))) {
212- sector = FLASH_SECTOR_4 ;
213- } else if (((Address < ADDR_FLASH_SECTOR_6_BANK1 ) && (Address >= ADDR_FLASH_SECTOR_5_BANK1 )) || \
214- ((Address < ADDR_FLASH_SECTOR_6_BANK2 ) && (Address >= ADDR_FLASH_SECTOR_5_BANK2 ))) {
215- sector = FLASH_SECTOR_5 ;
216- } else if (((Address < ADDR_FLASH_SECTOR_7_BANK1 ) && (Address >= ADDR_FLASH_SECTOR_6_BANK1 )) || \
217- ((Address < ADDR_FLASH_SECTOR_7_BANK2 ) && (Address >= ADDR_FLASH_SECTOR_6_BANK2 ))) {
218- sector = FLASH_SECTOR_6 ;
191+ if (Address < (FLASH_BASE + FLASH_BANK_SIZE )) {
192+ sector = (Address - FLASH_BASE ) / FLASH_SECTOR_SIZE ;
219193 } else {
220- sector = FLASH_SECTOR_7 ;
194+ sector = ( Address - ( FLASH_BASE + FLASH_BANK_SIZE )) / FLASH_SECTOR_SIZE ;
221195 }
222196
223197 return sector ;
224198}
225199
226- /**
227- * @brief Gets sector Size
228- * @param None
229- * @retval The size of a given sector
230- */
231- static uint32_t GetSectorSize (uint32_t Sector )
232- {
233- return FLASH_SECTOR_SIZE ;
234- }
235200
236201/**
237202 * @brief Gets sector base address
@@ -251,7 +216,7 @@ static uint32_t GetSectorBase(uint32_t SectorId, uint32_t BanksId)
251216 }
252217
253218 for (i = 0 ; i < SectorId ; i ++ ) {
254- address_sector += GetSectorSize ( i ) ;
219+ address_sector += FLASH_SECTOR_SIZE ;
255220 }
256221 return address_sector ;
257222}
0 commit comments