WFI and JTAG

JTAG and wfi do not place nicely together. You've probably noticed this yourself, wondering why JTAG can't connect or a halt/breakpoint doesn't work.

When the ARM core is executing the wait for interrupt operation, the core is placed into a low power mode and the core clock is gated. This wreaks havoc with JTAG, as the core clock is required to detect the JTAG clock transitions.

The simplest way to keep your debugging sanity is to provide hooks to disable wfi when JTAG is needed. One option is to use a #define to control whether your idle loop performs wfi or spins the processor. In quick pseudo-code:

#ifdef DISABLE_WFI
    tx_thread_sleep(1)
#else
    asm("wfi");
#endif

However, once you need to debug returned units, reliability units, or factory failures it's not always feasible to reflash a unit. A more complicated implementation is to incorporate an NVRAM library and set a software flag (more on that in the future). On boot, the software checks the flag and uses the correct idle loop. If you then need to debug a device but can't re-flash it, simply set the NVRAM flag, reboot, et voila!

Note that this strategy will have an effect on timing and power consumption, so it will not be the holy grail for all cases. But it will certainly make your life easier if you're running into the wfi wall.