Search code examples
cortex-mfault

Hardfault on Cortex M0 cannot be tracked down


I am having really hard time trying to track down an hardfault happening randomly on my nrf51822 board.

I am able to pin a breakpoint on the hardfault handler, but the stack info is not much useful. I can step debug around the instruction causing the hardfault and there's absolutely nothing there that could trigger such fault. In the example below, PRstrcpy is just a copy between two absolutely legit addresses. Other symptoms:

  • The stack still has plenty of room.
  • There are no IRQs pending (I inspected the NVIC registers)
  • If I move the breakpoint, or do a complete step debugging, the hardfault can happen at any other position of the code
  • I am now compiling with -o0 for SWD debugging. Compiling with higher optimizations solves (or better hides) it.
  • If I use the very same flash image, it happens both with and without compiling
  • Changing the code by the tiniest bit, also causes the hardfault to move randomly, to completely unrelated positions, or disappear.
  • Compiling the code on other machines with identical setup and toolchain also causes random changes to the hardfault.

Any ideas for the debug?

Here follows a stack trace:

Thread #1 <main> (Suspended : Breakpoint)   
    HardFault_Handler() at 0x34976  
    <signal handler called>() at 0xfffffff9 
    PRstrcpy() at kcs_stdlibs.c:94 0x1aa66  
    PrintConfigInfo() at Startup.c:36 0x15256   
    0xfffffffe  

Solution

  • I found the problem in my case, it was something different and difficult to debug. I had some flash write instructions around, which would overwrite upper flash areas. Only when using low optimization values, these flash area hosted legit instructions. So my code was modified on the fly, while running.