3

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?

filo
  • 8,801
  • 1
  • 25
  • 46

1 Answers1

4

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.

filo
  • 8,801
  • 1
  • 25
  • 46
  • I tried this on stm32f103 (which is Cortex-M3 based) using your code and I found that DebugMon won't be called after Power Reset. But it will be called if I do System Reset (i.e. by pulling RESET pin). I find this surprising. – Amomum Oct 12 '18 at 23:27
  • Filo - User K. Sai Bharadwaj has asked that you look at his related question here: https://electronics.stackexchange.com/q/619383/3288 – Russell McMahon May 17 '22 at 12:44