Skip to main content
Visitor II
July 16, 2024
Question

Issue with Running Application from Bootloader

  • July 16, 2024
  • 2 replies
  • 2934 views

I seek your assistance with an issue I'm encountering while attempting to jump from my bootloader to an application stored at a specific address in the flash memory of an STM32H723ZGT6TR microcontroller.

 

 

I am able to successfully flash the application binary from a USB to the flash memory using bootloader code. However, I am unable to run the application using the following code snippet:

 

 
 
 uint32_t msp_value = *(__IO uint32_t *)0x08040000;
__set_MSP(msp_value);
uint32_t resethandler_address = *(__IO uint32_t *) (0x08040000 + 4);
app_reset_handler = (void*) resethandler_address;
app_reset_handler();
NVIC_SystemReset();
 
I would appreciate your review and any  suggestions or corrections you might have. Thank you for your time and assistance.
    This topic has been closed for replies.

    2 replies

    Technical Moderator
    July 16, 2024

    Hello @Pushpalatha and welcome to the Community :)

    Please have a look at these articles which will help you on how to Jump to bootloader from application:

    Visitor II
    July 16, 2024

    When I flash the application code using STM32CubeIDE and then run the bootloader code, I am able to successfully jump to the application address and execute the application. However, when I attempt to flash the application code from within the bootloader itself, I encounter difficulties in running the application code afterward. I suspect the issue lies with the flash write process during the bootloader operation.

    Graduate II
    July 16, 2024

    Could you show us your process of how you write the flash in the bootloader?

    Visitor II
    July 16, 2024
    void Flash_Write(uint32_t address, uint8_t *data, uint32_t length) {
     HAL_FLASH_Unlock();
    
     for (uint32_t i = 0; i < length; i += 8) {
     uint32_t data_word = *(uint32_t *)(data + i);
     if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, address + i, data_word) != HAL_OK) {
     Error_Handler();
     }
     }
    Graduate II
    July 16, 2024

    Thanks.

    First, you should call HAL_FLASH_Lock(); at the end of your function. Maybe you call it later in your code, but you should lock back your flash as soon as you stop writing it.
    Also, in the HAL_FLASH_Program brief :

    /**
     * @brief Program a flash word at a specified address
     * @PAram TypeProgram Indicate the way to program at a specified address.
     * This parameter can be a value of @ref FLASH_Type_Program
     * @PAram FlashAddress specifies the address to be programmed.
     * This parameter shall be aligned to the Flash word:
     * - 256 bits for STM32H74x/5X devices (8x 32bits words)
     * - 128 bits for STM32H7Ax/BX devices (4x 32bits words)
     * - 256 bits for STM32H72x/3X devices (8x 32bits words)
     * @PAram DataAddress specifies the address of data to be programmed.
     * This parameter shall be 32-bit aligned
     *
     * @retval HAL_StatusTypeDef HAL Status
     */
    HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t FlashAddress, uint32_t DataAddress)


    It is specified that for STM32h72x devices, the writing data should be align with "256 bits for STM32H72x/3X devices (8x 32bits words)"

    So I think your for loop indentation should be i += 32.


    And last but not least, DataAddress isn't a pointer but still is the address of the data.

     

    Here is how I think your function should work:

    void Flash_Write(uint32_t address, uint8_t *data, uint32_t length) 
    {
     HAL_FLASH_Unlock();
    
     uint32_t data_word = (uint32_t)data;
     for (uint32_t i = 0; i < length; i += 32) 
     {
     if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, address + i, data_word + i) != HAL_OK)
     {
     Error_Handler();
     }
     }
     
     HAL_FLASH_Unlock();
    }


    Can you try this out and give us a feedback?