]> git.earman.xyz Git - sensor-watch.git/commitdiff
simplification: return date/time in same format as clock register
authorJoey Castillo <jose.castillo@gmail.com>
Tue, 28 Sep 2021 15:06:27 +0000 (11:06 -0400)
committerJoey Castillo <jose.castillo@gmail.com>
Tue, 28 Sep 2021 15:06:27 +0000 (11:06 -0400)
watch-library/watch/watch_rtc.c
watch-library/watch/watch_rtc.h

index 3d2104f3c0cd2ddecda33831b4ab84d4a9c1c38e..43cff478aecc2e942b8e5401e8defeb21960441d 100644 (file)
@@ -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);
index a71eab42ead51c1772e8ac8046646f5348ce8947..a2248758215becfa8d239a64b026bb3af8e2cb2c 100644 (file)
 
 #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();