Skip to main content
arnold_w
Senior II
December 4, 2017
Question

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

  • December 4, 2017
  • 4 replies
  • 629 views
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?

    This topic has been closed for replies.

    4 replies

    Tesla DeLorean
    Guru
    December 4, 2017
    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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
    arnold_w
    arnold_wAuthor
    Senior II
    December 4, 2017
    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.

    Tesla DeLorean
    Guru
    December 4, 2017
    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 (See Profile) Up vote any posts that you find helpful, it shows what's working..