armembeddedstm32startupcortex-m

what does the cortex-M4's initial SP value in the 0x0 use for?


through the Cortex-M4 Devices Generic User Guide, could find a initial sp value in the 0x0.

through startup_stm32l431xx.s, could find

Reset_Handler:
  ldr   sp, =_estack    /* Set stack pointer */ //LDR loads a register with a value from a PC-relative memory address.The memory address is specified by a label or by an offset from the PC.

so,my question is why there is still has initial SP value in the 0x0 because we already done the initial sp value in the startup_stm32l431xx.s(i think startup.s one of work is initial sp value)

Since we always set sp in the startup code, why do we still have an address of 0x0 to store the initial sp value?


Solution

  • This is an excellent question, and my guess is that this initialization is redundant since since 0x0 does contain _estack, as you mentioned, and that this value will be automatically loaded at reset into sp, the same way pc will be loaded with the addresse for Reset_Handler: For example, the equivalent code for the arm compiler does not contain any additional code loading sp: the gcc version of the startup code is the only one to use such a redundant initialization: you can compare The gcc version (the one I am assuming you are referencing), the arm version and the iar version.