2025-06-25 1:30 AM
Hi all,
I'm working on a project based on the STM32H7 and STM32G4 series, and I'm performing flash write and erase operations using the STM32 HAL functions:
HAL_FLASH_Program()
HAL_FLASHEx_Erase()
My question is:
Should these functions be placed into the .ramfunc section to avoid instruction fetch stalls or faults when writing/erasing flash on the same bank where code is executing?
Is there any direct feeback from ST about this topic?
On this topic I've the following observations:
I know that flash memory becomes unavailable during erase/programming, and the reference manual warns that executing from the same bank can cause CPU stalls or faults.
(from H7 RM: The embedded flash memory supports read-while-write operations provided the read and write operations target different banks)
However, in my testing:
I'm calling these HAL functions from flash, without placing them in RAM.
I'm writing to or erasing a sector in the same bank where my code resides (e.g. bootloader on sector 1 succesfully erase - and then re-program - all the other sectors of bank 1 with the application without ever encountering any problem so far on company history).
I haven’t encountered any faults or crashes so far.
What do you think about that?
2025-06-25 3:38 AM
Hello @MrJorge,
It is necessary to execute flash programming and erase routines from RAM to avoid issues when accessing the same flash bank.
The reference manuals (FLASH program and erase operations section) state that during a program or erase operation on a flash memory bank, any attempt to read from the same flash memory bank will stall the bus. The read operation will only proceed after the program/erase operation is completed. This means that if your code is running from the same bank being programmed or erased, the CPU will be stalled until the operation finishes.
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.