2025-09-05 4:14 AM
STM32L072 – LPTIM1 external pulse counting with LSE in STOP mode not working
Description:
Hello,
I am trying to configure LPTIM1 on an STM32L072 to count external pulses from a button connected to PB5 (LPTIM1_IN1). The goal is to keep counting pulses while the MCU is in STOP mode.
What I did:
RTC is already running from LSE in STOP mode, so LSE is enabled and stable.
In CubeMX I selected:
LPTIM1 clock = LSE (ULPTIM)
Counter source = External
Generated initialization looks like this:
hlptim1.Instance = LPTIM1;
hlptim1.Init.Clock.Source = LPTIM_CLOCKSOURCE_APBCLOCK_LPOSC; // CubeMX output, even if I picked LSE
hlptim1.Init.Clock.Prescaler = LPTIM_PRESCALER_DIV1;
hlptim1.Init.UltraLowPowerClock.Polarity = LPTIM_CLOCKPOLARITY_RISING;
hlptim1.Init.UltraLowPowerClock.SampleTime = LPTIM_CLOCKSAMPLETIME_DIRECTTRANSITION;
hlptim1.Init.Trigger.Source = LPTIM_TRIGSOURCE_SOFTWARE;
hlptim1.Init.UpdateMode = LPTIM_UPDATE_IMMEDIATE;
hlptim1.Init.CounterSource = LPTIM_COUNTERSOURCE_EXTERNAL;
HAL_LPTIM_Init(&hlptim1);
Then I start the counter with:
HAL_LPTIM_Counter_Start(&hlptim1, 0xFFFF);
and enter STOP mode with:
HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
What happens:
When awake: one button press increments the counter by one (works correctly).
In STOP mode:
If clock source = APB: counter gives wrong values (24–27 counts per press).
If clock source = LSE: counter does not increment at all.
I also checked LPTIM1->CFGR after init:
CFGR = 0x00800000
This means COUNTMODE=1 (external counter mode) but CKSEL=0 (internal clock).
Question:
How can I properly configure LPTIM1 with LSE clock, CKSEL=1, COUNTMODE=1 so it reliably counts external button pulses in STOP mode?
Why does CubeMX generate APBCLOCK_LPOSC even if I select LSE?
Any insights, register settings, or CubeMX workarounds would be very helpful.
Thanks in advance!