source: rtos_arduino/trunk/arduino_lib/libraries/RTC/src/RTCInt.cpp@ 136

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

ライブラリとOS及びベーシックなサンプルの追加.

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