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