cancel
Showing results for 
Search instead for 
Did you mean: 

Calculate number of RTC ticks MCU spends in STOP2 mode

deckhard
Associate III

I want to calculate the actual time the CPU spends in STOP2 mode.

I wake up the MCU either by RTC Alert or LPUART.

The issue is more relevant to LPUART wake use case, because it could happen before the RTC alert expires.

I'm using RTC with LSI which continues to work in low power modes.

Is there a proper way to save the RTC time before and after entering the stop mode?

The RTC is configured to scale from 488 µs to 32s.

So I want the timestamp calculation to support both milliseconds and seconds.

How can I achieve that?

Thanks All!!!

13 REPLIES 13
Peter BENSCH
ST Employee

Why not use the backup registers to store the timestamps before and after stop mode?

One of the L4 family reference manuals states:

The RTC and the 32 backup registers are supplied through a switch that takes power either from the VDD supply when present or from the VBAT pin.

The backup registers are 32-bit registers used to store xyz bytes of user application data when VDD power is not present. They are not reset by a system or power reset, or when the device wakes up from Standby or Shutdown mode.

When your question is answered, please close this topic by choosing Select as Best.

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

deckhard,

Which STM32?

Peter's solution is the most obvious one, requiring to read out and remember the time and subsecond registers and perform some math on them; but as you use LSI, you don't care about real time, so maybe it's simpler to set PREDIV as high as possible, and that should give you a cca 100 second period. Thus, for max. 32 seconds, it's enough to read out and remember the subsecond register, and the math is significantly simplified.

Peter,

How do you know it's 'L4? The OP did not care to tell us.

While using the RTC backup registers is indeed an universal method for all low-power modes, in 'L4, in STOP2 RAM content is retained, isn't it?

JW

Peter BENSCH
ST Employee

Jan,

the L4 was set as 4th topic.

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Peter,

Ah, I see. For inexplicable reason, it requires a click... :)

JW

Yap. It's the STM32L496.

deckhard
Associate III

Peter

The internal ram content is preserved during stop2 mode.

Why not just store the before and after as local variables and do the math later?

waclawek.jan

Could you please send the 'math' needed, just to make sure I'm doing it correctly.

BTW, I'm using LSI to save power consumption.

Thanks

Provided you set RTC_PRER.PREDIV_A=0x7F and RTC_PRER.PREDIV_S=0x7FFF as I've suggested, it's enough to:

uint32_t oldSsr = RTC->SSR;

[sleep]

uint32_t delta = (oldSsr - RTC->SSR) & 0x7FFF; // (the & is in place of modulo, thanks to the counter here being 2^N)

provided that RTC_CR.BYPSHAD = 1.

If you'd want to mimic the "normal" RTC action, i.e. let it increment each second, the math would involve:

  • multiple reading of the TR and SSR registers to assure consistency
  • calculation of subseconds from SSR and seconds of TR, bearing in mind that seconds are in BCD
  • similar subtraction and modulo, but not 2^N anymore so real % would be used

Unnecessarily tedious IMO, unless you have some other reason to stick to the "normal" dividers.

JW

Of course you can store data in RAM while being in Stop 2, but is volatile memory, i.e. depending on the power supply.

The backup battery or capacitor can help keeping the data as long as the energy there is enough.

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.