2
votes

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
I think the read access to the (whole) flash is blocked during flash controller erase/write (page) operations. It’s probably different using a MCU with dual-banked flashes because there is a dedicated flash controller per bank. Should be documented in the TRM of your STM32 MCU. – HS2
@HS2 Using stm32f411 disco. From what I have seen, there is nothing so far in the datasheet explaining this. However in their reference sheet for their HAL, they explain that there are two versions of the erase. There is the Polling and blocking HAL_FLashEx_Erase and the non-blocking HAL_FLashEx_Erase_IT version. I believe this implies that it should not be doing what it is doing when using HAL_FLashEx_Erase_IT. – Hadi Jaber
@HadiJaber you can also run the code from RAM. RAM is not stalled so you need to detect the end of the operation somehow. Reference Manual is important not datasheet. – 0___________

2 Answers

4
votes

Documentation is clear:

enter image description here

Always read the documentation, not internet forums.

0
votes

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.