| NANO102/112 BSP V3.04.000 The Board Support Package for Nano102/112 Series

rtc.c
Go to the documentation of this file.
1/**************************************************************************/
14#include <stdio.h>
15#include "Nano1X2Series.h"
16
17/*---------------------------------------------------------------------------------------------------------*/
18/* Includes of local headers */
19/*---------------------------------------------------------------------------------------------------------*/
20
21
22
31
32/*---------------------------------------------------------------------------------------------------------*/
33/* Macro, type and constant definitions */
34/*---------------------------------------------------------------------------------------------------------*/
35#define RTC_GLOBALS
36
37/*---------------------------------------------------------------------------------------------------------*/
38/* Global file scope (static) variables */
39/*---------------------------------------------------------------------------------------------------------*/
40 static volatile uint32_t g_u32Reg, g_u32Reg1,g_u32hiYear,g_u32loYear,g_u32hiMonth,g_u32loMonth,g_u32hiDay,g_u32loDay;
41 static volatile uint32_t g_u32hiHour,g_u32loHour,g_u32hiMin,g_u32loMin,g_u32hiSec,g_u32loSec;
42
44
58 void RTC_32KCalibration(int32_t i32FrequencyX10000)
59{
60 uint64_t u64Compensate;
61 int32_t i32TimeoutCnt = SystemCoreClock; // 1 second timeout
62
63 //u64Compensate = (uint64_t)(0x64000000000);
64 u64Compensate = (uint64_t)(0x2710000000000);
65 u64Compensate = (uint64_t)(u64Compensate / (uint64_t)i32FrequencyX10000);
66
67 if(u64Compensate >= 0x400000)
68 {
69 u64Compensate = 0x3FFFFF;
70 }
71
72 RTC->AER = RTC_WRITE_KEY;
73 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
74 RTC->AER = RTC_WRITE_KEY;
75 if(i32TimeoutCnt-- <= 0)
76 break;
77 }
78
79 RTC->FCR = (uint32_t)u64Compensate;
80
81}
82
106 void RTC_Open (S_RTC_TIME_DATA_T *sPt)
107{
108 uint32_t u32Reg;
109 int32_t i32TimeoutCnt = SystemCoreClock; // total 1 second timeout
110
111 volatile int32_t i32delay=1000;
112
113 RTC->INIR = RTC_INIT_KEY;
114
115 if(RTC->INIR != 0x1)
116 {
117 RTC->INIR = RTC_INIT_KEY;
118
119 while(RTC->INIR != 0x1) {
120 if(i32TimeoutCnt-- <= 0)
121 break;
122 }
123 }
124
125 if(sPt == NULL)
126 return;
127
128 /*-----------------------------------------------------------------------------------------------------*/
129 /* Second, set RTC 24/12 hour setting */
130 /*-----------------------------------------------------------------------------------------------------*/
131 if (sPt->u32TimeScale == RTC_CLOCK_12)
132 {
133 RTC->AER = RTC_WRITE_KEY;
134 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
135 RTC->AER = RTC_WRITE_KEY;
136 if(i32TimeoutCnt-- <= 0)
137 break;
138 }
139 RTC->TSSR &= ~RTC_TSSR_24H_12H_Msk;
140
141 /*-------------------------------------------------------------------------------------------------*/
142 /* important, range of 12-hour PM mode is 21 up to 32 */
143 /*-------------------------------------------------------------------------------------------------*/
144 if (sPt->u32AmPm == RTC_PM)
145 sPt->u32Hour += 20;
146 }
147 else
148 {
149 RTC->AER = RTC_WRITE_KEY;
150 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
151 RTC->AER = RTC_WRITE_KEY;
152 if(i32TimeoutCnt-- <= 0)
153 break;
154 }
155 RTC->TSSR |= RTC_TSSR_24H_12H_Msk;
156 }
157
158 /*-----------------------------------------------------------------------------------------------------*/
159 /* Set RTC Calender Loading */
160 /*-----------------------------------------------------------------------------------------------------*/
161 u32Reg = ((sPt->u32Year - RTC_YEAR2000) / 10) << 20;
162 u32Reg |= (((sPt->u32Year - RTC_YEAR2000) % 10) << 16);
163 u32Reg |= ((sPt->u32Month / 10) << 12);
164 u32Reg |= ((sPt->u32Month % 10) << 8);
165 u32Reg |= ((sPt->u32Day / 10) << 4);
166 u32Reg |= (sPt->u32Day % 10);
167 g_u32Reg = u32Reg;
168
169 RTC->AER = RTC_WRITE_KEY;
170 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
171 RTC->AER = RTC_WRITE_KEY;
172 if(i32TimeoutCnt-- <= 0)
173 break;
174 }
175
176 RTC->CLR = (uint32_t)g_u32Reg;
177
178 /*-----------------------------------------------------------------------------------------------------*/
179 /* Set RTC Time Loading */
180 /*-----------------------------------------------------------------------------------------------------*/
181 u32Reg = ((sPt->u32Hour / 10) << 20);
182 u32Reg |= ((sPt->u32Hour % 10) << 16);
183 u32Reg |= ((sPt->u32Minute / 10) << 12);
184 u32Reg |= ((sPt->u32Minute % 10) << 8);
185 u32Reg |= ((sPt->u32Second / 10) << 4);
186 u32Reg |= (sPt->u32Second % 10);
187 g_u32Reg = u32Reg;
188
189 RTC->AER = RTC_WRITE_KEY;
190 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
191 RTC->AER = RTC_WRITE_KEY;
192 if(i32TimeoutCnt-- <= 0)
193 break;
194 }
195
196 RTC->TLR = (uint32_t)g_u32Reg;
197
198 RTC->DWR = sPt->u32DayOfWeek;
199
200 /* Waiting for RTC settings stable */
201 while(i32delay--);
202
203}
204
223 void RTC_GetDateAndTime(S_RTC_TIME_DATA_T *sPt)
224{
225 uint32_t u32Tmp;
226
227 sPt->u32TimeScale = RTC->TSSR & RTC_TSSR_24H_12H_Msk; /* 12/24-hour */
228 sPt->u32DayOfWeek = RTC->DWR & RTC_DWR_DWR_Msk; /* Day of week */
229
230 g_u32hiYear = (RTC->CLR & RTC_CLR_10YEAR_Msk) >> RTC_CLR_10YEAR_Pos;
231 g_u32loYear = (RTC->CLR & RTC_CLR_1YEAR_Msk) >> RTC_CLR_1YEAR_Pos;
232 g_u32hiMonth = (RTC->CLR & RTC_CLR_10MON_Msk) >> RTC_CLR_10MON_Pos;
233 g_u32loMonth = (RTC->CLR & RTC_CLR_1MON_Msk) >> RTC_CLR_1MON_Pos;
234 g_u32hiDay = (RTC->CLR & RTC_CLR_10DAY_Msk) >> RTC_CLR_10DAY_Pos;
235 g_u32loDay = (RTC->CLR & RTC_CLR_1DAY_Msk);
236
237 g_u32hiHour = (RTC->TLR & RTC_TLR_10HR_Msk) >> RTC_TLR_10HR_Pos;
238 g_u32loHour = (RTC->TLR & RTC_TLR_1HR_Msk) >> RTC_TLR_1HR_Pos;
239 g_u32hiMin = (RTC->TLR & RTC_TLR_10MIN_Msk) >> RTC_TLR_10MIN_Pos;
240 g_u32loMin = (RTC->TLR & RTC_TLR_1MIN_Msk) >> RTC_TLR_1MIN_Pos;
241 g_u32hiSec = (RTC->TLR & RTC_TLR_10SEC_Msk) >> RTC_TLR_10SEC_Pos;
242 g_u32loSec = (RTC->TLR & RTC_TLR_1SEC_Msk);
243
244 u32Tmp = (g_u32hiYear * 10); /* Compute to 20XX year */
245 u32Tmp += g_u32loYear;
246 sPt->u32Year = u32Tmp + RTC_YEAR2000;
247
248 u32Tmp = (g_u32hiMonth * 10); /* Compute 0~12 month */
249 sPt->u32Month = u32Tmp + g_u32loMonth;
250
251 u32Tmp = (g_u32hiDay * 10); /* Compute 0~31 day */
252 sPt->u32Day = u32Tmp + g_u32loDay;
253
254 if (sPt->u32TimeScale == RTC_CLOCK_12) /* Compute12/24 hour */
255 {
256 u32Tmp = (g_u32hiHour * 10);
257 u32Tmp+= g_u32loHour;
258 sPt->u32Hour = u32Tmp; /* AM: 1~12. PM: 21~32. */
259
260 if (sPt->u32Hour >= 21)
261 {
262 sPt->u32AmPm = RTC_PM;
263 sPt->u32Hour -= 20;
264 }
265 else
266 {
267 sPt->u32AmPm = RTC_AM;
268 }
269
270 u32Tmp = (g_u32hiMin * 10);
271 u32Tmp+= g_u32loMin;
272 sPt->u32Minute = u32Tmp;
273
274 u32Tmp = (g_u32hiSec * 10);
275 u32Tmp+= g_u32loSec;
276 sPt->u32Second = u32Tmp;
277
278 }
279 else
280 {
281 u32Tmp = (g_u32hiHour * 10);
282 u32Tmp += g_u32loHour;
283 sPt->u32Hour = u32Tmp;
284
285 u32Tmp = (g_u32hiMin * 10);
286 u32Tmp += g_u32loMin;
287 sPt->u32Minute = u32Tmp;
288
289 u32Tmp = (g_u32hiSec * 10);
290 u32Tmp += g_u32loSec;
291 sPt->u32Second = u32Tmp;
292 }
293
294}
295
296
297
316 void RTC_GetAlarmDateAndTime(S_RTC_TIME_DATA_T *sPt)
317{
318 uint32_t u32Tmp;
319 int32_t i32TimeoutCnt = SystemCoreClock; // total 1 second timeout
320
321 sPt->u32TimeScale = RTC->TSSR & RTC_TSSR_24H_12H_Msk; /* 12/24-hour */
322 sPt->u32DayOfWeek = RTC->DWR & RTC_DWR_DWR_Msk; /* Day of week */
323
324 RTC->AER = RTC_WRITE_KEY;
325 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
326 RTC->AER = RTC_WRITE_KEY;
327 if(i32TimeoutCnt-- <= 0)
328 break;
329 }
330
331 g_u32hiYear = (RTC->CAR & RTC_CAR_10YEAR_Msk) >> RTC_CAR_10YEAR_Pos;
332 g_u32loYear = (RTC->CAR & RTC_CAR_1YEAR_Msk) >> RTC_CAR_1YEAR_Pos;
333 g_u32hiMonth = (RTC->CAR & RTC_CAR_10MON_Msk) >> RTC_CAR_10MON_Pos;
334 g_u32loMonth = (RTC->CAR & RTC_CAR_1MON_Msk) >> RTC_CAR_1MON_Pos;
335 g_u32hiDay = (RTC->CAR & RTC_CAR_10DAY_Msk) >> RTC_CAR_10DAY_Pos;
336 g_u32loDay = (RTC->CAR & RTC_CAR_1DAY_Msk);
337
338 RTC->AER = RTC_WRITE_KEY;
339 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
340 RTC->AER = RTC_WRITE_KEY;
341 if(i32TimeoutCnt-- <= 0)
342 break;
343 }
344
345 g_u32hiHour = (RTC->TAR & RTC_TAR_10HR_Msk) >> RTC_TAR_10HR_Pos;
346 g_u32loHour = (RTC->TAR & RTC_TAR_1HR_Msk) >> RTC_TAR_1HR_Pos;
347 g_u32hiMin = (RTC->TAR & RTC_TAR_10MIN_Msk) >> RTC_TAR_10MIN_Pos;
348 g_u32loMin = (RTC->TAR & RTC_TAR_1MIN_Msk) >> RTC_TAR_1MIN_Pos;
349 g_u32hiSec = (RTC->TAR & RTC_TAR_10SEC_Msk) >> RTC_TAR_10SEC_Pos;
350 g_u32loSec = (RTC->TAR & RTC_TAR_1SEC_Msk);
351
352 u32Tmp = (g_u32hiYear * 10); /* Compute to 20XX year */
353 u32Tmp += g_u32loYear;
354 sPt->u32Year = u32Tmp + RTC_YEAR2000;
355
356 u32Tmp = (g_u32hiMonth * 10); /* Compute 0~12 month */
357 sPt->u32Month = u32Tmp + g_u32loMonth;
358
359 u32Tmp = (g_u32hiDay * 10); /* Compute 0~31 day */
360 sPt->u32Day = u32Tmp + g_u32loDay;
361
362 if (sPt->u32TimeScale == RTC_CLOCK_12) /* Compute12/24 hour */
363 {
364 u32Tmp = (g_u32hiHour * 10);
365 u32Tmp += g_u32loHour;
366 sPt->u32Hour = u32Tmp; /* AM: 1~12. PM: 21~32. */
367
368 if (sPt->u32Hour >= 21)
369 {
370 sPt->u32AmPm = RTC_PM;
371 sPt->u32Hour -= 20;
372 }
373 else
374 {
375 sPt->u32AmPm = RTC_AM;
376 }
377
378 u32Tmp = (g_u32hiMin * 10);
379 u32Tmp += g_u32loMin;
380 sPt->u32Minute = u32Tmp;
381
382 u32Tmp = (g_u32hiSec * 10);
383 u32Tmp += g_u32loSec;
384 sPt->u32Second = u32Tmp;
385
386 }
387 else
388 {
389 u32Tmp = (g_u32hiHour * 10);
390 u32Tmp += g_u32loHour;
391 sPt->u32Hour = u32Tmp;
392
393 u32Tmp = (g_u32hiMin * 10);
394 u32Tmp+= g_u32loMin;
395 sPt->u32Minute = u32Tmp;
396
397 u32Tmp = (g_u32hiSec * 10);
398 u32Tmp += g_u32loSec;
399 sPt->u32Second = u32Tmp;
400 }
401
402}
403
404
405
428 void RTC_SetDateAndTime(S_RTC_TIME_DATA_T *sPt)
429{
430 uint32_t u32Reg;
431 int32_t i32TimeoutCnt = SystemCoreClock; // total 1 second timeout
432
433 RTC->AER = RTC_WRITE_KEY;
434 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
435 RTC->AER = RTC_WRITE_KEY;
436 if(i32TimeoutCnt-- <= 0)
437 break;
438 }
439
440 if (sPt->u32TimeScale == RTC_CLOCK_12)
441 {
442 RTC->TSSR &= ~RTC_TSSR_24H_12H_Msk;
443
444 /*-----------------------------------------------------------------------------------------*/
445 /* important, range of 12-hour PM mode is 21 upto 32 */
446 /*-----------------------------------------------------------------------------------------*/
447 if (sPt->u32AmPm == RTC_PM)
448 sPt->u32Hour += 20;
449 }
450 else
451 {
452 RTC->TSSR |= RTC_TSSR_24H_12H_Msk;
453 }
454
455 RTC->DWR = sPt->u32DayOfWeek & RTC_DWR_DWR_Msk;
456
457 u32Reg = ((sPt->u32Year - RTC_YEAR2000) / 10) << 20;
458 u32Reg |= (((sPt->u32Year - RTC_YEAR2000) % 10) << 16);
459 u32Reg |= ((sPt->u32Month / 10) << 12);
460 u32Reg |= ((sPt->u32Month % 10) << 8);
461 u32Reg |= ((sPt->u32Day / 10) << 4);
462 u32Reg |= (sPt->u32Day % 10);
463 g_u32Reg = u32Reg;
464
465 RTC->AER = RTC_WRITE_KEY;
466 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
467 RTC->AER = RTC_WRITE_KEY;
468 if(i32TimeoutCnt-- <= 0)
469 break;
470 }
471
472 RTC->CLR = (uint32_t)g_u32Reg;
473
474 u32Reg = ((sPt->u32Hour / 10) << 20);
475 u32Reg |= ((sPt->u32Hour % 10) << 16);
476 u32Reg |= ((sPt->u32Minute / 10) << 12);
477 u32Reg |= ((sPt->u32Minute % 10) << 8);
478 u32Reg |= ((sPt->u32Second / 10) << 4);
479 u32Reg |= (sPt->u32Second % 10);
480 g_u32Reg = u32Reg;
481
482 RTC->AER = RTC_WRITE_KEY;
483 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
484 RTC->AER = RTC_WRITE_KEY;
485 if(i32TimeoutCnt-- <= 0)
486 break;
487 }
488
489 RTC->TLR = (uint32_t)g_u32Reg;
490
491}
492
513 void RTC_SetAlarmDateAndTime(S_RTC_TIME_DATA_T *sPt)
514{
515 uint32_t u32Reg;
516 int32_t i32TimeoutCnt = SystemCoreClock; // total 1 second timeout
517
518 RTC->AER = RTC_WRITE_KEY;
519 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
520 RTC->AER = RTC_WRITE_KEY;
521 if(i32TimeoutCnt-- <= 0)
522 break;
523 }
524
525 if (sPt->u32TimeScale == RTC_CLOCK_12)
526 {
527 RTC->TSSR &= ~RTC_TSSR_24H_12H_Msk;
528
529 /*-----------------------------------------------------------------------------------------*/
530 /* important, range of 12-hour PM mode is 21 up to 32 */
531 /*-----------------------------------------------------------------------------------------*/
532 if (sPt->u32AmPm == RTC_PM)
533 sPt->u32Hour += 20;
534 }
535 else
536 {
537 RTC->TSSR |= RTC_TSSR_24H_12H_Msk;
538 }
539
540 RTC->DWR = sPt->u32DayOfWeek & RTC_DWR_DWR_Msk;
541
542
543 u32Reg = ((sPt->u32Year - RTC_YEAR2000) / 10) << 20;
544 u32Reg |= (((sPt->u32Year - RTC_YEAR2000) % 10) << 16);
545 u32Reg |= ((sPt->u32Month / 10) << 12);
546 u32Reg |= ((sPt->u32Month % 10) << 8);
547 u32Reg |= ((sPt->u32Day / 10) << 4);
548 u32Reg |= (sPt->u32Day % 10);
549 g_u32Reg = u32Reg;
550
551 RTC->AER = RTC_WRITE_KEY;
552 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
553 RTC->AER = RTC_WRITE_KEY;
554 if(i32TimeoutCnt-- <= 0)
555 break;
556 }
557
558 RTC->CAR = (uint32_t)g_u32Reg;
559
560 u32Reg = ((sPt->u32Hour / 10) << 20);
561 u32Reg |= ((sPt->u32Hour % 10) << 16);
562 u32Reg |= ((sPt->u32Minute / 10) << 12);
563 u32Reg |= ((sPt->u32Minute % 10) << 8);
564 u32Reg |= ((sPt->u32Second / 10) << 4);
565 u32Reg |= (sPt->u32Second % 10);
566 g_u32Reg = u32Reg;
567
568 RTC->AER = RTC_WRITE_KEY;
569 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
570 RTC->AER = RTC_WRITE_KEY;
571 if(i32TimeoutCnt-- <= 0)
572 break;
573 }
574
575 RTC->TAR = (uint32_t)g_u32Reg;
576
577}
578
579
593 void RTC_SetDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day, uint32_t u32DayOfWeek)
594{
595 __IO uint32_t u32Reg;
596 int32_t i32TimeoutCnt = SystemCoreClock; // total 1 second timeout
597
598 RTC->AER = RTC_WRITE_KEY;
599 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
600 RTC->AER = RTC_WRITE_KEY;
601 if(i32TimeoutCnt-- <= 0)
602 break;
603 }
604
605 RTC->DWR = u32DayOfWeek & RTC_DWR_DWR_Msk;
606
607 u32Reg = ((u32Year - RTC_YEAR2000) / 10) << 20;
608 u32Reg |= (((u32Year - RTC_YEAR2000) % 10) << 16);
609 u32Reg |= ((u32Month / 10) << 12);
610 u32Reg |= ((u32Month % 10) << 8);
611 u32Reg |= ((u32Day / 10) << 4);
612 u32Reg |= (u32Day % 10);
613 g_u32Reg = u32Reg;
614
615 RTC->AER = RTC_WRITE_KEY;
616 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
617 RTC->AER = RTC_WRITE_KEY;
618 if(i32TimeoutCnt-- <= 0)
619 break;
620 }
621
622 RTC->CLR = (uint32_t)g_u32Reg;
623
624}
625
638 void RTC_SetTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm)
639{
640 __IO uint32_t u32Reg;
641 int32_t i32TimeoutCnt = SystemCoreClock; // total 1 second timeout
642
643 RTC->AER = RTC_WRITE_KEY;
644 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
645 RTC->AER = RTC_WRITE_KEY;
646 if(i32TimeoutCnt-- <= 0)
647 break;
648 }
649
650 if (u32TimeMode == RTC_CLOCK_12)
651 {
652 RTC->TSSR &= ~RTC_TSSR_24H_12H_Msk;
653
654 if (u32AmPm == RTC_PM) /* important, range of 12-hour PM mode is 21 upto 32 */
655 u32Hour += 20;
656 }
657 else if(u32TimeMode == RTC_CLOCK_24)
658 {
659 RTC->TSSR |= RTC_TSSR_24H_12H_Msk;
660 }
661
662 u32Reg = ((u32Hour / 10) << 20);
663 u32Reg |= ((u32Hour % 10) << 16);
664 u32Reg |= ((u32Minute / 10) << 12);
665 u32Reg |= ((u32Minute % 10) << 8);
666 u32Reg |= ((u32Second / 10) << 4);
667 u32Reg |= (u32Second % 10);
668
669 g_u32Reg = u32Reg;
670
671 RTC->AER = RTC_WRITE_KEY;
672 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
673 RTC->AER = RTC_WRITE_KEY;
674 if(i32TimeoutCnt-- <= 0)
675 break;
676 }
677
678 RTC->TLR = (uint32_t)g_u32Reg;
679
680}
681
692 void RTC_SetAlarmDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day)
693{
694 __IO uint32_t u32Reg;
695 int32_t i32TimeoutCnt = SystemCoreClock; // total 1 second timeout
696
697 RTC->AER = RTC_WRITE_KEY;
698 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
699 RTC->AER = RTC_WRITE_KEY;
700 if(i32TimeoutCnt-- <= 0)
701 break;
702 }
703
704 u32Reg = ((u32Year - RTC_YEAR2000) / 10) << 20;
705 u32Reg |= (((u32Year - RTC_YEAR2000) % 10) << 16);
706 u32Reg |= ((u32Month / 10) << 12);
707 u32Reg |= ((u32Month % 10) << 8);
708 u32Reg |= ((u32Day / 10) << 4);
709 u32Reg |= (u32Day % 10);
710 g_u32Reg = u32Reg;
711
712 RTC->AER = RTC_WRITE_KEY;
713 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
714 RTC->AER = RTC_WRITE_KEY;
715 if(i32TimeoutCnt-- <= 0)
716 break;
717 }
718
719 RTC->CAR = (uint32_t)g_u32Reg;
720
721}
722
735 void RTC_SetAlarmTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm)
736{
737 __IO uint32_t u32Reg;
738 int32_t i32TimeoutCnt = SystemCoreClock; // total 1 second timeout
739
740 RTC->AER = RTC_WRITE_KEY;
741 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
742 RTC->AER = RTC_WRITE_KEY;
743 if(i32TimeoutCnt-- <= 0)
744 break;
745 }
746
747 if (u32TimeMode == RTC_CLOCK_12)
748 {
749 RTC->TSSR &= ~RTC_TSSR_24H_12H_Msk;
750
751 if (u32AmPm == RTC_PM) /* important, range of 12-hour PM mode is 21 up to 32 */
752 u32Hour += 20;
753 }
754 else if(u32TimeMode == RTC_CLOCK_24)
755 {
756 RTC->TSSR |= RTC_TSSR_24H_12H_Msk;
757 }
758
759 u32Reg = ((u32Hour / 10) << 20);
760 u32Reg |= ((u32Hour % 10) << 16);
761 u32Reg |= ((u32Minute / 10) << 12);
762 u32Reg |= ((u32Minute % 10) << 8);
763 u32Reg |= ((u32Second / 10) << 4);
764 u32Reg |= (u32Second % 10);
765
766 g_u32Reg = u32Reg;
767
768 RTC->AER = RTC_WRITE_KEY;
769 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
770 RTC->AER = RTC_WRITE_KEY;
771 if(i32TimeoutCnt-- <= 0)
772 break;
773 }
774
775 RTC->TAR = (uint32_t)g_u32Reg;
776
777}
778
779
790 void RTC_EnableTamperDetection(uint32_t u32PinCondition)
791{
792 int32_t i32TimeoutCnt = SystemCoreClock; // total 1 second timeout
793
794 RTC->AER = RTC_WRITE_KEY;
795 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
796 RTC->AER = RTC_WRITE_KEY;
797 if(i32TimeoutCnt-- <= 0)
798 break;
799 }
800
801 /* detection edge select */
802 if(u32PinCondition)
803 RTC->SPRCTL |= RTC_SPRCTL_SNOOPEDGE_Msk;
804 else
805 RTC->SPRCTL &= ~RTC_SPRCTL_SNOOPEDGE_Msk;
806
807 /* enable snooper pin event detection */
808 RTC->SPRCTL |= RTC_SPRCTL_SNOOPEN_Msk;
809}
810
819 void RTC_DisableTamperDetection(void)
820{
821 int32_t i32TimeoutCnt = SystemCoreClock; // 1 second timeout
822
823 RTC->AER = RTC_WRITE_KEY;
824 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
825 RTC->AER = RTC_WRITE_KEY;
826 if(i32TimeoutCnt-- <= 0)
827 break;
828 }
829
830 RTC->SPRCTL &= ~RTC_SPRCTL_SNOOPEN_Msk;
831}
832
841uint32_t RTC_GetDayOfWeek(void)
842{
843 return (RTC->DWR & RTC_DWR_DWR_Msk);
844}
845
846
865 void RTC_SetTickPeriod(uint32_t u32TickSelection)
866{
867 int32_t i32TimeoutCnt = SystemCoreClock; // 1 second timeout
868
869 RTC->AER = RTC_WRITE_KEY;
870 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
871 RTC->AER = RTC_WRITE_KEY;
872 if(i32TimeoutCnt-- <= 0)
873 break;
874 }
875
876 RTC->TTR = (RTC->TTR & ~RTC_TTR_TTR_Msk) | u32TickSelection;
877}
878
890 void RTC_EnableInt(uint32_t u32IntFlagMask)
891{
892 int32_t i32TimeoutCnt = SystemCoreClock; // 1 second timeout
893
894 RTC->AER = RTC_WRITE_KEY;
895 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
896 RTC->AER = RTC_WRITE_KEY;
897 if(i32TimeoutCnt-- <= 0)
898 break;
899 }
900
901 RTC->RIER |= u32IntFlagMask;
902}
903
915 void RTC_DisableInt(uint32_t u32IntFlagMask)
916{
917 int32_t i32TimeoutCnt = SystemCoreClock; // 1 second timeout
918
919 RTC->AER = RTC_WRITE_KEY;
920 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
921 RTC->AER = RTC_WRITE_KEY;
922 if(i32TimeoutCnt-- <= 0)
923 break;
924 }
925
926 if(u32IntFlagMask & RTC_RIER_TIER_Msk)
927 {
928 RTC->RIER &= ~RTC_RIER_TIER_Msk;
929 RTC->RIIR = RTC_RIIR_TIF_Msk;
930 }
931
932 if(u32IntFlagMask & RTC_RIER_AIER_Msk)
933 {
934 RTC->RIER &= ~RTC_RIER_AIER_Msk;
935 RTC->RIIR = RTC_RIIR_AIF_Msk;
936 }
937
938 if(u32IntFlagMask & RTC_RIER_SNOOPIER_Msk)
939 {
940 RTC->RIER &= ~RTC_RIER_SNOOPIER_Msk;
941 RTC->RIIR = RTC_RIIR_SNOOPIF_Msk;
942 }
943
944}
945
952 void RTC_Close (void)
953{
954 CLK->APBCLK &= ~CLK_APBCLK_RTC_EN_Msk;
955}
956
957 /* end of group NANO1X2_RTC_EXPORTED_FUNCTIONS */
959 /* end of group NANO1X2_RTC_Driver */
961 /* end of group NANO1X2_Device_Driver */
963
964/*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/
965
966
Nano1X2Series.h
Nano102/112 peripheral access layer header file. This file contains all the peripheral register's def...
RTC_TAR_10SEC_Msk
#define RTC_TAR_10SEC_Msk
Definition: Nano1X2Series.h:7408
RTC_TAR_10MIN_Pos
#define RTC_TAR_10MIN_Pos
Definition: Nano1X2Series.h:7413
RTC_CAR_10YEAR_Pos
#define RTC_CAR_10YEAR_Pos
Definition: Nano1X2Series.h:7437
RTC_TLR_10MIN_Pos
#define RTC_TLR_10MIN_Pos
Definition: Nano1X2Series.h:7371
RTC_TAR_10HR_Msk
#define RTC_TAR_10HR_Msk
Definition: Nano1X2Series.h:7420
RTC_TLR_1MIN_Pos
#define RTC_TLR_1MIN_Pos
Definition: Nano1X2Series.h:7368
RTC_CAR_10DAY_Msk
#define RTC_CAR_10DAY_Msk
Definition: Nano1X2Series.h:7426
RTC_TLR_10SEC_Pos
#define RTC_TLR_10SEC_Pos
Definition: Nano1X2Series.h:7365
RTC_TLR_1HR_Msk
#define RTC_TLR_1HR_Msk
Definition: Nano1X2Series.h:7375
RTC_CAR_10MON_Pos
#define RTC_CAR_10MON_Pos
Definition: Nano1X2Series.h:7431
RTC_CAR_1MON_Msk
#define RTC_CAR_1MON_Msk
Definition: Nano1X2Series.h:7429
RTC_TLR_1HR_Pos
#define RTC_TLR_1HR_Pos
Definition: Nano1X2Series.h:7374
RTC_SPRCTL_SNOOPEDGE_Msk
#define RTC_SPRCTL_SNOOPEDGE_Msk
Definition: Nano1X2Series.h:7471
RTC_TAR_1SEC_Msk
#define RTC_TAR_1SEC_Msk
Definition: Nano1X2Series.h:7405
RTC_RIER_TIER_Msk
#define RTC_RIER_TIER_Msk
Definition: Nano1X2Series.h:7447
RTC_DWR_DWR_Msk
#define RTC_DWR_DWR_Msk
Definition: Nano1X2Series.h:7402
RTC_CLR_10YEAR_Pos
#define RTC_CLR_10YEAR_Pos
Definition: Nano1X2Series.h:7395
RTC_CAR_10DAY_Pos
#define RTC_CAR_10DAY_Pos
Definition: Nano1X2Series.h:7425
RTC_CLR_1MON_Pos
#define RTC_CLR_1MON_Pos
Definition: Nano1X2Series.h:7386
RTC_CAR_10MON_Msk
#define RTC_CAR_10MON_Msk
Definition: Nano1X2Series.h:7432
RTC_CLR_10YEAR_Msk
#define RTC_CLR_10YEAR_Msk
Definition: Nano1X2Series.h:7396
RTC_RIIR_TIF_Msk
#define RTC_RIIR_TIF_Msk
Definition: Nano1X2Series.h:7456
RTC_CLR_10MON_Msk
#define RTC_CLR_10MON_Msk
Definition: Nano1X2Series.h:7390
RTC_CLR_1YEAR_Msk
#define RTC_CLR_1YEAR_Msk
Definition: Nano1X2Series.h:7393
RTC_CLR_10MON_Pos
#define RTC_CLR_10MON_Pos
Definition: Nano1X2Series.h:7389
RTC_RIER_AIER_Msk
#define RTC_RIER_AIER_Msk
Definition: Nano1X2Series.h:7444
RTC_TAR_1HR_Msk
#define RTC_TAR_1HR_Msk
Definition: Nano1X2Series.h:7417
RTC_CLR_1YEAR_Pos
#define RTC_CLR_1YEAR_Pos
Definition: Nano1X2Series.h:7392
RTC_CAR_1MON_Pos
#define RTC_CAR_1MON_Pos
Definition: Nano1X2Series.h:7428
RTC_TAR_10HR_Pos
#define RTC_TAR_10HR_Pos
Definition: Nano1X2Series.h:7419
RTC_CAR_1YEAR_Msk
#define RTC_CAR_1YEAR_Msk
Definition: Nano1X2Series.h:7435
RTC_CLR_1MON_Msk
#define RTC_CLR_1MON_Msk
Definition: Nano1X2Series.h:7387
RTC_TLR_1MIN_Msk
#define RTC_TLR_1MIN_Msk
Definition: Nano1X2Series.h:7369
RTC_CAR_10YEAR_Msk
#define RTC_CAR_10YEAR_Msk
Definition: Nano1X2Series.h:7438
RTC_RIIR_AIF_Msk
#define RTC_RIIR_AIF_Msk
Definition: Nano1X2Series.h:7453
RTC_TLR_10HR_Pos
#define RTC_TLR_10HR_Pos
Definition: Nano1X2Series.h:7377
RTC_TLR_1SEC_Msk
#define RTC_TLR_1SEC_Msk
Definition: Nano1X2Series.h:7363
RTC_TAR_10SEC_Pos
#define RTC_TAR_10SEC_Pos
Definition: Nano1X2Series.h:7407
RTC_CAR_1YEAR_Pos
#define RTC_CAR_1YEAR_Pos
Definition: Nano1X2Series.h:7434
RTC_CLR_10DAY_Pos
#define RTC_CLR_10DAY_Pos
Definition: Nano1X2Series.h:7383
RTC_CLR_10DAY_Msk
#define RTC_CLR_10DAY_Msk
Definition: Nano1X2Series.h:7384
RTC_TAR_1MIN_Msk
#define RTC_TAR_1MIN_Msk
Definition: Nano1X2Series.h:7411
RTC_CLR_1DAY_Msk
#define RTC_CLR_1DAY_Msk
Definition: Nano1X2Series.h:7381
RTC_TLR_10MIN_Msk
#define RTC_TLR_10MIN_Msk
Definition: Nano1X2Series.h:7372
RTC_TAR_1HR_Pos
#define RTC_TAR_1HR_Pos
Definition: Nano1X2Series.h:7416
RTC_SPRCTL_SNOOPEN_Msk
#define RTC_SPRCTL_SNOOPEN_Msk
Definition: Nano1X2Series.h:7468
RTC_TAR_1MIN_Pos
#define RTC_TAR_1MIN_Pos
Definition: Nano1X2Series.h:7410
RTC_RIER_SNOOPIER_Msk
#define RTC_RIER_SNOOPIER_Msk
Definition: Nano1X2Series.h:7450
RTC_TLR_10HR_Msk
#define RTC_TLR_10HR_Msk
Definition: Nano1X2Series.h:7378
RTC_TSSR_24H_12H_Msk
#define RTC_TSSR_24H_12H_Msk
Definition: Nano1X2Series.h:7399
RTC_AER_ENF_Msk
#define RTC_AER_ENF_Msk
Definition: Nano1X2Series.h:7357
RTC_TAR_10MIN_Msk
#define RTC_TAR_10MIN_Msk
Definition: Nano1X2Series.h:7414
RTC_RIIR_SNOOPIF_Msk
#define RTC_RIIR_SNOOPIF_Msk
Definition: Nano1X2Series.h:7459
RTC_TLR_10SEC_Msk
#define RTC_TLR_10SEC_Msk
Definition: Nano1X2Series.h:7366
RTC_CAR_1DAY_Msk
#define RTC_CAR_1DAY_Msk
Definition: Nano1X2Series.h:7423
CLK
#define CLK
Pointer to CLK register structure.
Definition: Nano1X2Series.h:10651
RTC
#define RTC
Pointer to RTC register structure.
Definition: Nano1X2Series.h:10632
RTC_CLOCK_24
#define RTC_CLOCK_24
Definition: rtc.h:44
RTC_CLOCK_12
#define RTC_CLOCK_12
Definition: rtc.h:43
RTC_INIT_KEY
#define RTC_INIT_KEY
Definition: rtc.h:35
RTC_PM
#define RTC_PM
Definition: rtc.h:47
RTC_AM
#define RTC_AM
Definition: rtc.h:46
RTC_WRITE_KEY
#define RTC_WRITE_KEY
Definition: rtc.h:36
RTC_YEAR2000
#define RTC_YEAR2000
Definition: rtc.h:40
RTC_EnableInt
void RTC_EnableInt(uint32_t u32IntFlagMask)
The function is used to enable specified interrupt.
Definition: rtc.c:890
RTC_32KCalibration
void RTC_32KCalibration(int32_t i32FrequencyX100)
Set Frequency Compensation Data.
Definition: rtc.c:58
RTC_GetDayOfWeek
uint32_t RTC_GetDayOfWeek(void)
This function is used to get day of week.
Definition: rtc.c:841
RTC_SetDateAndTime
void RTC_SetDateAndTime(S_RTC_TIME_DATA_T *sPt)
This function is used to update date/time to RTC.
Definition: rtc.c:428
S_RTC_TIME_DATA_T::u32Month
uint32_t u32Month
Definition: rtc.h:83
S_RTC_TIME_DATA_T::u32AmPm
uint32_t u32AmPm
Definition: rtc.h:90
RTC_GetAlarmDateAndTime
void RTC_GetAlarmDateAndTime(S_RTC_TIME_DATA_T *sPt)
Read alarm date/time from RTC setting.
Definition: rtc.c:316
RTC_Open
void RTC_Open(S_RTC_TIME_DATA_T *sPt)
This function is used to: .
Definition: rtc.c:106
RTC_SetAlarmDateAndTime
void RTC_SetAlarmDateAndTime(S_RTC_TIME_DATA_T *sPt)
This function is used to set alarm date/time to RTC.
Definition: rtc.c:513
S_RTC_TIME_DATA_T::u32Hour
uint32_t u32Hour
Definition: rtc.h:86
RTC_EnableTamperDetection
void RTC_EnableTamperDetection(uint32_t u32PinCondition)
This function is used to: .
Definition: rtc.c:790
RTC_SetDate
void RTC_SetDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day, uint32_t u32DayOfWeek)
This function is used to update date to RTC.
Definition: rtc.c:593
RTC_Close
void RTC_Close(void)
Disable RTC clock.
Definition: rtc.c:952
S_RTC_TIME_DATA_T::u32Minute
uint32_t u32Minute
Definition: rtc.h:87
RTC_SetTime
void RTC_SetTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm)
This function is used to update time to RTC.
Definition: rtc.c:638
S_RTC_TIME_DATA_T::u32Day
uint32_t u32Day
Definition: rtc.h:84
S_RTC_TIME_DATA_T::u32DayOfWeek
uint32_t u32DayOfWeek
Definition: rtc.h:85
RTC_SetAlarmTime
void RTC_SetAlarmTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm)
This function is used to set alarm date to RTC.
Definition: rtc.c:735
RTC_DisableInt
void RTC_DisableInt(uint32_t u32IntFlagMask)
The function is used to disable specified interrupt.
Definition: rtc.c:915
RTC_DisableTamperDetection
void RTC_DisableTamperDetection(void)
This function is used to disable tamper detection function.
Definition: rtc.c:819
S_RTC_TIME_DATA_T::u32Year
uint32_t u32Year
Definition: rtc.h:82
RTC_SetTickPeriod
void RTC_SetTickPeriod(uint32_t u32TickSelection)
The function is used to set time tick period for periodic time tick Interrupt.
Definition: rtc.c:865
RTC_SetAlarmDate
void RTC_SetAlarmDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day)
This function is used to set alarm date to RTC.
Definition: rtc.c:692
S_RTC_TIME_DATA_T::u32Second
uint32_t u32Second
Definition: rtc.h:88
S_RTC_TIME_DATA_T::u32TimeScale
uint32_t u32TimeScale
Definition: rtc.h:89
RTC_GetDateAndTime
void RTC_GetDateAndTime(S_RTC_TIME_DATA_T *sPt)
Read current date/time from RTC setting.
Definition: rtc.c:223
NULL
#define NULL
NULL pointer.
Definition: Nano1X2Series.h:10812
S_RTC_TIME_DATA_T
RTC define Time Data Struct.
Definition: rtc.h:81
SystemCoreClock
uint32_t SystemCoreClock
Definition: system_Nano1X2Series.c:20