Skip to content

Commit 43bc2cd

Browse files
committed
The library now attempts to preserve the clock time value.
It tests whether the reset cause was not due to POR or BOD (either). Additionally, it checks whether the clock was already configured in MODE2 (calendar mode). If both of these were the case it saves and the existing clock value and restores it after the configuration process has been completed. An optional parameter to begin() will always reset the clock time. This is default false, and it will by default attempt to preserve the clock time.
1 parent bf354f9 commit 43bc2cd

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/RTCZero.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,25 @@ RTCZero::RTCZero()
3131
_configured = false;
3232
}
3333

34-
void RTCZero::begin()
34+
void RTCZero::begin(bool resetTime)
3535
{
3636
uint16_t tmp_reg = 0;
3737

3838
PM->APBAMASK.reg |= PM_APBAMASK_RTC; // turn on digital interface clock
3939
config32kOSC();
4040

41+
// If the RTC is in clock mode and the reset was
42+
// not due to POR or BOD, preserve the clock time
43+
bool validTime = false;
44+
RTC_MODE2_CLOCK_Type oldTime;
45+
46+
if ((!resetTime) && (PM->RCAUSE.reg & (PM_RCAUSE_SYST | PM_RCAUSE_WDT | PM_RCAUSE_EXT))) {
47+
if (RTC->MODE2.CTRL.reg & RTC_MODE2_CTRL_MODE_CLOCK) {
48+
validTime = true;
49+
oldTime.reg = RTC->MODE2.CLOCK.reg;
50+
}
51+
}
52+
4153
// Setup clock GCLK2 with OSC32K divided by 32
4254
GCLK->GENDIV.reg = GCLK_GENDIV_ID(2)|GCLK_GENDIV_DIV(4);
4355
while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY)
@@ -78,6 +90,13 @@ void RTCZero::begin()
7890
RTCenable();
7991
RTCresetRemove();
8092

93+
// If desired and valid, restore the time value
94+
if ((!resetTime) && (validTime)) {
95+
RTC->MODE2.CLOCK.reg = oldTime.reg;
96+
while (RTCisSyncing())
97+
;
98+
}
99+
81100
_configured = true;
82101
}
83102

src/RTCZero.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class RTCZero {
3939
};
4040

4141
RTCZero();
42-
void begin();
42+
void begin(bool resetTime = false);
4343

4444
void enableAlarm(Alarm_Match match);
4545
void disableAlarm();

0 commit comments

Comments
 (0)