AnsweredAssumed Answered

1 second missing at reading RTC on STM32L151

Question asked by Alexander P. on Mar 3, 2016
Hello everyone,

I am using the RTC on a stm32l151VC. I am using sleep modes in order so save battery life. As I need to tell the OS how long I have been sleeping (Systick is turned off) I save the time tick before I start sleeping and one after I was sleeping. As I never sleep more than 1 second I just use the seconds and subsecond from rtc:
01.uint32_t DT_GetRtcMsTick(void)
02.{
03.   uint32_t result;
04.    
05.   /* To ensure consistency between the 3 values, reading either RTC_SSR or
06.      RTC_TR locks the values in the higher-order calendar shadow registers
07.      until RTC_DR is read. */
08.   __disable_irq();
09.   HAL_RTC_WaitForSynchro(&hrtc);
10.   uint32_t subSeconds = hrtc.Instance->SSR;
11.   uint32_t timeReg = hrtc.Instance->TR;
12.   uint32_t date = (uint32_t)hrtc.Instance->DR;
13.   (void)date;
14.   __enable_irq();
15.    
16.   subSeconds &= RTC_SSR_SS;
17.   timeReg &= RTC_TR_RESERVED_MASK;
18.    
19.   /* Second fraction = ( PREDIV_S - SS ) / ( PREDIV_S + 1 )
20.      PEDIV_S = 255 */
21.   /* in ms, so x1000 */
22.   result = ((DT_PREDIV_S - subSeconds) * 1000) / (DT_PREDIV_S + 1);
23.   result += RTC_Bcd2ToByte(timeReg & (RTC_TR_ST | RTC_TR_SU)) * 1000;
24.   return result;
25.}

It seems like sometimes there is 1 second (1000ms) missing from either start or end.
Any ideas where this could come from?

Alexander

Outcomes