bsp/atsam: Improve RTC power driver

Accept a time interval up to 24h.
This commit is contained in:
Sebastian Huber
2019-06-12 12:53:22 +02:00
parent 5d4a81f501
commit ee36616804
2 changed files with 25 additions and 10 deletions

View File

@@ -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;
/**

View File

@@ -18,23 +18,36 @@
#include <libchip/chip.h>
#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);
}