cancel
Showing results for 
Search instead for 
Did you mean: 

Flash sector erase is taking too long

td
Associate II

Hi all i'm developing a bootloader on a stm32f407. I want to erase all the flash sectors except of the first 3 which i have the bootloader code. The problem is that to erase a sector it takes 1 second and i have an external watchdog on the board that has to be toggled at least every 600 msec. What i have relized is that while a sector is erased the mcu does not execute any line of code :\ I used these 2 functions to erase the flash

HAL_FLASHEx_Erase_IT(&FLASH_EraseInitStruct)
HAL_FLASHEx_Erase(&FLASH_EraseInitStruct, &SectorError)

But both functions use this function

FLASH_Erase_Sector(pEraseInit->Sector, pEraseInit->VoltageRange)

which takes 1 second long to execute. I measure the time of a function by toggling a pin before and after execution and measuring the pin with a scope.

Any help will be very useful , thanks in advance

1 ACCEPTED SOLUTION

Accepted Solutions

> ... and i have an external watchdog on the board that has to be toggled at least every 600 msec.

Then , this is a design fault.

Flash erase can take several seconds under worst conditions.

Either use a dual bank MCU, execute the Flash routines from RAM, or keep the watchdog off when in the bootloader.

You digged that hole.

BTW, the IAR Workbench has direct support for running code (functions) from RAM. Perhaps you can use that as template.

View solution in original post

9 REPLIES 9

If you need to execute code during FLASH erasing/programming, that code has to run from RAM. This includes the interrupt vector table and all interrupts which are running (unless you disable interrupts before programming starts).

JW

AvaTar
Lead

Develop your code around the fact that Flash erase takes as long as it takes.

Take the maximum given in the datasheet into account.

Run from RAM, as waclawek.jan suggested, and/or do not enable a watchdog.

Thank for the answer, can you give me a direction how to run code from RAM, so i can figure out how to do it.

td
Associate II

:pensive_face: wathdog has no enable..

alister
Lead

If you're roadblocked, try another part that has dual flash banks, e.g. STM32F42xxx or STM32F43xxx, and see note on page 70 of RM0090 Rev 17.

> ... and i have an external watchdog on the board that has to be toggled at least every 600 msec.

Then , this is a design fault.

Flash erase can take several seconds under worst conditions.

Either use a dual bank MCU, execute the Flash routines from RAM, or keep the watchdog off when in the bootloader.

You digged that hole.

BTW, the IAR Workbench has direct support for running code (functions) from RAM. Perhaps you can use that as template.

td
Associate II

I agrre with you, it was a design fault.

The only choice i have is messing with RAM, cant turn the watchdog off and changing the MCU is not a realistic choice.

I tried the __ramfunc extended keyword on functions and they do run from RAM. I'm using IAR BTW did not mention it before.

So how does this work?


FLASH_Erase_Sector(idx, FLASH_VOLTAGE_RANGE_3);
done = FLASH_WaitForLastOperation(NVI_ERASE_TOUT_MS) == HAL_OK;
 
FLASH_WaitForLastOperation is not in RAM, so instructions are fetched from flash, while it's being erased.
datasheet says this should cause stalling.
 

@mrx23,

please start a new thread, stating your hardware/software, and what is the problem: what is the expected behaviour and how is the observed behaviour different from it.

JW