cancel
Showing results for 
Search instead for 
Did you mean: 

Test if running STM32 under debugger.

michaelrigby-jones9
Associate II
Posted on June 11, 2012 at 13:10

STM32F103RE

I'm using the 2 wire serial debug pins as GPIO under normal operating conditions, the JTAG/SW functionality gets disabled early in the main function.  Obviously this would prevent debugging as the debugger disconnects as soon as the SW lines as reconfigured, so I currently check the status of another I/O pin to see whether I should reconfigure the SW pins or not.  Is it possible to test (from user code) whether the STM is running under the debugger so I can automatically reconfigure these pins as required without having to set a value on another pin?

#debugger #stm32 #sw #jtag
4 REPLIES 4
michaelrigby-jones9
Associate II
Posted on June 11, 2012 at 14:49

It seems I can read the IDCODE register and it will return zero if not running under the debugger.  Is this behavior reliable and/or liable to change in future silicon revisions?

domen23
Associate II
Posted on June 13, 2012 at 10:33

In my test IDCODE register sometimes gets read even when no jtag is connected.

You could try checking TRCENA bit of DEMCR. I'm not sure if that is exactly what you want.
Posted on June 13, 2012 at 16:56

One could always pull BOOT0 high, let the JTAG break into the system loader, a safe harbour, and then debug into the FLASH based code.

Generally putting code early in the reset path runs the risk of not being breakable before the JTAG wrestles control.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
michaelrigby-jones9
Associate II
Posted on June 15, 2012 at 16:37

I tried your suggestion and it didn't work but you pointed me in the right direction, so thanks!  The bit that needs to be tested is the C_DEBUGEN bit in the DHCSR register.  This seems to be a ''official'' way to test if running under the debugger as it is described on the ARM website:

http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka14536.html