2016-03-03 08:09 AM
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:
uint32_t DT_GetRtcMsTick(
void
)
{
uint32_t result;
/* To ensure consistency between the 3 values, reading either RTC_SSR or
RTC_TR locks the values in the higher-order calendar shadow registers
until RTC_DR is read. */
__disable_irq();
HAL_RTC_WaitForSynchro(&hrtc);
uint32_t subSeconds = hrtc.Instance->SSR;
uint32_t timeReg = hrtc.Instance->TR;
uint32_t date = (uint32_t)hrtc.Instance->DR;
(
void
)date;
__enable_irq();
subSeconds &= RTC_SSR_SS;
timeReg &= RTC_TR_RESERVED_MASK;
/* Second fraction = ( PREDIV_S - SS ) / ( PREDIV_S + 1 )
PEDIV_S = 255 */
/* in ms, so x1000 */
result = ((DT_PREDIV_S - subSeconds) * 1000) / (DT_PREDIV_S + 1);
result += RTC_Bcd2ToByte(timeReg & (RTC_TR_ST | RTC_TR_SU)) * 1000;
return
result;
}
It seems like sometimes there is 1 second (1000ms) missing from either start or end.
Any ideas where this could come from?
Alexander
#stm32-stm32l151-rtc