source: rtos_arduino/trunk/arduino_lib/hardware/arduino/samd/libraries/RTC/src/RTCInt.cpp@ 259

Last change on this file since 259 was 259, checked in by ertl-honda, 8 years ago

1.7.11に対応.

File size: 21.7 KB
Line 
1/*
2 RTC library for Arduino M0/M0PRO.
3 Copyright (c) 2015 Arduino SRL. All right reserved.
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Lesser General Public License for more details.
12 You should have received a copy of the GNU Lesser General Public
13 License along with this library; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15*/
16
17#include "RTCInt.h"
18//#include <Arduino.h>
19
20static bool time_Mode = false;
21
22/*******************************************************************************************
23*Description: Function responsible of RTC initialization
24*Input Parameters: bool timeRep this parameter can be TIME_H24 (24 hour mode) or TIME_H12 (12 hour mode)
25*Return Parameter: None
26*******************************************************************************************/
27void RTCInt::begin(bool timeRep, int oscillatorType)
28{
29
30
31 PM->APBAMASK.reg |= PM_APBAMASK_RTC; // turn on digital interface clock for RTC
32
33 GCLK->CLKCTRL.reg = (uint32_t)((GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK4 | (RTC_GCLK_ID << GCLK_CLKCTRL_ID_Pos)));
34 while (GCLK->STATUS.bit.SYNCBUSY);
35
36 //Set the oscillator to use - M0 only
37 if(oscillatorType==HIGH_PRECISION)
38 //External oscillator
39 GCLK->GENCTRL.reg = (GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_XOSC32K | GCLK_GENCTRL_ID(4) | GCLK_GENCTRL_DIVSEL );
40
41 else
42 //Internal low power oscillator (default configuration)
43 GCLK->GENCTRL.reg = (GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_OSCULP32K | GCLK_GENCTRL_ID(4) | GCLK_GENCTRL_DIVSEL );
44
45 while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY);
46
47 GCLK->GENDIV.reg = GCLK_GENDIV_ID(4);
48 GCLK->GENDIV.bit.DIV=4;
49 while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY);
50
51
52
53 RTCdisable();
54
55 RTCreset();
56
57 RTC->MODE2.CTRL.reg |= RTC_MODE2_CTRL_MODE_CLOCK; // set RTC operating mode
58 RTC->MODE2.CTRL.reg |= RTC_MODE2_CTRL_PRESCALER_DIV1024; // set prescaler to 1024 for MODE2
59 RTC->MODE2.CTRL.bit.MATCHCLR = 0; // disable clear on match
60
61
62 if (timeRep)
63 {
64 RTC->MODE2.CTRL.bit.CLKREP = 0; // 24h time representation
65 time_Mode = true;
66 }
67 else
68 {
69 RTC->MODE2.CTRL.bit.CLKREP = 1; // 12h time representation
70 time_Mode = false;
71 }
72
73 RTC->MODE2.READREQ.reg &= ~RTC_READREQ_RCONT; // disable continuously mode
74
75
76 while (RTCSync());
77 RTCresetRemove();
78 RTCenable();
79
80}
81
82void RTCInt::begin(bool timeRep){
83 begin(timeRep, LOW_POWER);
84}
85
86/*
87 * Get Time Functions
88 */
89
90/**********************************************************************************************************************
91*Description: Function for getting hour, according to time representation mode this value can
92 be in the range 0-24 (in case of TIME_H24) or in the range 0-12 (in case of TIME_H12)
93*Input Parameters: None
94*Return Parameter: unsigned int (hour)
95***********************************************************************************************************************/
96 unsigned int RTCInt::getHour()
97{
98 unsigned int h=0;
99
100
101 h=RTC->MODE2.CLOCK.bit.HOUR;
102 while (RTCSync());
103
104 if(time_Mode ==TIME_H12)
105 {
106 h = h & 0x0000000F;
107 }
108
109 return h;
110}
111
112 /**********************************************************************************************************************
113*Description: Function for getting minute, this value varies in the range 0 - 59
114*Input Parameters: None
115*Return Parameter: unsigned int (minute)
116***********************************************************************************************************************/
117unsigned int RTCInt::getMinute()
118{
119 unsigned int m=0;
120
121 m=RTC->MODE2.CLOCK.bit.MINUTE;
122 while (RTCSync());
123 return m;
124}
125
126
127 /**********************************************************************************************************************
128*Description: Function for getting second, this value varies in the range 0 - 59
129*Input Parameters: None
130*Return Parameter: unsigned int (second)
131***********************************************************************************************************************/
132 unsigned int RTCInt::getSecond()
133{
134 unsigned int s=0;
135
136 s=RTC->MODE2.CLOCK.bit.SECOND;
137 while (RTCSync());
138 return s;
139}
140
141
142/**********************************************************************************************************************
143*Description: Function for getting time (hours, minutes and seconds). This function fills a structure called time
144 accessible in the class RTCInt.
145*Input Parameters: None
146*Return Parameter: None
147***********************************************************************************************************************/
148void RTCInt::getTime(void)
149{
150 unsigned int hour=0, h=0;
151
152 time.hour = getHour();
153 if(time_Mode == TIME_H12)
154 {
155 h=RTC->MODE2.CLOCK.bit.HOUR;
156 while (RTCSync());
157 time.Tmode = h & 0x10;
158 }
159 time.minute = getMinute();
160 time.second = getSecond();
161}
162
163unsigned char RTCInt::getMeridian(void)
164{
165 unsigned int h=0;
166 unsigned char m=0;
167
168 if(time_Mode == TIME_H12)
169 {
170 h=RTC->MODE2.CLOCK.bit.HOUR;
171 while (RTCSync());
172 m = h & 0x10;
173 m >> 4;
174 }
175 else m=0;
176return m;
177}
178
179
180/*
181 * Get Date Functions
182 */
183
184
185/**********************************************************************************************************************
186*Description: Function for getting day
187*Input Parameters: None
188*Return Parameter: unsigned int day in the range 1 - 31
189***********************************************************************************************************************/
190unsigned int RTCInt::getDay()
191{
192 unsigned int d=0;
193
194 d=RTC->MODE2.CLOCK.bit.DAY;
195 while (RTCSync());
196 return d;
197}
198
199/**********************************************************************************************************************
200*Description: Function for getting Month
201*Input Parameters: None
202*Return Parameter: unsigned int month in the range 1 - 12
203***********************************************************************************************************************/
204unsigned int RTCInt::getMonth()
205{
206 unsigned int month=0;
207
208 month=RTC->MODE2.CLOCK.bit.MONTH;
209 while (RTCSync());
210 return month;
211}
212
213
214/************************************************************************************************************************************
215*Description: Function for getting year
216*Input Parameters: None
217*Return Parameter: unsigned int year in the range 0 - 63 (note: add offset to this value to obtain correct representation for year)
218*************************************************************************************************************************************/
219unsigned int RTCInt::getYear()
220{
221 unsigned int y=0;
222
223 y=RTC->MODE2.CLOCK.bit.YEAR;
224 while (RTCSync());
225 return y;
226}
227
228
229/**********************************************************************************************************************
230*Description: Function for getting date (day, month and year). This function fills a structure called date
231 accessible in the class RTCInt.
232*Input Parameters: None
233*Return Parameter: None
234***********************************************************************************************************************/
235void RTCInt::getDate(void)
236{
237 date.day = getDay();
238 date.month = getMonth();
239 date.year = getYear();
240}
241
242/*
243 * Set Time Functions
244 */
245
246
247/************************************************************************************************************************************
248*Description: Function for setting hour
249*Input Parameters: unsigned int hour (according to time representation the range can be 0 - 23 or 0 - 12)
250*Return Parameter: None
251*************************************************************************************************************************************/
252void RTCInt::setHour(unsigned int hour, unsigned char meridian)
253{
254 if (time_Mode) {
255 if((hour >= 0) && (hour <= 23))
256 {
257 RTC->MODE2.CLOCK.bit.HOUR = hour;
258 while (RTCSync());
259 }
260 else return;
261 }
262 else {
263 if((hour >= 0) && (hour <=12))
264 {
265
266 hour=hour | (meridian << 4);
267 RTC->MODE2.CLOCK.bit.HOUR = hour;
268 while (RTCSync());
269 }
270 else return;
271 }
272
273}
274
275
276 /************************************************************************************************************************************
277*Description: Function for setting minute
278*Input Parameters: unsigned int minute (range 0 - 59)
279*Return Parameter: None
280*************************************************************************************************************************************/
281 void RTCInt::setMinute(unsigned int minute)
282{
283 if((minute >=0) && (minute <= 59))
284 {
285 RTC->MODE2.CLOCK.bit.MINUTE = minute;
286 while (RTCSync());
287 }
288 else return;
289}
290
291
292
293/************************************************************************************************************************************
294*Description: Function for setting second
295*Input Parameters: unsigned int second (range 0 - 59)
296*Return Parameter: None
297*************************************************************************************************************************************/
298void RTCInt::setSecond(unsigned int second)
299{
300 if((second >=0) && (second <= 59))
301 {
302 RTC->MODE2.CLOCK.bit.SECOND = second;
303 while (RTCSync());
304 }
305 else return;
306}
307
308
309/************************************************************************************************************************************
310*Description: Function for setting time
311*Input Parameters: unsigned int hour (range 0 - 23 or 0 - 12 according to time representation)
312 unsigned int minute (range 0 - 59)
313 unsigned int second (range 0 - 59)
314*Return Parameter: None
315*************************************************************************************************************************************/
316void RTCInt::setTime(unsigned int hour,unsigned char meridian, unsigned int minute, unsigned int second)
317{
318 setHour(hour,meridian);
319 setMinute(minute);
320 setSecond(second);
321}
322
323
324/**********************************************************************************************************************
325*Description: Function for setting time (hour, minute and second). This function sets time retrieving values from a
326 local structure called time accessible in the class RTCInt.
327*Input Parameters: None
328*Return Parameter: None
329***********************************************************************************************************************/
330void RTCInt::setTime(void)
331{
332 setHour(time.hour,time.Tmode);
333 setMinute(time.minute);
334 setSecond(time.second);
335}
336
337
338/*
339 * Set Date Functions
340 */
341
342
343/************************************************************************************************************************************
344*Description: Function for setting day
345*Input Parameters: unsigned int day (range 1 - 31)
346*Return Parameter: None
347*************************************************************************************************************************************/
348void RTCInt::setDay(unsigned int day)
349{
350 if((day > 1) && (day <=31))
351 {
352 RTC->MODE2.CLOCK.bit.DAY = day;
353 while (RTCSync());
354 }
355 else return;
356}
357
358
359/************************************************************************************************************************************
360*Description: Function for setting month
361*Input Parameters: unsigned int month (range 1 - 12)
362*Return Parameter: None
363*************************************************************************************************************************************/
364void RTCInt::setMonth(unsigned int month)
365{
366 if((month > 1) && (month <=12))
367 {
368 RTC->MODE2.CLOCK.bit.MONTH = month;
369 while (RTCSync());
370 }
371 else return;
372}
373
374
375/************************************************************************************************************************************
376*Description: Function for setting year
377*Input Parameters: unsigned int year range (0 - 63) (note: add offset to this value to obtain correct representation for year)
378*Return Parameter: None
379*************************************************************************************************************************************/
380void RTCInt::setYear(unsigned int year)
381{
382 if((year >= 0) && (year <=63))
383 {
384 RTC->MODE2.CLOCK.bit.YEAR = year;
385 while (RTCSync());
386 }
387 else return;
388}
389
390
391/**********************************************************************************************************************
392*Description: Function for setting date (day, month and year).
393*Input Parameters: unsigned int day (range 1 - 31)
394 unsigned int month (range 1 - 12)
395 unsigned int year (range 0 - 63)
396*Return Parameter: None
397***********************************************************************************************************************/
398void RTCInt::setDate(unsigned int day, unsigned int month, unsigned int year)
399{
400 setDay(day);
401 setMonth(month);
402 setYear(year);
403}
404
405
406/**********************************************************************************************************************
407*Description: Function for setting date (day, month and year). This function retrieves values from a local structure
408 called date accessible in the class RTCInt.
409*Input Parameters: None
410*Return Parameter: None
411***********************************************************************************************************************/
412void RTCInt::setDate(void)
413{
414 setDay(date.day);
415 setMonth(date.month);
416 setYear(date.year);
417}
418
419/*
420 * Set Alarm Functions
421 */
422
423
424 /**********************************************************************************************************************
425*Description: Function for enabling alarm.
426*Input Parameters: unsigned int mode:
427 OFF alarm disabled
428 SEC alarm match on seconds
429 MMSS alarm match on minutes and seconds
430 HHMMSS alarm match on hours, minute and seconds
431 DDHHMMSS alarm match on day, hours, minutes and seconds
432 MMDDHHMMSS alarm match on month, day hours, minutes and seconds
433 YYMMDDHHMMSS alarm match on year, month, day, hours, minutes and seconds
434 unsigned int type:
435 ALARM_POLLED alarm matching must be monitored from the code
436 ALARM_INTERRUPT when alarm match occurs will be generated an interrupt
437
438 voidFuncPtr callback: pointer to interrupt routine for alarm match (write NULL if you use ALARM_POLLED)
439*Return Parameter: None
440***********************************************************************************************************************/
441 void RTCInt::enableAlarm(unsigned int mode, unsigned int type, voidFuncPtr callback)
442 {
443
444 Alarm_Mode = mode;
445 RTC->MODE2.Mode2Alarm->MASK.reg = mode;
446 while (RTCSync());
447
448 if(type == ALARM_POLLED)
449 {
450 return;
451 }
452
453 else if(type == ALARM_INTERRUPT)
454 {
455 NVIC_DisableIRQ(RTC_IRQn); //disable any RTC interrupt
456 NVIC_ClearPendingIRQ(RTC_IRQn); //clear all pending interrupt for RTC
457 NVIC_SetPriority(RTC_IRQn,0); //set RTC interrupt priority
458 NVIC_EnableIRQ(RTC_IRQn); //enable general interrupt for RTC
459
460 RTC->MODE2.INTENSET.bit.ALARM0 = 1; //enable ALARM0 mathc
461 while (RTCSync());
462
463 _callback = callback;
464 RTC->MODE2.INTFLAG.bit.ALARM0 = 1; //clear interrupt flag for RTC ALARM0 interrupt
465
466 }
467 }
468
469
470 /**********************************************************************************************************************
471*Description: Function for setting alarm. This function retrieves values from two local structures
472 called date and time accessible in the class RTCInt. According to match alarm type, values
473 can be obtained from time, date or both structures.
474*Input Parameters: None
475*Return Parameter: None
476***********************************************************************************************************************/
477 void RTCInt::setAlarm(void)
478 {
479
480 switch(RTCInt::Alarm_Mode)
481 {
482 case OFF :
483 break;
484 case SEC :
485 RTC->MODE2.Mode2Alarm->ALARM.bit.SECOND= time.second;
486 while (RTCSync());
487 break;
488 case MMSS :
489 RTC->MODE2.Mode2Alarm->ALARM.bit.MINUTE= time.minute;
490 while (RTCSync());
491 RTC->MODE2.Mode2Alarm->ALARM.bit.SECOND= time.second;
492 while (RTCSync());
493 break;
494 case HHMMSS :
495 RTC->MODE2.Mode2Alarm->ALARM.bit.HOUR= time.hour;
496 while (RTCSync());
497 RTC->MODE2.Mode2Alarm->ALARM.bit.MINUTE= time.minute;
498 while (RTCSync());
499 RTC->MODE2.Mode2Alarm->ALARM.bit.SECOND= time.second;
500 while (RTCSync());
501 break;
502 case DDHHMMSS :
503 RTC->MODE2.Mode2Alarm->ALARM.bit.DAY= date.day;
504 while (RTCSync());
505 RTC->MODE2.Mode2Alarm->ALARM.bit.HOUR= time.hour;
506 while (RTCSync());
507 RTC->MODE2.Mode2Alarm->ALARM.bit.MINUTE= time.minute;
508 while (RTCSync());
509 RTC->MODE2.Mode2Alarm->ALARM.bit.SECOND= time.second;
510 while (RTCSync());
511 break;
512 case MMDDHHMMSS :
513 RTC->MODE2.Mode2Alarm->ALARM.bit.MONTH= date.month;
514 while (RTCSync());
515 RTC->MODE2.Mode2Alarm->ALARM.bit.DAY= date.day;
516 while (RTCSync());
517 RTC->MODE2.Mode2Alarm->ALARM.bit.HOUR= time.hour;
518 while (RTCSync());
519 RTC->MODE2.Mode2Alarm->ALARM.bit.MINUTE= time.minute;
520 while (RTCSync());
521 RTC->MODE2.Mode2Alarm->ALARM.bit.SECOND= time.second;
522 while (RTCSync());
523 break;
524 case YYMMDDHHMMSS :
525 RTC->MODE2.Mode2Alarm->ALARM.bit.YEAR= date.year;
526 while (RTCSync());
527 RTC->MODE2.Mode2Alarm->ALARM.bit.MONTH= date.month;
528 while (RTCSync());
529 RTC->MODE2.Mode2Alarm->ALARM.bit.DAY= date.day;
530 while (RTCSync());
531 RTC->MODE2.Mode2Alarm->ALARM.bit.HOUR= time.hour;
532 while (RTCSync());
533 RTC->MODE2.Mode2Alarm->ALARM.bit.MINUTE= time.minute;
534 while (RTCSync());
535 RTC->MODE2.Mode2Alarm->ALARM.bit.SECOND= time.second;
536 while (RTCSync());
537 break;
538 default :
539 break;
540 }
541
542}
543
544/**********************************************************************************************************************
545*Description: Function for getting status of alarm match. This function is used when you adopt ALARM_POLLED mode
546*Input Parameters: None
547*Return Parameter: bool alarm match
548***********************************************************************************************************************/
549bool RTCInt::alarmMatch(void)
550 {
551 return RTC->MODE2.INTFLAG.bit.ALARM0;
552 }
553
554/**********************************************************************************************************************
555*Description: Function for clearing the alarm flag.This function is used when you adopt ALARM_POLLED mode
556*Input Parameters: None
557*Return Parameter: None
558***********************************************************************************************************************/
559void RTCInt::alarmClearFlag(void){
560 RTC->MODE2.INTFLAG.bit.ALARM0=1; //clearing alarm0 flag
561}
562
563/*
564 * Private Utility Functions
565 */
566
567
568
569/**********************************************************************************************************************
570*Description: Function for retrieving sync status of RTC
571*Input Parameters: None
572*Return Parameter: bool RTC sync
573***********************************************************************************************************************/
574bool RTCInt::RTCSync()
575{
576 return (RTC->MODE2.STATUS.bit.SYNCBUSY);
577}
578
579
580/**********************************************************************************************************************
581*Description: Function for disabling RTC
582*Input Parameters: None
583*Return Parameter: None
584***********************************************************************************************************************/
585void RTCInt::RTCdisable()
586{
587 RTC->MODE2.CTRL.reg &= ~RTC_MODE2_CTRL_ENABLE; // disable RTC
588 while (RTCSync());
589}
590
591
592/**********************************************************************************************************************
593*Description: Function for enabling RTC
594*Input Parameters: None
595*Return Parameter: None
596***********************************************************************************************************************/
597void RTCInt::RTCenable()
598{
599 RTC->MODE2.CTRL.reg |= RTC_MODE2_CTRL_ENABLE; // enable RTC
600 while (RTCSync());
601}
602
603
604/**********************************************************************************************************************
605*Description: Function for resetting RTC. This function clears all RTC registers
606*Input Parameters: None
607*Return Parameter: None
608***********************************************************************************************************************/
609void RTCInt::RTCreset()
610{
611 RTC->MODE2.CTRL.reg |= RTC_MODE2_CTRL_SWRST; // software reset
612 while (RTCSync());
613}
614
615
616/**********************************************************************************************************************
617*Description: Function for remove reset to RTC
618*Input Parameters: None
619*Return Parameter: None
620***********************************************************************************************************************/
621void RTCInt::RTCresetRemove()
622{
623 RTC->MODE2.CTRL.reg &= ~RTC_MODE2_CTRL_SWRST; // software reset remove
624 while (RTCSync());
625}
626
627
628
629/**********************************************************************************************************************
630*Description: RTC handler interrupt routine. This function checks flag ALARM0 and if is set calls routine
631 pointed by _callback pointer then resets ALARM0 flag.
632*Input Parameters: None
633*Return Parameter: None
634***********************************************************************************************************************/
635void RTC_Handler(void)
636{
637 if(RTC->MODE2.INTFLAG.bit.ALARM0)
638 {
639 _callback(); //call interrupt routine
640 RTC->MODE2.INTFLAG.bit.ALARM0 = 1; // clear ALARM0 flag
641 }
642
643 else return;
644}
Note: See TracBrowser for help on using the repository browser.