This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How does the stack pointer work?

We have written our own softdevice, but now we have a question about the stack pointer. I read in the softdevice specification that the nordic softdevices share the stack with the application. That is what we want, so we thought we need to set the MSP to the value set by the application (first element in the application vector table). When our softdevice starts it gets initialised with a value at the end of region 0, as expected. Now the part I don't understand.

When I print the MSP in the application to uart I get a value at the end of the region 1. This is what we want. But how, I never did anything with the MSP to set it to this value.

We use the same startupcode for our softdevice as for our applications. The reset handler gets called from softdevice main. right before entering application reset handler the MSP is pointing to a region 0 ram address, in application main the MSP is pointing to region 1 ram address.

Can someone explain where the MSP gets set? I thought it was initialised once at core startup by the value set at address 0.

Parents
  • The MSP is being updated in the reset handler of the application in the __main function. You can verify this in debug mode by setting a breakpoint at the "LDR R0, =__main" line in the reset handler, then do one single step, and you will see that the stack pointer is being loaded in asm:

    0x000180C0 4803      LDR      r0,[pc,#12]  ; @0x000180D0 => location of stack pointer for this example
    0x000180C2 4685      MOV      sp,r0
    

    That said, both our SD and bootloader sets the MSP before branching to the reset handler of the application, so it's probably good practice to do so.

Reply
  • The MSP is being updated in the reset handler of the application in the __main function. You can verify this in debug mode by setting a breakpoint at the "LDR R0, =__main" line in the reset handler, then do one single step, and you will see that the stack pointer is being loaded in asm:

    0x000180C0 4803      LDR      r0,[pc,#12]  ; @0x000180D0 => location of stack pointer for this example
    0x000180C2 4685      MOV      sp,r0
    

    That said, both our SD and bootloader sets the MSP before branching to the reset handler of the application, so it's probably good practice to do so.

Children
Related