Skip to main content
July 25, 2024
Solved

STM32f0. No FLASH_CR_PER clear after erasing flash in HAL_FLASH_IRQHandler ?

In stm32f0xx_hal_flash.c line 319:

 

 

 

 

/* Check if there are still pages to erase */
 if(pFlash.DataRemaining != 0U)
 {
 addresstmp = pFlash.Address;
 /*Indicate user which sector has been erased */
 HAL_FLASH_EndOfOperationCallback(addresstmp);

 /*Increment sector number*/
 addresstmp = pFlash.Address + FLASH_PAGE_SIZE;
 pFlash.Address = addresstmp;

 /* If the erase operation is completed, disable the PER Bit */
 CLEAR_BIT(FLASH->CR, FLASH_CR_PER);

 FLASH_PageErase(addresstmp);
 }
 else
 {
 /* No more pages to Erase, user callback can be called. */
 /* Reset Sector and stop Erase pages procedure */
 pFlash.Address = addresstmp = 0xFFFFFFFFU;
 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
 /* FLASH EOP interrupt user callback */
 HAL_FLASH_EndOfOperationCallback(addresstmp);
 CLEAR_BIT(FLASH->CR, FLASH_CR_PER); // ?????????For example here???????????
 }

 

 

 

 

After interrupt I expect PER bit to be cleared but it is not, because of this I can't program flash. Currently I clear this bit myself before programming flash, but is there any reason not to do it in HAL_FLASH_IRQHandler?

P.S. I spent a lot of time figuring this out. Maybe I'm not the only one.

This topic has been closed for replies.
TDK
TDKBest answer
July 26, 2024

It's done later on in HAL_FLASH_IRQHandler on line 407.

stm32f0xx_hal_driver/Src/stm32f0xx_hal_flash.c at 24bab79134c86de993df7b6e9c4f66dc73f1799a · STMicroelectronics/stm32f0xx_hal_driver (github.com)

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

2 replies

TDK
TDKBest answer
July 26, 2024
"If you feel a post has answered your question, please click ""Accept as Solution""."
July 26, 2024

This is indeed true. It turned out that I was locking the flash memory in the interrupt handler, and after that these bits cannot be set. That's why these lines did not work for me. Thank you.