Skip to main content
Visitor II
September 5, 2025
Question

STM32L072 – LPTIM1 external pulse counting with LSE in STOP mode not working

  • September 5, 2025
  • 2 replies
  • 421 views

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!

2 replies

Visitor II
September 9, 2025

Hi!

I had a similar issue. Not that I can find any documentation to explain why it was happening but when my LPTIM1 was configured with LPTIM_CLOCKSOURCE_APBCLOCK_LPOSC, the pulses would get reset or not count properly when the micro entered lp mode. Swapping the clock source to 

"hlptim1.Init.Clock.Source = LPTIM_CLOCKSOURCE_ULPTIM" resolved the issue.

 

Hope this helps :)

waclawek.jan
Super User
September 10, 2025

LPTIM can be set as asynchronous and then it doesn't need any clock to work. For further investigation, read out and post LPTIM and RCC_CCIPR registers content.

A pushbutton is a bad signal source, as it may bounce. Use some better signal source with well-defined edges.

JW