From 152ba8aec6c4ab19a56822ddd7d77e999cb715ae Mon Sep 17 00:00:00 2001 From: Joey Castillo Date: Tue, 28 Sep 2021 11:06:27 -0400 Subject: simplification: return date/time in same format as clock register --- watch-library/watch/watch_rtc.c | 28 +++------------------------- watch-library/watch/watch_rtc.h | 32 +++++++++++++++++++------------- 2 files changed, 22 insertions(+), 38 deletions(-) (limited to 'watch-library/watch') diff --git a/watch-library/watch/watch_rtc.c b/watch-library/watch/watch_rtc.c index 3d2104f3..43cff478 100644 --- a/watch-library/watch/watch_rtc.c +++ b/watch-library/watch/watch_rtc.c @@ -55,16 +55,7 @@ void _watch_rtc_init() { } void watch_rtc_set_date_time(watch_date_time date_time) { - RTC_MODE2_CLOCK_Type val; - - val.bit.SECOND = date_time.second; - val.bit.MINUTE = date_time.minute; - val.bit.HOUR = date_time.hour; - val.bit.DAY = date_time.day; - val.bit.MONTH = date_time.month; - val.bit.YEAR = (uint8_t)(date_time.year - WATCH_RTC_REFERENCE_YEAR); - - RTC->MODE2.CLOCK.reg = val.reg; + RTC->MODE2.CLOCK.reg = date_time.reg; _sync_rtc(); } @@ -72,14 +63,7 @@ watch_date_time watch_rtc_get_date_time() { watch_date_time retval; _sync_rtc(); - RTC_MODE2_CLOCK_Type val = RTC->MODE2.CLOCK; - - retval.year = val.bit.YEAR + WATCH_RTC_REFERENCE_YEAR; - retval.month = val.bit.MONTH; - retval.day = val.bit.DAY; - retval.hour = val.bit.HOUR; - retval.minute = val.bit.MINUTE; - retval.second = val.bit.SECOND; + retval.reg = RTC->MODE2.CLOCK.reg; return retval; } @@ -96,14 +80,8 @@ void watch_disable_tick_callback() { } void watch_rtc_register_alarm_callback(ext_irq_cb_t callback, watch_date_time alarm_time, watch_rtc_alarm_match mask) { - RTC->MODE2.Mode2Alarm[0].ALARM.bit.SECOND = alarm_time.second; - RTC->MODE2.Mode2Alarm[0].ALARM.bit.MINUTE = alarm_time.minute; - RTC->MODE2.Mode2Alarm[0].ALARM.bit.HOUR = alarm_time.hour; - RTC->MODE2.Mode2Alarm[0].ALARM.bit.DAY = alarm_time.day; - RTC->MODE2.Mode2Alarm[0].ALARM.bit.MONTH = alarm_time.month; - RTC->MODE2.Mode2Alarm[0].ALARM.bit.YEAR = (uint8_t)(alarm_time.year - WATCH_RTC_REFERENCE_YEAR); + RTC->MODE2.Mode2Alarm[0].ALARM.reg = alarm_time.reg; RTC->MODE2.Mode2Alarm[0].MASK.reg = mask; - RTC->MODE2.INTENSET.reg = RTC_MODE2_INTENSET_ALARM0; alarm_callback = callback; NVIC_ClearPendingIRQ(RTC_IRQn); diff --git a/watch-library/watch/watch_rtc.h b/watch-library/watch/watch_rtc.h index a71eab42..a2248758 100644 --- a/watch-library/watch/watch_rtc.h +++ b/watch-library/watch/watch_rtc.h @@ -38,13 +38,16 @@ #define WATCH_RTC_REFERENCE_YEAR (2020) -typedef struct watch_date_time { - uint16_t year; - uint8_t month; - uint8_t day; - uint8_t hour; - uint8_t minute; - uint8_t second; +typedef union { + struct { + uint32_t second : 6; // 0-59 + uint32_t minute : 6; // 0-59 + uint32_t hour : 5; // 0-23 + uint32_t day : 5; // 1-31 + uint32_t month : 4; // 1-12 + uint32_t year : 6; // 0-63 (representing 2020-2083) + } unit; + uint32_t reg; // the bit-packed value as expected by the RTC peripheral's CLOCK register. } watch_date_time; typedef enum watch_rtc_alarm_match { @@ -60,15 +63,18 @@ typedef enum watch_rtc_alarm_match { bool _watch_rtc_is_enabled(); /** @brief Sets the date and time. - * @param date_time The time you wish to set. - * @note Internally, the SAM L22 stores the year as six bits representing a value from 0 to 63. It treats this - * as a year offset from a reference year, which must be a leap year. For now, this library uses 2020 as - * the reference year, so the range of valid values is 2020 to 2083. + * @param date_time The date and time you wish to set, with a year value from 0-63 representing 2020-2083. + * @note The SAM L22 stores the year as six bits representing a value from 0 to 63. It treats this as a year + * offset from a reference year, which must be a leap year. Since 2020 was a leap year, and it allows + * useful dates through 2083, it is assumed that watch apps will use 2020 as the reference year; thus + * 1 means 2021, 2 means 2022, etc. **You will be responsible for handling this offset in your code**, + * if the calendar year is needed for timestamp calculation logic or display purposes. */ void watch_rtc_set_date_time(watch_date_time date_time); -/** @brief Returns the system date and time in the provided struct. - * @return A watch_date_time with the current date and time. +/** @brief Returns the date and time. + * @return A watch_date_time with the current date and time, with a year value from 0-63 representing 2020-2083. + * @see watch_rtc_set_date_time for notes about how the year is stored. */ watch_date_time watch_rtc_get_date_time(); -- cgit v1.2.3