Skip to main content
Visitor II
August 13, 2020
Question

Delay needed before executing newly written code

  • August 13, 2020
  • 3 replies
  • 1673 views

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!

    This topic has been closed for replies.

    3 replies

    Super User
    August 13, 2020

    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.

    Graduate II
    August 13, 2020

    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.

    GabrielFAuthor
    Visitor II
    August 14, 2020

    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: