Hard fault handler problem - Cortex-M0+

Hi all,

I use generic Cortex-M0+ with ARM Development Studio 5.

I want to create a hard fault handler that prints out call stack list (unwind the stack).

At first I try to find just the return address from the stack (the cause of the hard fault) but it doesn't seem to work. I work without RTOS so I'm supposed to be in Handler mode and always use the MSP stack. Regardless I check the bit in the control register and it, as expected, indicates the MSP stack is in use. The problem is that the MSP stack holds no return address (I force hard fault so I know the supposed return address).

I check the PSP stack as well that has some valid return address but not the correct one.

Here is my code [NMI is routed to hard fault handler as well]:

typedef struct __attribute__((packed)) ExceptionStackContext
    uint32_t r0;
    uint32_t r1;
    uint32_t r2;
    uint32_t r3;
    uint32_t r12;
    uint32_t lr;
    uint32_t return_address;
    uint32_t xpsr;
} ExceptionStackContext_s;

#pragma GCC optimize ("O0")
void HardFault_Handler(void) {

    CONTROL_Type control_reg;
    xPSR_Type xpsr_reg;
    volatile ExceptionStackContext_s *stack_content;
    volatile ExceptionStackContext_s *psp_content;

    printf(""\n ---- !!! HARD FAULT EXCEPTION !!! ---- \n\n");

    control_reg = (CONTROL_Type)__get_CONTROL();

    if (control_reg.b.SPSEL == eMSP_STACK_USED)
        stack_content = __get_MSP();
        stack_content = __get_PSP();
    psp_content = __get_PSP();

    xpsr_reg = (xPSR_Type)stack_content->xpsr;

        case eNMI_EXCEPTION:
            printf(""NMI Exception detected\n");
            printf(""Hard Fault Exception detected\n");

    printf(""STACK Register r0: %x\n", stack_content->r0);
    printf(""STACK Register r1: %x\n", stack_content->r1);
    printf(""STACK Register r2: %x\n", stack_content->r2);
    printf(""STACK Register r3: %x\n", stack_content->r3);
    printf(""STACK Register r12: %x\n", stack_content->r12);
    printf(""STACK Register LR: %x\n", stack_content->lr);
    printf(""CALL STACK Return Address: %x\n", stack_content->return_address);
    printf(""STACK Register XPSR: %x\n\n", stack_content->xpsr);


Any ideas why I don't get the return address?

How can I unwind the stack and create a call stack list?

Thank you

Parents Reply Children
No data
More questions in this forum