From 897a3ceda9e164e98422ab68b57ca14165b779a0 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Wed, 6 Mar 2024 18:48:26 +0100 Subject: [PATCH] cpu/sam0_common: RTC: avoid negative month after POR --- cpu/sam0_common/periph/rtc_rtt.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/cpu/sam0_common/periph/rtc_rtt.c b/cpu/sam0_common/periph/rtc_rtt.c index adc7a952dc..ff0da40b57 100644 --- a/cpu/sam0_common/periph/rtc_rtt.c +++ b/cpu/sam0_common/periph/rtc_rtt.c @@ -559,12 +559,18 @@ int rtc_get_alarm(struct tm *time) return -1; } - time->tm_mon = alarm.bit.MONTH - 1; + time->tm_mon = alarm.bit.MONTH; 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; } @@ -583,11 +589,18 @@ int rtc_get_time(struct tm *time) return -1; } - time->tm_mon = clock.bit.MONTH - 1; + time->tm_mon = clock.bit.MONTH; 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; }