Skip to content

Commit d1ea86c

Browse files
Do not keep debugger enabled in sleep modes
It turns that that this causes problems when an interrupt or exception happens when blocking on a WFI or WFE instruction. In that case, when DBGMCU_CR_DBG_SLEEP is enabled, r0 is not stacked correctly (0 is written to the stack, rather than the actual value). On exception return, this incorrect value overwrites r0, potentially causing all kinds of havoc (this was discovered because Serial.flush() would never return because its `this` pointer was suddenly null). Originally, this bit DBGMCU_CR_DBG_SLEEP was only enabled when SWD was explicitly enabled by the sketch, but since commit f1d400f (Rework atomic primitives to allow for shorter interrupt disable code segments), it is enabled by default on startup. This commit disables DBGMCU_CR_DBG_SLEEP again to work around this problem. Hopefully there is a better fix for this problem, but for now it at least allows code to work again.
1 parent b41aa6b commit d1ea86c

File tree

4 files changed

+4
-2
lines changed

4 files changed

+4
-2
lines changed

system/STM32L0xx/Lib/libstm32l052xx.a

-4 Bytes
Binary file not shown.

system/STM32L0xx/Lib/libstm32l072xx.a

-8 Bytes
Binary file not shown.

system/STM32L0xx/Lib/libstm32l082xx.a

-4 Bytes
Binary file not shown.

system/STM32L0xx/Source/stm32l0_system.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,8 @@ void stm32l0_system_initialize(uint32_t hclk, uint32_t pclk1, uint32_t pclk2, ui
675675
*/
676676
RCC->APB2ENR |= RCC_APB2ENR_DBGEN;
677677

678-
DBGMCU->CR = (DBGMCU_CR_DBG_SLEEP | DBGMCU_CR_DBG_STOP | DBGMCU_CR_DBG_STANDBY);
678+
//DBGMCU->CR = (DBGMCU_CR_DBG_SLEEP | DBGMCU_CR_DBG_STOP | DBGMCU_CR_DBG_STANDBY);
679+
DBGMCU->CR = 0;
679680
DBGMCU->APB1FZ = (DBGMCU_APB1_FZ_DBG_RTC_STOP | DBGMCU_APB1_FZ_DBG_IWDG_STOP | DBGMCU_APB1_FZ_DBG_LPTIMER_STOP);
680681
DBGMCU->APB2FZ = 0;
681682

@@ -1510,7 +1511,8 @@ void stm32l0_system_swd_enable(void)
15101511
armv6m_atomic_or(&RCC->APB2ENR, RCC_APB2ENR_DBGEN);
15111512
RCC->APB2ENR;
15121513

1513-
DBGMCU->CR = (DBGMCU_CR_DBG_SLEEP | DBGMCU_CR_DBG_STOP | DBGMCU_CR_DBG_STANDBY);
1514+
//DBGMCU->CR = (DBGMCU_CR_DBG_SLEEP | DBGMCU_CR_DBG_STOP | DBGMCU_CR_DBG_STANDBY);
1515+
DBGMCU->CR = 0;
15141516
DBGMCU->APB1FZ = (DBGMCU_APB1_FZ_DBG_RTC_STOP | DBGMCU_APB1_FZ_DBG_IWDG_STOP | DBGMCU_APB1_FZ_DBG_LPTIMER_STOP);
15151517
DBGMCU->APB2FZ = 0;
15161518

0 commit comments

Comments
 (0)