This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Exception return for Cortex-M7

I have a function that generates an UsageFault exception for test:

int
UsageFault_Trigger_UnalignedAccess(void)
{
int r = 0;
volatile unsigned int* p;

/* Not word aligned address */
p = (unsigned int*)0x20000002;

/* Load word from unaligned address raises exception */
r = *p;

return r;
}

When the program jumps to UsageFault_Handler(), I extract the SP and then I call a C routine to print some debug info - Pmd_Fault_Handler()

void
UsageFault_Handler(void)
{
__asm__("TST LR, #4; ");
__asm__("ITE EQ; ");
__asm__("MRSEQ R0, MSP; ");
__asm__("MRSNE R0, PSP; ");
__asm__("MOV R1, LR; ");
__asm__("b Pmd_Fault_Handler ");

//......

__asm__("BX LR ");

}

void
Pmd_Fault_Handler (uint32_t* pSP, int32_t exc)
{
regsss[0] = pSP[0];
regsss[1] = pSP[1];
regsss[2] = pSP[2];
regsss[3] = pSP[3];
regsss[4] = pSP[4];
regsss[5] = pSP[5];
regsss[6] = pSP[6];
regsss[7] = pSP[7];

.....

return;

}

PROBLEM: When returning from Pmd_Fault_Handler(), the program jump again to the beginning of this function instead of returning to UsageFault_Handler()

The overall scope for me is to return from UsageFault_Handler() in a controlled manner and to continue execution from the next instruction from the caller of UsageFault_Trigger_UnalignedAccess(), but I cannot reach this - __asm__("BX LR "); - to be able to return from exception.

Thanks!

Parents
  • Thanks Toshi!

    Using BL, indeed, will return my code into UsageFault_Handler(), after execution of Pmd_Fault_Handler()

    When jumping to UsageFault_Handler() I have LR set to 0xFFFF.FFF9, but after I will return from  Pmd_Fault_Handler() into UsageFault_Handler(), I see the LR is pointing to some executable code.

    Should it work if I'll save the LR before jumping to Pmd_Fault_Handler() into some global variable (using asm), restore it back and then executing BX LR to return from exception?

    Dan

Reply
  • Thanks Toshi!

    Using BL, indeed, will return my code into UsageFault_Handler(), after execution of Pmd_Fault_Handler()

    When jumping to UsageFault_Handler() I have LR set to 0xFFFF.FFF9, but after I will return from  Pmd_Fault_Handler() into UsageFault_Handler(), I see the LR is pointing to some executable code.

    Should it work if I'll save the LR before jumping to Pmd_Fault_Handler() into some global variable (using asm), restore it back and then executing BX LR to return from exception?

    Dan

Children
  • Update:

    static volatile uint32_t lr_exc = 0;

    void
    UsageFault_Handler(void)
    {

    //...................

    __asm__("LDR R3, =lr_exc ");
    __asm__("STR R1, [R3]"); // Store LR code into lr_exc

    __asm__("bl Pmd_Fault_Handler "); // do some user processing


    __asm__("LDR R3, =lr_exc ");
    __asm__("LDR R4, [R3] ");
    __asm__("MOV LR, R4; ");

    __asm__("bx LR ");         // LR is restored with 0xFFFF.FFF9 from global "lr_exc", but it is jumping to the same UsageFault_Handler() instead of returning to the function that generated the exception

    //...................

    }