From 025f3def0492b3c2c4816dcbffb7829f927089a8 Mon Sep 17 00:00:00 2001 From: Aidan MacDonald Date: Fri, 5 Sep 2025 08:19:06 +0100 Subject: [PATCH] stm32h743: make debugger attach more reliable Add an optional sequence in the startup code to disable clock gating when the CPU goes to sleep. Normally the D1 and D3 domain clocks can be automatically gated when the CPU is asleep, and because the debug infrastructure is clocked from these domains, the debugger cannot attach when those clocks are stopped. Setting some bits in DBGMCU_CR prevents this problem, but will increase power consumption, hence it isn't enabled by default. Change-Id: Iba87750371a493adf72655aab86a908ef2702cef --- firmware/target/arm/stm32/system-stm32h7.c | 22 ++++++++++++++++++++++ firmware/target/arm/stm32/system-target.h | 4 ++++ 2 files changed, 26 insertions(+) diff --git a/firmware/target/arm/stm32/system-stm32h7.c b/firmware/target/arm/stm32/system-stm32h7.c index 61da70c151..7454463480 100644 --- a/firmware/target/arm/stm32/system-stm32h7.c +++ b/firmware/target/arm/stm32/system-stm32h7.c @@ -27,6 +27,7 @@ #include "regs/stm32h743/pwr.h" #include "regs/stm32h743/syscfg.h" #include "regs/stm32h743/flash.h" +#include "regs/stm32h743/dbgmcu.h" /* EXT timer is 1/8th of CPU clock */ #define SYSTICK_FREQ (CPU_FREQ / 8) @@ -168,6 +169,10 @@ static void stm_init_lse(void) void system_init(void) { +#if defined(DEBUG) + system_debug_enable(true); +#endif + /* Ensure IRQs are disabled and set vector table address */ disable_irq(); reg_var(CM_SCB_VTOR) = (uint32_t)__vectors_arm; @@ -192,6 +197,23 @@ void system_init(void) fmc_init(); } +void system_debug_enable(bool enable) +{ + /* + * Debug infrastructure is split across D3 and D1 power domains and + * normally these domains can be automatically clock gated when the + * CPU enters sleep mode. Because of this, the debugger might not be + * able to properly attach if the CPU is in sleep mode. + * + * Overriding clock gating using DBGMCU_CR makes debugger attaches + * more reliable, although it will increase power consumption. + */ + reg_writef(DBGMCU_CR, + D1DBGCKEN(enable), + D3DBGCKEN(enable), + DBGSLEEP_D1(enable)); +} + void tick_start(unsigned int interval_in_ms) { (void)interval_in_ms; diff --git a/firmware/target/arm/stm32/system-target.h b/firmware/target/arm/stm32/system-target.h index 58d37145ca..fa3e206baa 100644 --- a/firmware/target/arm/stm32/system-target.h +++ b/firmware/target/arm/stm32/system-target.h @@ -23,6 +23,10 @@ #include "system-arm.h" #include "cpucache-armv7m.h" +#include + +/* Enable/disable debug clock domain during sleep mode. */ +void system_debug_enable(bool enable); /* Implemented by the target -- can be a no-op if not needed */ void gpio_init(void) INIT_ATTR;