In this lesson, you will learn how to configure hardware triggers to capture and store high-fidelity trace data precisely when a system crash or watchdog reset occurs, turning elusive "Heisenbugs" into actionable post-mortem data.
The Challenge of Post-Mortem Debugging
Standard debugging often stops the processor, which clears the context of the crash. However, by leveraging Embedded Trace Macrocell (ETM) data streams and Data Watchpoint and Trace (DWT) units, we can maintain a continuous "flight recorder" of system execution. The goal is to configure the hardware so that the circular trace buffer freezes the moment a specific fault condition—like a HardFault exception or a watchdog timer timeout—is detected.
Configuring the Trigger
To automate this, we configure a hardware trigger that links an event (e.g., a write to a memory-mapped register or the transition of the processor into Lockup state) to the trace memory controller. Instead of simply stopping the CPU, the trigger sends a signal to the trace unit to "capture and hold."
In a typical implementation, you would set a trigger on the Fault Status Register:
// Configure DWT to trigger trace capture on a bus fault
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
// Monitor the System Handler Control and State Register (SHCSR)
// Set a watchpoint on the address of the HardFault status register
DWT->COMP0 = (uint32_t)&SCB->HFSR;
DWT->MASK0 = 0; // Trigger on any change
DWT->FUNCTION0 = DWT_FUNCTION_DATAVMATCH_Msk | DWT_FUNCTION_CYCMATCH_Msk;
When the HFSR register updates due to a fault, the hardware trigger forces the trace buffer to stop overwriting data. This ensures the buffer contains the exact instruction stream leading up to the crash.
Analyzing the Capture
Once the system halts, you do not need to step through code. Instead, you extract the trace buffer via the Debug Access Port (DAP). By reconstructing the trace data, you can see the sequence of instructions that preceded the crash, even if the error happened milliseconds before the final state. This effectively bridges the gap between a non-intrusive runtime trace and the static state left behind after a reset.
Key Takeaway: By binding trace buffer capture to hardware-level fault signals, you create an automated, non-intrusive flight recorder that preserves the vital context of a system crash for offline forensic analysis.