From 8c4d0c53f21b5e4adaf137c5c80aa1e1786bf486 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Thu, 7 Mar 2024 11:30:23 +0100 Subject: [PATCH 1/2] cpu/sam0_common: RTC: avoid negative month after POR --- cpu/sam0_common/periph/rtc_rtt.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/cpu/sam0_common/periph/rtc_rtt.c b/cpu/sam0_common/periph/rtc_rtt.c index ff0da40b57..09266cf71b 100644 --- a/cpu/sam0_common/periph/rtc_rtt.c +++ b/cpu/sam0_common/periph/rtc_rtt.c @@ -330,6 +330,13 @@ static void _rtc_init(void) RTC->MODE2.CTRLA.reg = RTC_MODE2_CTRLA_PRESCALER_DIV1024 /* CLK_RTC_CNT = 1KHz / 1024 -> 1Hz */ | RTC_MODE2_CTRLA_CLOCKSYNC /* Clock Read Synchronization Enable */ | RTC_MODE2_CTRLA_MODE_CLOCK; + + /* RTC is all 0 after POR, avoid reading invalid date right after boot */ + if (RTC->MODE2.CLOCK.reg == 0) { + RTC->MODE2.CLOCK.reg = RTC_MODE2_CLOCK_MONTH(1) + | RTC_MODE2_CLOCK_DAY(1); + } + #ifdef RTC_MODE2_CTRLB_GP2EN /* RTC driver does not use COMP[1] or ALARM[1] */ /* Use second set of Compare registers as general purpose register */ From 6389b3c83bea7d7e81fc0cf4efba50107ddf4721 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Thu, 7 Mar 2024 12:55:23 +0100 Subject: [PATCH 2/2] Revert "cpu/sam0_common: RTC: avoid negative month after POR" This reverts commit 897a3ceda9e164e98422ab68b57ca14165b779a0. --- cpu/sam0_common/periph/rtc_rtt.c | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/cpu/sam0_common/periph/rtc_rtt.c b/cpu/sam0_common/periph/rtc_rtt.c index 09266cf71b..6676c1427e 100644 --- a/cpu/sam0_common/periph/rtc_rtt.c +++ b/cpu/sam0_common/periph/rtc_rtt.c @@ -566,18 +566,12 @@ int rtc_get_alarm(struct tm *time) return -1; } - time->tm_mon = alarm.bit.MONTH; + time->tm_mon = alarm.bit.MONTH - 1; time->tm_mday = alarm.bit.DAY; time->tm_hour = alarm.bit.HOUR; time->tm_min = alarm.bit.MINUTE; time->tm_sec = alarm.bit.SECOND; - /* struct tm has January as 0, RTC has January as 1 */ - /* But after power-on reset, all RTC values are 0 - avoid negative month */ - if (time->tm_mon) { - --time->tm_mon; - } - return 0; } @@ -596,18 +590,11 @@ int rtc_get_time(struct tm *time) return -1; } - time->tm_mon = clock.bit.MONTH; + time->tm_mon = clock.bit.MONTH - 1; time->tm_mday = clock.bit.DAY; time->tm_hour = clock.bit.HOUR; time->tm_min = clock.bit.MINUTE; time->tm_sec = clock.bit.SECOND; - - /* struct tm has January as 0, RTC has January as 1 */ - /* But after power-on reset, all RTC values are 0 - avoid negative month */ - if (time->tm_mon) { - --time->tm_mon; - } - return 0; }