Beau R.
Beau R.

Reputation: 1

Hardfault handling - Arm Cortex-M0

I'm having some difficulty creating a hard fault handler for the NRF51 which has an ARM CortexM0.

(note: the following code has been combined from different sources on the internet) Here's what I have so far:

    static void hard_fault_handler_c(unsigned int * hardfault_args)
{
    unsigned int stacked_r0;
    unsigned int stacked_r1;
    unsigned int stacked_r2;
    unsigned int stacked_r3;
    unsigned int stacked_r12;
    unsigned int stacked_lr;
    unsigned int stacked_pc;
    unsigned int stacked_psr;

    stacked_r0 = ((unsigned long) hardfault_args[0]);
    stacked_r1 = ((unsigned long) hardfault_args[1]);
    stacked_r2 = ((unsigned long) hardfault_args[2]);
    stacked_r3 = ((unsigned long) hardfault_args[3]);

    stacked_r12 = ((unsigned long) hardfault_args[4]);
    stacked_lr = ((unsigned long) hardfault_args[5]);
    stacked_pc = ((unsigned long) hardfault_args[6]);
    stacked_psr = ((unsigned long) hardfault_args[7]);

    for(;;);
}

void HardFault_Handler(void)
{
    asm volatile(  
        "movs r0, #4\t\n"  
        "mov  r1, lr\t\n"  
        "tst  r0, r1\t\n" /* Check EXC_RETURN[2] */  
        "beq 1f\t\n"  
        "mrs r0, psp\t\n"  
        "ldr r1,=hard_fault_handler_c\t\n"  
        "bx r1\t\n"  
        "1:mrs r0,msp\t\n"  
        "ldr r1,=hard_fault_handler_c\t\n"  
        : /* no output */  
        : /* no input */  
        : "r0" /* clobber */  
    );  
}

The error during compilation is the following: Building target: project.elf Invoking: Cross ARM C++ Linker C:\Users\Steven\AppData\Local\Temp\ccuAgDyP.ltrans9.ltrans.o: In function HardFault_Handler': <artificial>:(.text.HardFault_Handler+0x18): undefined reference tohard_fault_handler_c' collect2.exe: error: ld returned 1 exit status make: *** [FruityMesh.elf] Error 1 makefile:65: recipe for target 'project.elf' failed

In summary, it looks like the linker is having trouble finding the address for hard_fault_handler_c function. I would think that i would need to write assembly to import or include the path to this function but, this is just my suggestion. I haven't been able to write assembly for the M0 that compiles to do so.

Thank you

Upvotes: 0

Views: 3654

Answers (1)

Nic Dahlquist
Nic Dahlquist

Reputation: 1105

I'd suggest upgrading to version 11 of the NRF SDK, which adds built-in support for a hard fault handler (see nRF5_SDK_11.0.0_89a8197/components/libraries/hardfault).

Upvotes: 1

Related Questions