Skip to content

Commit caa5248

Browse files
authored
Merge pull request #30 from GabrielNotman/master
Fix for issue #29: Added a fix for the default CLOCK register value of 0.
2 parents f3d2587 + 6ded601 commit caa5248

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/RTCZero.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@
2424
#define EPOCH_TIME_OFF 946684800 // This is 1st January 2000, 00:00:00 in epoch time
2525
#define EPOCH_TIME_YEAR_OFF 100 // years since 1900
2626

27+
// Default date & time after reset
28+
#define DEFAULT_YEAR 2000 // 2000..2063
29+
#define DEFAULT_MONTH 1 // 1..12
30+
#define DEFAULT_DAY 1 // 1..31
31+
#define DEFAULT_HOUR 0 // 1..23
32+
#define DEFAULT_MINUTE 0 // 0..59
33+
#define DEFAULT_SECOND 0 // 0..59
34+
2735
voidFuncPtr RTC_callBack = NULL;
2836

2937
RTCZero::RTCZero()
@@ -83,12 +91,17 @@ void RTCZero::begin(bool resetTime)
8391
RTCenable();
8492
RTCresetRemove();
8593

86-
// If desired and valid, restore the time value
87-
if ((!resetTime) && (validTime)) {
94+
// If desired and valid, restore the time value, else use first valid time value
95+
if ((!resetTime) && (validTime) && (oldTime.reg != 0L)) {
8896
RTC->MODE2.CLOCK.reg = oldTime.reg;
89-
while (RTCisSyncing())
90-
;
9197
}
98+
else {
99+
RTC->MODE2.CLOCK.reg = RTC_MODE2_CLOCK_YEAR(DEFAULT_YEAR - 2000) | RTC_MODE2_CLOCK_MONTH(DEFAULT_MONTH)
100+
| RTC_MODE2_CLOCK_DAY(DEFAULT_DAY) | RTC_MODE2_CLOCK_HOUR(DEFAULT_HOUR)
101+
| RTC_MODE2_CLOCK_MINUTE(DEFAULT_MINUTE) | RTC_MODE2_CLOCK_SECOND(DEFAULT_SECOND);
102+
}
103+
while (RTCisSyncing())
104+
;
92105

93106
_configured = true;
94107
}

0 commit comments

Comments
 (0)