Hardfault handlers on Teensyduino 1.54

Status
Not open for further replies.

damiend

Member
I found that after upgrading to Teensyduino 1.54 my custom hardfault handlers did not get called any more. Here's the result of my investigations for future reference.

The cause is this commit. What it does is enable exception handlers for memory, bus and usage faults. This means that these types of faults no longer default to a hardfault, therefore one needs to set up the three corresponding interrupt handlers, rather than just one.

Pre-Teensyduino 1.54:
Code:
_VectorsRam[3] = hardfault_handler_isr; // hardfaults / mem / bus / usage

Post-Teensyduino 1.54:
Code:
_VectorsRam[3] = hardfault_handler_isr; // hardfaults
_VectorsRam[4] = hardfault_handler_isr; // mem
_VectorsRam[5] = hardfault_handler_isr; // bus
_VectorsRam[6] = hardfault_handler_isr; // usage

In my application the custom handler saves core dumps to the Flash memory using this library: https://github.com/adamgreen/CrashCatcher
This lets me recover the stack trace info (including source files and line numbers) through gdb, without needing to have an active debugger connection at the time of the crash.
 
This reply from other thread - second of two ...
Just to further demonstrate, you can generate a hard fault by causing any sort of fault inside the memory fault handler.

Here's your minimal example extended to cause a memory fault, and then another memory access violation inside the memory fault handler, which then causes a hard fault.

Code:
FASTRUN
static void hardfault_handler_isr(void) {
    Serial.println("Hardfault");
    Serial.flush();
    while (true);
}

FASTRUN
static void memoryfault_handler_isr(void) {
    Serial.println("Memoryfault");
    Serial.flush();
    int *p = NULL;
    *p = 3;
    while (true);
}

void setup() {
    Serial.begin(9600);
    while (!Serial);
    _VectorsRam[3] = hardfault_handler_isr;
    _VectorsRam[4] = memoryfault_handler_isr;
    Serial.println("Hello!");
    int *p = NULL;
    *p = 3;
    Serial.println(*p);
}

void loop() {
}

Works as it should with Teensyduino 1.54.

View attachment 25482
 
Status
Not open for further replies.
Back
Top