simplification: return date/time in same format as clock register

This commit is contained in:
Joey Castillo 2021-09-28 11:06:27 -04:00
parent b353e47506
commit 152ba8aec6
2 changed files with 22 additions and 38 deletions

View File

@ -55,16 +55,7 @@ void _watch_rtc_init() {
} }
void watch_rtc_set_date_time(watch_date_time date_time) { void watch_rtc_set_date_time(watch_date_time date_time) {
RTC_MODE2_CLOCK_Type val; RTC->MODE2.CLOCK.reg = date_time.reg;
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;
_sync_rtc(); _sync_rtc();
} }
@ -72,14 +63,7 @@ watch_date_time watch_rtc_get_date_time() {
watch_date_time retval; watch_date_time retval;
_sync_rtc(); _sync_rtc();
RTC_MODE2_CLOCK_Type val = RTC->MODE2.CLOCK; retval.reg = RTC->MODE2.CLOCK.reg;
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;
return retval; 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) { 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.reg = alarm_time.reg;
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].MASK.reg = mask; RTC->MODE2.Mode2Alarm[0].MASK.reg = mask;
RTC->MODE2.INTENSET.reg = RTC_MODE2_INTENSET_ALARM0; RTC->MODE2.INTENSET.reg = RTC_MODE2_INTENSET_ALARM0;
alarm_callback = callback; alarm_callback = callback;
NVIC_ClearPendingIRQ(RTC_IRQn); NVIC_ClearPendingIRQ(RTC_IRQn);

View File

@ -38,13 +38,16 @@
#define WATCH_RTC_REFERENCE_YEAR (2020) #define WATCH_RTC_REFERENCE_YEAR (2020)
typedef struct watch_date_time { typedef union {
uint16_t year; struct {
uint8_t month; uint32_t second : 6; // 0-59
uint8_t day; uint32_t minute : 6; // 0-59
uint8_t hour; uint32_t hour : 5; // 0-23
uint8_t minute; uint32_t day : 5; // 1-31
uint8_t second; 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; } watch_date_time;
typedef enum watch_rtc_alarm_match { typedef enum watch_rtc_alarm_match {
@ -60,15 +63,18 @@ typedef enum watch_rtc_alarm_match {
bool _watch_rtc_is_enabled(); bool _watch_rtc_is_enabled();
/** @brief Sets the date and time. /** @brief Sets the date and time.
* @param date_time The time you wish to set. * @param date_time The date and time you wish to set, with a year value from 0-63 representing 2020-2083.
* @note Internally, the SAM L22 stores the year as six bits representing a value from 0 to 63. It treats this * @note The SAM L22 stores the year as six bits representing a value from 0 to 63. It treats this as a year
* as a year offset from a reference year, which must be a leap year. For now, this library uses 2020 as * offset from a reference year, which must be a leap year. Since 2020 was a leap year, and it allows
* the reference year, so the range of valid values is 2020 to 2083. * 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); void watch_rtc_set_date_time(watch_date_time date_time);
/** @brief Returns the system date and time in the provided struct. /** @brief Returns the date and time.
* @return A watch_date_time with the current 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(); watch_date_time watch_rtc_get_date_time();