In this lesson, you will learn how to configure Data Watchpoint and Trace (DWT) units to monitor specific memory addresses in real-time without halting the processor.
Moving Beyond Simple Breakpoints
While hardware breakpoints pause your system when an instruction is reached, the Data Watchpoint and Trace (DWT) unit offers a more sophisticated approach. It allows you to track "silent" events—specifically, when data at a particular memory location is read from or written to—while your application continues to run at full speed. This is essential for debugging intermittent memory corruption or race conditions where a debugger halt would change the timing and mask the bug.
Configuring the DWT Unit
The DWT is typically configured through specialized registers in the ARM CoreSight architecture. To monitor a variable, you define a Comparator, a Mask, and a Function.
- Comparator Register (
DWT_COMPx): You load this with the target memory address you want to watch. - Mask Register (
DWT_MASKx): This defines the "interesting" bits of the address; setting this to zero allows you to monitor exactly one word, while higher values can broaden the watch range to a block of memory. - Function Register (
DWT_FUNCTIONx): This is where you specify the trigger condition. You can configure it to fire on aread, awrite, orany accessto that address.
// Pseudo-configuration of DWT Comparator 0
DWT->COMP0 = (uint32_t)&critical_system_variable; // Set address to watch
DWT->MASK0 = 0x00; // Watch exactly 4 bytes
DWT->FUNCTION0 = 0x02; // Trigger on Write access only
From Trigger to Trace
Once configured, the DWT unit acts as a hardware observer. When the memory access occurs, it triggers an event. Depending on your debug setup, this event can either halt the CPU (like a traditional watchpoint) or, more powerfully, stream a packet into the Embedded Trace Macrocell (ETM). By routing DWT events into your trace stream, you can see a timestamped history of exactly when a variable was corrupted, relative to other system events, providing a non-intrusive "black box" recording of your data flow.
Key Takeaway: The DWT unit enables non-intrusive, real-time hardware monitoring of specific memory addresses, allowing you to catch data access events without altering the timing of your execution.