Error: jtag status contains invalid mode value - communication failure = SOLVED!
This issue bugged me a long time, finally I solved it this evening. Debugging code on my PIP-Watch using my ST-LINK-v2 JTAG debugger was very painful because the debugger software – OpenOCD and GDB – kept failing randomly during debug sessions with a rather cryptic message:
Error: jtag status contains invalid mode value - communication failure
Polling target stm32f1x.cpu failed, GDB will be halted. Polling again in 100ms
I scratched my head, updated firmware in ST-Link, looked at JTAG/SWDIO signals using a scope… But nothing helped.
Finally I found this message in a discussion forum. The problem is in the low-power mode! When CPU core clock is halted the debugger connection fails and debug session is halted.
I am using low-power mode to halt CPU clock when OS is idle - in vApplicationIdleHook() function I have the __WFI() - wait for interrupt - intrinsic function.
The solution is either to entirely disable low-power modes, or allow low-power debugging in the DBGMCU_CR register:
DBGMCU_Config(DBGMCU_SLEEP | DBGMCU_STOP | DBGMCU_STANDBY, ENABLE);
This code must be executed during CPU initialization before any low-power mode is first activated. After this, debugger connections will be kept even in sleep, standby and stop modes. The problem is gone!