Skip to main content
LS813
Associate
Oct 28, 2025 1:23 PM UTC
Solved

Non-polling mode Flash Erasure

Hello, everyone. Can anyone tell me how? I want to do something while STM erases a sector, Since it is STM32F401, it takes too long to erase a sector. What can be the best solution?

This topic has been closed for replies.
TDK
TDKBest answer
Oct 28, 2025 1:32 PM UTC

Use the nonblocking HAL call HAL_FLASHEx_Erase_IT.

Be aware that you cannot erase a sector in the same bank as you're executing and still run code.

 

As @Ozone correctly notes below, the STM32F401 only has a single bank of flash. If you want to run code and erase a sector, that code will need to be in SRAM.

"If you feel a post has answered your question, please click ""Accept as Solution""."

4 replies

TDK
TDKBest answer
Oct 28, 2025 1:32 PM UTC

Use the nonblocking HAL call HAL_FLASHEx_Erase_IT.

Be aware that you cannot erase a sector in the same bank as you're executing and still run code.

 

As @Ozone correctly notes below, the STM32F401 only has a single bank of flash. If you want to run code and erase a sector, that code will need to be in SRAM.

"If you feel a post has answered your question, please click ""Accept as Solution""."
LS813
LS813Author
Associate
Oct 28, 2025 2:09 PM UTC

Thank you very much. One thing is unclear.

At which point do I have to unlock and lock the flash in case of using HAL_FLASHEx_Erase_IT?

HAL_FLASH_Unlock before HAL_FLASHEx_Erase_IT, and HAL_FLASH_Lock in FLASH_IRQ_Handler function?

 

TDK
Oct 28, 2025 2:22 PM UTC

> HAL_FLASH_Unlock before HAL_FLASHEx_Erase_IT, and HAL_FLASH_Lock in FLASH_IRQ_Handler function?

Yes, that works.

"If you feel a post has answered your question, please click ""Accept as Solution""."