-deleted-
9 years, 8 months ago.

How to write a Hard Fault Handler

What is the best way to handle faults?

Obviously a watch dog timer (WDT) is a good option.

Is there a way to code in mbed a Hard Fault Handler, something like the following: https://blog.feabhas.com/2013/02/developing-a-generic-hard-fault-handler-for-arm-cortex-m3cortex-m4/

Does the following code override the Default Hard Fault_Handler?

void HardFault_Handler(void)
{ 
   while(1); 
}

???

posted by -deleted- 07 Apr 2015

You may also need this

SCB->SHCSR |= 0x00070000;//Enable fault handler.
posted by David Fletcher 07 Apr 2015

Thank you.

posted by -deleted- 07 Apr 2015

1 Answer

9 years, 8 months ago.

This would be nice to have ! have you searched if any user did not implement those fault handlers for mbed? I haven't seen yet but might be around.

Yes your above code does overwrite as it's strong symbol and the default one is weak (those placed in the startup files.

Regards, 0xc0170

Accepted Answer

Trying to post a comment here was giving me a "Page Error" until I posted ??? under my question.

Where do I find the startup files?

posted by -deleted- 07 Apr 2015

I have found this on mbed: https://developer.mbed.org/forum/helloworld/topic/624/

and

http://developer.mbed.org/forum/mbed/topic/2712/

One code option was:

#include "mbed.h"

extern "C" void HardFault_Handler() { error("Hard Fault!\n"); }

DigitalOut myled(LED1);
char * test;
 
int main() {

strcpy(test,"test");
    while(1) {
        myled = 1;
        wait(0.2);
        myled = 0;
        wait(0.2);
    }
}

and

 // Reset_Handler
// NMI_Handler
// HardFault_Handler
// MemManage_Handler
// BusFault_Handler
//  UsageFault_Handler
extern "C" void HardFault_Handler() {
    pc.printf("Hard Fault!\n");
    //NVIC_SystemReset();
}
extern "C" void NMI_Handler() {
    pc.printf("NMI Fault!\n");
    //NVIC_SystemReset();
}
extern "C" void MemManage_Handler() {
    pc.printf("MemManage Fault!\n");
    //NVIC_SystemReset();
}
extern "C" void BusFault_Handler() {
    pc.printf("BusFault Fault!\n");
    //NVIC_SystemReset();
}
extern "C" void UsageFault_Handler() {
    pc.printf("UsageFault Fault!\n");
    //NVIC_SystemReset();
}
posted by -deleted- 07 Apr 2015