cancel
Showing results for 
Search instead for 
Did you mean: 

Delay needed before executing newly written code

GabrielF
Associate

I am using a STM32F401 MCU and STM32 HAL library. I am writing a bootloader that will update an application that is located into a different sector. The sequence is this:

/* Unlock the flash */

HAL_FLASH_Unlock();

/* Erase the sector that will hold the application */

HAL_FLASHEx_Erase();

/* Write each word into the new sector */

for(uint32_t word)

HAL_FLASH_Program(word);

/* Lock the flash */

HAL_FLASH_Lock();

/* Add a delay; why? */

HAL_Delay(200);

/* Jump to application code */

jump();

The flash is erased and written successfully. However I have discovered that I need to add a delay of arround 200ms after I lock the flash memory and before jumping into the new application. Otherwise the MCU hangs.

Is there another solution then adding this delay or can somebody point me out to some documentation about this issue?

Thanks!

3 REPLIES 3
TDK
Guru

It’s not normal to need a delay. Debug the code and figure out where/why the processor hangs. You could try disabling the art accelerator and data/instruction caches to see if that’s the issue.

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

Check write buffers

Check ART, perhaps turn off or flush, and let receiving code re-enable.

200 ms seems arbitrary, and a bit long for processor related issues.

Check the flash controller has actually completed, and if any errors/faults are reported.

Understand the mechanics of the code you're calling, and what its time related dependency is.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
GabrielF
Associate

I found the problem and discovered that it's not related to the flash after all.

The jump_to_application code is the problem. I have taken a look at stm32-bootloader project to see how the jump is performed. It seems that HAL de-initialization is also needed :

    HAL_RCC_DeInit();
    HAL_DeInit();

If I add the calls, the code runs perfectly without any delay. Somehow the delay is just a workaround for the de-initialization procedure.

Thank you for your support :smiling_face_with_smiling_eyes: