stm32_common/periph/rtc: fix wrong read order of rtc regs, add wait for calender sync to rtc_set_time

This commit is contained in:
MichelRottleuthner 2017-08-07 14:39:07 +02:00
parent 5d04defe09
commit 0a9383228e
2 changed files with 15 additions and 9 deletions

View File

@ -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;
}

View File

@ -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,