1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

Merge pull request #14032 from benpicco/cpu/sam0_common-rtc_cleanup

This commit is contained in:
Kees Bakker 2020-05-13 22:54:31 +02:00 committed by GitHub
commit 5ef4b1843a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -66,7 +66,9 @@ static inline void _rtc_set_enabled(bool on)
static void _rtc_clock_setup(void)
{
/* Use 1024 Hz GCLK3 */
GCLK->CLKCTRL.reg = GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN(SAM0_GCLK_1KHZ) | GCLK_CLKCTRL_ID_RTC;
GCLK->CLKCTRL.reg = GCLK_CLKCTRL_CLKEN
| GCLK_CLKCTRL_GEN(SAM0_GCLK_1KHZ)
| GCLK_CLKCTRL_ID_RTC;
while (GCLK->STATUS.bit.SYNCBUSY) {}
}
#else
@ -110,8 +112,8 @@ void rtc_init(void)
RTC->MODE2.INTENSET.reg = RTC_MODE2_INTENSET_OVF;
/* Clear interrupt flags */
RTC->MODE2.INTFLAG.reg |= RTC_MODE2_INTFLAG_OVF;
RTC->MODE2.INTFLAG.reg |= RTC_MODE2_INTFLAG_ALARM0;
RTC->MODE2.INTFLAG.reg = RTC_MODE2_INTFLAG_OVF
| RTC_MODE2_INTFLAG_ALARM0;
_rtc_set_enabled(1);
}
@ -121,7 +123,8 @@ int rtc_set_time(struct tm *time)
/* normalize input */
rtc_tm_normalize(time);
if ((time->tm_year < reference_year) || (time->tm_year > reference_year + 63)) {
if ((time->tm_year < reference_year) ||
(time->tm_year > reference_year + 63)) {
return -1;
}
else {
@ -139,15 +142,18 @@ int rtc_set_time(struct tm *time)
int rtc_get_time(struct tm *time)
{
RTC_MODE2_CLOCK_Type clock;
RTC_MODE2_CLOCK_Type clock;
/* Read register in one time */
clock.reg = RTC->MODE2.CLOCK.reg;
time->tm_year = clock.bit.YEAR + reference_year;
if ((time->tm_year < reference_year) || (time->tm_year > (reference_year + 63))) {
if ((time->tm_year < reference_year) ||
(time->tm_year > (reference_year + 63))) {
return -1;
}
time->tm_mon = clock.bit.MONTH - 1;
time->tm_mday = clock.bit.DAY;
time->tm_hour = clock.bit.HOUR;
@ -158,11 +164,14 @@ int rtc_get_time(struct tm *time)
int rtc_set_alarm(struct tm *time, rtc_alarm_cb_t cb, void *arg)
{
/* prevent old alarm from ringing */
rtc_clear_alarm();
/* normalize input */
rtc_tm_normalize(time);
rtc_clear_alarm();
if ((time->tm_year < reference_year) || (time->tm_year > (reference_year + 63))) {
if ((time->tm_year < reference_year) ||
(time->tm_year > (reference_year + 63))) {
return -2;
}
else {
@ -183,7 +192,7 @@ int rtc_set_alarm(struct tm *time, rtc_alarm_cb_t cb, void *arg)
/* Enable IRQ */
rtc_callback.cb = cb;
rtc_callback.arg = arg;
RTC->MODE2.INTFLAG.reg |= RTC_MODE2_INTFLAG_ALARM0;
RTC->MODE2.INTFLAG.reg = RTC_MODE2_INTFLAG_ALARM0;
RTC->MODE2.INTENSET.bit.ALARM0 = 1;
return 0;
@ -197,9 +206,11 @@ int rtc_get_alarm(struct tm *time)
alarm.reg = RTC->MODE2.Mode2Alarm[0].ALARM.reg;
time->tm_year = alarm.bit.YEAR + reference_year;
if ((time->tm_year < reference_year) || (time->tm_year > (reference_year + 63))) {
if ((time->tm_year < reference_year) ||
(time->tm_year > (reference_year + 63))) {
return -1;
}
time->tm_mon = alarm.bit.MONTH - 1;
time->tm_mday = alarm.bit.DAY;
time->tm_hour = alarm.bit.HOUR;
@ -240,11 +251,11 @@ void isr_rtc(void)
if (RTC->MODE2.INTFLAG.bit.ALARM0) {
rtc_callback.cb(rtc_callback.arg);
/* clear flag */
RTC->MODE2.INTFLAG.reg |= RTC_MODE2_INTFLAG_ALARM0;
RTC->MODE2.INTFLAG.reg = RTC_MODE2_INTFLAG_ALARM0;
}
if (RTC->MODE2.INTFLAG.bit.OVF) {
/* clear flag */
RTC->MODE2.INTFLAG.reg |= RTC_MODE2_INTFLAG_OVF;
RTC->MODE2.INTFLAG.reg = RTC_MODE2_INTFLAG_OVF;
/* At 1Hz, RTC goes till 63 years (2^5, see 17.8.22 in datasheet)
* Start RTC again with reference_year 64 years more (Be careful with alarm set) */
reference_year += 64;