2020-11-09 12:00 AM
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!!!
2020-11-10 02:09 AM
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
2020-11-10 02:47 AM
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
2020-11-10 02:51 AM
Jan,
the L4 was set as 4th topic.
2020-11-10 02:52 AM
Peter,
Ah, I see. For inexplicable reason, it requires a click... :)
JW
2020-11-10 04:51 AM
Yap. It's the STM32L496.
2020-11-10 04:55 AM
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?
2020-11-10 05:00 AM
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
2020-11-10 05:51 AM
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:
Unnecessarily tedious IMO, unless you have some other reason to stick to the "normal" dividers.
JW
2020-11-10 06:06 AM
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.