Hello everyone,
I am porting a Hard Fault handler code I have been using for a while. It was originally written for a Cortex-M3, and has worked so far for M3, M4 and M7:
void __attribute__ ((naked)) _hardfault_isr()
{
__asm__ volatile
(
" tst lr, #4 \n"
" ite eq \n"
" mrseq r0, msp \n"
" mrsne r0, psp \n"
" ldr r1, [r0, #24] \n"
" ldr r2, hardfault_trace_stack_addr \n"
" bx r2 \n"
" hardfault_trace_stack_addr: .word hardfault_trace_stack \n"
);
}But now I am working with a Cortex-M0+, and it wouldn't compile. After a close look, one quickly realises some of the instructions are not supported by the poor M0+.
After some head scratching, I came up with a solution that I think should implement the same algorithm, in an M0+ compatible way, but I am not sure:
void __attribute__ ((naked)) _hardfault_isr()
{
__asm__ volatile
(
" movs r0, #4 \n"
" movs r1, lr \n"
" tst r1, r0 \n"
" mrs r0, msp \n"
" beq not_psp \n"
" mrs r0, psp \n"
" nop \n"
" nop \n"
" nop \n"
" not_psp: \n"
" ldr r1, [r0, #24] \n"
" ldr r2, hardfault_trace_stack_addr \n"
" bx r2 \n"
" hardfault_trace_stack_addr: .word hardfault_trace_stack \n"
);
}I would like someone who is more experienced to give some input, please.
It should determine whether the program was running off the PSP or MSP when the fault happened, determine the address of the stack frame and then call a C routine that will use that information.
Thank you,
João