Skip to content

Commit c337b9f

Browse files
aurel32nashif
authored andcommitted
drivers: flash: stm32h7x: use barriers to ensure the writes are flushed
The STM32H7 flash driver read-back a register after writing it to ensure it is flushed. This is very fragile and might break if a new compiler version slightly reorder the instructions. Instead use a __DSB() barrier like done on other STM32 SoC, which ensures that the registers and the data writes are flushed at the compiler level, but also at the Cortex-M7 level. Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
1 parent 67bceb0 commit c337b9f

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

drivers/flash/flash_stm32h7x.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ static struct flash_stm32_sector_t get_sector(const struct device *dev,
239239
static int erase_sector(const struct device *dev, int offset)
240240
{
241241
int rc;
242-
uint32_t tmp;
243242
struct flash_stm32_sector_t sector = get_sector(dev, offset);
244243

245244
if (sector.bank == 0) {
@@ -263,7 +262,7 @@ static int erase_sector(const struct device *dev, int offset)
263262
| ((sector.sector_index << FLASH_CR_SNB_Pos) & FLASH_CR_SNB));
264263
*(sector.cr) |= FLASH_CR_START;
265264
/* flush the register write */
266-
tmp = *(sector.cr);
265+
__DSB();
267266

268267
rc = flash_stm32_wait_flash_idle(dev);
269268
*(sector.cr) &= ~(FLASH_CR_SER | FLASH_CR_SNB);
@@ -314,7 +313,6 @@ static int write_ndwords(const struct device *dev,
314313
{
315314
volatile uint64_t *flash = (uint64_t *)(offset
316315
+ CONFIG_FLASH_BASE_ADDRESS);
317-
uint32_t tmp;
318316
int rc;
319317
int i;
320318
struct flash_stm32_sector_t sector = get_sector(dev, offset);
@@ -346,11 +344,15 @@ static int write_ndwords(const struct device *dev,
346344
*(sector.cr) |= FLASH_CR_PG;
347345

348346
/* Flush the register write */
349-
tmp = *(sector.cr);
347+
__DSB();
350348

351349
/* Perform the data write operation at the desired memory address */
352350
for (i = 0; i < n; ++i) {
353351
flash[i] = data[i];
352+
353+
/* Flush the data write */
354+
__DSB();
355+
354356
wait_write_queue(dev, offset);
355357
}
356358

0 commit comments

Comments
 (0)