From ee366168042de61057a89e3e7aee49c39678c5a3 Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Wed, 12 Jun 2019 12:53:22 +0200 Subject: bsp/atsam: Improve RTC power driver Accept a time interval up to 24h. --- bsps/arm/atsam/include/bsp/power.h | 4 +++- bsps/arm/atsam/start/power-rtc.c | 31 ++++++++++++++++++++++--------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/bsps/arm/atsam/include/bsp/power.h b/bsps/arm/atsam/include/bsp/power.h index a352386a0e..dd9946acfc 100644 --- a/bsps/arm/atsam/include/bsp/power.h +++ b/bsps/arm/atsam/include/bsp/power.h @@ -221,8 +221,10 @@ void atsam_power_handler_sleep_mode( typedef struct { /** * @brief Interval in seconds for which the power off mode should be active. + * + * An interval up to 24h is supported. */ - uint8_t interval; + uint32_t interval; } atsam_power_data_rtc_driver; /** diff --git a/bsps/arm/atsam/start/power-rtc.c b/bsps/arm/atsam/start/power-rtc.c index b60235ac29..313efff220 100644 --- a/bsps/arm/atsam/start/power-rtc.c +++ b/bsps/arm/atsam/start/power-rtc.c @@ -18,23 +18,36 @@ #include -#define ATSAM_ENABLE_ALARM_INTERRUPT (1u << 1) +#define ATSAM_ENABLE_ALARM_INTERRUPT RTC_IER_ALREN -static void set_rtc_alarm_interrupt(uint8_t interval) +static void set_rtc_alarm_interrupt(uint32_t interval) { Rtc *rtc = RTC; - rtems_time_of_day tod; + uint8_t hour; + uint8_t minute; + uint8_t second; + uint32_t time; /* Clear current status register */ RTC_ClearSCCR(rtc, 0x3F); - atsam_rtc_get_time(&tod); - tod.second = (tod.second + interval) % 60; - tod.second = (((tod.second / 10) << 4) | (tod.second % 10)); + RTC_GetTime(rtc, &hour, &minute, &second); + + time = UINT32_C(3600) * hour + UINT32_C(60) * minute + second; + time = (time + interval) % (UINT32_C(24) * 3600); + + second = (uint8_t) (time % 60); + minute = (uint8_t) ((time / 60) % 60); + hour = (uint8_t) (time / 3600); + + if (interval < 60) { + RTC_SetTimeAlarm(rtc, NULL, NULL, &second); + } else if (interval < 3600) { + RTC_SetTimeAlarm(rtc, NULL, &minute, &second); + } else { + RTC_SetTimeAlarm(rtc, &hour, &minute, &second); + } - rtc->RTC_TIMALR &= ~RTC_TIMALR_SECEN; - rtc->RTC_TIMALR = tod.second; - rtc->RTC_TIMALR |= RTC_TIMALR_SECEN; RTC_EnableIt(rtc, ATSAM_ENABLE_ALARM_INTERRUPT); } -- cgit v1.2.3