-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathATT_LoRaWAN_RTCZero.cpp
458 lines (381 loc) · 9.24 KB
/
ATT_LoRaWAN_RTCZero.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
RTC library for Arduino Zero.
Copyright (c) 2015 Arduino LLC. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <time.h>
#include "ATT_LoRaWAN_RTCZero.h"
#define EPOCH_TIME_OFF 946684800 // This is 1st January 2000, 00:00:00 in epoch time
#define EPOCH_TIME_YEAR_OFF 100 // years since 1900
voidFuncPtr RTC_callBack = NULL;
RTCZero::RTCZero()
{
_configured = false;
}
void RTCZero::begin()
{
uint16_t tmp_reg = 0;
PM->APBAMASK.reg |= PM_APBAMASK_RTC; // turn on digital interface clock
config32kOSC();
// Setup clock GCLK2 with OSC32K divided by 32
GCLK->GENDIV.reg = GCLK_GENDIV_ID(2)|GCLK_GENDIV_DIV(4);
while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY)
;
GCLK->GENCTRL.reg = (GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_XOSC32K | GCLK_GENCTRL_ID(2) | GCLK_GENCTRL_DIVSEL );
while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY)
;
GCLK->CLKCTRL.reg = (uint32_t)((GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK2 | (RTC_GCLK_ID << GCLK_CLKCTRL_ID_Pos)));
while (GCLK->STATUS.bit.SYNCBUSY)
;
RTCdisable();
RTCreset();
tmp_reg |= RTC_MODE2_CTRL_MODE_CLOCK; // set clock operating mode
tmp_reg |= RTC_MODE2_CTRL_PRESCALER_DIV1024; // set prescaler to 1024 for MODE2
tmp_reg &= ~RTC_MODE2_CTRL_MATCHCLR; // disable clear on match
//According to the datasheet RTC_MODE2_CTRL_CLKREP = 0 for 24h
tmp_reg &= ~RTC_MODE2_CTRL_CLKREP; // 24h time representation
RTC->MODE2.READREQ.reg &= ~RTC_READREQ_RCONT; // disable continuously mode
RTC->MODE2.CTRL.reg = tmp_reg;
while (RTCisSyncing())
;
NVIC_EnableIRQ(RTC_IRQn); // enable RTC interrupt
NVIC_SetPriority(RTC_IRQn, 0x00);
RTC->MODE2.INTENSET.reg |= RTC_MODE2_INTENSET_ALARM0; // enable alarm interrupt
RTC->MODE2.Mode2Alarm[0].MASK.bit.SEL = MATCH_OFF; // default alarm match is off (disabled)
while (RTCisSyncing())
;
RTCenable();
RTCresetRemove();
_configured = true;
}
void RTC_Handler(void)
{
if (RTC_callBack != NULL) {
RTC_callBack();
}
RTC->MODE2.INTFLAG.reg = RTC_MODE2_INTFLAG_ALARM0; // must clear flag at end
}
void RTCZero::enableAlarm(Alarm_Match match)
{
if (_configured) {
RTC->MODE2.Mode2Alarm[0].MASK.bit.SEL = match;
while (RTCisSyncing())
;
}
}
void RTCZero::disableAlarm()
{
if (_configured) {
RTC->MODE2.Mode2Alarm[0].MASK.bit.SEL = 0x00;
while (RTCisSyncing())
;
}
}
void RTCZero::attachInterrupt(voidFuncPtr callback)
{
RTC_callBack = callback;
}
void RTCZero::detachInterrupt()
{
RTC_callBack = NULL;
}
void RTCZero::standbyMode()
{
// Entering standby mode when connected
// via the native USB port causes issues.
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
__WFI();
}
/*
* Get Functions
*/
uint8_t RTCZero::getSeconds()
{
RTCreadRequest();
return RTC->MODE2.CLOCK.bit.SECOND;
}
uint8_t RTCZero::getMinutes()
{
RTCreadRequest();
return RTC->MODE2.CLOCK.bit.MINUTE;
}
uint8_t RTCZero::getHours()
{
RTCreadRequest();
return RTC->MODE2.CLOCK.bit.HOUR;
}
uint8_t RTCZero::getDay()
{
RTCreadRequest();
return RTC->MODE2.CLOCK.bit.DAY;
}
uint8_t RTCZero::getMonth()
{
RTCreadRequest();
return RTC->MODE2.CLOCK.bit.MONTH;
}
uint8_t RTCZero::getYear()
{
RTCreadRequest();
return RTC->MODE2.CLOCK.bit.YEAR;
}
uint8_t RTCZero::getAlarmSeconds()
{
return RTC->MODE2.Mode2Alarm[0].ALARM.bit.SECOND;
}
uint8_t RTCZero::getAlarmMinutes()
{
return RTC->MODE2.Mode2Alarm[0].ALARM.bit.MINUTE;
}
uint8_t RTCZero::getAlarmHours()
{
return RTC->MODE2.Mode2Alarm[0].ALARM.bit.HOUR;
}
uint8_t RTCZero::getAlarmDay()
{
return RTC->MODE2.Mode2Alarm[0].ALARM.bit.DAY;
}
uint8_t RTCZero::getAlarmMonth()
{
return RTC->MODE2.Mode2Alarm[0].ALARM.bit.MONTH;
}
uint8_t RTCZero::getAlarmYear()
{
return RTC->MODE2.Mode2Alarm[0].ALARM.bit.YEAR;
}
/*
* Set Functions
*/
void RTCZero::setSeconds(uint8_t seconds)
{
if (_configured) {
RTC->MODE2.CLOCK.bit.SECOND = seconds;
while (RTCisSyncing())
;
}
}
void RTCZero::setMinutes(uint8_t minutes)
{
if (_configured) {
RTC->MODE2.CLOCK.bit.MINUTE = minutes;
while (RTCisSyncing())
;
}
}
void RTCZero::setHours(uint8_t hours)
{
if (_configured) {
RTC->MODE2.CLOCK.bit.HOUR = hours;
while (RTCisSyncing())
;
}
}
void RTCZero::setTime(uint8_t hours, uint8_t minutes, uint8_t seconds)
{
if (_configured) {
setSeconds(seconds);
setMinutes(minutes);
setHours(hours);
}
}
void RTCZero::setDay(uint8_t day)
{
if (_configured) {
RTC->MODE2.CLOCK.bit.DAY = day;
while (RTCisSyncing())
;
}
}
void RTCZero::setMonth(uint8_t month)
{
if (_configured) {
RTC->MODE2.CLOCK.bit.MONTH = month;
while (RTCisSyncing())
;
}
}
void RTCZero::setYear(uint8_t year)
{
if (_configured) {
RTC->MODE2.CLOCK.bit.YEAR = year;
while (RTCisSyncing())
;
}
}
void RTCZero::setDate(uint8_t day, uint8_t month, uint8_t year)
{
if (_configured) {
setDay(day);
setMonth(month);
setYear(year);
}
}
void RTCZero::setAlarmSeconds(uint8_t seconds)
{
if (_configured) {
RTC->MODE2.Mode2Alarm[0].ALARM.bit.SECOND = seconds;
while (RTCisSyncing())
;
}
}
void RTCZero::setAlarmMinutes(uint8_t minutes)
{
if (_configured) {
RTC->MODE2.Mode2Alarm[0].ALARM.bit.MINUTE = minutes;
while (RTCisSyncing())
;
}
}
void RTCZero::setAlarmHours(uint8_t hours)
{
if (_configured) {
RTC->MODE2.Mode2Alarm[0].ALARM.bit.HOUR = hours;
while (RTCisSyncing())
;
}
}
void RTCZero::setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds)
{
if (_configured) {
setAlarmSeconds(seconds);
setAlarmMinutes(minutes);
setAlarmHours(hours);
}
}
void RTCZero::setAlarmDay(uint8_t day)
{
if (_configured) {
RTC->MODE2.Mode2Alarm[0].ALARM.bit.DAY = day;
while (RTCisSyncing())
;
}
}
void RTCZero::setAlarmMonth(uint8_t month)
{
if (_configured) {
RTC->MODE2.Mode2Alarm[0].ALARM.bit.MONTH = month;
while (RTCisSyncing())
;
}
}
void RTCZero::setAlarmYear(uint8_t year)
{
if (_configured) {
RTC->MODE2.Mode2Alarm[0].ALARM.bit.YEAR = year;
while (RTCisSyncing())
;
}
}
void RTCZero::setAlarmDate(uint8_t day, uint8_t month, uint8_t year)
{
if (_configured) {
setAlarmDay(day);
setAlarmMonth(month);
setAlarmYear(year);
}
}
uint32_t RTCZero::getEpoch()
{
RTCreadRequest();
RTC_MODE2_CLOCK_Type clockTime;
clockTime.reg = RTC->MODE2.CLOCK.reg;
struct tm tm;
tm.tm_isdst = -1;
tm.tm_yday = 0;
tm.tm_wday = 0;
tm.tm_year = clockTime.bit.YEAR + EPOCH_TIME_YEAR_OFF;
tm.tm_mon = clockTime.bit.MONTH - 1;
tm.tm_mday = clockTime.bit.DAY;
tm.tm_hour = clockTime.bit.HOUR;
tm.tm_min = clockTime.bit.MINUTE;
tm.tm_sec = clockTime.bit.SECOND;
return mktime(&tm);
}
uint32_t RTCZero::getY2kEpoch()
{
return (getEpoch() - EPOCH_TIME_OFF);
}
void RTCZero::setEpoch(uint32_t ts)
{
if (_configured) {
if (ts < EPOCH_TIME_OFF) {
ts = EPOCH_TIME_OFF;
}
time_t t = ts;
struct tm* tmp = gmtime(&t);
RTC->MODE2.CLOCK.bit.YEAR = tmp->tm_year - EPOCH_TIME_YEAR_OFF;
RTC->MODE2.CLOCK.bit.MONTH = tmp->tm_mon + 1;
RTC->MODE2.CLOCK.bit.DAY = tmp->tm_mday;
RTC->MODE2.CLOCK.bit.HOUR = tmp->tm_hour;
RTC->MODE2.CLOCK.bit.MINUTE = tmp->tm_min;
RTC->MODE2.CLOCK.bit.SECOND = tmp->tm_sec;
while (RTCisSyncing())
;
}
}
void RTCZero::setY2kEpoch(uint32_t ts)
{
if (_configured) {
setEpoch(ts + EPOCH_TIME_OFF);
}
}
/*
* Private Utility Functions
*/
/* Configure the 32768Hz Oscillator */
void RTCZero::config32kOSC()
{
SYSCTRL->XOSC32K.reg = SYSCTRL_XOSC32K_ONDEMAND |
SYSCTRL_XOSC32K_RUNSTDBY |
SYSCTRL_XOSC32K_EN32K |
SYSCTRL_XOSC32K_XTALEN |
SYSCTRL_XOSC32K_STARTUP(6) |
SYSCTRL_XOSC32K_ENABLE;
}
/* Synchronise the CLOCK register for reading*/
inline void RTCZero::RTCreadRequest() {
if (_configured) {
RTC->MODE2.READREQ.reg = RTC_READREQ_RREQ;
while (RTCisSyncing())
;
}
}
/* Wait for sync in write operations */
inline bool RTCZero::RTCisSyncing()
{
return (RTC->MODE2.STATUS.bit.SYNCBUSY);
}
void RTCZero::RTCdisable()
{
RTC->MODE2.CTRL.reg &= ~RTC_MODE2_CTRL_ENABLE; // disable RTC
while (RTCisSyncing())
;
}
void RTCZero::RTCenable()
{
RTC->MODE2.CTRL.reg |= RTC_MODE2_CTRL_ENABLE; // enable RTC
while (RTCisSyncing())
;
}
void RTCZero::RTCreset()
{
RTC->MODE2.CTRL.reg |= RTC_MODE2_CTRL_SWRST; // software reset
while (RTCisSyncing())
;
}
void RTCZero::RTCresetRemove()
{
RTC->MODE2.CTRL.reg &= ~RTC_MODE2_CTRL_SWRST; // software reset remove
while (RTCisSyncing())
;
}