Embedded Software Interview – Peripheral Section: 5-MCU Code Debugging

When the MCU code crashes, first determine whether the current stack being used is the MSP (Main Stack) or the PSP (Process Stack).

After an exception occurs, the stack order of the ARM core is as follows:

R0-> R1->R2->R3->R12->LR->PC->XPSR

The LR (R14) register is the link register, which stores the exception return value EXC_RETURN where its bit2 determines whether it is MSP or PSP.

EXC_RETURN value meanings:

  • 0xFFFF_FFF1: Return to handler mode
  • 0xFFFF_FFF9: Return to thread mode, using the main stack (SP=MSP)
  • 0xFFFF_FFFD: Return to thread mode, using the thread stack (SP=PSP)

After determining the stack, find the value of the stack, then open the memory address indicated by the stack value, and find the sixth register which is <span><span>LR</span></span>, the seventh is <span><span>PC</span></span>. Based on the <span><span>PC</span></span> pointer value, locate the position in the assembly, which is where the problem occurred.

Based on the <span><span>LR</span></span> pointer value, locate the position in the assembly, which is the next instruction called after the exception.

Additionally, you can use CmBacktrace!

CmBacktrace (Cortex Microcontroller Backtrace) is an open-source library for automatic error code tracing, localization, and automatic analysis of error causes for ARM Cortex-M series MCUs. For more details, see:

https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/application-note/debug/cmbacktrace/an0013-CmBacktrace?id=cmbacktrace-%e7%ae%80%e4%bb%8b

Leave a Comment