HardFault detected

The target MCU has entered a HardFault exception.

CORTEX_M_HARDFAULT_DETECTED

Symptoms

  • MCUHex reports the target sits in a HardFault exception.
  • The firmware stops responding; peripherals freeze.
  • The fault happens at boot, or reproducibly when a specific feature runs.

Likely causes

  • Invalid memory access: dereferencing a null/uninitialized pointer, or reading a peripheral whose clock is disabled.
  • Stack overflow clobbering memory until execution lands somewhere illegal.
  • Unaligned access on regions/instructions that require alignment, or executing corrupted code (bad function pointer).
  • Escalation: a configurable fault (BusFault, UsageFault, MemManage) with its handler disabled escalates to HardFault.

How to fix it

  1. Read the fault registers

    While connected, inspect SCB->CFSR (0xE000ED28) and SCB->HFSR (0xE000ED2C). CFSR bits narrow the class: e.g. PRECISERR with BFAR valid gives you the exact faulting address in SCB->BFAR.

  2. Locate the faulting instruction

    The exception stack frame holds the return PC: read SP inside the handler context; the stacked PC is at offset 0x18. Map that address to a line via your .elf (addr2line or your IDE’s disassembly).

  3. Check the usual suspects

    Verify peripheral clocks are enabled before register access, check stack sizes (fill the stack with a pattern and inspect the high-water mark), and audit recent pointer arithmetic.

  4. Reset and reproduce deliberately

    Reset the target from MCUHex, then reproduce the trigger while watching relevant variables in the monitor — narrowing when the fault fires is usually the fastest path to why.