cancel
Showing results for 
Search instead for 
Did you mean: 

What registers do I need to change when switching execution context with goto-statement?

arnold_w
Senior
Posted on December 04, 2017 at 10:55

I am working with the stm32f7 microcontroller and I have 2 projects, a bootloader project and an application project. The bootloader resides in the beginning of the flash memory and the application resides in the end. If I want to switch execution from application to bootloader or vice versa with a goto-statement to the other project's main-function, what registers (stack pointer, program counter, etc?) do I need to change to ensure correct execution?

4 REPLIES 4
Posted on December 04, 2017 at 14:19

You can't just change context, the firmwares are built with different RAM utilization as fixed by the linker. ie variables in completely different locations.

You need to transfer control through the SP and PC described in the vector table for the Reset_Handler entry point.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on December 04, 2017 at 14:32

No variables are shared between the 2 projects and each of the projects initializes all modules (GPIO-pins, clocks, timers, UART:s, etc) it needs, in its main-function. I don't want to do a software reset because it also resets the USB-module and that means that the USB-connection to the PC (the PC performing the firmware upgrade) will be lost and there's a need to reconnect, which could be slow if the PC has a hard time finding the correct driver.

Posted on December 04, 2017 at 16:46

Then carry a flag across in a RAM location that isn't used to flag it should reinitialize USB, etc.

The C run time code initializes the statics before calling main(), so random entry can have risks that variables are initialized as expected. See how qsort() passes functions, review IAP examples.

If you really want to jump into arbitrary code, set up a function pointer with the address and use that to call the code you want.

Perhaps think about how you want to transfer control and pass information, and why it is desirable to ping back and forth.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on December 05, 2017 at 11:33

It is fairly easy to pass information between the 2 projects, in the linker script I specify that the RAM is 511 kilobyte instead of 512 and then the last kilobyte will remain untouched at all times (even if doing a software reset).

If I used the function pointer approach, wouldn't I eventually get stack overflow if I keep switching back and forth between application and bootloader enough times? Wouldn't it be kind of like recursive function calling?