Skip to content

deepSleep() returns immediately when DBGMCU DBG_STOP is set: LowPower_stop() doesn't suspend SysTick #135

Closed

Description

@M4rkWilhelm

Describe the bug

When the DBG_STOP bit in DBGMCU_CR is set, LowPower.deepSleep(ms) returns almost immediately instead of sleeping.

LowPower_sleep() calls HAL_SuspendTick() before entering Sleep mode, but LowPower_stop() doesn't. Normally that's fine, because the core clock and SysTick stop in Stop mode. With DBG_STOP set, the clock keeps running, SysTick keeps counting, and its pending interrupt wakes WFI even though LowPower_stop() has set PRIMASK (on Cortex-M a pending interrupt wakes WFI regardless of PRIMASK).

DBG_STOP survives a system reset and is only cleared by a power-on reset, so once something sets it (debug code, a debug session), deepSleep() stays broken across resets and re-uploads until the board is power-cycled.

This may be related to #123 (F103, HAL_SuspendTick() needed before deepSleep(), uploaded with "OpenOCD STLink (SWD)" according to its screenshot) and #102. On STM32F1, OpenOCD's stm32f1x.cfg sets DBG_STOP when it connects (mmw 0xE0042004 0x00000307 0), which would explain #123, but I haven't been able to test that on an F1. On my STM32L0, the equivalent write in stm32l0.cfg did not take effect (the DBGMCU clock was off), and uploading with OpenOCD alone did not trigger the problem.

To Reproduce

#include "STM32LowPower.h"

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  __HAL_RCC_DBGMCU_CLK_ENABLE();    // needed on STM32L0 for the next line to work
  HAL_DBGMCU_EnableDBGStopMode();
  LowPower.begin();
}

void loop() {
  digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));  // toggle on every wake-up
  LowPower.deepSleep(2000);
}
  1. Upload and run.
  2. Watch the LED.

Result: the LED toggles about 40 times per second (82 changes in 2 s, sampled over SWD) instead of once every 2 s. It stays that way after a reset or a new upload; only a power cycle restores normal behavior when the sketch no longer sets DBG_STOP.

With HAL_SuspendTick() before and HAL_ResumeTick() after LowPower.deepSleep(2000) (same DBG_STOP state), the LED toggles once every 2 s (3 changes in 6 s), which is the expected behavior.

Control: the same sketch without the two DBGMCU lines sleeps correctly.

Expected behavior

deepSleep(2000) sleeps for 2 s (or until another configured wake-up source) whether or not DBG_STOP is set, the same as LowPower_sleep() does by suspending the tick.

Proposed fix

In LowPower_stop(), call HAL_SuspendTick() after __disable_irq() and HAL_ResumeTick() before __enable_irq() (so the tick is running again for the existing HAL_Delay(10)).

Desktop:

  • OS: Ubuntu 26.04.1 LTS
  • Arduino IDE version: none (PlatformIO Core 6.2.0, platform ststm32 20.0.0)
  • STM32 core version: 3.0.0
  • Library version: STM32duino Low Power 2.0.0, STM32duino RTC 2.0.0
  • Tools menu settings if not the default: default (plus -DHSE_VALUE=24000000, see stm32duino/Arduino_Core_STM32 variant for ACSIP_S76S)
  • Upload method: SWD (ST-Link V2 clone, OpenOCD 0.12.0)

Hardware:

  • Board Name: AcSiP S76S (STM32L073RZ), core variant ACSIP_S76S; LED on PB5
  • Hardware Revision: S76S module inside a third-party product
  • Extra hardware used if any: none

Additional context

DBGMCU_CR read through the debugger sometimes returned 0 while DBG_STOP was clearly in effect; reading it from the CPU (after enabling the DBGMCU clock) was reliable.

Activity

  1. fpistm commented on Nov 5, 2026

    @fpistm
    Member

    If I well understand, you upload on STM32F1 using openocd and then the DBG_STOP is set preventing deepsleep ?
    Which board you used? mcu ?

  2. fpistm commented on Nov 5, 2026

    @fpistm
    Member

    looking at stm32F103C8 errata sheet:

    Image

    So I guess the workaround can be deploy only for STM32F1 series. I don't think the enable systick is needed as it should be restore by the clock config. To be verified. Could you do the PR ?

  3. M4rkWilhelm commented on Nov 5, 2026

    @M4rkWilhelm
    ContributorAuthor

    All my testing has been done on a STM32L0 (STM32L073RZ in an AcSiP S76S module, variant ACSIP_S76S), not an F1. I only mentioned the F103 issue #123 as possibly the same root cause.

    I can't find an entry about debugging stop mode and the systick timer in the L0 errata sheet (ES0292). I don't know if the fix should be limited to the F1, as any series where DBG_STOP keeps the core clock running during stop would be affected.

    Agreed on re-enabling systick. On my board SystemClock_ConfigFromStop() restores it through HAL_RCC_ClockConfig() -> HAL_InitTick(), so I'll leave HAL_ResumeTick() out of the PR.

  4. fpistm commented on Nov 6, 2026

    @fpistm
    Member

    I have to check on my side. I never met this issue before probably because the debug clock was not enabled..

  5. fpistm commented on Nov 6, 2026

    @fpistm
    Member

    Well, it sounds reasonable to disable the systick. If you can made the PR, it will be really appreciated.

  6. M4rkWilhelm commented on Nov 7, 2026

    @M4rkWilhelm
    ContributorAuthor

    PR opened here. Sorry for the late reply: #137

  7. added a commit that references this issue on Nov 9, 2026
    3c38503
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions