From 0a9383228e747ffb5fb9cd24e3688a084eb1fac2 Mon Sep 17 00:00:00 2001 From: MichelRottleuthner Date: Mon, 7 Aug 2017 14:39:07 +0200 Subject: [PATCH] stm32_common/periph/rtc: fix wrong read order of rtc regs, add wait for calender sync to rtc_set_time --- cpu/stm32_common/periph/rtc.c | 22 +++++++++++++++------- tests/periph_rtc/main.c | 2 -- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/cpu/stm32_common/periph/rtc.c b/cpu/stm32_common/periph/rtc.c index a5fcc0f540..c00bae935f 100644 --- a/cpu/stm32_common/periph/rtc.c +++ b/cpu/stm32_common/periph/rtc.c @@ -133,17 +133,25 @@ int rtc_set_time(struct tm *time) RTC->ISR &= (uint32_t) ~RTC_ISR_INIT; /* Enable RTC write protection */ RTC->WPR = 0xFF; + + /* wait till the new calender values are synced and can actually be read from the rtc */ + while((RTC->ISR & RTC_ISR_RSF) == 0){} + return 0; } int rtc_get_time(struct tm *time) { + /* reading TR locks the content in DR till DR is read to ensure consistency between both */ + uint32_t tr = RTC->TR; + uint32_t dr = RTC->DR; + time->tm_year = MCU_YEAR_OFFSET; - time->tm_year += (((RTC->DR & RTC_DR_YT) >> 20) * 10) + ((RTC->DR & RTC_DR_YU) >> 16); - time->tm_mon = (((RTC->DR & RTC_DR_MT) >> 12) * 10) + ((RTC->DR & RTC_DR_MU) >> 8) - 1; - time->tm_mday = (((RTC->DR & RTC_DR_DT) >> 4) * 10) + ((RTC->DR & RTC_DR_DU) >> 0); - time->tm_hour = (((RTC->TR & RTC_TR_HT) >> 20) * 10) + ((RTC->TR & RTC_TR_HU) >> 16); - if (RTC->TR & RTC_TR_PM) { + time->tm_year += (((dr & RTC_DR_YT) >> 20) * 10) + ((dr & RTC_DR_YU) >> 16); + time->tm_mon = (((dr & RTC_DR_MT) >> 12) * 10) + ((dr & RTC_DR_MU) >> 8) - 1; + time->tm_mday = (((dr & RTC_DR_DT) >> 4) * 10) + ((dr & RTC_DR_DU) >> 0); + time->tm_hour = (((tr & RTC_TR_HT) >> 20) * 10) + ((tr & RTC_TR_HU) >> 16); + if (tr & RTC_TR_PM) { /* 12PM is noon */ if (time->tm_hour != 12) { time->tm_hour += 12; @@ -153,8 +161,8 @@ int rtc_get_time(struct tm *time) /* 12AM is midnight */ time->tm_hour -= 12; } - time->tm_min = (((RTC->TR & RTC_TR_MNT) >> 12) * 10) + ((RTC->TR & RTC_TR_MNU) >> 8); - time->tm_sec = (((RTC->TR & RTC_TR_ST) >> 4) * 10) + ((RTC->TR & RTC_TR_SU) >> 0); + time->tm_min = (((tr & RTC_TR_MNT) >> 12) * 10) + ((tr & RTC_TR_MNU) >> 8); + time->tm_sec = (((tr & RTC_TR_ST) >> 4) * 10) + ((tr & RTC_TR_SU) >> 0); return 0; } diff --git a/tests/periph_rtc/main.c b/tests/periph_rtc/main.c index 586903954e..3f409371e3 100644 --- a/tests/periph_rtc/main.c +++ b/tests/periph_rtc/main.c @@ -74,8 +74,6 @@ int main(void) time.tm_sec); rtc_set_time(&time); - xtimer_usleep(100); - rtc_get_time(&time); printf("Clock set to %04d-%02d-%02d %02d:%02d:%02d\n", time.tm_year + TM_YEAR_OFFSET,