3 years ago.

deepsleep - LPTIM1 Vs RTC on STM32?


I'm working on a battery a powered application and therefore power consumption is very important. Our board is based on a STM32L4.

I think what I'm looking for is a better description and/or an example of how to use Mbed OS with long periods of deep sleep.

There are number of questions on the forum about sleeping but all the ones I've found are years old and hence I believe they are no longer relevant; Mbed OS has moved on a great deal. We're using the latest Mbed OS, i.e. V5.8 but will probably move to V5.9 when it is released.

I've been looking into sleep() and deepsleep() and here's what I think I've learnt:

  • By default TIM2 is used to generate the 1 ms SysTick for Mbed OS. deepsleep() is never called. OK, but no use for an application that does nothing for long periods. (I don't understand the significance of target.lowpowertimer_lptim in this scenario.)
  • Tickless operation can be enabled by defining MBED_TICKLESS. The RTOS may now call deepsleep() and LPTIM1 is set to generate the interrupt so that the MCU wakes from deep sleep. This is better but, as I understand it, the maximum timeout of LPTIM1 is seconds so the MCU will wake from deep sleep every few seconds. In our application the time between activity is minutes, if not hours, hence I would like to sleep for longer. (Is TIM2 still used to generate SysTick?)
  • With tickless operation enabled "target.lowpowertimer_lptim": 0 will mean that the RTC is used instead of LPTIM1 for the deep sleep wake-up. I think this means the MCU can remain in deep sleep for longer but I don't really understand the ramifications. (Is TIM2 still used to generate SysTick?)

The description of lowpowertimer_lptim in targets.json hasn't helped me understand the pros and cons of the various timers.

Any thoughts greatly appreciated, Matt

Just came across this discussion about SysTick and TIM2.

It hasn't helped me understand how to create long sleeps but it is interesting.

Question: Why is the SysTick timer not used in STM32 support?

posted by Matt Brown 05 Jun 2018

I cross posted this question so there's more info here:

How to Achieve Long Periods of Deep Sleep on STM32 #7156

posted by Matt Brown 03 Aug 2018

1 Answer

3 years ago.

Hi Matt,

The deepsleep() implementation uses the LPTIM for the timeout. It supports up to 32 bit at 32KHz. In that case, you can get up to 1.5 days of sleep. The STM32L4 has 16 bit LPTIM1 which limits the deepsleep to few seconds.

lowpowertimer_lptim has been renamed to lpticker_lptim in 6e9f04bf2fcda5446dcb2af6e7af5cece733adf9. By enabling this, it is possible to achieve higher power savings as you won't be using the RTC. (see targets/TARGET_STM/lp_ticker.c) You are correct that the MCU will be in deepsleep() for longer periods of time and this helps to achieve higher power savings.

Hope that answers your question. Please let us know if you have further questions.

Thanks, Naveen, Team mbed

Accepted Answer

Hi Naveen,

So the summary is:

If your using a STM32L4 and expect to deep sleep for more than a few seconds add "target.lpticker_lptim": 0 to get the best from deepsleep().


The STM32L4 includes both an LPTIM and an RTC and both will be enabled while stop 2 is selected so I suppose, to some extent, it doesn't matter which one is actually being used. In our application we need the actual time and therefore will be using the RTC anyway.

Is the other downside to using the RTC accuracy? I.e. the wake-up from a short deep sleep might not be as precise as if the LPTIM was used. But, you if you are deep sleeping for hours you can't expect the wake-up to be accurate to the us!!

As I understand it, not all STM32 devices include a RTC and some may have a 32-bit LPTIM, I haven't checked any datasheets. If it is necessary to add an external RTC then you have to way the power saved by deep sleeping for longer against the increase of adding an extra device?

Thanks, Matt

posted by Matt Brown 15 Jun 2018

The accuracy tolerance on both RTC and LPTIM based sleep mechanism is about 10% and they are similar. If you have an external RTC, you wouldn't be able use deepsleep as the feature is reliant on LPTIM.

posted by Naveen Kaje 19 Jun 2018