cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 - FLASH Erase

dibs
Associate II
Posted on December 11, 2013 at 20:54

I am attempting to erase a FLASH sector, but the process hangs. Any ideas on why this might be happening?

    FLASH_Unlock();

    FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR |                 FLASH_FLAG_WRPERR | 

            FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR); 

    sector = GetSector(addr);

    if (FLASH_EraseSector(sector, VoltageRange_3) != FLASH_COMPLETE) { 

        // error (flash,erase);

        FLASH_Lock();

        return 1;

    }

#stm32f4 #flash #flash-erase-watchdog
12 REPLIES 12
jpeacock2399
Associate II
Posted on June 09, 2014 at 21:37

<< Here's the thing that seems weird to me: How can the function FLASH_WaitForLastOperation do anything at all? >>

If you have a hardware watchdog running while you erase the flash then you need to run the flash erase routine from SRAM, which doesn't stall.  Once the erase starts there's a polling loop along with periodic watchdog resets as needed. 

FLASH_WaitForLastOperation() is called from RAM to make sure a prior operation is done before the next flash command.

  Jack Peacock
munger
Associate II
Posted on June 11, 2014 at 13:25

Thanks for the response Jack. Obviously there is something here I still don't understand ... who says that FLASH_WaitForLastOperation() is called from RAM? How the heck do I ... put that function in RAM and run it there? There must be some method of copying code from flash to RAM and then running that code in RAM but I have not seen that described.

From what I can see in the documentation any timers are still going to run while in this wait state including the watchdog timer, right? I imagine one would either halt the watchdog timer or give it an extra big kick before starting a flash erase or flash write activity, yes?

Thanks.

Mike

jpeacock2399
Associate II
Posted on June 11, 2014 at 16:12

The catch is the IWDG can't be stopped, and it will time out if you are doing a large multi-sector erase.  If you use IWDG you have to erase from a RAM routine.

You can search the forum for ways to load code into RAM.  They are dependent on compiler and IDE.  The general principle is the same C runtime startup that initializes variables also copies code sections into RAM.

  Jack Peacock