forked from bcmi-labs/brackets-arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRTCInt.cpp
More file actions
626 lines (519 loc) · 21.1 KB
/
Copy pathRTCInt.cpp
File metadata and controls
626 lines (519 loc) · 21.1 KB
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/*
RTC library for Arduino M0/M0PRO.
Copyright (c) 2015 Arduino SRL. 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 "RTCInt.h"
//#include <Arduino.h>
static bool time_Mode = false;
/*******************************************************************************************
*Description: Function responsible of RTC initialization
*Input Parameters: bool timeRep this parameter can be TIME_H24 (24 hour mode) or TIME_H12 (12 hour mode)
*Return Parameter: None
*******************************************************************************************/
void RTCInt::begin(bool timeRep)
{
PM->APBAMASK.reg |= PM_APBAMASK_RTC; // turn on digital interface clock for RTC
GCLK->CLKCTRL.reg = (uint32_t)((GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK4 | (RTC_GCLK_ID << GCLK_CLKCTRL_ID_Pos)));
while (GCLK->STATUS.bit.SYNCBUSY);
GCLK->GENCTRL.reg = (GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_OSCULP32K | GCLK_GENCTRL_ID(4) | GCLK_GENCTRL_DIVSEL );
while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY);
GCLK->GENDIV.reg = GCLK_GENDIV_ID(4);
GCLK->GENDIV.bit.DIV=4;
while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY);
RTCdisable();
RTCreset();
RTC->MODE2.CTRL.reg |= RTC_MODE2_CTRL_MODE_CLOCK; // set RTC operating mode
RTC->MODE2.CTRL.reg |= RTC_MODE2_CTRL_PRESCALER_DIV1024; // set prescaler to 1024 for MODE2
RTC->MODE2.CTRL.bit.MATCHCLR = 0; // disable clear on match
if (timeRep)
{
RTC->MODE2.CTRL.bit.CLKREP = 0; // 24h time representation
time_Mode = true;
}
else
{
RTC->MODE2.CTRL.bit.CLKREP = 1; // 12h time representation
time_Mode = false;
}
RTC->MODE2.READREQ.reg &= ~RTC_READREQ_RCONT; // disable continuously mode
while (RTCSync());
RTCresetRemove();
RTCenable();
}
/*
* Get Time Functions
*/
/**********************************************************************************************************************
*Description: Function for getting hour, according to time representation mode this value can
be in the range 0-24 (in case of TIME_H24) or in the range 0-12 (in case of TIME_H12)
*Input Parameters: None
*Return Parameter: unsigned int (hour)
***********************************************************************************************************************/
unsigned int RTCInt::getHour()
{
unsigned int h=0;
h=RTC->MODE2.CLOCK.bit.HOUR;
while (RTCSync());
if(time_Mode ==TIME_H12)
{
h = h & 0x0000000F;
}
return h;
}
/**********************************************************************************************************************
*Description: Function for getting minute, this value varies in the range 0 - 59
*Input Parameters: None
*Return Parameter: unsigned int (minute)
***********************************************************************************************************************/
unsigned int RTCInt::getMinute()
{
unsigned int m=0;
m=RTC->MODE2.CLOCK.bit.MINUTE;
while (RTCSync());
return m;
}
/**********************************************************************************************************************
*Description: Function for getting second, this value varies in the range 0 - 59
*Input Parameters: None
*Return Parameter: unsigned int (second)
***********************************************************************************************************************/
unsigned int RTCInt::getSecond()
{
unsigned int s=0;
s=RTC->MODE2.CLOCK.bit.SECOND;
while (RTCSync());
return s;
}
/**********************************************************************************************************************
*Description: Function for getting time (hours, minutes and seconds). This function fills a structure called local_time
accessible in the class RTCInt.
*Input Parameters: None
*Return Parameter: None
***********************************************************************************************************************/
void RTCInt::getTime(void)
{
unsigned int hour=0, h=0;
local_time.hour = getHour();
if(time_Mode == TIME_H12)
{
h=RTC->MODE2.CLOCK.bit.HOUR;
while (RTCSync());
local_time.Tmode = h & 0x10;
}
local_time.minute = getMinute();
local_time.second = getSecond();
}
unsigned char RTCInt::getMeridian(void)
{
unsigned int h=0;
unsigned char m=0;
if(time_Mode == TIME_H12)
{
h=RTC->MODE2.CLOCK.bit.HOUR;
while (RTCSync());
m = h & 0x10;
m >> 4;
}
else m=0;
return m;
}
/*
* Get Date Functions
*/
/**********************************************************************************************************************
*Description: Function for getting day
*Input Parameters: None
*Return Parameter: unsigned int day in the range 1 - 31
***********************************************************************************************************************/
unsigned int RTCInt::getDay()
{
unsigned int d=0;
d=RTC->MODE2.CLOCK.bit.DAY;
while (RTCSync());
return d;
}
/**********************************************************************************************************************
*Description: Function for getting Month
*Input Parameters: None
*Return Parameter: unsigned int month in the range 1 - 12
***********************************************************************************************************************/
unsigned int RTCInt::getMonth()
{
unsigned int month=0;
month=RTC->MODE2.CLOCK.bit.MONTH;
while (RTCSync());
return month;
}
/************************************************************************************************************************************
*Description: Function for getting year
*Input Parameters: None
*Return Parameter: unsigned int year in the range 0 - 63 (note: add offset to this value to obtain correct representation for year)
*************************************************************************************************************************************/
unsigned int RTCInt::getYear()
{
unsigned int y=0;
y=RTC->MODE2.CLOCK.bit.YEAR;
while (RTCSync());
return y;
}
/**********************************************************************************************************************
*Description: Function for getting date (day, month and year). This function fills a structure called local_date
accessible in the class RTCInt.
*Input Parameters: None
*Return Parameter: None
***********************************************************************************************************************/
void RTCInt::getDate(void)
{
local_date.day = getDay();
local_date.month = getMonth();
local_date.year = getYear();
}
/*
* Set Time Functions
*/
/************************************************************************************************************************************
*Description: Function for setting hour
*Input Parameters: unsigned int hour (according to time representation the range can be 0 - 23 or 0 - 12)
*Return Parameter: None
*************************************************************************************************************************************/
void RTCInt::setHour(unsigned int hour, unsigned char meridian)
{
if (time_Mode) {
if((hour >= 0) && (hour <= 23))
{
RTC->MODE2.CLOCK.bit.HOUR = hour;
while (RTCSync());
}
else return;
}
else {
if((hour >= 0) && (hour <=12))
{
hour=hour | (meridian << 4);
RTC->MODE2.CLOCK.bit.HOUR = hour;
while (RTCSync());
}
else return;
}
}
/************************************************************************************************************************************
*Description: Function for setting minute
*Input Parameters: unsigned int minute (range 0 - 59)
*Return Parameter: None
*************************************************************************************************************************************/
void RTCInt::setMinute(unsigned int minute)
{
if((minute >=0) && (minute <= 59))
{
RTC->MODE2.CLOCK.bit.MINUTE = minute;
while (RTCSync());
}
else return;
}
/************************************************************************************************************************************
*Description: Function for setting second
*Input Parameters: unsigned int second (range 0 - 59)
*Return Parameter: None
*************************************************************************************************************************************/
void RTCInt::setSecond(unsigned int second)
{
if((second >=0) && (second <= 59))
{
RTC->MODE2.CLOCK.bit.SECOND = second;
while (RTCSync());
}
else return;
}
/************************************************************************************************************************************
*Description: Function for setting time
*Input Parameters: unsigned int hour (range 0 - 23 or 0 - 12 according to time representation)
unsigned int minute (range 0 - 59)
unsigned int second (range 0 - 59)
*Return Parameter: None
*************************************************************************************************************************************/
void RTCInt::setTime(unsigned int hour,unsigned char meridian, unsigned int minute, unsigned int second)
{
setHour(hour,meridian);
setMinute(minute);
setSecond(second);
}
/**********************************************************************************************************************
*Description: Function for setting time (hour, minute and second). This function sets time retrieving values from a
local structure called local_time accessible in the class RTCInt.
*Input Parameters: None
*Return Parameter: None
***********************************************************************************************************************/
void RTCInt::setTime(void)
{
setHour(local_time.hour,local_time.Tmode);
setMinute(local_time.minute);
setSecond(local_time.second);
}
/*
* Set Date Functions
*/
/************************************************************************************************************************************
*Description: Function for setting day
*Input Parameters: unsigned int day (range 1 - 31)
*Return Parameter: None
*************************************************************************************************************************************/
void RTCInt::setDay(unsigned int day)
{
if((day > 1) && (day <=31))
{
RTC->MODE2.CLOCK.bit.DAY = day;
while (RTCSync());
}
else return;
}
/************************************************************************************************************************************
*Description: Function for setting month
*Input Parameters: unsigned int month (range 1 - 12)
*Return Parameter: None
*************************************************************************************************************************************/
void RTCInt::setMonth(unsigned int month)
{
if((month > 1) && (month <=12))
{
RTC->MODE2.CLOCK.bit.MONTH = month;
while (RTCSync());
}
else return;
}
/************************************************************************************************************************************
*Description: Function for setting year
*Input Parameters: unsigned int year range (0 - 63) (note: add offset to this value to obtain correct representation for year)
*Return Parameter: None
*************************************************************************************************************************************/
void RTCInt::setYear(unsigned int year)
{
if((year >= 0) && (year <=63))
{
RTC->MODE2.CLOCK.bit.YEAR = year;
while (RTCSync());
}
else return;
}
/**********************************************************************************************************************
*Description: Function for setting date (day, month and year).
*Input Parameters: unsigned int day (range 1 - 31)
unsigned int month (range 1 - 12)
unsigned int year (range 0 - 63)
*Return Parameter: None
***********************************************************************************************************************/
void RTCInt::setDate(unsigned int day, unsigned int month, unsigned int year)
{
setDay(day);
setMonth(month);
setYear(year);
}
/**********************************************************************************************************************
*Description: Function for setting date (day, month and year). This function retrieves values from a local structure
called local_date accessible in the class RTCInt.
*Input Parameters: None
*Return Parameter: None
***********************************************************************************************************************/
void RTCInt::setDate(void)
{
setDay(local_date.day);
setMonth(local_date.month);
setYear(local_date.year);
}
/*
* Set Alarm Functions
*/
/**********************************************************************************************************************
*Description: Function for enabling alarm.
*Input Parameters: unsigned int mode:
OFF alarm disabled
SEC alarm match on seconds
MMSS alarm match on minutes and seconds
HHMMSS alarm match on hours, minute and seconds
DDHHMMSS alarm match on day, hours, minutes and seconds
MMDDHHMMSS alarm match on month, day hours, minutes and seconds
YYMMDDHHMMSS alarm match on year, month, day, hours, minutes and seconds
unsigned int type:
ALARM_POLLED alarm matching must be monitored from the code
ALARM_INTERRUPT when alarm match occurs will be generated an interrupt
voidFuncPtr callback: pointer to interrupt routine for alarm match (write NULL if you use ALARM_POLLED)
*Return Parameter: None
***********************************************************************************************************************/
void RTCInt::enableAlarm(unsigned int mode, unsigned int type, voidFuncPtr callback)
{
Alarm_Mode = mode;
RTC->MODE2.Mode2Alarm->MASK.reg = mode;
while (RTCSync());
if(type == ALARM_POLLED)
{
return;
}
else if(type == ALARM_INTERRUPT)
{
NVIC_DisableIRQ(RTC_IRQn); //disable any RTC interrupt
NVIC_ClearPendingIRQ(RTC_IRQn); //clear all pending interrupt for RTC
NVIC_SetPriority(RTC_IRQn,0); //set RTC interrupt priority
NVIC_EnableIRQ(RTC_IRQn); //enable general interrupt for RTC
RTC->MODE2.INTENSET.bit.ALARM0 = 1; //enable ALARM0 mathc
while (RTCSync());
_callback = callback;
RTC->MODE2.INTFLAG.bit.ALARM0 = 1; //clear interrupt flag for RTC ALARM0 interrupt
}
}
/**********************************************************************************************************************
*Description: Function for setting alarm. This function retrieves values from two local structures
called local_date and local_time accessible in the class RTCInt. According to match alarm type, values
can be obtained from local_time, local_date or both structures.
*Input Parameters: None
*Return Parameter: None
***********************************************************************************************************************/
void RTCInt::setAlarm(void)
{
switch(RTCInt::Alarm_Mode)
{
case OFF :
break;
case SEC :
RTC->MODE2.Mode2Alarm->ALARM.bit.SECOND= local_time.second;
while (RTCSync());
break;
case MMSS :
RTC->MODE2.Mode2Alarm->ALARM.bit.MINUTE= local_time.minute;
while (RTCSync());
RTC->MODE2.Mode2Alarm->ALARM.bit.SECOND= local_time.second;
while (RTCSync());
break;
case HHMMSS :
RTC->MODE2.Mode2Alarm->ALARM.bit.HOUR= local_time.hour;
while (RTCSync());
RTC->MODE2.Mode2Alarm->ALARM.bit.MINUTE= local_time.minute;
while (RTCSync());
RTC->MODE2.Mode2Alarm->ALARM.bit.SECOND= local_time.second;
while (RTCSync());
break;
case DDHHMMSS :
RTC->MODE2.Mode2Alarm->ALARM.bit.DAY= local_date.day;
while (RTCSync());
RTC->MODE2.Mode2Alarm->ALARM.bit.HOUR= local_time.hour;
while (RTCSync());
RTC->MODE2.Mode2Alarm->ALARM.bit.MINUTE= local_time.minute;
while (RTCSync());
RTC->MODE2.Mode2Alarm->ALARM.bit.SECOND= local_time.second;
while (RTCSync());
break;
case MMDDHHMMSS :
RTC->MODE2.Mode2Alarm->ALARM.bit.MONTH= local_date.month;
while (RTCSync());
RTC->MODE2.Mode2Alarm->ALARM.bit.DAY= local_date.day;
while (RTCSync());
RTC->MODE2.Mode2Alarm->ALARM.bit.HOUR= local_time.hour;
while (RTCSync());
RTC->MODE2.Mode2Alarm->ALARM.bit.MINUTE= local_time.minute;
while (RTCSync());
RTC->MODE2.Mode2Alarm->ALARM.bit.SECOND= local_time.second;
while (RTCSync());
break;
case YYMMDDHHMMSS :
RTC->MODE2.Mode2Alarm->ALARM.bit.YEAR= local_date.year;
while (RTCSync());
RTC->MODE2.Mode2Alarm->ALARM.bit.MONTH= local_date.month;
while (RTCSync());
RTC->MODE2.Mode2Alarm->ALARM.bit.DAY= local_date.day;
while (RTCSync());
RTC->MODE2.Mode2Alarm->ALARM.bit.HOUR= local_time.hour;
while (RTCSync());
RTC->MODE2.Mode2Alarm->ALARM.bit.MINUTE= local_time.minute;
while (RTCSync());
RTC->MODE2.Mode2Alarm->ALARM.bit.SECOND= local_time.second;
while (RTCSync());
break;
default :
break;
}
}
/**********************************************************************************************************************
*Description: Function for getting status of alarm match. This function is used when you adopt ALARM_POLLED mode
*Input Parameters: None
*Return Parameter: bool alarm match
***********************************************************************************************************************/
bool RTCInt::alarmMatch(void)
{
return RTC->MODE2.INTFLAG.bit.ALARM0;
}
/*
* Private Utility Functions
*/
/**********************************************************************************************************************
*Description: Function for retrieving sync status of RTC
*Input Parameters: None
*Return Parameter: bool RTC sync
***********************************************************************************************************************/
bool RTCInt::RTCSync()
{
return (RTC->MODE2.STATUS.bit.SYNCBUSY);
}
/**********************************************************************************************************************
*Description: Function for disabling RTC
*Input Parameters: None
*Return Parameter: None
***********************************************************************************************************************/
void RTCInt::RTCdisable()
{
RTC->MODE2.CTRL.reg &= ~RTC_MODE2_CTRL_ENABLE; // disable RTC
while (RTCSync());
}
/**********************************************************************************************************************
*Description: Function for enabling RTC
*Input Parameters: None
*Return Parameter: None
***********************************************************************************************************************/
void RTCInt::RTCenable()
{
RTC->MODE2.CTRL.reg |= RTC_MODE2_CTRL_ENABLE; // enable RTC
while (RTCSync());
}
/**********************************************************************************************************************
*Description: Function for resetting RTC. This function clears all RTC registers
*Input Parameters: None
*Return Parameter: None
***********************************************************************************************************************/
void RTCInt::RTCreset()
{
RTC->MODE2.CTRL.reg |= RTC_MODE2_CTRL_SWRST; // software reset
while (RTCSync());
}
/**********************************************************************************************************************
*Description: Function for remove reset to RTC
*Input Parameters: None
*Return Parameter: None
***********************************************************************************************************************/
void RTCInt::RTCresetRemove()
{
RTC->MODE2.CTRL.reg &= ~RTC_MODE2_CTRL_SWRST; // software reset remove
while (RTCSync());
}
/**********************************************************************************************************************
*Description: RTC handler interrupt routine. This function checks flag ALARM0 and if is set calls routine
pointed by _callback pointer then resets ALARM0 flag.
*Input Parameters: None
*Return Parameter: None
***********************************************************************************************************************/
void RTC_Handler(void)
{
if(RTC->MODE2.INTFLAG.bit.ALARM0)
{
_callback(); //call interrupt routine
RTC->MODE2.INTFLAG.bit.ALARM0 = 1; // clear ALARM0 flag
}
else return;
}