Electronic – Can Cortex M4 data watchpoint trigger an interrupt without a debugger

breakpointcortexcortex-m4debugging

I am trying to diagnose memory corruption on a Cortex M4 (Kinetis K64). With the debugger (J-Link) attached I can set data watchpoints and the core will stop when a memory address is written.

I want to use this feature for field diagnostics. I set up my watchpoint like this:

uint32_t test_variable;
DWT->COMP1 = &test_variable;
DWT->MASK1 = 0; //match all comparator bits, don't ignore any
DWT->FUNCTION1 = (1 << 11)/*DATAVSIZE 1 - match whole word*/
           | (1 << 1) | (1 << 2)/*generate a watchpoint event on write*/;
test_variable = 5; // <<----- CPU stops after this line

With the debugger connected I get a breakpoint, the CPU simply stops. When the debugger is not connected nothing happens, the code just runs. I would expect a debug fault or a system reset (like with the BKPT instruction without a debugger).

Is it possible to configure the DWT (or perhaps ETM) to generate an interrupt or fault when a watchpoint is encountered without a debugger connected?

Best Answer

I looked deeper into the documentation and I found out that DebugMon_Handler is enabled by:

    CoreDebug->DEMCR = CoreDebug_DEMCR_TRCENA_Msk /*enable tracing*/ |
                       CoreDebug_DEMCR_MON_EN_Msk /*enable debug interrupt*/;

Now I get DebugMon_Handler interrupt when the watchpoint is hit.