Skip to main content
Super User
December 13, 2017
Solved

Detect Debugger Connected To STM32L0 ?

  • December 13, 2017
  • 5 replies
  • 5590 views

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)

 

http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0337e/CEGCJAHJ.html

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

    This topic has been closed for replies.
    Best answer by Tesla DeLorean
    Posted on December 13, 2017 at 19:31

    So digging a bit, BKPT instruction causes a Hard Fault when the debugger is not attached, and this is exploitable

    Googling pulled this interesting thread, which has usable information

    https://community.nxp.com/thread/424925

    5 replies

    Graduate II
    December 13, 2017
    Posted on December 13, 2017 at 17:37

    Most debug session will set at least DBG_CR_SLEEP

    Graduate II
    December 13, 2017
    Posted on December 13, 2017 at 17:57

    Arguably a CM0+ r0p1

    I would look at the DBGMCU clocks and the bits in the DBG registers at 0x40015800..0x4001580F

    Super User
    December 13, 2017
    Posted on December 13, 2017 at 18:21

    Turvey.Clive.002 wrote:

    I would look at ... the bits in the DBG registers at 0x40015800..0x4001580F

    But those are the bits I'd be looking to set depending on whether the debugger was connected or not!

    Something along the lines of:

    if( debugger_connected() )

    {

       HAL_DBGMCU_EnableDBGStopMode( );

    Without that, The ST-Link Utility seems to lose connection during STOP even with 'Enble debug in Low power mode' checked.

    But Keil seems fine with it

    Super User
    December 13, 2017
    Posted on December 13, 2017 at 18:33

    Hmmm ....

    Keil says that RCC_APB2SMENR.DBGSMEN is set, but RCC_APB2ENR.DBGEN is not set!

    So it thinks the DBG clock is enabled during Sleep mode, but not otherwise?!

    It does say that DBG_STANDBY, DBG_STOP, and DBG_SLEEP are all set in DBG_CR.

    Explorer
    December 13, 2017
    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 ....

    Super User
    December 5, 2023

    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 ...

    Explorer
    June 10, 2024

    For STM32H743 this worked for me

     

    if(READ_BIT(DBGMCU->CR, DBGMCU_CR_DBG_CKD1EN))

    {

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

    }

    Super User
    June 10, 2024

    @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.