3
\$\begingroup\$

I wrote a custom bootloader for the STM32F4 microcontroller and I'm able to write an application to the flash memory at the address of 0x08008000. The issue I have is when I use the jump function the application doesn't seem to execute. I have attached the jump function below. I have disabled all the peripherals, timers and interrupts. Also, relocated the vector table to 0x08008000 to run the new app.

void bootloader_jump_to_user_app(void)
{

   //just a function pointer to hold the address of the reset handler of the user app.
    void (*app_reset_handler)(void);

    //shut down any tasks running
    HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10);
    HAL_GPIO_DeInit(GPIOB, GPIO_PIN_11);
    HAL_TIM_Base_Stop_IT(&htim4);
    HAL_TIM_Base_DeInit(&htim4);
    HAL_RCC_DeInit();
    HAL_DeInit();


    SysTick->CTRL = 0;
    SysTick->LOAD = 0;
    SysTick->VAL = 0;

    //disbale interuppts
    __set_PRIMASK(1);
    __disable_irq();

     SCB->VTOR = FLASH_SECTOR2_BASE_ADDRESS; //0x080080000

    // 1. configure the MSP by reading the value from the base address of the sector 2
    uint32_t msp_value = *(__IO uint32_t *)FLASH_SECTOR2_BASE_ADDRESS;

    __set_MSP(msp_value);

    uint32_t resethandler_address = *(__IO uint32_t *) (FLASH_SECTOR2_BASE_ADDRESS + 4);

    app_reset_handler = (void*) resethandler_address;

    //3. jump to reset handler of the user application
    app_reset_handler();

}

In the linker file for the bootloader I have broken the flash into two parts, one for the app and the other for the bootloader. For the application linker file I have the flash starting at 0x08008000.

/* Memories definition */
MEMORY
{
    CCMRAM  (xrw)   : ORIGIN = 0x10000000,  LENGTH = 64K
    RAM (xrw)   : ORIGIN = 0x20000000,  LENGTH = 128K
    FLASH   (rx)    : ORIGIN = 0x8000000,   LENGTH = 128K
    APPLICATION (rx) : ORIGIN = 0x08008000, LENGTH = 896K
}
\$\endgroup\$
13
  • \$\begingroup\$ How/where is FLASH_SECTOR2_BASE_ADDRESS defined? Is it just an integer or is it already a pointer of some sort? \$\endgroup\$
    – brhans
    Feb 5 '20 at 16:05
  • \$\begingroup\$ defined in header file as #define FLASH_SECTOR2_BASE_ADDRESS 0x08008000U \$\endgroup\$
    – doge
    Feb 5 '20 at 16:08
  • \$\begingroup\$ I have an STM32F4 bootloader which does something like: extern uint32_t __FLASH_segment_start__[]; #define APPLICATION_START (uint32_t)__FLASH_segment_start__ SCB->VTOR = (uint32_t)APPLICATION_START; __set_MSP(APPLICATION_START[0]); ((void (*)(void))APPLICATION_START[1])(); and it seems like your code _should have the same effect ... \$\endgroup\$
    – brhans
    Feb 5 '20 at 16:15
  • \$\begingroup\$ code seems similar. Maybe there's something that i missed since my jump function doesnt work \$\endgroup\$
    – doge
    Feb 5 '20 at 16:26
  • 1
    \$\begingroup\$ Did you link your code to start at 0x08008000? You can't just take an image linked for a different start address, load it, and expect it to work. \$\endgroup\$ Feb 5 '20 at 18:47
3
\$\begingroup\$

I managed to get the bootloader to work. I just needed to rename the reset_handler in the linker script and startup_stm32F40xx.s file to bootloader_reset_handler for the bootloader program.

\$\endgroup\$
1
  • \$\begingroup\$ Can you please tell me how you renamed reset_handler in the linker script and startup_stm32F40xx.s file \$\endgroup\$
    – Rohith R
    Sep 15 at 6:13
0
\$\begingroup\$

Renaming reset_handler in the linker script and startup_stm32F40xx.s file

  1. Open STM32XXXXX_FLASH.ld file in your project search text Reset_Handler. replace this text with Boot_Reset_Handler.
  2. Open startup_stm32F40xx.s file in startup folder search text Reset_Handler. replace this text with Boot_Reset_Handler for all occurances.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.