Skip to content

Commit bf354f9

Browse files
committed
Added initialisation safety checks to write operations.
If the RTC is not initialised any calls to set (time, alarm etc) will be ignored. Otherwise it would result in endless blocking.
1 parent d28415e commit bf354f9

File tree

1 file changed

+108
-68
lines changed

1 file changed

+108
-68
lines changed

src/RTCZero.cpp

Lines changed: 108 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,20 @@ void RTC_Handler(void)
9292

9393
void RTCZero::enableAlarm(Alarm_Match match)
9494
{
95-
RTC->MODE2.Mode2Alarm[0].MASK.bit.SEL = match;
96-
while (RTCisSyncing())
97-
;
95+
if (_configured) {
96+
RTC->MODE2.Mode2Alarm[0].MASK.bit.SEL = match;
97+
while (RTCisSyncing())
98+
;
99+
}
98100
}
99101

100102
void RTCZero::disableAlarm()
101103
{
102-
RTC->MODE2.Mode2Alarm[0].MASK.bit.SEL = 0x00;
103-
while (RTCisSyncing())
104-
;
104+
if (_configured) {
105+
RTC->MODE2.Mode2Alarm[0].MASK.bit.SEL = 0x00;
106+
while (RTCisSyncing())
107+
;
108+
}
105109
}
106110

107111
void RTCZero::attachInterrupt(voidFuncPtr callback)
@@ -198,114 +202,146 @@ uint8_t RTCZero::getAlarmYear()
198202

199203
void RTCZero::setSeconds(uint8_t seconds)
200204
{
201-
RTC->MODE2.CLOCK.bit.SECOND = seconds;
202-
while (RTCisSyncing())
203-
;
205+
if (_configured) {
206+
RTC->MODE2.CLOCK.bit.SECOND = seconds;
207+
while (RTCisSyncing())
208+
;
209+
}
204210
}
205211

206212
void RTCZero::setMinutes(uint8_t minutes)
207213
{
208-
RTC->MODE2.CLOCK.bit.MINUTE = minutes;
209-
while (RTCisSyncing())
210-
;
214+
if (_configured) {
215+
RTC->MODE2.CLOCK.bit.MINUTE = minutes;
216+
while (RTCisSyncing())
217+
;
218+
}
211219
}
212220

213221
void RTCZero::setHours(uint8_t hours)
214222
{
215-
RTC->MODE2.CLOCK.bit.HOUR = hours;
216-
while (RTCisSyncing())
217-
;
223+
if (_configured) {
224+
RTC->MODE2.CLOCK.bit.HOUR = hours;
225+
while (RTCisSyncing())
226+
;
227+
}
218228
}
219229

220230
void RTCZero::setTime(uint8_t hours, uint8_t minutes, uint8_t seconds)
221231
{
222-
setSeconds(seconds);
223-
setMinutes(minutes);
224-
setHours(hours);
232+
if (_configured) {
233+
setSeconds(seconds);
234+
setMinutes(minutes);
235+
setHours(hours);
236+
}
225237
}
226238

227239
void RTCZero::setDay(uint8_t day)
228240
{
229-
RTC->MODE2.CLOCK.bit.DAY = day;
230-
while (RTCisSyncing())
231-
;
241+
if (_configured) {
242+
RTC->MODE2.CLOCK.bit.DAY = day;
243+
while (RTCisSyncing())
244+
;
245+
}
232246
}
233247

234248
void RTCZero::setMonth(uint8_t month)
235249
{
236-
RTC->MODE2.CLOCK.bit.MONTH = month;
237-
while (RTCisSyncing())
238-
;
250+
if (_configured) {
251+
RTC->MODE2.CLOCK.bit.MONTH = month;
252+
while (RTCisSyncing())
253+
;
254+
}
239255
}
240256

241257
void RTCZero::setYear(uint8_t year)
242258
{
243-
RTC->MODE2.CLOCK.bit.YEAR = year;
244-
while (RTCisSyncing())
245-
;
259+
if (_configured) {
260+
RTC->MODE2.CLOCK.bit.YEAR = year;
261+
while (RTCisSyncing())
262+
;
263+
}
246264
}
247265

248266
void RTCZero::setDate(uint8_t day, uint8_t month, uint8_t year)
249267
{
250-
setDay(day);
251-
setMonth(month);
252-
setYear(year);
268+
if (_configured) {
269+
setDay(day);
270+
setMonth(month);
271+
setYear(year);
272+
}
253273
}
254274

255275
void RTCZero::setAlarmSeconds(uint8_t seconds)
256276
{
257-
RTC->MODE2.Mode2Alarm[0].ALARM.bit.SECOND = seconds;
258-
while (RTCisSyncing())
259-
;
277+
if (_configured) {
278+
RTC->MODE2.Mode2Alarm[0].ALARM.bit.SECOND = seconds;
279+
while (RTCisSyncing())
280+
;
281+
}
260282
}
261283

262284
void RTCZero::setAlarmMinutes(uint8_t minutes)
263285
{
264-
RTC->MODE2.Mode2Alarm[0].ALARM.bit.MINUTE = minutes;
265-
while (RTCisSyncing())
266-
;
286+
if (_configured) {
287+
RTC->MODE2.Mode2Alarm[0].ALARM.bit.MINUTE = minutes;
288+
while (RTCisSyncing())
289+
;
290+
}
267291
}
268292

269293
void RTCZero::setAlarmHours(uint8_t hours)
270294
{
271-
RTC->MODE2.Mode2Alarm[0].ALARM.bit.HOUR = hours;
272-
while (RTCisSyncing())
273-
;
295+
if (_configured) {
296+
RTC->MODE2.Mode2Alarm[0].ALARM.bit.HOUR = hours;
297+
while (RTCisSyncing())
298+
;
299+
}
274300
}
275301

276302
void RTCZero::setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds)
277303
{
278-
setAlarmSeconds(seconds);
279-
setAlarmMinutes(minutes);
280-
setAlarmHours(hours);
304+
if (_configured) {
305+
setAlarmSeconds(seconds);
306+
setAlarmMinutes(minutes);
307+
setAlarmHours(hours);
308+
}
281309
}
282310

283311
void RTCZero::setAlarmDay(uint8_t day)
284312
{
285-
RTC->MODE2.Mode2Alarm[0].ALARM.bit.DAY = day;
286-
while (RTCisSyncing())
287-
;
313+
if (_configured) {
314+
RTC->MODE2.Mode2Alarm[0].ALARM.bit.DAY = day;
315+
while (RTCisSyncing())
316+
;
317+
}
288318
}
289319

290320
void RTCZero::setAlarmMonth(uint8_t month)
291321
{
292-
RTC->MODE2.Mode2Alarm[0].ALARM.bit.MONTH = month;
293-
while (RTCisSyncing())
294-
;
322+
if (_configured) {
323+
RTC->MODE2.Mode2Alarm[0].ALARM.bit.MONTH = month;
324+
while (RTCisSyncing())
325+
;
326+
}
295327
}
296328

297329
void RTCZero::setAlarmYear(uint8_t year)
298330
{
299-
RTC->MODE2.Mode2Alarm[0].ALARM.bit.YEAR = year;
300-
while (RTCisSyncing())
301-
;
331+
if (_configured) {
332+
RTC->MODE2.Mode2Alarm[0].ALARM.bit.YEAR = year;
333+
while (RTCisSyncing())
334+
;
335+
}
302336
}
303337

304338
void RTCZero::setAlarmDate(uint8_t day, uint8_t month, uint8_t year)
305339
{
306-
setAlarmDay(day);
307-
setAlarmMonth(month);
308-
setAlarmYear(year);
340+
if (_configured) {
341+
setAlarmDay(day);
342+
setAlarmMonth(month);
343+
setAlarmYear(year);
344+
}
309345
}
310346

311347
uint32_t RTCZero::getEpoch()
@@ -336,27 +372,31 @@ uint32_t RTCZero::getY2kEpoch()
336372

337373
void RTCZero::setEpoch(uint32_t ts)
338374
{
339-
if (ts < EPOCH_TIME_OFF) {
340-
ts = EPOCH_TIME_OFF;
341-
}
375+
if (_configured) {
376+
if (ts < EPOCH_TIME_OFF) {
377+
ts = EPOCH_TIME_OFF;
378+
}
342379

343-
time_t t = ts;
344-
struct tm* tmp = gmtime(&t);
380+
time_t t = ts;
381+
struct tm* tmp = gmtime(&t);
345382

346-
RTC->MODE2.CLOCK.bit.YEAR = tmp->tm_year - EPOCH_TIME_YEAR_OFF;
347-
RTC->MODE2.CLOCK.bit.MONTH = tmp->tm_mon + 1;
348-
RTC->MODE2.CLOCK.bit.DAY = tmp->tm_mday;
349-
RTC->MODE2.CLOCK.bit.HOUR = tmp->tm_hour;
350-
RTC->MODE2.CLOCK.bit.MINUTE = tmp->tm_min;
351-
RTC->MODE2.CLOCK.bit.SECOND = tmp->tm_sec;
383+
RTC->MODE2.CLOCK.bit.YEAR = tmp->tm_year - EPOCH_TIME_YEAR_OFF;
384+
RTC->MODE2.CLOCK.bit.MONTH = tmp->tm_mon + 1;
385+
RTC->MODE2.CLOCK.bit.DAY = tmp->tm_mday;
386+
RTC->MODE2.CLOCK.bit.HOUR = tmp->tm_hour;
387+
RTC->MODE2.CLOCK.bit.MINUTE = tmp->tm_min;
388+
RTC->MODE2.CLOCK.bit.SECOND = tmp->tm_sec;
352389

353-
while (RTCisSyncing())
354-
;
390+
while (RTCisSyncing())
391+
;
392+
}
355393
}
356394

357395
void RTCZero::setY2kEpoch(uint32_t ts)
358396
{
359-
setEpoch(ts + EPOCH_TIME_OFF);
397+
if (_configured) {
398+
setEpoch(ts + EPOCH_TIME_OFF);
399+
}
360400
}
361401

362402
/*

0 commit comments

Comments
 (0)