This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

NRF52840 app - random hard fault

Hi,

My application has hard faults sometimes, and I'm trying to figure out the reason.

When it occurs - I have a hard fault log, but p_stack->pc is 0. I can't understand what is the problematic code line.

Do I have another ways to catch the issue?

Thanks!

  • Maybe the issue is with an interrupt allocating too much stack space by using (say) a print buffer inside the interrupt, so the interrupt generates the hard fault in "random" foreground code locations since the buffer is allocated on the stack in the interrupt before the interrupt code executes, which generates another hard fault interrupt. This probably only happens under worst-case stack usage scenarios, so rare. This is what Susheel is driving at, I think.

    "Pointer to the stack bottom. This pointer might be NULL if the HardFault was called when the main stack was the active stack and a stack overrun is detected. In such a situation, the stack pointer is reinitialized to the default position, and the stack content is lost."

    In such a case, examine all interrupt (event) handlers and remove any buffers and instead set a flag and process in (say) the main loop.

    A second (classic) scenario is that a function allocates a local buffer on the stack using a variable parameter as the size of the stack; IAR would warn against that but SES does not. If the is an error in that parameter then the stack blows up. No good checking the parameters since the stack blows up before the parameters can be checked. Some of the TWI example code has this "feature" (or used to).

  • Hi,

    Thanks for your help!

    The hard fault occurs in a function which has a lot of external flash write and erase actions (via SPI interface). Hence - I think it may be the reason.

    Do you have any ideas?

    Thanks!

Related