Skip to content

Commit 08c1b31

Browse files
committed
CLOCK register is now only accessed once in getEpoch()
This reduces the number of synchronised accesses to the CLOCK register. Also it ensures that the values haven't changed in the middle of the epoch calculation.
1 parent 4ac2c3c commit 08c1b31

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/RTCZero.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -303,17 +303,21 @@ void RTCZero::setAlarmDate(uint8_t day, uint8_t month, uint8_t year)
303303

304304
uint32_t RTCZero::getEpoch()
305305
{
306+
RTCreadRequest();
307+
RTC_MODE2_CLOCK_Type clockTime;
308+
clockTime.reg = RTC->MODE2.CLOCK.reg;
309+
306310
struct tm tm;
307311

308312
tm.tm_isdst = -1;
309313
tm.tm_yday = 0;
310314
tm.tm_wday = 0;
311-
tm.tm_year = getYear() + EPOCH_TIME_YEAR_OFF;
312-
tm.tm_mon = getMonth() - 1;
313-
tm.tm_mday = getDay();
314-
tm.tm_hour = getHours();
315-
tm.tm_min = getMinutes();
316-
tm.tm_sec = getSeconds();
315+
tm.tm_year = clockTime.bit.YEAR + EPOCH_TIME_YEAR_OFF;
316+
tm.tm_mon = clockTime.bit.MONTH - 1;
317+
tm.tm_mday = clockTime.bit.DAY;
318+
tm.tm_hour = clockTime.bit.HOUR;
319+
tm.tm_min = clockTime.bit.MINUTE;
320+
tm.tm_sec = clockTime.bit.SECOND;
317321

318322
return mktime(&tm);
319323
}
@@ -365,7 +369,7 @@ void RTCZero::config32kOSC()
365369

366370
/* Synchronise the CLOCK register for reading*/
367371
inline void RTCZero::RTCreadRequest() {
368-
RTC->MODE2.READREQ.reg |= RTC_READREQ_RREQ;
372+
RTC->MODE2.READREQ.reg = RTC_READREQ_RREQ | RTC_READREQ_ADDR(0x10);
369373
while (RTCisSyncing())
370374
;
371375
}

0 commit comments

Comments
 (0)