2025-10-28 6:23 AM
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?
Solved! Go to Solution.
2025-10-28 6:32 AM - edited 2025-10-28 7:35 AM
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.
2025-10-28 6:32 AM - edited 2025-10-28 7:35 AM
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.
2025-10-28 7:09 AM
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?
2025-10-28 7:22 AM
> HAL_FLASH_Unlock before HAL_FLASHEx_Erase_IT, and HAL_FLASH_Lock in FLASH_IRQ_Handler function?
Yes, that works.
2025-10-28 7:29 AM
While I can't find any clue in the datasheet, I strongly suspect the F401, being at the cheaper end, is a single-bank design.
I find it very unfortunate the ST obscures this important criterium on most sites and documents.