Skip to content

Commit 5765c4d

Browse files
committed
TARGET_STM32F7: Refresh cache when erasing or programming flash
The cache must be refreshed when we erase or program flash memory. It fix 2 issues : Fix #9934 Fix #6380 This solution was initially proposed in #6380. Signed-off-by: Vincent Veron <[email protected]>
1 parent 1549c5c commit 5765c4d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

targets/TARGET_STM/TARGET_STM32F7/flash_api.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
static uint32_t GetSector(uint32_t Address);
3838
static uint32_t GetSectorSize(uint32_t Sector);
39+
static uint32_t GetSectorBase(uint32_t SectorId);
3940

4041
int32_t flash_init(flash_t *obj)
4142
{
@@ -130,6 +131,9 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address)
130131
status = -1;
131132
}
132133

134+
SCB_CleanInvalidateDCache_by_Addr((uint32_t *)GetSectorBase(SectorId), GetSectorSize(SectorId));
135+
SCB_InvalidateICache();
136+
133137
flash_lock();
134138

135139
return status;
@@ -139,6 +143,8 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
139143
uint32_t size)
140144
{
141145
int32_t status = 0;
146+
uint32_t StartAddress = address;
147+
uint32_t FullSize = size;
142148

143149
if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) {
144150
return -1;
@@ -167,6 +173,9 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
167173
}
168174
}
169175

176+
SCB_CleanInvalidateDCache_by_Addr((uint32_t *)StartAddress, FullSize);
177+
SCB_InvalidateICache();
178+
170179
flash_lock();
171180

172181
return status;
@@ -265,6 +274,23 @@ static uint32_t GetSectorSize(uint32_t Sector)
265274
return sectorsize;
266275
}
267276

277+
/**
278+
* @brief Gets sector base address
279+
* @param SectorId
280+
* @retval base address of a given sector
281+
*/
282+
static uint32_t GetSectorBase(uint32_t SectorId)
283+
{
284+
int i = 0;
285+
uint32_t address_sector = FLASH_BASE;
286+
287+
for(i=0;i<SectorId;i++){
288+
address_sector += GetSectorSize(i);
289+
}
290+
return address_sector;
291+
}
292+
293+
268294
uint8_t flash_get_erase_value(const flash_t *obj)
269295
{
270296
(void)obj;

0 commit comments

Comments
 (0)