1 /**************************************************************************//**
2  * @file     rtc.h
3  * @version  V3.00
4  * @brief    Real Time Clock(RTC) driver header file
5  *
6  * @copyright SPDX-License-Identifier: Apache-2.0
7  * @copyright Copyright (C) 2016-2020 Nuvoton Technology Corp. All rights reserved.
8  *****************************************************************************/
9 #ifndef __RTC_H__
10 #define __RTC_H__
11 
12 #ifdef __cplusplus
13 extern "C"
14 {
15 #endif
16 
17 
18 /** @addtogroup Standard_Driver Standard Driver
19   @{
20 */
21 
22 /** @addtogroup RTC_Driver RTC Driver
23   @{
24 */
25 
26 /** @addtogroup RTC_EXPORTED_CONSTANTS RTC Exported Constants
27   @{
28 */
29 /*---------------------------------------------------------------------------------------------------------*/
30 /*  RTC Initial Keyword Constant Definitions                                                               */
31 /*---------------------------------------------------------------------------------------------------------*/
32 #define RTC_INIT_KEY            0xA5EB1357UL    /*!< RTC Initiation Key to make RTC leaving reset state \hideinitializer */
33 #define RTC_WRITE_KEY           0x0000A965UL    /*!< RTC Register Access Enable Key to enable RTC read/write accessible and kept 1024 RTC clock \hideinitializer */
34 
35 /*---------------------------------------------------------------------------------------------------------*/
36 /*  RTC Time Attribute Constant Definitions                                                                */
37 /*---------------------------------------------------------------------------------------------------------*/
38 #define RTC_CLOCK_12            0UL             /*!< RTC as 12-hour time scale with AM and PM indication \hideinitializer */
39 #define RTC_CLOCK_24            1UL             /*!< RTC as 24-hour time scale \hideinitializer */
40 #define RTC_AM                  1UL             /*!< RTC as AM indication \hideinitializer */
41 #define RTC_PM                  2UL             /*!< RTC as PM indication \hideinitializer */
42 
43 /*---------------------------------------------------------------------------------------------------------*/
44 /*  RTC Tick Period Constant Definitions                                                                   */
45 /*---------------------------------------------------------------------------------------------------------*/
46 #define RTC_TICK_1_SEC          0x0UL           /*!< RTC time tick period is 1 second \hideinitializer */
47 #define RTC_TICK_1_2_SEC        0x1UL           /*!< RTC time tick period is 1/2 second \hideinitializer */
48 #define RTC_TICK_1_4_SEC        0x2UL           /*!< RTC time tick period is 1/4 second \hideinitializer */
49 #define RTC_TICK_1_8_SEC        0x3UL           /*!< RTC time tick period is 1/8 second \hideinitializer */
50 #define RTC_TICK_1_16_SEC       0x4UL           /*!< RTC time tick period is 1/16 second \hideinitializer */
51 #define RTC_TICK_1_32_SEC       0x5UL           /*!< RTC time tick period is 1/32 second \hideinitializer */
52 #define RTC_TICK_1_64_SEC       0x6UL           /*!< RTC time tick period is 1/64 second \hideinitializer */
53 #define RTC_TICK_1_128_SEC      0x7UL           /*!< RTC time tick period is 1/128 second \hideinitializer */
54 
55 /*---------------------------------------------------------------------------------------------------------*/
56 /*  RTC Day of Week Constant Definitions                                                                   */
57 /*---------------------------------------------------------------------------------------------------------*/
58 #define RTC_SUNDAY              0x0UL           /*!< Day of the Week is Sunday \hideinitializer */
59 #define RTC_MONDAY              0x1UL           /*!< Day of the Week is Monday \hideinitializer */
60 #define RTC_TUESDAY             0x2UL           /*!< Day of the Week is Tuesday \hideinitializer */
61 #define RTC_WEDNESDAY           0x3UL           /*!< Day of the Week is Wednesday \hideinitializer */
62 #define RTC_THURSDAY            0x4UL           /*!< Day of the Week is Thursday \hideinitializer */
63 #define RTC_FRIDAY              0x5UL           /*!< Day of the Week is Friday \hideinitializer */
64 #define RTC_SATURDAY            0x6UL           /*!< Day of the Week is Saturday \hideinitializer */
65 
66 /*---------------------------------------------------------------------------------------------------------*/
67 /*  RTC Miscellaneous Constant Definitions                                                                         */
68 /*---------------------------------------------------------------------------------------------------------*/
69 #define RTC_YEAR2000            2000UL          /*!< RTC Reference for compute year data \hideinitializer */
70 
71 #define RTC_TAMPER0_SELECT      (0x1ul << 0)    /*!< Select Tamper 0 \hideinitializer */
72 #define RTC_TAMPER1_SELECT      (0x1ul << 1)    /*!< Select Tamper 1 \hideinitializer */
73 #define RTC_TAMPER2_SELECT      (0x1ul << 2)    /*!< Select Tamper 2 \hideinitializer */
74 #define RTC_TAMPER3_SELECT      (0x1ul << 3)    /*!< Select Tamper 3 \hideinitializer */
75 #define RTC_TAMPER4_SELECT      (0x1ul << 4)    /*!< Select Tamper 4 \hideinitializer */
76 #define RTC_TAMPER5_SELECT      (0x1ul << 5)    /*!< Select Tamper 5 \hideinitializer */
77 #define MAX_TAMPER_PIN_NUM      6ul             /*!< Tamper Pin number \hideinitializer */
78 
79 #define RTC_TAMPER_HIGH_LEVEL_DETECT    1ul     /*!< Tamper pin detect voltage level is high \hideinitializer */
80 #define RTC_TAMPER_LOW_LEVEL_DETECT     0ul     /*!< Tamper pin detect voltage level is low  \hideinitializer */
81 
82 #define RTC_TAMPER_DEBOUNCE_ENABLE      1ul     /*!< Enable RTC tamper pin de-bounce function \hideinitializer */
83 #define RTC_TAMPER_DEBOUNCE_DISABLE     0ul     /*!< Disable RTC tamper pin de-bounce function \hideinitializer */
84 
85 #define RTC_PAIR0_SELECT        (0x1ul << 0)    /*!< Select Pair 0 \hideinitializer */
86 #define RTC_PAIR1_SELECT        (0x1ul << 1)    /*!< Select Pair 1 \hideinitializer */
87 #define RTC_PAIR2_SELECT        (0x1ul << 2)    /*!< Select Pair 2 \hideinitializer */
88 #define MAX_PAIR_NUM            3ul             /*!< Pair number \hideinitializer */
89 
90 #define RTC_2POW10_CLK          (0x0ul << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 RTC clock cycles \hideinitializer */
91 #define RTC_2POW11_CLK          (0x1ul << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 2 RTC clock cycles \hideinitializer */
92 #define RTC_2POW12_CLK          (0x2ul << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 4 RTC clock cycles \hideinitializer */
93 #define RTC_2POW13_CLK          (0x3ul << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 6 RTC clock cycles \hideinitializer */
94 #define RTC_2POW14_CLK          (0x4ul << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 8 RTC clock cycles \hideinitializer */
95 #define RTC_2POW15_CLK          (0x5ul << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 16 RTC clock cycles \hideinitializer */
96 #define RTC_2POW16_CLK          (0x6ul << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 32 RTC clock cycles \hideinitializer */
97 #define RTC_2POW17_CLK          (0x7ul << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 64 RTC clock cycles \hideinitializer */
98 
99 #define REF_RANDOM_PATTERN      0x0ul   /*!< The new reference pattern is generated by random number generator when the reference pattern run out \hideinitializer */
100 #define REF_PREVIOUS_PATTERN    0x1ul   /*!< The new reference pattern is repeated previous random value when the reference pattern run out \hideinitializer */
101 #define REF_SEED                0x3ul   /*!< The new reference pattern is repeated from SEED (RTC_TAMPSEED[31:0]) when the reference pattern run out \hideinitializer */
102 
103 /*@}*/ /* end of group RTC_EXPORTED_CONSTANTS */
104 
105 
106 /** @addtogroup RTC_EXPORTED_STRUCTS RTC Exported Structs
107   @{
108 */
109 /**
110   * @details    RTC define Time Data Struct
111   */
112 typedef struct
113 {
114     uint32_t u32Year;           /*!< Year value */
115     uint32_t u32Month;          /*!< Month value */
116     uint32_t u32Day;            /*!< Day value */
117     uint32_t u32DayOfWeek;      /*!< Day of week value */
118     uint32_t u32Hour;           /*!< Hour value */
119     uint32_t u32Minute;         /*!< Minute value */
120     uint32_t u32Second;         /*!< Second value */
121     uint32_t u32TimeScale;      /*!< 12-Hour, 24-Hour */
122     uint32_t u32AmPm;           /*!< Only Time Scale select 12-hr used */
123 } S_RTC_TIME_DATA_T;
124 
125 /*@}*/ /* end of group RTC_EXPORTED_STRUCTS */
126 
127 
128 /** @addtogroup RTC_EXPORTED_FUNCTIONS RTC Exported Functions
129   @{
130 */
131 
132 /**
133   * @brief      Indicate is Leap Year or not
134   *
135   * @param[in]  rtc         The pointer of RTC module.
136   *
137   * @retval     0   This year is not a leap year
138   * @retval     1   This year is a leap year
139   *
140   * @details    According to current date, return this year is leap year or not.
141   * \hideinitializer
142   */
143 #define RTC_IS_LEAP_YEAR(rtc)           ((rtc)->LEAPYEAR & RTC_LEAPYEAR_LEAPYEAR_Msk ? 1:0)
144 
145 /**
146   * @brief      Clear RTC Alarm Interrupt Flag
147   *
148   * @param[in]  rtc         The pointer of RTC module.
149   *
150   * @return     None
151   *
152   * @details    This macro is used to clear RTC alarm interrupt flag.
153   * \hideinitializer
154   */
155 #define RTC_CLEAR_ALARM_INT_FLAG(rtc)   ((rtc)->INTSTS = RTC_INTSTS_ALMIF_Msk)
156 
157 /**
158   * @brief      Clear RTC Tick Interrupt Flag
159   *
160   * @param[in]  rtc         The pointer of RTC module.
161   *
162   * @return     None
163   *
164   * @details    This macro is used to clear RTC tick interrupt flag.
165   * \hideinitializer
166   */
167 #define RTC_CLEAR_TICK_INT_FLAG(rtc)    ((rtc)->INTSTS = RTC_INTSTS_TICKIF_Msk)
168 
169 /**
170   * @brief      Clear RTC Tamper Interrupt Flag
171   *
172   * @param[in]  rtc             The pointer of RTC module.
173   * @param[in]  u32TamperFlag   Tamper interrupt flag. It consists of:    \n
174   *                             - \ref RTC_INTSTS_TAMP0IF_Msk    \n
175   *                             - \ref RTC_INTSTS_TAMP1IF_Msk    \n
176   *                             - \ref RTC_INTSTS_TAMP2IF_Msk    \n
177   *                             - \ref RTC_INTSTS_TAMP3IF_Msk    \n
178   *                             - \ref RTC_INTSTS_TAMP4IF_Msk    \n
179   *                             - \ref RTC_INTSTS_TAMP5IF_Msk
180   *
181   * @return     None
182   *
183   * @details    This macro is used to clear RTC tamper pin interrupt flag.
184   * \hideinitializer
185   */
186 #define RTC_CLEAR_TAMPER_INT_FLAG(rtc, u32TamperFlag)   ((rtc)->INTSTS = (u32TamperFlag))
187 
188 /**
189   * @brief      Get RTC Alarm Interrupt Flag
190   *
191   * @param[in]  rtc         The pointer of RTC module.
192   *
193   * @retval     0   RTC alarm interrupt did not occur
194   * @retval     1   RTC alarm interrupt occurred
195   *
196   * @details    This macro indicates RTC alarm interrupt occurred or not.
197   * \hideinitializer
198   */
199 #define RTC_GET_ALARM_INT_FLAG(rtc)         (((rtc)->INTSTS & RTC_INTSTS_ALMIF_Msk)? 1:0)
200 
201 /**
202   * @brief      Get RTC Time Tick Interrupt Flag
203   *
204   * @param[in]  rtc         The pointer of RTC module.
205   *
206   * @retval     0   RTC time tick interrupt did not occur
207   * @retval     1   RTC time tick interrupt occurred
208   *
209   * @details    This macro indicates RTC time tick interrupt occurred or not.
210   * \hideinitializer
211   */
212 #define RTC_GET_TICK_INT_FLAG(rtc)          (((rtc)->INTSTS & RTC_INTSTS_TICKIF_Msk)? 1:0)
213 
214 /**
215   * @brief      Get RTC Tamper Interrupt Flag
216   *
217   * @param[in]  rtc         The pointer of RTC module.
218   *
219   * @retval     0   RTC tamper event interrupt did not occur
220   * @retval     1   RTC tamper event interrupt occurred
221   *
222   * @details    This macro indicates RTC tamper event occurred or not.
223   * \hideinitializer
224   */
225 #define RTC_GET_TAMPER_INT_FLAG(rtc)        (((rtc)->INTSTS & (0x3F00))? 1:0)
226 
227 /**
228   * @brief      Get RTC Tamper Interrupt Status
229   *
230   * @param[in]  rtc         The pointer of RTC module.
231   *
232   * @retval     RTC_INTSTS_TAMP0IF_Msk    Tamper 0 interrupt flag is generated
233   * @retval     RTC_INTSTS_TAMP1IF_Msk    Tamper 1 interrupt flag is generated
234   * @retval     RTC_INTSTS_TAMP2IF_Msk    Tamper 2 interrupt flag is generated
235   * @retval     RTC_INTSTS_TAMP3IF_Msk    Tamper 3 interrupt flag is generated
236   * @retval     RTC_INTSTS_TAMP4IF_Msk    Tamper 4 interrupt flag is generated
237   * @retval     RTC_INTSTS_TAMP5IF_Msk    Tamper 5 interrupt flag is generated
238   *
239   * @details    This macro indicates RTC tamper interrupt status.
240   * \hideinitializer
241   */
242 #define RTC_GET_TAMPER_INT_STATUS(rtc)      (((rtc)->INTSTS & (0x3F00)))
243 
244 /**
245   * @brief      Enable RTC Tick Wake-up Function
246   *
247   * @param[in]  rtc         The pointer of RTC module.
248   *
249   * @return     None
250   *
251   * @details    This macro is used to enable RTC tick interrupt wake-up function.
252   * \hideinitializer
253   */
254 #define RTC_ENABLE_TICK_WAKEUP(rtc)         ((rtc)->INTEN |= RTC_INTEN_TICKIEN_Msk);
255 
256 /**
257   * @brief      Disable RTC Tick Wake-up Function
258   *
259   * @param[in]  rtc         The pointer of RTC module.
260   *
261   * @return     None
262   *
263   * @details    This macro is used to disable RTC tick interrupt wake-up function.
264   * \hideinitializer
265   */
266 #define RTC_DISABLE_TICK_WAKEUP(rtc)        ((rtc)->INTEN &= ~RTC_INTEN_TICKIEN_Msk);
267 
268 /**
269   * @brief      Read Spare Register
270   *
271   * @param[in]  rtc         The pointer of RTC module.
272   * @param[in]  u32RegNum   The spare register number, 0~19.
273   *
274   * @return     Spare register content
275   *
276   * @details    Read the specify spare register content.
277         The returned value is valid only when RWENF (RTC_RWEN[16]) bit is set. \n
278   * \hideinitializer
279   */
280 #define RTC_READ_SPARE_REGISTER(rtc, u32RegNum)     ((rtc)->SPR[(u32RegNum)])
281 
282 /**
283   * @brief      Write Spare Register
284   *
285   * @param[in]  rtc             The pointer of RTC module.
286   * @param[in]  u32RegNum       The spare register number, 0~19.
287   * @param[in]  u32RegValue     The spare register value.
288   *
289   * @return     None
290   *
291   * @details    Write specify data to spare register.
292         This macro is effect only when RWENF (RTC_RWEN[16]) bit is set. \n
293   * \hideinitializer
294   */
295 #define RTC_WRITE_SPARE_REGISTER(rtc, u32RegNum, u32RegValue)   ((rtc)->SPR[(u32RegNum)] = (u32RegValue))
296 
297 
298 /*---------------------------------------------------------------------------------------------------------*/
299 /* static inline functions                                                                                 */
300 /*---------------------------------------------------------------------------------------------------------*/
301 /* Declare these inline functions here to avoid MISRA C 2004 rule 8.1 error */
302 __STATIC_INLINE void RTC_WaitAccessEnable(void);
303 
304 /**
305   * @brief      Wait RTC Access Enable
306   *
307   * @param      None
308   *
309   * @return     None
310   *
311   * @details    This function is used to enable the maximum RTC read/write accessible time.
312   */
RTC_WaitAccessEnable(void)313 __STATIC_INLINE void RTC_WaitAccessEnable(void)
314 {
315     while((RTC->RWEN & RTC_RWEN_RWENF_Msk) == 0x0UL) {}
316 }
317 
318 void RTC_Open(S_RTC_TIME_DATA_T *sPt);
319 void RTC_Close(void);
320 void RTC_32KCalibration(int32_t i32FrequencyX10000);
321 void RTC_GetDateAndTime(S_RTC_TIME_DATA_T *sPt);
322 void RTC_GetAlarmDateAndTime(S_RTC_TIME_DATA_T *sPt);
323 void RTC_SetDateAndTime(S_RTC_TIME_DATA_T *sPt);
324 void RTC_SetAlarmDateAndTime(S_RTC_TIME_DATA_T *sPt);
325 void RTC_SetDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day, uint32_t u32DayOfWeek);
326 void RTC_SetTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm);
327 void RTC_SetAlarmDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day);
328 void RTC_SetAlarmTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm);
329 void RTC_SetAlarmDateMask(uint8_t u8IsTenYMsk, uint8_t u8IsYMsk, uint8_t u8IsTenMMsk, uint8_t u8IsMMsk, uint8_t u8IsTenDMsk, uint8_t u8IsDMsk);
330 void RTC_SetAlarmTimeMask(uint8_t u8IsTenHMsk, uint8_t u8IsHMsk, uint8_t u8IsTenMMsk, uint8_t u8IsMMsk, uint8_t u8IsTenSMsk, uint8_t u8IsSMsk);
331 uint32_t RTC_GetDayOfWeek(void);
332 void RTC_SetTickPeriod(uint32_t u32TickSelection);
333 void RTC_EnableInt(uint32_t u32IntFlagMask);
334 void RTC_DisableInt(uint32_t u32IntFlagMask);
335 void RTC_EnableSpareAccess(void);
336 void RTC_DisableSpareRegister(void);
337 void RTC_StaticTamperEnable(uint32_t u32TamperSelect, uint32_t u32DetecLevel, uint32_t u32DebounceEn);
338 void RTC_StaticTamperDisable(uint32_t u32TamperSelect);
339 void RTC_DynamicTamperEnable(uint32_t u32PairSel, uint32_t u32DebounceEn, uint32_t u32Pair1Source, uint32_t u32Pair2Source);
340 void RTC_DynamicTamperDisable(uint32_t u32PairSel);
341 void RTC_DynamicTamperConfig(uint32_t u32ChangeRate, uint32_t u32SeedReload, uint32_t u32RefPattern, uint32_t u32Seed);
342 
343 /*@}*/ /* end of group RTC_EXPORTED_FUNCTIONS */
344 
345 /*@}*/ /* end of group RTC_Driver */
346 
347 /*@}*/ /* end of group Standard_Driver */
348 
349 #ifdef __cplusplus
350 }
351 #endif
352 
353 #endif /* __RTC_H__ */
354 
355