Skip to main content
DrDro
Associate III
April 29, 2026
Solved

Reset reason, detect debug session

  • April 29, 2026
  • 1 reply
  • 108 views

Hallo,

is it possible to detect in my code whether it was just loaded and started under debugger? The code should behave deifferant as usual because flash loader writes into RAM and .noinit section is modified because of this.

I have already checked RSR register. It looks the same as after software reset: CPURSTFPINRSTF and SFTRSTF are high.

Moreover RSR gegister is independent from debug configuration: Debugger / Reset behaviour. I use ST-LINK v3 with ST-LINK GDB server.

 

Best answer by Andrew Neil

You can detect the reset reason:

AndrewNeil_0-1777458855719.png

https://www.st.com/resource/en/reference_manual/rm0468-stm32h723733-stm32h725735-and-stm32h730-value-line-advanced-armbased-32bit-mcus-stmicroelectronics.pdf#page=314

 

And you can detect when a debugger is connected by checking the DEBUGEN bit in DHCSR (the Debug Halting Control and Status Register):

https://developer.arm.com/documentation/ddi0403/d/Debug-Architecture/ARMv7-M-Debug/Debug-register-support-in-the-SCS/Debug-Halting-Control-and-Status-Register--DHCSR

 

Not sure that you can directly tell whether it just uploaded code, though.

You'd probably have to implement a "sentinel" to do that ...

 

See also:

https://community.st.com/t5/stm32-mcus-products/detect-debugger-connected-to-stm32l0/m-p/684426/highlight/true#M251865

1 reply

Andrew Neil
Super User
April 29, 2026

You can detect the reset reason:

AndrewNeil_0-1777458855719.png

https://www.st.com/resource/en/reference_manual/rm0468-stm32h723733-stm32h725735-and-stm32h730-value-line-advanced-armbased-32bit-mcus-stmicroelectronics.pdf#page=314

 

And you can detect when a debugger is connected by checking the DEBUGEN bit in DHCSR (the Debug Halting Control and Status Register):

https://developer.arm.com/documentation/ddi0403/d/Debug-Architecture/ARMv7-M-Debug/Debug-register-support-in-the-SCS/Debug-Halting-Control-and-Status-Register--DHCSR

 

Not sure that you can directly tell whether it just uploaded code, though.

You'd probably have to implement a "sentinel" to do that ...

 

See also:

https://community.st.com/t5/stm32-mcus-products/detect-debugger-connected-to-stm32l0/m-p/684426/highlight/true#M251865

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.