8 years, 1 month ago.

Any fix for silly lockup due to HardFault in KL25Z?

Hi all,

I have found that silly variation of "Blinky LED Hello World" hits a HardFault. I am sure it is a HardFault because I have added a handler and it constantly resets the micro, without it the micro stops execution.

I suspect an unaligned address problem, is there any way to change compiler options to fix that?.

#include "mbed.h"


#define LIGHT 0
#define DARK 1

DigitalOut myled(LED_RED);
DigitalOut myled_green(LED_GREEN);
DigitalOut myled_blue(LED_BLUE);


// RESET ON HardFault (invalid address-bad pointer, bus error: access register before clock available, unaligned address)
//  default is lockup (stop execution).
// Replace default infinite loop by a reseting handler, this strong symbols replaces the weak one in startup
// see: https://developer.mbed.org/questions/6974/How-to-write-a-Hard-Fault-Handler/
// This DOES WORK in KL25Z.
extern "C" void HardFault_Handler() {
    NVIC_SystemReset();
}

int main() {
    myled = DARK;
    myled_green = DARK;
    myled_blue = DARK;    //that lockup micro

    while(1) {
        myled = DARK;
        wait(0.2);
        myled = LIGHT;
        wait(0.2);
    }
}

1 Answer

8 years, 1 month ago.

Your code works fine for me (as in, it blinks happily). If something as simple as this would generate a hardfault there need to be way more people with issues.

Are you using the latest mbed lib version? (Right mouse button, update). If remember correctly the blue LED is also used as SPI clock, so do you have any other hardware connected to it? (Not that I see how that can give a hardfault, but still). Finally, you are compiling for correct platform? I see you only have one Freescale board selected, so it is unlikely you are compiling for the KL46Z for example, but you also got a normal KL25 board? And not another board with a similar but different MCU?

Hi Erik, I am still biten by HardFault in fuctions called as callback in timeout, interruptin, etc I see MBED version 122 adds "align" attribute, I can not find how to use it in the doc I would like to use that to align functions to be used as callback and avoid the HardFault - Is that possible?

posted by Manel Marin 21 Aug 2016