Search code examples
stm32stm32ldiscoverystm32l152

In Application Programming issue


I'm working on project on STM32L152RCT6, where i have to build a mechanism to self update the code from the newly gated file(HEX file). For that i have implemented such mechanism like boot loader where it checks for the new firmware if there it it has to cross verify and if found valid it has to store on "Application location".

I'm taking following steps.

  1. Boot loader address = 0x08000000
  2. Application address = 0x08008000
  3. Somewhere on specified location it has to check for new file through Boot loader program.
  4. If found valid it has to be copy all the HEX on location(as per the guide).
  5. Than running the application code through jump on that location.

Now problem comes from step 5, all the above steps I've done even storing of data has been done properly(verify in STM32 utility), but when i'm jump to the application code it won't work.

Is there i have to cross check or something i'm missing?


Solution

  • Unlike other ARM controllers that directly jump to address 0 at reset, the Cortex-M series takes the start address from a vector table. If the program is loaded directly (without a bootloader), the vector table is at the start of the binary (loaded or mapped to address 0). First entry at offset 0 is the initial value of the stack pointer, second entry at address 4 is called the reset vector, it contains the address of the first instruction to be executed.

    Programs loaded with a bootloader usually preserve this arrangement, and put the vector table at the start of the binary, 0x08008000 in your case. Then the reset vector would be at 0x08008004. But it's your application, you should check where did you put your vector table. Hint: look at the .map file generated by the linker to be sure. If it's indeed at 0x08008000, then you can transfer control to the application reset vector so:

    void (*app)(void);                   // declare a pointer to a function
    app = *(void (**)(void))0x08008004;  // see below
    app();                               // invoke the function through the pointer
    

    The complicated cast in the second line converts the physical address to a pointer to a pointer to a function, takes the value pointed to it, which is now a pointer to a function, and assigns it to app.

    Then you should manage the switchover to the application vector table. You can do it either in the bootloader or in the application, or divide the steps between them.

    • Disable all interrupts and stop SysTick. Note that SysTick is not an interrupt, don't call NVIC_DisableIRQ() on it. I'd do this step in the bootloader, so it gets responsible to disable whatever it has enabled.
    • Assign the new vector table address to SCB->VTOR. Beware that the boilerplate SystemInit() function in system_stm32l1xx.c unconditionally changes SCB->VTOR back to the start of the flash, i.e. to 0x08000000, you should edit it to use the proper offset.

    You can load the stack pointer value from the vector table too, but it's tricky to do it properly, and not really necessary, the application can just continue to use the stack that was set up in the bootloader. Just check it to make sure it's reasonable.