1919#if DEVICE_FLASH
2020#include "mbed_assert.h"
2121#include "cmsis.h"
22- #include "stdio.h"
2322
2423/**
2524 * @brief Gets the bank of a given address
@@ -94,12 +93,12 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address)
9493 return -1 ;
9594 }
9695
97- if (HAL_ICACHE_Disable () != HAL_OK )
98- {
96+ if (HAL_FLASH_Unlock () != HAL_OK ) {
9997 return -1 ;
10098 }
10199
102- if (HAL_FLASH_Unlock () != HAL_OK ) {
100+ if (HAL_ICACHE_Disable () != HAL_OK )
101+ {
103102 return -1 ;
104103 }
105104
@@ -115,23 +114,18 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address)
115114 EraseInitStruct .Sector = GetSector (address );
116115 EraseInitStruct .NbSectors = 1 ;
117116
118- /* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache,
119- you have to make sure that these data are rewritten before they are accessed during code
120- execution. If this cannot be done safely, it is recommended to flush the caches by setting the
121- DCRST and ICRST bits in the FLASH_CR register. */
122-
123117 if (HAL_FLASHEx_Erase (& EraseInitStruct , & PAGEError ) != HAL_OK ) {
124118 status = -1 ;
125119 }
126120
127121 core_util_critical_section_exit ();
128122
129- if (HAL_FLASH_Lock () != HAL_OK ) {
123+ if (HAL_ICACHE_Enable () != HAL_OK )
124+ {
130125 return -1 ;
131126 }
132127
133- if (HAL_ICACHE_Enable () != HAL_OK )
134- {
128+ if (HAL_FLASH_Lock () != HAL_OK ) {
135129 return -1 ;
136130 }
137131
@@ -164,12 +158,12 @@ int32_t flash_program_page(flash_t *obj, uint32_t address,
164158 return -1 ;
165159 }
166160
167- if (HAL_ICACHE_Disable () != HAL_OK )
168- {
161+ if (HAL_FLASH_Unlock () != HAL_OK ) {
169162 return -1 ;
170163 }
171164
172- if (HAL_FLASH_Unlock () != HAL_OK ) {
165+ if (HAL_ICACHE_Disable () != HAL_OK )
166+ {
173167 return -1 ;
174168 }
175169
@@ -179,42 +173,23 @@ int32_t flash_program_page(flash_t *obj, uint32_t address,
179173 /* Program the user Flash area word by word */
180174 StartAddress = address ;
181175
182- /* HW needs an aligned address to program flash, which data
183- * parameters doesn't ensure */
184- if ((uint32_t ) data % 16 != 0 ) {
185- volatile uint64_t data128 [2 ];
186- while ((address < (StartAddress + size )) && (status == 0 )) {
187- for (uint8_t i = 0 ; i < 16 ; i ++ ) {
188- * (((uint8_t * ) data128 ) + i ) = * (data + i );
189- }
190-
191- if (HAL_FLASH_Program (FLASH_TYPEPROGRAM_QUADWORD , address , (uint32_t ) data128 )
192- == HAL_OK ) {
193- address = address + 16 ;
194- data = data + 16 ;
195- } else {
196- status = -1 ;
197- }
198- }
199- } else { /* case where data is aligned, so let's avoid any copy */
200- while ((address < (StartAddress + size )) && (status == 0 )) {
201- if (HAL_FLASH_Program (FLASH_TYPEPROGRAM_QUADWORD , address ,
202- (uint32_t ) data )
203- == HAL_OK ) {
204- address = address + 16 ;
205- data = data + 16 ;
206- } else {
207- status = -1 ;
208- }
176+ while ((address < (StartAddress + size )) && (status == 0 )) {
177+ if (HAL_FLASH_Program (FLASH_TYPEPROGRAM_QUADWORD , address ,
178+ (uint32_t ) data )
179+ == HAL_OK ) {
180+ address = address + 16 ;
181+ data = data + 16 ;
182+ } else {
183+ status = -1 ;
209184 }
210185 }
211186
212- if (HAL_FLASH_Lock () != HAL_OK ) {
187+ if (HAL_ICACHE_Enable () != HAL_OK )
188+ {
213189 return -1 ;
214190 }
215191
216- if (HAL_ICACHE_Enable () != HAL_OK )
217- {
192+ if (HAL_FLASH_Lock () != HAL_OK ) {
218193 return -1 ;
219194 }
220195
@@ -274,4 +249,4 @@ uint8_t flash_get_erase_value(const flash_t *obj)
274249 return 0xFF ;
275250}
276251
277- #endif
252+ #endif
0 commit comments