1 /**************************************************************************//**
2  * @file     rtc.c
3  * @version  V3.00
4  * @brief    M480 series RTC driver source file
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  * @copyright (C) 2016-2020 Nuvoton Technology Corp. All rights reserved.
8 *****************************************************************************/
9 #include "NuMicro.h"
10 
11 
12 /** @cond HIDDEN_SYMBOLS */
13 
14 /*---------------------------------------------------------------------------------------------------------*/
15 /* Macro, type and constant definitions                                                                    */
16 /*---------------------------------------------------------------------------------------------------------*/
17 #define RTC_GLOBALS
18 
19 /*---------------------------------------------------------------------------------------------------------*/
20 /* Global file scope (static) variables                                                                    */
21 /*---------------------------------------------------------------------------------------------------------*/
22 static volatile uint32_t g_u32hiYear, g_u32loYear, g_u32hiMonth, g_u32loMonth, g_u32hiDay, g_u32loDay;
23 static volatile uint32_t g_u32hiHour, g_u32loHour, g_u32hiMin, g_u32loMin, g_u32hiSec, g_u32loSec;
24 
25 /** @endcond HIDDEN_SYMBOLS */
26 
27 
28 
29 
30 /** @addtogroup Standard_Driver Standard Driver
31   @{
32 */
33 
34 /** @addtogroup RTC_Driver RTC Driver
35   @{
36 */
37 
38 /** @addtogroup RTC_EXPORTED_FUNCTIONS RTC Exported Functions
39   @{
40 */
41 
42 /**
43   * @brief      Initialize RTC module and start counting
44   *
45   * @param[in]  sPt     Specify the time property and current date and time. It includes:           \n
46   *                     u32Year: Year value, range between 2000 ~ 2099.                             \n
47   *                     u32Month: Month value, range between 1 ~ 12.                                \n
48   *                     u32Day: Day value, range between 1 ~ 31.                                    \n
49   *                     u32DayOfWeek: Day of the week. [RTC_SUNDAY / RTC_MONDAY / RTC_TUESDAY /
50   *                                                     RTC_WEDNESDAY / RTC_THURSDAY / RTC_FRIDAY /
51   *                                                     RTC_SATURDAY]                               \n
52   *                     u32Hour: Hour value, range between 0 ~ 23.                                  \n
53   *                     u32Minute: Minute value, range between 0 ~ 59.                              \n
54   *                     u32Second: Second value, range between 0 ~ 59.                              \n
55   *                     u32TimeScale: [RTC_CLOCK_12 / RTC_CLOCK_24]                                 \n
56   *                     u8AmPm: [RTC_AM / RTC_PM]                                                   \n
57   *
58   * @return     None
59   *
60   * @details    This function is used to: \n
61   *                 1. Write initial key to let RTC start count.  \n
62   *                 2. Input parameter indicates start date/time. \n
63   *                 3. User has to make sure that parameters of RTC date/time are reasonable. \n
64   * @note       Null pointer for using default starting date/time.
65   */
RTC_Open(S_RTC_TIME_DATA_T * sPt)66 void RTC_Open(S_RTC_TIME_DATA_T *sPt)
67 {
68     RTC->INIT = RTC_INIT_KEY;
69 
70     if(RTC->INIT != RTC_INIT_ACTIVE_Msk)
71     {
72         RTC->INIT = RTC_INIT_KEY;
73         while(RTC->INIT != RTC_INIT_ACTIVE_Msk)
74         {
75         }
76     }
77 
78     if(sPt == 0)
79     {
80     }
81     else
82     {
83         /* Set RTC date and time */
84         RTC_SetDateAndTime(sPt);
85     }
86 }
87 
88 /**
89   * @brief      Disable RTC Clock
90   *
91   * @param      None
92   *
93   * @return     None
94   *
95   * @details    This API will disable RTC peripheral clock and stops RTC counting.
96   */
RTC_Close(void)97 void RTC_Close(void)
98 {
99     CLK->APBCLK0 &= ~CLK_APBCLK0_RTCCKEN_Msk;
100 }
101 
102 /**
103  *  @brief    Set Frequency Compensation Data
104   *
105  *  @param[in]    i32FrequencyX10000    Specify the RTC clock X10000, ex: 327736512 means 32773.6512.
106   *
107   * @return     None
108   *
109   */
RTC_32KCalibration(int32_t i32FrequencyX10000)110 void RTC_32KCalibration(int32_t i32FrequencyX10000)
111 {
112     uint64_t u64Compensate;
113     int32_t i32RegInt,i32RegFra ;
114 
115     if(!(SYS->CSERVER & 0x1))
116     {
117         u64Compensate = (uint64_t)(0x2710000000000);
118         u64Compensate = (uint64_t)(u64Compensate / (uint64_t)i32FrequencyX10000);
119 
120         if(u64Compensate >= (uint64_t)0x400000)
121         {
122             u64Compensate = (uint64_t)0x3FFFFF;
123         }
124 
125         RTC_WaitAccessEnable();
126         RTC->FREQADJ = (uint32_t)u64Compensate;
127     }
128     else
129     {
130         /* Compute Integer and Fraction for RTC register*/
131         i32RegInt = (i32FrequencyX10000/10000) - 32752;
132         i32RegFra = ((((i32FrequencyX10000%10000)) * 64) + 5000) / 10000;
133 
134         if(i32RegFra >= 0x40)
135         {
136             i32RegFra = 0x0;
137             i32RegInt++;
138         }
139 
140         /* Judge Integer part is reasonable */
141         if ( (i32RegInt < 0) | (i32RegInt > 31) )
142         {
143             return;
144         }
145 
146         RTC_WaitAccessEnable();
147         RTC->FREQADJ = (uint32_t)((i32RegInt<<8) | i32RegFra);
148     }
149 
150 }
151 
152 /**
153   * @brief      Get Current RTC Date and Time
154   *
155   * @param[out] sPt     The returned pointer is specified the current RTC value. It includes: \n
156   *                     u32Year: Year value                                                   \n
157   *                     u32Month: Month value                                                 \n
158   *                     u32Day: Day value                                                     \n
159   *                     u32DayOfWeek: Day of week                                             \n
160   *                     u32Hour: Hour value                                                   \n
161   *                     u32Minute: Minute value                                               \n
162   *                     u32Second: Second value                                               \n
163   *                     u32TimeScale: [RTC_CLOCK_12 / RTC_CLOCK_24]                           \n
164   *                     u8AmPm: [RTC_AM / RTC_PM]                                             \n
165   *
166   * @return     None
167   *
168   * @details    This API is used to get the current RTC date and time value.
169   */
RTC_GetDateAndTime(S_RTC_TIME_DATA_T * sPt)170 void RTC_GetDateAndTime(S_RTC_TIME_DATA_T *sPt)
171 {
172     uint32_t u32Tmp;
173 
174     sPt->u32TimeScale = RTC->CLKFMT & RTC_CLKFMT_24HEN_Msk;     /* 12/24-hour */
175     sPt->u32DayOfWeek = RTC->WEEKDAY & RTC_WEEKDAY_WEEKDAY_Msk; /* Day of the week */
176 
177     /* Get [Date digit] data */
178     g_u32hiYear  = (RTC->CAL & RTC_CAL_TENYEAR_Msk) >> RTC_CAL_TENYEAR_Pos;
179     g_u32loYear  = (RTC->CAL & RTC_CAL_YEAR_Msk) >> RTC_CAL_YEAR_Pos;
180     g_u32hiMonth = (RTC->CAL & RTC_CAL_TENMON_Msk) >> RTC_CAL_TENMON_Pos;
181     g_u32loMonth = (RTC->CAL & RTC_CAL_MON_Msk) >> RTC_CAL_MON_Pos;
182     g_u32hiDay   = (RTC->CAL & RTC_CAL_TENDAY_Msk) >> RTC_CAL_TENDAY_Pos;
183     g_u32loDay   = (RTC->CAL & RTC_CAL_DAY_Msk) >> RTC_CAL_DAY_Pos;
184 
185     /* Get [Time digit] data */
186     g_u32hiHour = (RTC->TIME & RTC_TIME_TENHR_Msk) >> RTC_TIME_TENHR_Pos;
187     g_u32loHour = (RTC->TIME & RTC_TIME_HR_Msk) >> RTC_TIME_HR_Pos;
188     g_u32hiMin  = (RTC->TIME & RTC_TIME_TENMIN_Msk) >> RTC_TIME_TENMIN_Pos;
189     g_u32loMin  = (RTC->TIME & RTC_TIME_MIN_Msk) >> RTC_TIME_MIN_Pos;
190     g_u32hiSec  = (RTC->TIME & RTC_TIME_TENSEC_Msk) >> RTC_TIME_TENSEC_Pos;
191     g_u32loSec  = (RTC->TIME & RTC_TIME_SEC_Msk) >> RTC_TIME_SEC_Pos;
192 
193     /* Compute to 20XX year */
194     u32Tmp  = (g_u32hiYear * 10ul);
195     u32Tmp += g_u32loYear;
196     sPt->u32Year = u32Tmp + RTC_YEAR2000;
197 
198     /* Compute 0~12 month */
199     u32Tmp = (g_u32hiMonth * 10ul);
200     sPt->u32Month = u32Tmp + g_u32loMonth;
201 
202     /* Compute 0~31 day */
203     u32Tmp = (g_u32hiDay * 10ul);
204     sPt->u32Day =  u32Tmp  + g_u32loDay;
205 
206     /* Compute 12/24 hour */
207     if(sPt->u32TimeScale == RTC_CLOCK_12)
208     {
209         u32Tmp = (g_u32hiHour * 10ul);
210         u32Tmp += g_u32loHour;
211         sPt->u32Hour = u32Tmp;          /* AM: 1~12. PM: 21~32. */
212 
213         if(sPt->u32Hour >= 21ul)
214         {
215             sPt->u32AmPm  = RTC_PM;
216             sPt->u32Hour -= 20ul;
217         }
218         else
219         {
220             sPt->u32AmPm = RTC_AM;
221         }
222 
223         u32Tmp  = (g_u32hiMin  * 10ul);
224         u32Tmp += g_u32loMin;
225         sPt->u32Minute = u32Tmp;
226 
227         u32Tmp  = (g_u32hiSec  * 10ul);
228         u32Tmp += g_u32loSec;
229         sPt->u32Second = u32Tmp;
230     }
231     else
232     {
233         u32Tmp  = (g_u32hiHour * 10ul);
234         u32Tmp += g_u32loHour;
235         sPt->u32Hour = u32Tmp;
236 
237         u32Tmp  = (g_u32hiMin * 10ul);
238         u32Tmp +=  g_u32loMin;
239         sPt->u32Minute = u32Tmp;
240 
241         u32Tmp  = (g_u32hiSec * 10ul);
242         u32Tmp += g_u32loSec;
243         sPt->u32Second = u32Tmp;
244     }
245 }
246 
247 /**
248   * @brief      Get RTC Alarm Date and Time
249   *
250   * @param[out] sPt     The returned pointer is specified the RTC alarm value. It includes: \n
251   *                     u32Year: Year value                                                 \n
252   *                     u32Month: Month value                                               \n
253   *                     u32Day: Day value                                                   \n
254   *                     u32DayOfWeek: Day of week                                           \n
255   *                     u32Hour: Hour value                                                 \n
256   *                     u32Minute: Minute value                                             \n
257   *                     u32Second: Second value                                             \n
258   *                     u32TimeScale: [RTC_CLOCK_12 / RTC_CLOCK_24]                         \n
259   *                     u8AmPm: [RTC_AM / RTC_PM]                                           \n
260   *
261   * @return     None
262   *
263   * @details    This API is used to get the RTC alarm date and time setting.
264   */
RTC_GetAlarmDateAndTime(S_RTC_TIME_DATA_T * sPt)265 void RTC_GetAlarmDateAndTime(S_RTC_TIME_DATA_T *sPt)
266 {
267     uint32_t u32Tmp;
268 
269     sPt->u32TimeScale = RTC->CLKFMT & RTC_CLKFMT_24HEN_Msk;     /* 12/24-hour */
270     sPt->u32DayOfWeek = RTC->WEEKDAY & RTC_WEEKDAY_WEEKDAY_Msk; /* Day of the week */
271 
272     /* Get alarm [Date digit] data */
273     RTC_WaitAccessEnable();
274     g_u32hiYear  = (RTC->CALM & RTC_CALM_TENYEAR_Msk) >> RTC_CALM_TENYEAR_Pos;
275     g_u32loYear  = (RTC->CALM & RTC_CALM_YEAR_Msk) >> RTC_CALM_YEAR_Pos;
276     g_u32hiMonth = (RTC->CALM & RTC_CALM_TENMON_Msk) >> RTC_CALM_TENMON_Pos;
277     g_u32loMonth = (RTC->CALM & RTC_CALM_MON_Msk) >> RTC_CALM_MON_Pos;
278     g_u32hiDay   = (RTC->CALM & RTC_CALM_TENDAY_Msk) >> RTC_CALM_TENDAY_Pos;
279     g_u32loDay   = (RTC->CALM & RTC_CALM_DAY_Msk) >> RTC_CALM_DAY_Pos;
280 
281     /* Get alarm [Time digit] data */
282     RTC_WaitAccessEnable();
283     g_u32hiHour = (RTC->TALM & RTC_TALM_TENHR_Msk) >> RTC_TALM_TENHR_Pos;
284     g_u32loHour = (RTC->TALM & RTC_TALM_HR_Msk) >> RTC_TALM_HR_Pos;
285     g_u32hiMin  = (RTC->TALM & RTC_TALM_TENMIN_Msk) >> RTC_TALM_TENMIN_Pos;
286     g_u32loMin  = (RTC->TALM & RTC_TALM_MIN_Msk) >> RTC_TALM_MIN_Pos;
287     g_u32hiSec  = (RTC->TALM & RTC_TALM_TENSEC_Msk) >> RTC_TALM_TENSEC_Pos;
288     g_u32loSec  = (RTC->TALM & RTC_TALM_SEC_Msk) >> RTC_TALM_SEC_Pos;
289 
290     /* Compute to 20XX year */
291     u32Tmp  = (g_u32hiYear * 10ul);
292     u32Tmp += g_u32loYear;
293     sPt->u32Year = u32Tmp + RTC_YEAR2000;
294 
295     /* Compute 0~12 month */
296     u32Tmp = (g_u32hiMonth * 10ul);
297     sPt->u32Month = u32Tmp + g_u32loMonth;
298 
299     /* Compute 0~31 day */
300     u32Tmp = (g_u32hiDay * 10ul);
301     sPt->u32Day = u32Tmp + g_u32loDay;
302 
303     /* Compute 12/24 hour */
304     if(sPt->u32TimeScale == RTC_CLOCK_12)
305     {
306         u32Tmp  = (g_u32hiHour * 10ul);
307         u32Tmp += g_u32loHour;
308         sPt->u32Hour = u32Tmp;          /* AM: 1~12. PM: 21~32. */
309 
310         if(sPt->u32Hour >= 21ul)
311         {
312             sPt->u32AmPm  = RTC_PM;
313             sPt->u32Hour -= 20ul;
314         }
315         else
316         {
317             sPt->u32AmPm = RTC_AM;
318         }
319 
320         u32Tmp  = (g_u32hiMin * 10ul);
321         u32Tmp += g_u32loMin;
322         sPt->u32Minute = u32Tmp;
323 
324         u32Tmp  = (g_u32hiSec * 10ul);
325         u32Tmp += g_u32loSec;
326         sPt->u32Second = u32Tmp;
327 
328     }
329     else
330     {
331         u32Tmp  = (g_u32hiHour * 10ul);
332         u32Tmp +=  g_u32loHour;
333         sPt->u32Hour = u32Tmp;
334 
335         u32Tmp  = (g_u32hiMin * 10ul);
336         u32Tmp += g_u32loMin;
337         sPt->u32Minute = u32Tmp;
338 
339         u32Tmp  = (g_u32hiSec * 10ul);
340         u32Tmp += g_u32loSec;
341         sPt->u32Second = u32Tmp;
342     }
343 }
344 
345 /**
346   * @brief      Update Current RTC Date and Time
347   *
348   * @param[in]  sPt     Specify the time property and current date and time. It includes:           \n
349   *                     u32Year: Year value, range between 2000 ~ 2099.                             \n
350   *                     u32Month: Month value, range between 1 ~ 12.                                \n
351   *                     u32Day: Day value, range between 1 ~ 31.                                    \n
352   *                     u32DayOfWeek: Day of the week. [RTC_SUNDAY / RTC_MONDAY / RTC_TUESDAY /
353   *                                                     RTC_WEDNESDAY / RTC_THURSDAY / RTC_FRIDAY /
354   *                                                     RTC_SATURDAY]                               \n
355   *                     u32Hour: Hour value, range between 0 ~ 23.                                  \n
356   *                     u32Minute: Minute value, range between 0 ~ 59.                              \n
357   *                     u32Second: Second value, range between 0 ~ 59.                              \n
358   *                     u32TimeScale: [RTC_CLOCK_12 / RTC_CLOCK_24]                                 \n
359   *                     u8AmPm: [RTC_AM / RTC_PM]                                                   \n
360   *
361   * @return     None
362   *
363   * @details    This API is used to update current date and time to RTC.
364   */
RTC_SetDateAndTime(S_RTC_TIME_DATA_T * sPt)365 void RTC_SetDateAndTime(S_RTC_TIME_DATA_T *sPt)
366 {
367     uint32_t u32RegCAL, u32RegTIME;
368 
369     if(sPt == 0ul)
370     {
371     }
372     else
373     {
374         /*-----------------------------------------------------------------------------------------------------*/
375         /* Set RTC 24/12 hour setting and Day of the Week                                                      */
376         /*-----------------------------------------------------------------------------------------------------*/
377         RTC_WaitAccessEnable();
378         if(sPt->u32TimeScale == RTC_CLOCK_12)
379         {
380             RTC->CLKFMT &= ~RTC_CLKFMT_24HEN_Msk;
381 
382             /*-------------------------------------------------------------------------------------------------*/
383             /* Important, range of 12-hour PM mode is 21 up to 32                                               */
384             /*-------------------------------------------------------------------------------------------------*/
385             if(sPt->u32AmPm == RTC_PM)
386             {
387                 sPt->u32Hour += 20ul;
388             }
389         }
390         else
391         {
392             RTC->CLKFMT |= RTC_CLKFMT_24HEN_Msk;
393         }
394 
395         /* Set Day of the Week */
396         RTC_WaitAccessEnable();
397         RTC->WEEKDAY = sPt->u32DayOfWeek;
398 
399         /*-----------------------------------------------------------------------------------------------------*/
400         /* Set RTC Current Date and Time                                                                       */
401         /*-----------------------------------------------------------------------------------------------------*/
402         u32RegCAL  = ((sPt->u32Year - RTC_YEAR2000) / 10ul) << 20;
403         u32RegCAL |= (((sPt->u32Year - RTC_YEAR2000) % 10ul) << 16);
404         u32RegCAL |= ((sPt->u32Month  / 10ul) << 12);
405         u32RegCAL |= ((sPt->u32Month  % 10ul) << 8);
406         u32RegCAL |= ((sPt->u32Day    / 10ul) << 4);
407         u32RegCAL |= (sPt->u32Day     % 10ul);
408 
409         u32RegTIME  = ((sPt->u32Hour   / 10ul) << 20);
410         u32RegTIME |= ((sPt->u32Hour   % 10ul) << 16);
411         u32RegTIME |= ((sPt->u32Minute / 10ul) << 12);
412         u32RegTIME |= ((sPt->u32Minute % 10ul) << 8);
413         u32RegTIME |= ((sPt->u32Second / 10ul) << 4);
414         u32RegTIME |= (sPt->u32Second % 10ul);
415 
416         /*-----------------------------------------------------------------------------------------------------*/
417         /* Set RTC Calender and Time Loading                                                                   */
418         /*-----------------------------------------------------------------------------------------------------*/
419         RTC_WaitAccessEnable();
420         RTC->CAL  = (uint32_t)u32RegCAL;
421         RTC_WaitAccessEnable();
422         RTC->TIME = (uint32_t)u32RegTIME;
423     }
424 }
425 
426 /**
427   * @brief      Update RTC Alarm Date and Time
428   *
429   * @param[in]  sPt     Specify the time property and alarm date and time. It includes:             \n
430   *                     u32Year: Year value, range between 2000 ~ 2099.                             \n
431   *                     u32Month: Month value, range between 1 ~ 12.                                \n
432   *                     u32Day: Day value, range between 1 ~ 31.                                    \n
433   *                     u32DayOfWeek: Day of the week. [RTC_SUNDAY / RTC_MONDAY / RTC_TUESDAY /
434   *                                                     RTC_WEDNESDAY / RTC_THURSDAY / RTC_FRIDAY /
435   *                                                     RTC_SATURDAY]                               \n
436   *                     u32Hour: Hour value, range between 0 ~ 23.                                  \n
437   *                     u32Minute: Minute value, range between 0 ~ 59.                              \n
438   *                     u32Second: Second value, range between 0 ~ 59.                              \n
439   *                     u32TimeScale: [RTC_CLOCK_12 / RTC_CLOCK_24]                                 \n
440   *                     u8AmPm: [RTC_AM / RTC_PM]                                                   \n
441   *
442   * @return     None
443   *
444   * @details    This API is used to update alarm date and time setting to RTC.
445   */
RTC_SetAlarmDateAndTime(S_RTC_TIME_DATA_T * sPt)446 void RTC_SetAlarmDateAndTime(S_RTC_TIME_DATA_T *sPt)
447 {
448     uint32_t u32RegCALM, u32RegTALM;
449 
450     if(sPt == 0)
451     {
452     }
453     else
454     {
455         /*-----------------------------------------------------------------------------------------------------*/
456         /* Set RTC 24/12 hour setting and Day of the Week                                                      */
457         /*-----------------------------------------------------------------------------------------------------*/
458         RTC_WaitAccessEnable();
459         if(sPt->u32TimeScale == RTC_CLOCK_12)
460         {
461             RTC->CLKFMT &= ~RTC_CLKFMT_24HEN_Msk;
462 
463             /*-------------------------------------------------------------------------------------------------*/
464             /* Important, range of 12-hour PM mode is 21 up to 32                                               */
465             /*-------------------------------------------------------------------------------------------------*/
466             if(sPt->u32AmPm == RTC_PM)
467             {
468                 sPt->u32Hour += 20ul;
469             }
470         }
471         else
472         {
473             RTC->CLKFMT |= RTC_CLKFMT_24HEN_Msk;
474         }
475 
476         /*-----------------------------------------------------------------------------------------------------*/
477         /* Set RTC Alarm Date and Time                                                                         */
478         /*-----------------------------------------------------------------------------------------------------*/
479         u32RegCALM  = ((sPt->u32Year - RTC_YEAR2000) / 10ul) << 20;
480         u32RegCALM |= (((sPt->u32Year - RTC_YEAR2000) % 10ul) << 16);
481         u32RegCALM |= ((sPt->u32Month  / 10ul) << 12);
482         u32RegCALM |= ((sPt->u32Month  % 10ul) << 8);
483         u32RegCALM |= ((sPt->u32Day    / 10ul) << 4);
484         u32RegCALM |= (sPt->u32Day    % 10ul);
485 
486         u32RegTALM  = ((sPt->u32Hour   / 10ul) << 20);
487         u32RegTALM |= ((sPt->u32Hour   % 10ul) << 16);
488         u32RegTALM |= ((sPt->u32Minute / 10ul) << 12);
489         u32RegTALM |= ((sPt->u32Minute % 10ul) << 8);
490         u32RegTALM |= ((sPt->u32Second / 10ul) << 4);
491         u32RegTALM |= (sPt->u32Second % 10ul);
492 
493         RTC_WaitAccessEnable();
494         RTC->CALM = (uint32_t)u32RegCALM;
495         RTC_WaitAccessEnable();
496         RTC->TALM = (uint32_t)u32RegTALM;
497     }
498 }
499 
500 /**
501   * @brief      Update RTC Current Date
502   *
503   * @param[in]  u32Year         The year calendar digit of current RTC setting.
504   * @param[in]  u32Month        The month calendar digit of current RTC setting.
505   * @param[in]  u32Day          The day calendar digit of current RTC setting.
506   * @param[in]  u32DayOfWeek    The Day of the week. [RTC_SUNDAY / RTC_MONDAY / RTC_TUESDAY /
507   *                                                   RTC_WEDNESDAY / RTC_THURSDAY / RTC_FRIDAY /
508   *                                                   RTC_SATURDAY]
509   *
510   * @return     None
511   *
512   * @details    This API is used to update current date to RTC.
513   */
RTC_SetDate(uint32_t u32Year,uint32_t u32Month,uint32_t u32Day,uint32_t u32DayOfWeek)514 void RTC_SetDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day, uint32_t u32DayOfWeek)
515 {
516     uint32_t u32RegCAL;
517 
518     u32RegCAL  = ((u32Year - RTC_YEAR2000) / 10ul) << 20;
519     u32RegCAL |= (((u32Year - RTC_YEAR2000) % 10ul) << 16);
520     u32RegCAL |= ((u32Month / 10ul) << 12);
521     u32RegCAL |= ((u32Month % 10ul) << 8);
522     u32RegCAL |= ((u32Day   / 10ul) << 4);
523     u32RegCAL |= (u32Day   % 10ul);
524 
525     /* Set Day of the Week */
526     RTC_WaitAccessEnable();
527     RTC->WEEKDAY = u32DayOfWeek & RTC_WEEKDAY_WEEKDAY_Msk;
528 
529     /* Set RTC Calender Loading */
530     RTC_WaitAccessEnable();
531     RTC->CAL = (uint32_t)u32RegCAL;
532 }
533 
534 /**
535   * @brief      Update RTC Current Time
536   *
537   * @param[in]  u32Hour         The hour time digit of current RTC setting.
538   * @param[in]  u32Minute       The minute time digit of current RTC setting.
539   * @param[in]  u32Second       The second time digit of current RTC setting.
540   * @param[in]  u32TimeMode     The 24-Hour / 12-Hour Time Scale Selection. [RTC_CLOCK_12 / RTC_CLOCK_24]
541   * @param[in]  u32AmPm         12-hour time scale with AM and PM indication. Only Time Scale select 12-hour used. [RTC_AM / RTC_PM]
542   *
543   * @return     None
544   *
545   * @details    This API is used to update current time to RTC.
546   */
RTC_SetTime(uint32_t u32Hour,uint32_t u32Minute,uint32_t u32Second,uint32_t u32TimeMode,uint32_t u32AmPm)547 void RTC_SetTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm)
548 {
549     uint32_t u32RegTIME;
550 
551     /* Important, range of 12-hour PM mode is 21 up to 32 */
552     if((u32TimeMode == RTC_CLOCK_12) && (u32AmPm == RTC_PM))
553     {
554         u32Hour += 20ul;
555     }
556 
557     u32RegTIME  = ((u32Hour   / 10ul) << 20);
558     u32RegTIME |= ((u32Hour   % 10ul) << 16);
559     u32RegTIME |= ((u32Minute / 10ul) << 12);
560     u32RegTIME |= ((u32Minute % 10ul) << 8);
561     u32RegTIME |= ((u32Second / 10ul) << 4);
562     u32RegTIME |= (u32Second % 10ul);
563 
564     /*-----------------------------------------------------------------------------------------------------*/
565     /* Set RTC 24/12 hour setting and Day of the Week                                                      */
566     /*-----------------------------------------------------------------------------------------------------*/
567     RTC_WaitAccessEnable();
568     if(u32TimeMode == RTC_CLOCK_12)
569     {
570         RTC->CLKFMT &= ~RTC_CLKFMT_24HEN_Msk;
571     }
572     else
573     {
574         RTC->CLKFMT |= RTC_CLKFMT_24HEN_Msk;
575     }
576 
577     RTC_WaitAccessEnable();
578     RTC->TIME = (uint32_t)u32RegTIME;
579 }
580 
581 /**
582   * @brief      Update RTC Alarm Date
583   *
584   * @param[in]  u32Year         The year calendar digit of RTC alarm setting.
585   * @param[in]  u32Month        The month calendar digit of RTC alarm setting.
586   * @param[in]  u32Day          The day calendar digit of RTC alarm setting.
587   *
588   * @return     None
589   *
590   * @details    This API is used to update alarm date setting to RTC.
591   */
RTC_SetAlarmDate(uint32_t u32Year,uint32_t u32Month,uint32_t u32Day)592 void RTC_SetAlarmDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day)
593 {
594     uint32_t u32RegCALM;
595 
596     u32RegCALM  = ((u32Year - RTC_YEAR2000) / 10ul) << 20;
597     u32RegCALM |= (((u32Year - RTC_YEAR2000) % 10ul) << 16);
598     u32RegCALM |= ((u32Month / 10ul) << 12);
599     u32RegCALM |= ((u32Month % 10ul) << 8);
600     u32RegCALM |= ((u32Day   / 10ul) << 4);
601     u32RegCALM |= (u32Day   % 10ul);
602 
603     RTC_WaitAccessEnable();
604 
605     /* Set RTC Alarm Date */
606     RTC->CALM = (uint32_t)u32RegCALM;
607 }
608 
609 /**
610   * @brief      Update RTC Alarm Time
611   *
612   * @param[in]  u32Hour         The hour time digit of RTC alarm setting.
613   * @param[in]  u32Minute       The minute time digit of RTC alarm setting.
614   * @param[in]  u32Second       The second time digit of RTC alarm setting.
615   * @param[in]  u32TimeMode     The 24-Hour / 12-Hour Time Scale Selection. [RTC_CLOCK_12 / RTC_CLOCK_24]
616   * @param[in]  u32AmPm         12-hour time scale with AM and PM indication. Only Time Scale select 12-hour used. [RTC_AM / RTC_PM]
617   *
618   * @return     None
619   *
620   * @details    This API is used to update alarm time setting to RTC.
621   */
RTC_SetAlarmTime(uint32_t u32Hour,uint32_t u32Minute,uint32_t u32Second,uint32_t u32TimeMode,uint32_t u32AmPm)622 void RTC_SetAlarmTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm)
623 {
624     uint32_t u32RegTALM;
625 
626     /* Important, range of 12-hour PM mode is 21 up to 32 */
627     if((u32TimeMode == RTC_CLOCK_12) && (u32AmPm == RTC_PM))
628     {
629         u32Hour += 20ul;
630     }
631 
632     u32RegTALM  = ((u32Hour   / 10ul) << 20);
633     u32RegTALM |= ((u32Hour   % 10ul) << 16);
634     u32RegTALM |= ((u32Minute / 10ul) << 12);
635     u32RegTALM |= ((u32Minute % 10ul) << 8);
636     u32RegTALM |= ((u32Second / 10ul) << 4);
637     u32RegTALM |= (u32Second % 10ul);
638 
639     /*-----------------------------------------------------------------------------------------------------*/
640     /* Set RTC 24/12 hour setting and Day of the Week                                                      */
641     /*-----------------------------------------------------------------------------------------------------*/
642     RTC_WaitAccessEnable();
643     if(u32TimeMode == RTC_CLOCK_12)
644     {
645         RTC->CLKFMT &= ~RTC_CLKFMT_24HEN_Msk;
646     }
647     else
648     {
649         RTC->CLKFMT |= RTC_CLKFMT_24HEN_Msk;
650     }
651 
652     /* Set RTC Alarm Time */
653     RTC_WaitAccessEnable();
654     RTC->TALM = (uint32_t)u32RegTALM;
655 }
656 
657 /**
658   * @brief      Set RTC Alarm Date Mask Function
659   *
660   * @param[in]  u8IsTenYMsk     1: enable 10-Year digit alarm mask; 0: disabled.
661   * @param[in]  u8IsYMsk        1: enable 1-Year digit alarm mask; 0: disabled.
662   * @param[in]  u8IsTenMMsk     1: enable 10-Mon digit alarm mask; 0: disabled.
663   * @param[in]  u8IsMMsk        1: enable 1-Mon digit alarm mask; 0: disabled.
664   * @param[in]  u8IsTenDMsk     1: enable 10-Day digit alarm mask; 0: disabled.
665   * @param[in]  u8IsDMsk        1: enable 1-Day digit alarm mask; 0: disabled.
666   *
667   * @return     None
668   *
669   * @details    This API is used to enable or disable RTC alarm date mask function.
670   */
RTC_SetAlarmDateMask(uint8_t u8IsTenYMsk,uint8_t u8IsYMsk,uint8_t u8IsTenMMsk,uint8_t u8IsMMsk,uint8_t u8IsTenDMsk,uint8_t u8IsDMsk)671 void RTC_SetAlarmDateMask(uint8_t u8IsTenYMsk, uint8_t u8IsYMsk, uint8_t u8IsTenMMsk, uint8_t u8IsMMsk, uint8_t u8IsTenDMsk, uint8_t u8IsDMsk)
672 {
673     RTC_WaitAccessEnable();
674     RTC->CAMSK = ((uint32_t)u8IsTenYMsk << RTC_CAMSK_MTENYEAR_Pos) |
675                  ((uint32_t)u8IsYMsk    << RTC_CAMSK_MYEAR_Pos) |
676                  ((uint32_t)u8IsTenMMsk << RTC_CAMSK_MTENMON_Pos) |
677                  ((uint32_t)u8IsMMsk    << RTC_CAMSK_MMON_Pos) |
678                  ((uint32_t)u8IsTenDMsk << RTC_CAMSK_MTENDAY_Pos) |
679                  ((uint32_t)u8IsDMsk    << RTC_CAMSK_MDAY_Pos);
680 }
681 
682 /**
683   * @brief      Set RTC Alarm Time Mask Function
684   *
685   * @param[in]  u8IsTenHMsk     1: enable 10-Hour digit alarm mask; 0: disabled.
686   * @param[in]  u8IsHMsk        1: enable 1-Hour digit alarm mask; 0: disabled.
687   * @param[in]  u8IsTenMMsk     1: enable 10-Min digit alarm mask; 0: disabled.
688   * @param[in]  u8IsMMsk        1: enable 1-Min digit alarm mask; 0: disabled.
689   * @param[in]  u8IsTenSMsk     1: enable 10-Sec digit alarm mask; 0: disabled.
690   * @param[in]  u8IsSMsk        1: enable 1-Sec digit alarm mask; 0: disabled.
691   *
692   * @return     None
693   *
694   * @details    This API is used to enable or disable RTC alarm time mask function.
695   */
RTC_SetAlarmTimeMask(uint8_t u8IsTenHMsk,uint8_t u8IsHMsk,uint8_t u8IsTenMMsk,uint8_t u8IsMMsk,uint8_t u8IsTenSMsk,uint8_t u8IsSMsk)696 void RTC_SetAlarmTimeMask(uint8_t u8IsTenHMsk, uint8_t u8IsHMsk, uint8_t u8IsTenMMsk, uint8_t u8IsMMsk, uint8_t u8IsTenSMsk, uint8_t u8IsSMsk)
697 {
698     RTC_WaitAccessEnable();
699     RTC->TAMSK = ((uint32_t)u8IsTenHMsk << RTC_TAMSK_MTENHR_Pos) |
700                  ((uint32_t)u8IsHMsk    << RTC_TAMSK_MHR_Pos) |
701                  ((uint32_t)u8IsTenMMsk << RTC_TAMSK_MTENMIN_Pos) |
702                  ((uint32_t)u8IsMMsk    << RTC_TAMSK_MMIN_Pos) |
703                  ((uint32_t)u8IsTenSMsk << RTC_TAMSK_MTENSEC_Pos) |
704                  ((uint32_t)u8IsSMsk    << RTC_TAMSK_MSEC_Pos);
705 }
706 
707 /**
708   * @brief      Get Day of the Week
709   *
710   * @param      None
711   *
712   * @retval     0   Sunday
713   * @retval     1   Monday
714   * @retval     2   Tuesday
715   * @retval     3   Wednesday
716   * @retval     4   Thursday
717   * @retval     5   Friday
718   * @retval     6   Saturday
719   *
720   * @details    This API is used to get day of the week of current RTC date.
721   */
RTC_GetDayOfWeek(void)722 uint32_t RTC_GetDayOfWeek(void)
723 {
724     return (RTC->WEEKDAY & RTC_WEEKDAY_WEEKDAY_Msk);
725 }
726 
727 /**
728   * @brief      Set RTC Tick Period Time
729   *
730   * @param[in]  u32TickSelection    It is used to set the RTC tick period time for Periodic Time Tick request. \n
731   *                                 It consists of:
732   *                                     - \ref RTC_TICK_1_SEC     : Time tick is 1 second
733   *                                     - \ref RTC_TICK_1_2_SEC   : Time tick is 1/2 second
734   *                                     - \ref RTC_TICK_1_4_SEC   : Time tick is 1/4 second
735   *                                     - \ref RTC_TICK_1_8_SEC   : Time tick is 1/8 second
736   *                                     - \ref RTC_TICK_1_16_SEC  : Time tick is 1/16 second
737   *                                     - \ref RTC_TICK_1_32_SEC  : Time tick is 1/32 second
738   *                                     - \ref RTC_TICK_1_64_SEC  : Time tick is 1/64 second
739   *                                     - \ref RTC_TICK_1_128_SEC : Time tick is 1/128 second
740   *
741   * @return     None
742   *
743   * @details    This API is used to set RTC tick period time for each tick interrupt.
744   */
RTC_SetTickPeriod(uint32_t u32TickSelection)745 void RTC_SetTickPeriod(uint32_t u32TickSelection)
746 {
747     RTC_WaitAccessEnable();
748 
749     RTC->TICK = (RTC->TICK & ~RTC_TICK_TICK_Msk) | u32TickSelection;
750 }
751 
752 /**
753   * @brief      Enable RTC Interrupt
754   *
755   * @param[in]  u32IntFlagMask      Specify the interrupt source. It consists of:
756   *                                     - \ref RTC_INTEN_ALMIEN_Msk   : Alarm interrupt
757   *                                     - \ref RTC_INTEN_TICKIEN_Msk  : Tick interrupt
758   *                                     - \ref RTC_INTEN_TAMP0IEN_Msk : Tamper 0 Pin Event Detection interrupt
759   *                                     - \ref RTC_INTEN_TAMP1IEN_Msk : Tamper 1 Pin Event Detection interrupt
760   *                                     - \ref RTC_INTEN_TAMP2IEN_Msk : Tamper 2 Pin Event Detection interrupt
761   *                                     - \ref RTC_INTEN_TAMP3IEN_Msk : Tamper 3 Pin Event Detection interrupt
762   *                                     - \ref RTC_INTEN_TAMP4IEN_Msk : Tamper 4 Pin Event Detection interrupt
763   *                                     - \ref RTC_INTEN_TAMP5IEN_Msk : Tamper 5 Pin Event Detection interrupt
764   *
765   * @return     None
766   *
767   * @details    This API is used to enable the specify RTC interrupt function.
768   */
RTC_EnableInt(uint32_t u32IntFlagMask)769 void RTC_EnableInt(uint32_t u32IntFlagMask)
770 {
771     RTC_WaitAccessEnable();
772     RTC->INTEN |= u32IntFlagMask;
773 }
774 
775 /**
776   * @brief      Disable RTC Interrupt
777   *
778   * @param[in]  u32IntFlagMask      Specify the interrupt source. It consists of:
779   *                                     - \ref RTC_INTEN_ALMIEN_Msk   : Alarm interrupt
780   *                                     - \ref RTC_INTEN_TICKIEN_Msk  : Tick interrupt
781   *                                     - \ref RTC_INTEN_TAMP0IEN_Msk : Tamper 0 Pin Event Detection interrupt
782   *                                     - \ref RTC_INTEN_TAMP1IEN_Msk : Tamper 1 Pin Event Detection interrupt
783   *                                     - \ref RTC_INTEN_TAMP2IEN_Msk : Tamper 2 Pin Event Detection interrupt
784   *                                     - \ref RTC_INTEN_TAMP3IEN_Msk : Tamper 3 Pin Event Detection interrupt
785   *                                     - \ref RTC_INTEN_TAMP4IEN_Msk : Tamper 4 Pin Event Detection interrupt
786   *                                     - \ref RTC_INTEN_TAMP5IEN_Msk : Tamper 5 Pin Event Detection interrupt
787   *
788   * @return     None
789   *
790   * @details    This API is used to disable the specify RTC interrupt function.
791   */
RTC_DisableInt(uint32_t u32IntFlagMask)792 void RTC_DisableInt(uint32_t u32IntFlagMask)
793 {
794     RTC_WaitAccessEnable();
795     RTC->INTEN  &= ~u32IntFlagMask;
796     RTC_WaitAccessEnable();
797     RTC->INTSTS = u32IntFlagMask;
798 }
799 
800 /**
801   * @brief      Enable Spare Registers Access
802   *
803   * @param      None
804   *
805   * @return     None
806   *
807   * @details    This API is used to enable the spare registers 0~19 can be accessed.
808   */
RTC_EnableSpareAccess(void)809 void RTC_EnableSpareAccess(void)
810 {
811     RTC_WaitAccessEnable();
812 
813     RTC->SPRCTL |= RTC_SPRCTL_SPRRWEN_Msk;
814 }
815 
816 /**
817   * @brief      Disable Spare Register
818   *
819   * @param      None
820   *
821   * @return     None
822   *
823   * @details    This API is used to disable the spare register 0~19 cannot be accessed.
824   */
RTC_DisableSpareRegister(void)825 void RTC_DisableSpareRegister(void)
826 {
827     RTC_WaitAccessEnable();
828 
829     RTC->SPRCTL &= ~RTC_SPRCTL_SPRRWEN_Msk;
830 }
831 
832 /**
833   * @brief      Static Tamper Detect
834   *
835   * @param[in]  u32TamperSelect     Tamper pin select. Possible options are
836   *                                 - \ref RTC_TAMPER5_SELECT
837   *                                 - \ref RTC_TAMPER4_SELECT
838   *                                 - \ref RTC_TAMPER3_SELECT
839   *                                 - \ref RTC_TAMPER2_SELECT
840   *                                 - \ref RTC_TAMPER1_SELECT
841   *                                 - \ref RTC_TAMPER0_SELECT
842   *
843   * @param[in]  u32DetecLevel       Tamper pin detection level select. Possible options are
844   *                                 - \ref RTC_TAMPER_HIGH_LEVEL_DETECT
845   *                                 - \ref RTC_TAMPER_LOW_LEVEL_DETECT
846   *
847   * @param[in]  u32DebounceEn       Tamper pin de-bounce enable
848   *                                 - \ref RTC_TAMPER_DEBOUNCE_ENABLE
849   *                                 - \ref RTC_TAMPER_DEBOUNCE_DISABLE
850   *
851   * @return     None
852   *
853   * @details    This API is used to enable the tamper pin detect function with specify trigger condition.
854   */
RTC_StaticTamperEnable(uint32_t u32TamperSelect,uint32_t u32DetecLevel,uint32_t u32DebounceEn)855 void RTC_StaticTamperEnable(uint32_t u32TamperSelect, uint32_t u32DetecLevel, uint32_t u32DebounceEn)
856 {
857     uint32_t i;
858     uint32_t u32Reg;
859     uint32_t u32TmpReg;
860 
861     RTC_WaitAccessEnable();
862     u32Reg = RTC->TAMPCTL;
863 
864     u32TmpReg = ( RTC_TAMPCTL_TAMP0EN_Msk | (u32DetecLevel << RTC_TAMPCTL_TAMP0LV_Pos) |
865                   (u32DebounceEn << RTC_TAMPCTL_TAMP0DBEN_Pos) );
866 
867     for(i = 0ul; i < MAX_TAMPER_PIN_NUM; i++)
868     {
869         if(u32TamperSelect & (0x1ul << i))
870         {
871             u32Reg &= ~((RTC_TAMPCTL_TAMP0EN_Msk|RTC_TAMPCTL_TAMP0LV_Msk|RTC_TAMPCTL_TAMP0DBEN_Msk) << (i*4ul));
872             u32Reg |= (u32TmpReg << (i*4ul));
873         }
874     }
875 
876     RTC_WaitAccessEnable();
877     RTC->TAMPCTL = u32Reg;
878 
879 }
880 
881 /**
882   * @brief      Static Tamper Disable
883   *
884   * @param[in]  u32TamperSelect     Tamper pin select. Possible options are
885   *                                 - \ref RTC_TAMPER5_SELECT
886   *                                 - \ref RTC_TAMPER4_SELECT
887   *                                 - \ref RTC_TAMPER3_SELECT
888   *                                 - \ref RTC_TAMPER2_SELECT
889   *                                 - \ref RTC_TAMPER1_SELECT
890   *                                 - \ref RTC_TAMPER0_SELECT
891   *
892   * @return     None
893   *
894   * @details    This API is used to disable the static tamper pin detect.
895   */
RTC_StaticTamperDisable(uint32_t u32TamperSelect)896 void RTC_StaticTamperDisable(uint32_t u32TamperSelect)
897 {
898     uint32_t i;
899     uint32_t u32Reg;
900     uint32_t u32TmpReg;
901 
902     RTC_WaitAccessEnable();
903     u32Reg = RTC->TAMPCTL;
904 
905     u32TmpReg = (RTC_TAMPCTL_TAMP0EN_Msk);
906 
907     for(i = 0ul; i < MAX_TAMPER_PIN_NUM; i++)
908     {
909         if(u32TamperSelect & (0x1ul << i))
910         {
911             u32Reg &= ~(u32TmpReg << (i*4ul));
912         }
913     }
914 
915     RTC_WaitAccessEnable();
916     RTC->TAMPCTL = u32Reg;
917 }
918 
919 /**
920   * @brief      Dynamic Tamper Detect
921   *
922   * @param[in]  u32PairSel          Tamper pin detection enable. Possible options are
923   *                                 - \ref RTC_PAIR0_SELECT
924   *                                 - \ref RTC_PAIR1_SELECT
925   *                                 - \ref RTC_PAIR2_SELECT
926   *
927   * @param[in]  u32DebounceEn       Tamper pin de-bounce enable
928   *                                 - \ref RTC_TAMPER_DEBOUNCE_ENABLE
929   *                                 - \ref RTC_TAMPER_DEBOUNCE_DISABLE
930   *
931   *  @param[in]  u32Pair1Source     Dynamic Pair 1 Input Source Select
932   *                                 0: Pair 1 source select tamper 2
933   *                                 1: Pair 1 source select tamper 0
934   *
935   *  @param[in]  u32Pair2Source     Dynamic Pair 2 Input Source Select
936   *                                 0: Pair 2 source select tamper 4
937   *                                 1: Pair 2 source select tamper 0
938   *
939   * @return     None
940   *
941   * @details    This API is used to enable the dynamic tamper.
942   */
RTC_DynamicTamperEnable(uint32_t u32PairSel,uint32_t u32DebounceEn,uint32_t u32Pair1Source,uint32_t u32Pair2Source)943 void RTC_DynamicTamperEnable(uint32_t u32PairSel, uint32_t u32DebounceEn, uint32_t u32Pair1Source, uint32_t u32Pair2Source)
944 {
945     uint32_t i;
946     uint32_t u32Reg;
947     uint32_t u32TmpReg;
948     uint32_t u32Tamper2Debounce, u32Tamper4Debounce;
949 
950     RTC_WaitAccessEnable();
951     u32Reg = RTC->TAMPCTL;
952 
953     u32Tamper2Debounce = u32Reg & RTC_TAMPCTL_TAMP2DBEN_Msk;
954     u32Tamper4Debounce = u32Reg & RTC_TAMPCTL_TAMP4DBEN_Msk;
955 
956     u32Reg &= ~(RTC_TAMPCTL_TAMP0EN_Msk | RTC_TAMPCTL_TAMP1EN_Msk | RTC_TAMPCTL_TAMP2EN_Msk |
957                 RTC_TAMPCTL_TAMP3EN_Msk | RTC_TAMPCTL_TAMP4EN_Msk | RTC_TAMPCTL_TAMP5EN_Msk);
958     u32Reg &= ~(RTC_TAMPCTL_DYN1ISS_Msk | RTC_TAMPCTL_DYN2ISS_Msk);
959     u32Reg |= ((u32Pair1Source & 0x1ul) << RTC_TAMPCTL_DYN1ISS_Pos) | ((u32Pair2Source & 0x1ul) << RTC_TAMPCTL_DYN2ISS_Pos);
960 
961     if(u32DebounceEn)
962     {
963         u32TmpReg = (RTC_TAMPCTL_TAMP0EN_Msk | RTC_TAMPCTL_TAMP1EN_Msk |
964                      RTC_TAMPCTL_TAMP0DBEN_Msk | RTC_TAMPCTL_TAMP1DBEN_Msk | RTC_TAMPCTL_DYNPR0EN_Msk);
965     }
966     else
967     {
968         u32TmpReg = (RTC_TAMPCTL_TAMP0EN_Msk | RTC_TAMPCTL_TAMP1EN_Msk | RTC_TAMPCTL_DYNPR0EN_Msk);
969     }
970 
971     for(i = 0ul; i < MAX_PAIR_NUM; i++)
972     {
973         if(u32PairSel & (0x1ul << i))
974         {
975             u32Reg &= ~((RTC_TAMPCTL_TAMP0DBEN_Msk | RTC_TAMPCTL_TAMP1DBEN_Msk) << (i*8ul));
976             u32Reg |= (u32TmpReg << (i*8ul));
977         }
978     }
979 
980     if((u32Pair1Source) && (u32PairSel & RTC_PAIR1_SELECT))
981     {
982         u32Reg &= ~RTC_TAMPCTL_TAMP2EN_Msk;
983         u32Reg |= u32Tamper2Debounce;
984     }
985 
986     if((u32Pair2Source) && (u32PairSel & RTC_PAIR2_SELECT))
987     {
988         u32Reg &= ~RTC_TAMPCTL_TAMP4EN_Msk;
989         u32Reg |= u32Tamper4Debounce;
990     }
991 
992     RTC_WaitAccessEnable();
993     RTC->TAMPCTL = u32Reg;
994 }
995 
996 /**
997   * @brief      Dynamic Tamper Disable
998   *
999   * @param[in]  u32PairSel          Tamper pin detection enable. Possible options are
1000   *                                 - \ref RTC_PAIR0_SELECT
1001   *                                 - \ref RTC_PAIR1_SELECT
1002   *                                 - \ref RTC_PAIR2_SELECT
1003   *
1004   * @return     None
1005   *
1006   * @details    This API is used to disable the dynamic tamper.
1007   */
RTC_DynamicTamperDisable(uint32_t u32PairSel)1008 void RTC_DynamicTamperDisable(uint32_t u32PairSel)
1009 {
1010     uint32_t i;
1011     uint32_t u32Reg;
1012     uint32_t u32TmpReg;
1013     uint32_t u32Tamper2En = 0ul, u32Tamper4En = 0ul;
1014 
1015     RTC_WaitAccessEnable();
1016     u32Reg = RTC->TAMPCTL;
1017 
1018     if((u32Reg & RTC_TAMPCTL_DYN1ISS_Msk) && (u32PairSel & RTC_PAIR1_SELECT))
1019     {
1020         u32Tamper2En = u32Reg & RTC_TAMPCTL_TAMP2EN_Msk;
1021     }
1022 
1023     if((u32Reg & RTC_TAMPCTL_DYN2ISS_Msk) && (u32PairSel & RTC_PAIR2_SELECT))
1024     {
1025         u32Tamper4En = u32Reg & RTC_TAMPCTL_TAMP4EN_Msk;
1026     }
1027 
1028     u32TmpReg = (RTC_TAMPCTL_TAMP0EN_Msk | RTC_TAMPCTL_TAMP1EN_Msk | RTC_TAMPCTL_DYNPR0EN_Msk);
1029 
1030     for(i = 0ul; i < MAX_PAIR_NUM; i++)
1031     {
1032         if(u32PairSel & (0x1ul << i))
1033         {
1034             u32Reg &= ~(u32TmpReg << ((i*8ul)));
1035         }
1036     }
1037 
1038     u32Reg |= (u32Tamper2En | u32Tamper4En);
1039 
1040     RTC_WaitAccessEnable();
1041     RTC->TAMPCTL = u32Reg;
1042 }
1043 
1044 /**
1045   * @brief      Config dynamic tamper
1046   *
1047   * @param[in]  u32ChangeRate       The dynamic tamper output change rate
1048   *                                 - \ref RTC_2POW10_CLK
1049   *                                 - \ref RTC_2POW11_CLK
1050   *                                 - \ref RTC_2POW12_CLK
1051   *                                 - \ref RTC_2POW13_CLK
1052   *                                 - \ref RTC_2POW14_CLK
1053   *                                 - \ref RTC_2POW15_CLK
1054   *                                 - \ref RTC_2POW16_CLK
1055   *                                 - \ref RTC_2POW17_CLK
1056   *
1057   * @param[in]  u32SeedReload       Reload new seed or not
1058   *                                 0: not reload new seed
1059   *                                 1: reload new seed
1060   *
1061   * @param[in]  u32RefPattern       Reference pattern
1062   *                                 - \ref REF_RANDOM_PATTERN
1063   *                                 - \ref REF_PREVIOUS_PATTERN
1064   *                                 - \ref REF_SEED
1065   *
1066   * @param[in]  u32Seed             Seed Value (0x0 ~ 0xFFFFFFFF)
1067   *
1068   * @return     None
1069   *
1070   * @details    This API is used to config dynamic tamper setting.
1071   */
RTC_DynamicTamperConfig(uint32_t u32ChangeRate,uint32_t u32SeedReload,uint32_t u32RefPattern,uint32_t u32Seed)1072 void RTC_DynamicTamperConfig(uint32_t u32ChangeRate, uint32_t u32SeedReload, uint32_t u32RefPattern, uint32_t u32Seed)
1073 {
1074     uint32_t u32Reg;
1075     RTC_WaitAccessEnable();
1076     u32Reg = RTC->TAMPCTL;
1077 
1078     u32Reg &= ~(RTC_TAMPCTL_DYNSRC_Msk | RTC_TAMPCTL_SEEDRLD_Msk | RTC_TAMPCTL_DYNRATE_Msk);
1079 
1080     u32Reg |= (u32ChangeRate) | ((u32SeedReload & 0x1ul) << RTC_TAMPCTL_SEEDRLD_Pos) |
1081               ((u32RefPattern & 0x3ul) << RTC_TAMPCTL_DYNSRC_Pos);
1082 
1083     RTC_WaitAccessEnable();
1084     RTC->TAMPSEED = u32Seed; /* need set seed value before re-load seed */
1085     RTC_WaitAccessEnable();
1086     RTC->TAMPCTL = u32Reg;
1087 }
1088 
1089 /*@}*/ /* end of group RTC_EXPORTED_FUNCTIONS */
1090 
1091 /*@}*/ /* end of group RTC_Driver */
1092 
1093 /*@}*/ /* end of group Standard_Driver */
1094 
1095 /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/
1096