Showing results for 
Search instead for 
Did you mean: 

Detect Debugger Connected To STM32L0 ?

Andrew Neil
Evangelist III

Posted on December 13, 2017 at 17:32




In Cortex-M3, M4, etc, we can just check the DEBUGEN bit in DHCSR (the Debug Halting Control and Status Register)

But in Cortex-M0, that register is not accessible to the user code.



So - is there any other way on an STM32L0 to detect when the debugger is connected?


#stm32l0 #debug

Thomas Roell
Associate III
Posted on December 13, 2017 at 22:06

While I would not dare to question Clive One ...

SWDIO has a pullup on the STM32L0 side. The IDLE state of SWDIO is a logic 0. So one could simply read back SDWIO (in a loop with multiple samples). If there is a 0 detected, then a debugger is driving the line.

N.b. that this should detect the presence of a debugger, not necessarily whether it's active. So after detecting presence, one could use the __BKPT() idea to check whether it's active ....

Andrew Neil
Evangelist III

Coming back to this, on an STM32F0, the RCC_APB2ENR_DBGMCUEN bit is set only when a debug session is active.

(not set when no debugger is connected, and also not set when a debugger is physically connected, but no debug session is active)

At least that's the case with STM32CubeIDE v1.13.0 and an ST-Link/V2 ...


For STM32H743 this worked for me




// This Code here is only entered when Debugging is active


@ChrisAtWork wrote:

For STM32H743

But that's not a Cortex-M0.

The point is that higher Cortex-Mx give access to DBGMCU status bits - I know that (see OP) - but that's not available on M0.