Erasing STM32 Flash Sector is hanging program

Viewed 2024

I am using FreeRtos and in one of the tasks I Erase a sector of the flash using the following code

HAL_FLASH_Unlock();
    // Fill EraseInit structure
        static FLASH_EraseInitTypeDef EraseInitStruct;
    EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS; 
    EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
    EraseInitStruct.Sector = sector;
    EraseInitStruct.NbSectors = numOfSectorsToErase;
HAL_FLASHEx_Erase_IT(&EraseInitStruct); 
  
    HAL_FLASH_Lock();

I thought this was a non-blocking invocation to erase the sector however when this is called, all other threads seem to be Preempted for 100 ms (as seen on oscilloscope) until the erase is completed. I must be doing something wrong because I am using the interrupt version of the erase. It shouldn't hang everything like this correct?

(I am sure that I am erasing the sector where the program code does not reside. Sector 6)

2 Answers

Documentation is clear:

enter image description here

Always read the documentation, not internet forums.

So it would seem that @Hs2 is Correct. Among further research, erasing a sector on the flash will Block execution as stated here https://community.st.com/s/question/0D50X00009XkXwuSAF/how-to-save-a-variable-in-nonvolatile-memory

saying "flashing blocks code execution" .

Now this brings up even more questions though like why in the world would the engineers at stm include a interrupt version of a sector erase when either way the invocation is going to be blocking anyways. Its very misleading. There seems to be no use case for this.

Related