| NANO100_BSP V3.05.000 The Board Support Package for Nano100BN Series

rtc.c
Go to the documentation of this file.
1/**************************************************************************/
14#include <stdio.h>
15#include "Nano100Series.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 i32FrequencyX100)
59{
60 int32_t i32RegInt,i32RegFra ;
61 int32_t i32TimeoutCnt = SystemCoreClock; // 1 second timeout
62
63 /* Compute Integer and Fraction for RTC register*/
64 i32RegInt = (i32FrequencyX100/100) - RTC_FCR_REFERENCE;
65 i32RegFra = (((i32FrequencyX100%100)) * 60) / 100;
66
67 /* Judge Integer part is reasonable */
68 if ( (i32RegInt < 0) | (i32RegInt > 15) )
69 {
70 return;
71 }
72
73 RTC->AER = RTC_WRITE_KEY;
74 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
75 if(i32TimeoutCnt-- <= 0)
76 break;
77 }
78
79 RTC->FCR = (uint32_t)((i32RegInt<<8) | i32RegFra);
80
81}
82
104 void RTC_Open (S_RTC_TIME_DATA_T *sPt)
105{
106 uint32_t u32Reg;
107 int32_t i32TimeoutCnt = SystemCoreClock; // total 1 second timeout
108
109 volatile int32_t i32delay=1000;
110
111 RTC->INIR = RTC_INIT_KEY;
112
113 if(RTC->INIR != 0x1)
114 {
115 RTC->INIR = RTC_INIT_KEY;
116
117 while(RTC->INIR != 0x1) {
118 if(i32TimeoutCnt-- <= 0)
119 break;
120 }
121 }
122
123 if(sPt == NULL)
124 return;
125
126 /*-----------------------------------------------------------------------------------------------------*/
127 /* Second, set RTC 24/12 hour setting */
128 /*-----------------------------------------------------------------------------------------------------*/
129 if (sPt->u32TimeScale == RTC_CLOCK_12)
130 {
131 RTC->AER = RTC_WRITE_KEY;
132 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
133 RTC->AER = RTC_WRITE_KEY;
134 if(i32TimeoutCnt-- <= 0)
135 break;
136 }
137 RTC->TSSR &= ~RTC_TSSR_24H_12H_Msk;
138
139 /*-------------------------------------------------------------------------------------------------*/
140 /* important, range of 12-hour PM mode is 21 upto 32 */
141 /*-------------------------------------------------------------------------------------------------*/
142 if (sPt->u32AmPm == RTC_PM)
143 sPt->u32Hour += 20;
144 }
145 else
146 {
147 RTC->AER = RTC_WRITE_KEY;
148 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
149 RTC->AER = RTC_WRITE_KEY;
150 if(i32TimeoutCnt-- <= 0)
151 break;
152 }
153 RTC->TSSR |= RTC_TSSR_24H_12H_Msk;
154 }
155
156 /*-----------------------------------------------------------------------------------------------------*/
157 /* Set RTC Calender Loading */
158 /*-----------------------------------------------------------------------------------------------------*/
159 u32Reg = ((sPt->u32Year - RTC_YEAR2000) / 10) << 20;
160 u32Reg |= (((sPt->u32Year - RTC_YEAR2000) % 10) << 16);
161 u32Reg |= ((sPt->u32Month / 10) << 12);
162 u32Reg |= ((sPt->u32Month % 10) << 8);
163 u32Reg |= ((sPt->u32Day / 10) << 4);
164 u32Reg |= (sPt->u32Day % 10);
165 g_u32Reg = u32Reg;
166
167 RTC->AER = RTC_WRITE_KEY;
168 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
169 if(i32TimeoutCnt-- <= 0)
170 break;
171 }
172
173 RTC->CLR = (uint32_t)g_u32Reg;
174
175 /*-----------------------------------------------------------------------------------------------------*/
176 /* Set RTC Time Loading */
177 /*-----------------------------------------------------------------------------------------------------*/
178 u32Reg = ((sPt->u32Hour / 10) << 20);
179 u32Reg |= ((sPt->u32Hour % 10) << 16);
180 u32Reg |= ((sPt->u32Minute / 10) << 12);
181 u32Reg |= ((sPt->u32Minute % 10) << 8);
182 u32Reg |= ((sPt->u32Second / 10) << 4);
183 u32Reg |= (sPt->u32Second % 10);
184 g_u32Reg = u32Reg;
185
186 RTC->AER = RTC_WRITE_KEY;
187 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
188 if(i32TimeoutCnt-- <= 0)
189 break;
190 }
191
192 RTC->TLR = (uint32_t)g_u32Reg;
193
194 RTC->DWR = sPt->u32DayOfWeek;
195
196 /* Waiting for RTC settings stable */
197 while(i32delay--);
198
199}
200
219 void RTC_GetDateAndTime(S_RTC_TIME_DATA_T *sPt)
220{
221 uint32_t u32Tmp;
222
223 sPt->u32TimeScale = RTC->TSSR & RTC_TSSR_24H_12H_Msk; /* 12/24-hour */
224 sPt->u32DayOfWeek = RTC->DWR & RTC_DWR_DWR_Msk; /* Day of week */
225
226 g_u32hiYear = (RTC->CLR & RTC_CLR_10YEAR_Msk) >> RTC_CLR_10YEAR_Pos;
227 g_u32loYear = (RTC->CLR & RTC_CLR_1YEAR_Msk) >> RTC_CLR_1YEAR_Pos;
228 g_u32hiMonth = (RTC->CLR & RTC_CLR_10MON_Msk) >> RTC_CLR_10MON_Pos;
229 g_u32loMonth = (RTC->CLR & RTC_CLR_1MON_Msk) >> RTC_CLR_1MON_Pos;
230 g_u32hiDay = (RTC->CLR & RTC_CLR_10DAY_Msk) >> RTC_CLR_10DAY_Pos;
231 g_u32loDay = (RTC->CLR & RTC_CLR_1DAY_Msk);
232
233 g_u32hiHour = (RTC->TLR & RTC_TLR_10HR_Msk) >> RTC_TLR_10HR_Pos;
234 g_u32loHour = (RTC->TLR & RTC_TLR_1HR_Msk) >> RTC_TLR_1HR_Pos;
235 g_u32hiMin = (RTC->TLR & RTC_TLR_10MIN_Msk) >> RTC_TLR_10MIN_Pos;
236 g_u32loMin = (RTC->TLR & RTC_TLR_1MIN_Msk) >> RTC_TLR_1MIN_Pos;
237 g_u32hiSec = (RTC->TLR & RTC_TLR_10SEC_Msk) >> RTC_TLR_10SEC_Pos;
238 g_u32loSec = (RTC->TLR & RTC_TLR_1SEC_Msk);
239
240 u32Tmp = (g_u32hiYear * 10); /* Compute to 20XX year */
241 u32Tmp += g_u32loYear;
242 sPt->u32Year = u32Tmp + RTC_YEAR2000;
243
244 u32Tmp = (g_u32hiMonth * 10); /* Compute 0~12 month */
245 sPt->u32Month = u32Tmp + g_u32loMonth;
246
247 u32Tmp = (g_u32hiDay * 10); /* Compute 0~31 day */
248 sPt->u32Day = u32Tmp + g_u32loDay;
249
250 if (sPt->u32TimeScale == RTC_CLOCK_12) /* Compute12/24 hour */
251 {
252 u32Tmp = (g_u32hiHour * 10);
253 u32Tmp+= g_u32loHour;
254 sPt->u32Hour = u32Tmp; /* AM: 1~12. PM: 21~32. */
255
256 if (sPt->u32Hour >= 21)
257 {
258 sPt->u32AmPm = RTC_PM;
259 sPt->u32Hour -= 20;
260 }
261 else
262 {
263 sPt->u32AmPm = RTC_AM;
264 }
265
266 u32Tmp = (g_u32hiMin * 10);
267 u32Tmp+= g_u32loMin;
268 sPt->u32Minute = u32Tmp;
269
270 u32Tmp = (g_u32hiSec * 10);
271 u32Tmp+= g_u32loSec;
272 sPt->u32Second = u32Tmp;
273
274 }
275 else
276 {
277 u32Tmp = (g_u32hiHour * 10);
278 u32Tmp += g_u32loHour;
279 sPt->u32Hour = u32Tmp;
280
281 u32Tmp = (g_u32hiMin * 10);
282 u32Tmp += g_u32loMin;
283 sPt->u32Minute = u32Tmp;
284
285 u32Tmp = (g_u32hiSec * 10);
286 u32Tmp += g_u32loSec;
287 sPt->u32Second = u32Tmp;
288 }
289
290}
291
292
293
312 void RTC_GetAlarmDateAndTime(S_RTC_TIME_DATA_T *sPt)
313{
314 uint32_t u32Tmp;
315 int32_t i32TimeoutCnt = SystemCoreClock; // total 1 second timeout
316
317 sPt->u32TimeScale = RTC->TSSR & RTC_TSSR_24H_12H_Msk; /* 12/24-hour */
318 sPt->u32DayOfWeek = RTC->DWR & RTC_DWR_DWR_Msk; /* Day of week */
319
320 RTC->AER = RTC_WRITE_KEY;
321 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
322 if(i32TimeoutCnt-- <= 0)
323 break;
324 }
325
326 g_u32hiYear = (RTC->CAR & RTC_CAR_10YEAR_Msk) >> RTC_CAR_10YEAR_Pos;
327 g_u32loYear = (RTC->CAR & RTC_CAR_1YEAR_Msk) >> RTC_CAR_1YEAR_Pos;
328 g_u32hiMonth = (RTC->CAR & RTC_CAR_10MON_Msk) >> RTC_CAR_10MON_Pos;
329 g_u32loMonth = (RTC->CAR & RTC_CAR_1MON_Msk) >> RTC_CAR_1MON_Pos;
330 g_u32hiDay = (RTC->CAR & RTC_CAR_10DAY_Msk) >> RTC_CAR_10DAY_Pos;
331 g_u32loDay = (RTC->CAR & RTC_CAR_1DAY_Msk);
332
333 RTC->AER = RTC_WRITE_KEY;
334 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
335 if(i32TimeoutCnt-- <= 0)
336 break;
337 }
338
339 g_u32hiHour = (RTC->TAR & RTC_TAR_10HR_Msk) >> RTC_TAR_10HR_Pos;
340 g_u32loHour = (RTC->TAR & RTC_TAR_1HR_Msk) >> RTC_TAR_1HR_Pos;
341 g_u32hiMin = (RTC->TAR & RTC_TAR_10MIN_Msk) >> RTC_TAR_10MIN_Pos;
342 g_u32loMin = (RTC->TAR & RTC_TAR_1MIN_Msk) >> RTC_TAR_1MIN_Pos;
343 g_u32hiSec = (RTC->TAR & RTC_TAR_10SEC_Msk) >> RTC_TAR_10SEC_Pos;
344 g_u32loSec = (RTC->TAR & RTC_TAR_1SEC_Msk);
345
346 u32Tmp = (g_u32hiYear * 10); /* Compute to 20XX year */
347 u32Tmp += g_u32loYear;
348 sPt->u32Year = u32Tmp + RTC_YEAR2000;
349
350 u32Tmp = (g_u32hiMonth * 10); /* Compute 0~12 month */
351 sPt->u32Month = u32Tmp + g_u32loMonth;
352
353 u32Tmp = (g_u32hiDay * 10); /* Compute 0~31 day */
354 sPt->u32Day = u32Tmp + g_u32loDay;
355
356 if (sPt->u32TimeScale == RTC_CLOCK_12) /* Compute12/24 hour */
357 {
358 u32Tmp = (g_u32hiHour * 10);
359 u32Tmp += g_u32loHour;
360 sPt->u32Hour = u32Tmp; /* AM: 1~12. PM: 21~32. */
361
362 if (sPt->u32Hour >= 21)
363 {
364 sPt->u32AmPm = RTC_PM;
365 sPt->u32Hour -= 20;
366 }
367 else
368 {
369 sPt->u32AmPm = RTC_AM;
370 }
371
372 u32Tmp = (g_u32hiMin * 10);
373 u32Tmp += g_u32loMin;
374 sPt->u32Minute = u32Tmp;
375
376 u32Tmp = (g_u32hiSec * 10);
377 u32Tmp += g_u32loSec;
378 sPt->u32Second = u32Tmp;
379
380 }
381 else
382 {
383 u32Tmp = (g_u32hiHour * 10);
384 u32Tmp += g_u32loHour;
385 sPt->u32Hour = u32Tmp;
386
387 u32Tmp = (g_u32hiMin * 10);
388 u32Tmp+= g_u32loMin;
389 sPt->u32Minute = u32Tmp;
390
391 u32Tmp = (g_u32hiSec * 10);
392 u32Tmp += g_u32loSec;
393 sPt->u32Second = u32Tmp;
394 }
395
396}
397
398
399
422 void RTC_SetDateAndTime(S_RTC_TIME_DATA_T *sPt)
423{
424 uint32_t u32Reg;
425 int32_t i32TimeoutCnt = SystemCoreClock; // total 1 second timeout
426
427 RTC->AER = RTC_WRITE_KEY;
428 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
429 if(i32TimeoutCnt-- <= 0)
430 break;
431 }
432
433 if (sPt->u32TimeScale == RTC_CLOCK_12)
434 {
435 RTC->TSSR &= ~RTC_TSSR_24H_12H_Msk;
436
437 /*-----------------------------------------------------------------------------------------*/
438 /* important, range of 12-hour PM mode is 21 upto 32 */
439 /*-----------------------------------------------------------------------------------------*/
440 if (sPt->u32AmPm == RTC_PM)
441 sPt->u32Hour += 20;
442 }
443 else
444 {
445 RTC->TSSR |= RTC_TSSR_24H_12H_Msk;
446 }
447
448 RTC->DWR = sPt->u32DayOfWeek & RTC_DWR_DWR_Msk;
449
450 u32Reg = ((sPt->u32Year - RTC_YEAR2000) / 10) << 20;
451 u32Reg |= (((sPt->u32Year - RTC_YEAR2000) % 10) << 16);
452 u32Reg |= ((sPt->u32Month / 10) << 12);
453 u32Reg |= ((sPt->u32Month % 10) << 8);
454 u32Reg |= ((sPt->u32Day / 10) << 4);
455 u32Reg |= (sPt->u32Day % 10);
456 g_u32Reg = u32Reg;
457
458 RTC->AER = RTC_WRITE_KEY;
459 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
460 if(i32TimeoutCnt-- <= 0)
461 break;
462 }
463
464 RTC->CLR = (uint32_t)g_u32Reg;
465
466 u32Reg = ((sPt->u32Hour / 10) << 20);
467 u32Reg |= ((sPt->u32Hour % 10) << 16);
468 u32Reg |= ((sPt->u32Minute / 10) << 12);
469 u32Reg |= ((sPt->u32Minute % 10) << 8);
470 u32Reg |= ((sPt->u32Second / 10) << 4);
471 u32Reg |= (sPt->u32Second % 10);
472 g_u32Reg = u32Reg;
473
474 RTC->AER = RTC_WRITE_KEY;
475 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
476 if(i32TimeoutCnt-- <= 0)
477 break;
478 }
479
480 RTC->TLR = (uint32_t)g_u32Reg;
481
482}
483
504 void RTC_SetAlarmDateAndTime(S_RTC_TIME_DATA_T *sPt)
505{
506 uint32_t u32Reg;
507 int32_t i32TimeoutCnt = SystemCoreClock; // total 1 second timeout
508
509 RTC->AER = RTC_WRITE_KEY;
510 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
511 if(i32TimeoutCnt-- <= 0)
512 break;
513 }
514
515 if (sPt->u32TimeScale == RTC_CLOCK_12)
516 {
517 RTC->TSSR &= ~RTC_TSSR_24H_12H_Msk;
518
519 /*-----------------------------------------------------------------------------------------*/
520 /* important, range of 12-hour PM mode is 21 upto 32 */
521 /*-----------------------------------------------------------------------------------------*/
522 if (sPt->u32AmPm == RTC_PM)
523 sPt->u32Hour += 20;
524 }
525 else
526 {
527 RTC->TSSR |= RTC_TSSR_24H_12H_Msk;
528 }
529
530 RTC->DWR = sPt->u32DayOfWeek & RTC_DWR_DWR_Msk;
531
532
533 u32Reg = ((sPt->u32Year - RTC_YEAR2000) / 10) << 20;
534 u32Reg |= (((sPt->u32Year - RTC_YEAR2000) % 10) << 16);
535 u32Reg |= ((sPt->u32Month / 10) << 12);
536 u32Reg |= ((sPt->u32Month % 10) << 8);
537 u32Reg |= ((sPt->u32Day / 10) << 4);
538 u32Reg |= (sPt->u32Day % 10);
539 g_u32Reg = u32Reg;
540
541 RTC->AER = RTC_WRITE_KEY;
542 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
543 if(i32TimeoutCnt-- <= 0)
544 break;
545 }
546
547 RTC->CAR = (uint32_t)g_u32Reg;
548
549 u32Reg = ((sPt->u32Hour / 10) << 20);
550 u32Reg |= ((sPt->u32Hour % 10) << 16);
551 u32Reg |= ((sPt->u32Minute / 10) << 12);
552 u32Reg |= ((sPt->u32Minute % 10) << 8);
553 u32Reg |= ((sPt->u32Second / 10) << 4);
554 u32Reg |= (sPt->u32Second % 10);
555 g_u32Reg = u32Reg;
556
557 RTC->AER = RTC_WRITE_KEY;
558 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
559 if(i32TimeoutCnt-- <= 0)
560 break;
561 }
562
563 RTC->TAR = (uint32_t)g_u32Reg;
564
565}
566
567
581 void RTC_SetDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day, uint32_t u32DayOfWeek)
582{
583 __IO uint32_t u32Reg;
584 int32_t i32TimeoutCnt = SystemCoreClock; // total 1 second timeout
585
586 RTC->AER = RTC_WRITE_KEY;
587 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
588 RTC->AER = RTC_WRITE_KEY;
589 if(i32TimeoutCnt-- <= 0)
590 break;
591 }
592
593 RTC->DWR = u32DayOfWeek & RTC_DWR_DWR_Msk;
594
595 u32Reg = ((u32Year - RTC_YEAR2000) / 10) << 20;
596 u32Reg |= (((u32Year - RTC_YEAR2000) % 10) << 16);
597 u32Reg |= ((u32Month / 10) << 12);
598 u32Reg |= ((u32Month % 10) << 8);
599 u32Reg |= ((u32Day / 10) << 4);
600 u32Reg |= (u32Day % 10);
601 g_u32Reg = u32Reg;
602
603 RTC->AER = RTC_WRITE_KEY;
604 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
605 if(i32TimeoutCnt-- <= 0)
606 break;
607 }
608
609 RTC->CLR = (uint32_t)g_u32Reg;
610
611}
612
625 void RTC_SetTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm)
626{
627 __IO uint32_t u32Reg;
628 int32_t i32TimeoutCnt = SystemCoreClock; // total 1 second timeout
629
630 RTC->AER = RTC_WRITE_KEY;
631 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
632 if(i32TimeoutCnt-- <= 0)
633 break;
634 }
635
636 if (u32TimeMode == RTC_CLOCK_12)
637 {
638 RTC->TSSR &= ~RTC_TSSR_24H_12H_Msk;
639
640 if (u32AmPm == RTC_PM) /* important, range of 12-hour PM mode is 21 upto 32 */
641 u32Hour += 20;
642 }
643 else if(u32TimeMode == RTC_CLOCK_24)
644 {
645 RTC->TSSR |= RTC_TSSR_24H_12H_Msk;
646 }
647
648 u32Reg = ((u32Hour / 10) << 20);
649 u32Reg |= ((u32Hour % 10) << 16);
650 u32Reg |= ((u32Minute / 10) << 12);
651 u32Reg |= ((u32Minute % 10) << 8);
652 u32Reg |= ((u32Second / 10) << 4);
653 u32Reg |= (u32Second % 10);
654
655 g_u32Reg = u32Reg;
656
657 RTC->AER = RTC_WRITE_KEY;
658 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
659 if(i32TimeoutCnt-- <= 0)
660 break;
661 }
662
663 RTC->TLR = (uint32_t)g_u32Reg;
664
665}
666
677 void RTC_SetAlarmDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day)
678{
679 __IO uint32_t u32Reg;
680 int32_t i32TimeoutCnt = SystemCoreClock; // total 1 second timeout
681
682 RTC->AER = RTC_WRITE_KEY;
683 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
684 RTC->AER = RTC_WRITE_KEY;
685 if(i32TimeoutCnt-- <= 0)
686 break;
687 }
688
689 u32Reg = ((u32Year - RTC_YEAR2000) / 10) << 20;
690 u32Reg |= (((u32Year - RTC_YEAR2000) % 10) << 16);
691 u32Reg |= ((u32Month / 10) << 12);
692 u32Reg |= ((u32Month % 10) << 8);
693 u32Reg |= ((u32Day / 10) << 4);
694 u32Reg |= (u32Day % 10);
695 g_u32Reg = u32Reg;
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 RTC->CAR = (uint32_t)g_u32Reg;
705
706}
707
720 void RTC_SetAlarmTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm)
721{
722 __IO uint32_t u32Reg;
723 int32_t i32TimeoutCnt = SystemCoreClock; // total 1 second timeout
724
725 RTC->AER = RTC_WRITE_KEY;
726 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
727 RTC->AER = RTC_WRITE_KEY;
728 if(i32TimeoutCnt-- <= 0)
729 break;
730 }
731
732 if (u32TimeMode == RTC_CLOCK_12)
733 {
734 RTC->TSSR &= ~RTC_TSSR_24H_12H_Msk;
735
736 if (u32AmPm == RTC_PM) /* important, range of 12-hour PM mode is 21 upto 32 */
737 u32Hour += 20;
738 }
739 else if(u32TimeMode == RTC_CLOCK_24)
740 {
741 RTC->TSSR |= RTC_TSSR_24H_12H_Msk;
742 }
743
744 u32Reg = ((u32Hour / 10) << 20);
745 u32Reg |= ((u32Hour % 10) << 16);
746 u32Reg |= ((u32Minute / 10) << 12);
747 u32Reg |= ((u32Minute % 10) << 8);
748 u32Reg |= ((u32Second / 10) << 4);
749 u32Reg |= (u32Second % 10);
750
751 g_u32Reg = u32Reg;
752
753 RTC->AER = RTC_WRITE_KEY;
754 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
755 RTC->AER = RTC_WRITE_KEY;
756 if(i32TimeoutCnt-- <= 0)
757 break;
758 }
759
760 RTC->TAR = (uint32_t)g_u32Reg;
761
762}
763
764
773 void RTC_EnableTamperDetection(uint32_t u32PinCondition)
774{
775 int32_t i32TimeoutCnt = SystemCoreClock; // total 1 second timeout
776
777 RTC->AER = RTC_WRITE_KEY;
778 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
779 RTC->AER = RTC_WRITE_KEY;
780 if(i32TimeoutCnt-- <= 0)
781 break;
782 }
783
784 /* detection edge select */
785 if(u32PinCondition)
786 RTC->SPRCTL |= RTC_SPRCTL_SNOOPEDGE_Msk;
787 else
788 RTC->SPRCTL &= ~RTC_SPRCTL_SNOOPEDGE_Msk;
789
790 while(!(RTC->SPRCTL & RTC_SPRCTL_SPRRDY_Msk)) {
791 if(i32TimeoutCnt-- <= 0)
792 break;
793 }
794
795 /* enable snooper pin event detection */
796 RTC->SPRCTL |= RTC_SPRCTL_SNOOPEN_Msk;
797 while(!(RTC->SPRCTL & RTC_SPRCTL_SPRRDY_Msk)) {
798 if(i32TimeoutCnt-- <= 0)
799 break;
800 }
801}
802
811 void RTC_DisableTamperDetection(void)
812{
813 int32_t i32TimeoutCnt = SystemCoreClock; // 1 second timeout
814
815 RTC->AER = RTC_WRITE_KEY;
816 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
817 RTC->AER = RTC_WRITE_KEY;
818 if(i32TimeoutCnt-- <= 0)
819 break;
820 }
821
822 RTC->SPRCTL &= ~RTC_SPRCTL_SNOOPEN_Msk;
823}
824
833uint32_t RTC_GetDayOfWeek(void)
834{
835 return (RTC->DWR & RTC_DWR_DWR_Msk);
836}
837
838
857 void RTC_SetTickPeriod(uint32_t u32TickSelection)
858{
859 int32_t i32TimeoutCnt = SystemCoreClock; // 1 second timeout
860
861 RTC->AER = RTC_WRITE_KEY;
862 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
863 RTC->AER = RTC_WRITE_KEY;
864 if(i32TimeoutCnt-- <= 0)
865 break;
866 }
867
868 RTC->TTR = (RTC->TTR & ~RTC_TTR_TTR_Msk) | u32TickSelection;
869}
870
882 void RTC_EnableInt(uint32_t u32IntFlagMask)
883{
884 int32_t i32TimeoutCnt = SystemCoreClock; // 1 second timeout
885
886 RTC->AER = RTC_WRITE_KEY;
887 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
888 RTC->AER = RTC_WRITE_KEY;
889 if(i32TimeoutCnt-- <= 0)
890 break;
891 }
892
893 RTC->RIER |= u32IntFlagMask;
894}
895
907 void RTC_DisableInt(uint32_t u32IntFlagMask)
908{
909 int32_t i32TimeoutCnt = SystemCoreClock; // 1 second timeout
910
911 RTC->AER = RTC_WRITE_KEY;
912 while(!(RTC->AER & RTC_AER_ENF_Msk)) {
913 RTC->AER = RTC_WRITE_KEY;
914 if(i32TimeoutCnt-- <= 0)
915 break;
916 }
917
918 if(u32IntFlagMask & RTC_RIER_TIER_Msk)
919 {
920 RTC->RIER &= ~RTC_RIER_TIER_Msk;
921 RTC->RIIR = RTC_RIIR_TIF_Msk;
922 }
923
924 if(u32IntFlagMask & RTC_RIER_AIER_Msk)
925 {
926 RTC->RIER &= ~RTC_RIER_AIER_Msk;
927 RTC->RIIR = RTC_RIIR_AIF_Msk;
928 }
929
930 if(u32IntFlagMask & RTC_RIER_SNOOPIER_Msk)
931 {
932 RTC->RIER &= ~RTC_RIER_SNOOPIER_Msk;
933 RTC->RIIR = RTC_RIIR_SNOOPIF_Msk;
934 }
935}
936
943 void RTC_Close (void)
944{
945 CLK->APBCLK &= ~CLK_APBCLK_RTC_EN_Msk;
946}
947
948 /* end of group NANO100_RTC_EXPORTED_FUNCTIONS */
950 /* end of group NANO100_RTC_Driver */
952 /* end of group NANO100_Device_Driver */
954
955/*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/
956
957
Nano100Series.h
Nano100 series peripheral access layer header file. This file contains all the peripheral register's ...
RTC_TAR_10SEC_Msk
#define RTC_TAR_10SEC_Msk
Definition: Nano100Series.h:8135
RTC_TAR_10MIN_Pos
#define RTC_TAR_10MIN_Pos
Definition: Nano100Series.h:8140
RTC_CAR_10YEAR_Pos
#define RTC_CAR_10YEAR_Pos
Definition: Nano100Series.h:8164
RTC_TLR_10MIN_Pos
#define RTC_TLR_10MIN_Pos
Definition: Nano100Series.h:8098
RTC_TAR_10HR_Msk
#define RTC_TAR_10HR_Msk
Definition: Nano100Series.h:8147
RTC_TLR_1MIN_Pos
#define RTC_TLR_1MIN_Pos
Definition: Nano100Series.h:8095
RTC_CAR_10DAY_Msk
#define RTC_CAR_10DAY_Msk
Definition: Nano100Series.h:8153
RTC_TLR_10SEC_Pos
#define RTC_TLR_10SEC_Pos
Definition: Nano100Series.h:8092
RTC_TLR_1HR_Msk
#define RTC_TLR_1HR_Msk
Definition: Nano100Series.h:8102
RTC_CAR_10MON_Pos
#define RTC_CAR_10MON_Pos
Definition: Nano100Series.h:8158
RTC_CAR_1MON_Msk
#define RTC_CAR_1MON_Msk
Definition: Nano100Series.h:8156
RTC_TLR_1HR_Pos
#define RTC_TLR_1HR_Pos
Definition: Nano100Series.h:8101
RTC_SPRCTL_SNOOPEDGE_Msk
#define RTC_SPRCTL_SNOOPEDGE_Msk
Definition: Nano100Series.h:8198
RTC_TAR_1SEC_Msk
#define RTC_TAR_1SEC_Msk
Definition: Nano100Series.h:8132
RTC_RIER_TIER_Msk
#define RTC_RIER_TIER_Msk
Definition: Nano100Series.h:8174
RTC_DWR_DWR_Msk
#define RTC_DWR_DWR_Msk
Definition: Nano100Series.h:8129
RTC_CLR_10YEAR_Pos
#define RTC_CLR_10YEAR_Pos
Definition: Nano100Series.h:8122
RTC_CAR_10DAY_Pos
#define RTC_CAR_10DAY_Pos
Definition: Nano100Series.h:8152
RTC_CLR_1MON_Pos
#define RTC_CLR_1MON_Pos
Definition: Nano100Series.h:8113
RTC_CAR_10MON_Msk
#define RTC_CAR_10MON_Msk
Definition: Nano100Series.h:8159
RTC_CLR_10YEAR_Msk
#define RTC_CLR_10YEAR_Msk
Definition: Nano100Series.h:8123
RTC_RIIR_TIF_Msk
#define RTC_RIIR_TIF_Msk
Definition: Nano100Series.h:8183
RTC_CLR_10MON_Msk
#define RTC_CLR_10MON_Msk
Definition: Nano100Series.h:8117
RTC_CLR_1YEAR_Msk
#define RTC_CLR_1YEAR_Msk
Definition: Nano100Series.h:8120
RTC_CLR_10MON_Pos
#define RTC_CLR_10MON_Pos
Definition: Nano100Series.h:8116
RTC_RIER_AIER_Msk
#define RTC_RIER_AIER_Msk
Definition: Nano100Series.h:8171
RTC_TAR_1HR_Msk
#define RTC_TAR_1HR_Msk
Definition: Nano100Series.h:8144
RTC_CLR_1YEAR_Pos
#define RTC_CLR_1YEAR_Pos
Definition: Nano100Series.h:8119
RTC_CAR_1MON_Pos
#define RTC_CAR_1MON_Pos
Definition: Nano100Series.h:8155
RTC_TAR_10HR_Pos
#define RTC_TAR_10HR_Pos
Definition: Nano100Series.h:8146
RTC_CAR_1YEAR_Msk
#define RTC_CAR_1YEAR_Msk
Definition: Nano100Series.h:8162
RTC_CLR_1MON_Msk
#define RTC_CLR_1MON_Msk
Definition: Nano100Series.h:8114
RTC_TLR_1MIN_Msk
#define RTC_TLR_1MIN_Msk
Definition: Nano100Series.h:8096
RTC_CAR_10YEAR_Msk
#define RTC_CAR_10YEAR_Msk
Definition: Nano100Series.h:8165
RTC_RIIR_AIF_Msk
#define RTC_RIIR_AIF_Msk
Definition: Nano100Series.h:8180
RTC_TLR_10HR_Pos
#define RTC_TLR_10HR_Pos
Definition: Nano100Series.h:8104
RTC_TLR_1SEC_Msk
#define RTC_TLR_1SEC_Msk
Definition: Nano100Series.h:8090
RTC_TAR_10SEC_Pos
#define RTC_TAR_10SEC_Pos
Definition: Nano100Series.h:8134
RTC_CAR_1YEAR_Pos
#define RTC_CAR_1YEAR_Pos
Definition: Nano100Series.h:8161
RTC_CLR_10DAY_Pos
#define RTC_CLR_10DAY_Pos
Definition: Nano100Series.h:8110
RTC_CLR_10DAY_Msk
#define RTC_CLR_10DAY_Msk
Definition: Nano100Series.h:8111
RTC_TAR_1MIN_Msk
#define RTC_TAR_1MIN_Msk
Definition: Nano100Series.h:8138
RTC_CLR_1DAY_Msk
#define RTC_CLR_1DAY_Msk
Definition: Nano100Series.h:8108
RTC_TLR_10MIN_Msk
#define RTC_TLR_10MIN_Msk
Definition: Nano100Series.h:8099
RTC_TAR_1HR_Pos
#define RTC_TAR_1HR_Pos
Definition: Nano100Series.h:8143
RTC_SPRCTL_SNOOPEN_Msk
#define RTC_SPRCTL_SNOOPEN_Msk
Definition: Nano100Series.h:8195
RTC_TAR_1MIN_Pos
#define RTC_TAR_1MIN_Pos
Definition: Nano100Series.h:8137
RTC_RIER_SNOOPIER_Msk
#define RTC_RIER_SNOOPIER_Msk
Definition: Nano100Series.h:8177
RTC_TLR_10HR_Msk
#define RTC_TLR_10HR_Msk
Definition: Nano100Series.h:8105
RTC_TSSR_24H_12H_Msk
#define RTC_TSSR_24H_12H_Msk
Definition: Nano100Series.h:8126
RTC_SPRCTL_SPRRDY_Msk
#define RTC_SPRCTL_SPRRDY_Msk
Definition: Nano100Series.h:8201
RTC_AER_ENF_Msk
#define RTC_AER_ENF_Msk
Definition: Nano100Series.h:8081
RTC_TAR_10MIN_Msk
#define RTC_TAR_10MIN_Msk
Definition: Nano100Series.h:8141
RTC_RIIR_SNOOPIF_Msk
#define RTC_RIIR_SNOOPIF_Msk
Definition: Nano100Series.h:8186
RTC_TLR_10SEC_Msk
#define RTC_TLR_10SEC_Msk
Definition: Nano100Series.h:8093
RTC_CAR_1DAY_Msk
#define RTC_CAR_1DAY_Msk
Definition: Nano100Series.h:8150
CLK
#define CLK
Pointer to CLK register structure.
Definition: Nano100Series.h:11693
RTC
#define RTC
Pointer to RTC register structure.
Definition: Nano100Series.h:11668
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_FCR_REFERENCE
#define RTC_FCR_REFERENCE
Definition: rtc.h:41
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:882
RTC_GetDayOfWeek
uint32_t RTC_GetDayOfWeek(void)
This function is used to get day of week.
Definition: rtc.c:833
RTC_SetDateAndTime
void RTC_SetDateAndTime(S_RTC_TIME_DATA_T *sPt)
This function is used to update date/time to RTC.
Definition: rtc.c:422
S_RTC_TIME_DATA_T::u32Month
uint32_t u32Month
Definition: rtc.h:84
S_RTC_TIME_DATA_T::u32AmPm
uint32_t u32AmPm
Definition: rtc.h:91
RTC_GetAlarmDateAndTime
void RTC_GetAlarmDateAndTime(S_RTC_TIME_DATA_T *sPt)
Read alarm date/time from RTC setting.
Definition: rtc.c:312
RTC_Open
void RTC_Open(S_RTC_TIME_DATA_T *sPt)
This function is used to write initial key to let RTC start count and set current time.
Definition: rtc.c:104
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:504
S_RTC_TIME_DATA_T::u32Hour
uint32_t u32Hour
Definition: rtc.h:87
RTC_EnableTamperDetection
void RTC_EnableTamperDetection(uint32_t u32PinCondition)
This function is used to enable tamper detection function and set tamper control register,...
Definition: rtc.c:773
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:581
RTC_Close
void RTC_Close(void)
Disable RTC clock.
Definition: rtc.c:943
RTC_32KCalibration
void RTC_32KCalibration(int32_t i32FrequencyX100)
Set Frequency Compensation Data.
Definition: rtc.c:58
S_RTC_TIME_DATA_T::u32Minute
uint32_t u32Minute
Definition: rtc.h:88
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:625
S_RTC_TIME_DATA_T::u32Day
uint32_t u32Day
Definition: rtc.h:85
S_RTC_TIME_DATA_T::u32DayOfWeek
uint32_t u32DayOfWeek
Definition: rtc.h:86
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:720
RTC_DisableInt
void RTC_DisableInt(uint32_t u32IntFlagMask)
The function is used to disable specified interrupt.
Definition: rtc.c:907
RTC_DisableTamperDetection
void RTC_DisableTamperDetection(void)
This function is used to disable tamper detection function.
Definition: rtc.c:811
S_RTC_TIME_DATA_T::u32Year
uint32_t u32Year
Definition: rtc.h:83
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:857
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:677
S_RTC_TIME_DATA_T::u32Second
uint32_t u32Second
Definition: rtc.h:89
S_RTC_TIME_DATA_T::u32TimeScale
uint32_t u32TimeScale
Definition: rtc.h:90
RTC_GetDateAndTime
void RTC_GetDateAndTime(S_RTC_TIME_DATA_T *sPt)
Read current date/time from RTC setting.
Definition: rtc.c:219
NULL
#define NULL
NULL pointer.
Definition: Nano100Series.h:11859
S_RTC_TIME_DATA_T
RTC define Time Data Struct.
Definition: rtc.h:82
SystemCoreClock
uint32_t SystemCoreClock
Definition: system_Nano100Series.c:20