Firmware – When Does the Stack Pointer Get Copied from Flash to SP on Startup?

cortex-m4firmwarestack-pointer

I'm trying to track down a problem on a MKV31F256 micro. The firmware immediately jumps to the hard fault handler on startup.

I'm able to set a breakpoint on the first assembly instruction in the reset handler and look at memory and the registers, and I was surprised to find the stack pointer was not set to the top of RAM (0x20008000). Rather, SP is set to 0x00000000

enter image description here

However, the initial stack value is correct in the vector table.

enter image description here

I was under the impression the Cortex M4 automatically loaded the first and second entry in the vector table into the SP and PC registers. Is this not the case?

I double checked on a different Cortex M4 micro (nRF52832) and SP is set correctly by the hardware on startup.

Is the automatic SP initialization an optional feature by the individual chip manufacturers?

Best Answer

I was under the impression the Cortex M4 automatically loaded the first and second entry in the vector table into the SP and PC registers. Is this not the case?

It should.
0x0 contains stack pointer.
0x4 contains reset handler address.

LR should also have been cleared to FF. I suspect your debugger is not resetting the core correctly.