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) 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 
34 /*---------------------------------------------------------------------------------------------------------*/
35 /*  RTC Time Attribute Constant Definitions                                                                */
36 /*---------------------------------------------------------------------------------------------------------*/
37 #define RTC_CLOCK_12            0UL             /*!< RTC as 12-hour time scale with AM and PM indication \hideinitializer */
38 #define RTC_CLOCK_24            1UL             /*!< RTC as 24-hour time scale \hideinitializer */
39 #define RTC_AM                  1UL             /*!< RTC as AM indication \hideinitializer */
40 #define RTC_PM                  2UL             /*!< RTC as PM indication \hideinitializer */
41 
42 /*---------------------------------------------------------------------------------------------------------*/
43 /*  RTC Tick Period Constant Definitions                                                                   */
44 /*---------------------------------------------------------------------------------------------------------*/
45 #define RTC_TICK_1_SEC          0x0UL           /*!< RTC time tick period is 1 second \hideinitializer */
46 #define RTC_TICK_1_2_SEC        0x1UL           /*!< RTC time tick period is 1/2 second \hideinitializer */
47 #define RTC_TICK_1_4_SEC        0x2UL           /*!< RTC time tick period is 1/4 second \hideinitializer */
48 #define RTC_TICK_1_8_SEC        0x3UL           /*!< RTC time tick period is 1/8 second \hideinitializer */
49 #define RTC_TICK_1_16_SEC       0x4UL           /*!< RTC time tick period is 1/16 second \hideinitializer */
50 #define RTC_TICK_1_32_SEC       0x5UL           /*!< RTC time tick period is 1/32 second \hideinitializer */
51 #define RTC_TICK_1_64_SEC       0x6UL           /*!< RTC time tick period is 1/64 second \hideinitializer */
52 #define RTC_TICK_1_128_SEC      0x7UL           /*!< RTC time tick period is 1/128 second \hideinitializer */
53 
54 /*---------------------------------------------------------------------------------------------------------*/
55 /*  RTC Day of Week Constant Definitions                                                                   */
56 /*---------------------------------------------------------------------------------------------------------*/
57 #define RTC_SUNDAY              0x0UL           /*!< Day of the Week is Sunday \hideinitializer */
58 #define RTC_MONDAY              0x1UL           /*!< Day of the Week is Monday \hideinitializer */
59 #define RTC_TUESDAY             0x2UL           /*!< Day of the Week is Tuesday \hideinitializer */
60 #define RTC_WEDNESDAY           0x3UL           /*!< Day of the Week is Wednesday \hideinitializer */
61 #define RTC_THURSDAY            0x4UL           /*!< Day of the Week is Thursday \hideinitializer */
62 #define RTC_FRIDAY              0x5UL           /*!< Day of the Week is Friday \hideinitializer */
63 #define RTC_SATURDAY            0x6UL           /*!< Day of the Week is Saturday \hideinitializer */
64 
65 /*---------------------------------------------------------------------------------------------------------*/
66 /*  RTC Miscellaneous Constant Definitions                                                                 */
67 /*---------------------------------------------------------------------------------------------------------*/
68 #define RTC_YEAR2000            2000UL          /*!< RTC Reference for compute year data \hideinitializer */
69 #define RTC_FCR_REFERENCE       32752           /*!< RTC Reference for frequency compensation */
70 
71 /*---------------------------------------------------------------------------------------------------------*/
72 /*  RTC Tamper Constant Definitions                                                                        */
73 /*---------------------------------------------------------------------------------------------------------*/
74 #define RTC_TAMPER0_SELECT      (0x1UL << 0)    /*!< Select Tamper 0 \hideinitializer */
75 #define RTC_TAMPER1_SELECT      (0x1UL << 1)    /*!< Select Tamper 1 \hideinitializer */
76 #define RTC_TAMPER2_SELECT      (0x1UL << 2)    /*!< Select Tamper 2 \hideinitializer */
77 #define RTC_TAMPER3_SELECT      (0x1UL << 3)    /*!< Select Tamper 3 \hideinitializer */
78 #define RTC_TAMPER4_SELECT      (0x1UL << 4)    /*!< Select Tamper 4 \hideinitializer */
79 #define RTC_TAMPER5_SELECT      (0x1UL << 5)    /*!< Select Tamper 5 \hideinitializer */
80 #define RTC_MAX_TAMPER_PIN_NUM  6UL             /*!< Tamper Pin number \hideinitializer */
81 
82 #define RTC_TAMPER_LOW_LEVEL_DETECT     0UL     /*!< Tamper pin detect voltage level is low  \hideinitializer */
83 #define RTC_TAMPER_HIGH_LEVEL_DETECT    1UL     /*!< Tamper pin detect voltage level is high \hideinitializer */
84 
85 #define RTC_TAMPER_DEBOUNCE_DISABLE     0UL     /*!< Disable RTC tamper pin de-bounce function \hideinitializer */
86 #define RTC_TAMPER_DEBOUNCE_ENABLE      1UL     /*!< Enable RTC tamper pin de-bounce function \hideinitializer */
87 
88 #define RTC_PAIR0_SELECT        (0x1UL << 0)    /*!< Select Pair 0 \hideinitializer */
89 #define RTC_PAIR1_SELECT        (0x1UL << 1)    /*!< Select Pair 1 \hideinitializer */
90 #define RTC_PAIR2_SELECT        (0x1UL << 2)    /*!< Select Pair 2 \hideinitializer */
91 #define RTC_MAX_PAIR_NUM        3UL             /*!< Pair number \hideinitializer */
92 
93 #define RTC_2POW10_CLK          (0x0UL << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 RTC clock cycles \hideinitializer */
94 #define RTC_2POW11_CLK          (0x1UL << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 2 RTC clock cycles \hideinitializer */
95 #define RTC_2POW12_CLK          (0x2UL << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 4 RTC clock cycles \hideinitializer */
96 #define RTC_2POW13_CLK          (0x3UL << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 6 RTC clock cycles \hideinitializer */
97 #define RTC_2POW14_CLK          (0x4UL << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 8 RTC clock cycles \hideinitializer */
98 #define RTC_2POW15_CLK          (0x5UL << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 16 RTC clock cycles \hideinitializer */
99 #define RTC_2POW16_CLK          (0x6UL << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 32 RTC clock cycles \hideinitializer */
100 #define RTC_2POW17_CLK          (0x7UL << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 64 RTC clock cycles \hideinitializer */
101 
102 #define RTC_REF_RANDOM_PATTERN  0x0UL   /*!< The new reference pattern is generated by random number generator when the reference pattern run out \hideinitializer */
103 #define RTC_REF_SEED_VALUE      0x1UL   /*!< The new reference pattern is repeated from SEED (RTC_TAMPSEED[31:0]) when the reference pattern run out \hideinitializer */
104 
105 /*---------------------------------------------------------------------------------------------------------*/
106 /*  RTC Clock Source Constant Definitions                                                                  */
107 /*---------------------------------------------------------------------------------------------------------*/
108 #define RTC_CLOCK_SOURCE_LXT        0UL /*!< Set RTC clock source as external LXT \hideinitializer */
109 #define RTC_CLOCK_SOURCE_LIRC       1UL /*!< Set RTC clock source as LIRC \hideinitializer */
110 #define RTC_CLOCK_SOURCE_LIRC32K    2UL /*!< Set RTC clock source as LIRC32K \hideinitializer */
111 
112 /*---------------------------------------------------------------------------------------------------------*/
113 /*  RTC GPIO_MODE Constant Definitions                                                                     */
114 /*---------------------------------------------------------------------------------------------------------*/
115 #define RTC_IO_MODE_INPUT       0x0UL   /*!< Input Mode */
116 #define RTC_IO_MODE_OUTPUT      0x1UL   /*!< Output Mode */
117 #define RTC_IO_MODE_OPEN_DRAIN  0x2UL   /*!< Open-Drain Mode */
118 #define RTC_IO_MODE_QUASI       0x3UL   /*!< Quasi-bidirectional Mode */
119 
120 #define RTC_IO_DIGITAL_ENABLE   0UL     /*!< I/O digital path is enabled */
121 #define RTC_IO_DIGITAL_DISABLE  1UL     /*!< I/O digital path is disabled */
122 
123 #define RTC_IO_PULL_UP_DOWN_DISABLE     0x0UL   /*!< I/O pull-up and pull-down is disabled */
124 #define RTC_IO_PULL_UP_ENABLE           0x1UL   /*!< I/O pull-up is enabled */
125 #define RTC_IO_PULL_DOWN_ENABLE         0x2UL   /*!< I/O pull-down is enabled */
126 
127 
128 /**@}*/ /* end of group RTC_EXPORTED_CONSTANTS */
129 
130 
131 /** @addtogroup RTC_EXPORTED_STRUCTS RTC Exported Structs
132   @{
133 */
134 /**
135   * @details    RTC define Time Data Struct
136   */
137 typedef struct
138 {
139     uint32_t u32Year;           /*!< Year value */
140     uint32_t u32Month;          /*!< Month value */
141     uint32_t u32Day;            /*!< Day value */
142     uint32_t u32DayOfWeek;      /*!< Day of week value */
143     uint32_t u32Hour;           /*!< Hour value */
144     uint32_t u32Minute;         /*!< Minute value */
145     uint32_t u32Second;         /*!< Second value */
146     uint32_t u32TimeScale;      /*!< 12-Hour, 24-Hour */
147     uint32_t u32AmPm;           /*!< Only Time Scale select 12-hr used */
148 } S_RTC_TIME_DATA_T;
149 
150 /**@}*/ /* end of group RTC_EXPORTED_STRUCTS */
151 
152 
153 /** @addtogroup RTC_EXPORTED_FUNCTIONS RTC Exported Functions
154   @{
155 */
156 
157 /**
158   * @brief      Indicate is Leap Year or not
159   *
160   * @param[in]  rtc         The pointer of RTC module.
161   *
162   * @retval     0   This year is not a leap year
163   * @retval     1   This year is a leap year
164   *
165   * @details    According to current date, return this year is leap year or not.
166   * \hideinitializer
167   */
168 #define RTC_IS_LEAP_YEAR(rtc)           (((rtc)->LEAPYEAR & RTC_LEAPYEAR_LEAPYEAR_Msk)? 1:0)
169 
170 /**
171   * @brief      Clear RTC Alarm Interrupt Flag
172   *
173   * @param[in]  rtc         The pointer of RTC module.
174   *
175   * @return     None
176   *
177   * @details    This macro is used to clear RTC alarm interrupt flag.
178   * \hideinitializer
179   */
180 #define RTC_CLEAR_ALARM_INT_FLAG(rtc)   ((rtc)->INTSTS = RTC_INTSTS_ALMIF_Msk)
181 
182 /**
183   * @brief      Clear RTC Tick Interrupt Flag
184   *
185   * @param[in]  rtc         The pointer of RTC module.
186   *
187   * @return     None
188   *
189   * @details    This macro is used to clear RTC tick interrupt flag.
190   * \hideinitializer
191   */
192 #define RTC_CLEAR_TICK_INT_FLAG(rtc)    ((rtc)->INTSTS = RTC_INTSTS_TICKIF_Msk)
193 
194 /**
195   * @brief      Clear RTC Tamper Interrupt Flag
196   *
197   * @param[in]  rtc             The pointer of RTC module.
198   * @param[in]  u32TamperFlag   Tamper interrupt flag. It consists of:    \n
199   *                             - \ref RTC_INTSTS_TAMP0IF_Msk    \n
200   *                             - \ref RTC_INTSTS_TAMP1IF_Msk    \n
201   *                             - \ref RTC_INTSTS_TAMP2IF_Msk    \n
202   *                             - \ref RTC_INTSTS_TAMP3IF_Msk    \n
203   *                             - \ref RTC_INTSTS_TAMP4IF_Msk    \n
204   *                             - \ref RTC_INTSTS_TAMP5IF_Msk
205   *
206   * @return     None
207   *
208   * @details    This macro is used to clear RTC tamper pin interrupt flag.
209   * \hideinitializer
210   */
211 #define RTC_CLEAR_TAMPER_INT_FLAG(rtc, u32TamperFlag)   ((rtc)->INTSTS = (u32TamperFlag))
212 
213 /**
214   * @brief      Get RTC Alarm Interrupt Flag
215   *
216   * @param[in]  rtc         The pointer of RTC module.
217   *
218   * @retval     0   RTC alarm interrupt did not occur
219   * @retval     1   RTC alarm interrupt occurred
220   *
221   * @details    This macro indicates RTC alarm interrupt occurred or not.
222   * \hideinitializer
223   */
224 #define RTC_GET_ALARM_INT_FLAG(rtc)         (((rtc)->INTSTS & RTC_INTSTS_ALMIF_Msk)? 1:0)
225 
226 /**
227   * @brief      Get RTC Time Tick Interrupt Flag
228   *
229   * @param[in]  rtc         The pointer of RTC module.
230   *
231   * @retval     0   RTC time tick interrupt did not occur
232   * @retval     1   RTC time tick interrupt occurred
233   *
234   * @details    This macro indicates RTC time tick interrupt occurred or not.
235   * \hideinitializer
236   */
237 #define RTC_GET_TICK_INT_FLAG(rtc)          (((rtc)->INTSTS & RTC_INTSTS_TICKIF_Msk)? 1:0)
238 
239 /**
240   * @brief      Set I/O Control By GPIO
241   *
242   * @param[in]  rtc         The pointer of RTC module.
243   *
244   * @return     None
245   *
246   * @details    This macro sets the PF.4~11 pin I/O is controlled by GPIO module.
247   * \hideinitializer
248   */
249 #define RTC_SET_IOCTL_BY_GPIO(rtc)          ((rtc)->LXTCTL &= ~RTC_LXTCTL_IOCTLSEL_Msk)
250 
251 /**
252   * @brief      Set I/O Control By RTC
253   *
254   * @param[in]  rtc         The pointer of RTC module.
255   *
256   * @return     None
257   *
258   * @details    This macro sets the PF.4~11 pin I/O is controlled by RTC module.
259   * \hideinitializer
260   */
261 #define RTC_SET_IOCTL_BY_RTC(rtc)           ((rtc)->LXTCTL |= RTC_LXTCTL_IOCTLSEL_Msk)
262 
263 /**
264   * @brief      Get I/O Control Property
265   *
266   * @param[in]  rtc         The pointer of RTC module.
267   *
268   * @retval     0   PF.4~11 pin I/O is controlled by GPIO module
269   * @retval     1   PF.4~11 pin I/O is controlled by RTC module
270   *
271   * @details    This macro indicates the PF.4~11 pin I/O control property.
272   * \hideinitializer
273   */
274 #define RTC_GET_IOCTL_PROPERTY(rtc)          (((rtc)->LXTCTL & RTC_LXTCTL_IOCTLSEL_Msk)? 1:0)
275 
276 /**
277   * @brief      Get RTC Tamper Interrupt Flag
278   *
279   * @param[in]  rtc         The pointer of RTC module.
280   *
281   * @retval     0   RTC tamper event interrupt did not occur
282   * @retval     1   RTC tamper event interrupt occurred
283   *
284   * @details    This macro indicates RTC tamper event occurred or not.
285   * \hideinitializer
286   */
287 #define RTC_GET_TAMPER_INT_FLAG(rtc)        (((rtc)->INTSTS & (0x3F00))? 1:0)
288 
289 /**
290   * @brief      Get RTC Tamper Interrupt Status
291   *
292   * @param[in]  rtc         The pointer of RTC module.
293   *
294   * @retval     RTC_INTSTS_TAMP0IF_Msk    Tamper 0 interrupt flag is generated
295   * @retval     RTC_INTSTS_TAMP1IF_Msk    Tamper 1 interrupt flag is generated
296   * @retval     RTC_INTSTS_TAMP2IF_Msk    Tamper 2 interrupt flag is generated
297   * @retval     RTC_INTSTS_TAMP3IF_Msk    Tamper 3 interrupt flag is generated
298   * @retval     RTC_INTSTS_TAMP4IF_Msk    Tamper 4 interrupt flag is generated
299   * @retval     RTC_INTSTS_TAMP5IF_Msk    Tamper 5 interrupt flag is generated
300   *
301   * @details    This macro indicates RTC tamper interrupt status.
302   * \hideinitializer
303   */
304 #define RTC_GET_TAMPER_INT_STATUS(rtc)      (((rtc)->INTSTS & (0x3F00)))
305 
306 /**
307   * @brief      Enable RTC Tick Wake-up Function
308   *
309   * @param[in]  rtc         The pointer of RTC module.
310   *
311   * @return     None
312   *
313   * @details    This macro is used to enable RTC tick interrupt wake-up function.
314   * \hideinitializer
315   */
316 #define RTC_ENABLE_TICK_WAKEUP(rtc)         ((rtc)->INTEN |= RTC_INTEN_TICKIEN_Msk);
317 
318 /**
319   * @brief      Disable RTC Tick Wake-up Function
320   *
321   * @param[in]  rtc         The pointer of RTC module.
322   *
323   * @return     None
324   *
325   * @details    This macro is used to disable RTC tick interrupt wake-up function.
326   * \hideinitializer
327   */
328 #define RTC_DISABLE_TICK_WAKEUP(rtc)        ((rtc)->INTEN &= ~RTC_INTEN_TICKIEN_Msk);
329 
330 /**
331   * @brief      Read Spare Register
332   *
333   * @param[in]  rtc         The pointer of RTC module.
334   * @param[in]  u32RegNum   The spare register number, 0~19.
335   *
336   * @return     Spare register content
337   *
338   * @details    Read the specify spare register content.
339   * \hideinitializer
340   */
341 #define RTC_READ_SPARE_REGISTER(rtc, u32RegNum)     ((rtc)->SPR[(u32RegNum)])
342 
343 /**
344   * @brief      Write Spare Register
345   *
346   * @param[in]  rtc             The pointer of RTC module.
347   * @param[in]  u32RegNum       The spare register number, 0~19.
348   * @param[in]  u32RegValue     The spare register value.
349   *
350   * @return     None
351   *
352   * @details    Write specify data to spare register.
353   * \hideinitializer
354   */
355 #define RTC_WRITE_SPARE_REGISTER(rtc, u32RegNum, u32RegValue)   ((rtc)->SPR[(u32RegNum)] = (u32RegValue))
356 
357 
358 void RTC_Open(S_RTC_TIME_DATA_T *sPt);
359 void RTC_Close(void);
360 void RTC_32KCalibration(int32_t i32FrequencyX10000);
361 void RTC_GetDateAndTime(S_RTC_TIME_DATA_T *sPt);
362 void RTC_GetAlarmDateAndTime(S_RTC_TIME_DATA_T *sPt);
363 void RTC_SetDateAndTime(S_RTC_TIME_DATA_T *sPt);
364 void RTC_SetAlarmDateAndTime(S_RTC_TIME_DATA_T *sPt);
365 void RTC_SetDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day, uint32_t u32DayOfWeek);
366 void RTC_SetTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm);
367 void RTC_SetAlarmDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day);
368 void RTC_SetAlarmTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm);
369 void RTC_SetAlarmDateMask(uint8_t u8IsTenYMsk, uint8_t u8IsYMsk, uint8_t u8IsTenMMsk, uint8_t u8IsMMsk, uint8_t u8IsTenDMsk, uint8_t u8IsDMsk);
370 void RTC_SetAlarmTimeMask(uint8_t u8IsTenHMsk, uint8_t u8IsHMsk, uint8_t u8IsTenMMsk, uint8_t u8IsMMsk, uint8_t u8IsTenSMsk, uint8_t u8IsSMsk);
371 uint32_t RTC_GetDayOfWeek(void);
372 void RTC_SetTickPeriod(uint32_t u32TickSelection);
373 void RTC_EnableInt(uint32_t u32IntFlagMask);
374 void RTC_DisableInt(uint32_t u32IntFlagMask);
375 void RTC_EnableSpareAccess(void);
376 void RTC_DisableSpareRegister(void);
377 void RTC_StaticTamperEnable(uint32_t u32TamperSelect, uint32_t u32DetecLevel, uint32_t u32DebounceEn);
378 void RTC_StaticTamperDisable(uint32_t u32TamperSelect);
379 void RTC_DynamicTamperEnable(uint32_t u32PairSel, uint32_t u32DebounceEn, uint32_t u32Pair1Source, uint32_t u32Pair2Source);
380 void RTC_DynamicTamperDisable(uint32_t u32PairSel);
381 void RTC_DynamicTamperConfig(uint32_t u32ChangeRate, uint32_t u32SeedReload, uint32_t u32RefPattern, uint32_t u32Seed);
382 uint32_t RTC_SetClockSource(uint32_t u32ClkSrc);
383 void RTC_SetGPIOMode(uint32_t u32PFPin, uint32_t u32Mode, uint32_t u32DigitalCtl, uint32_t u32PullCtl, uint32_t u32OutputLevel);
384 void RTC_SetGPIOLevel(uint32_t u32PFPin, uint32_t u32OutputLevel);
385 
386 /**@}*/ /* end of group RTC_EXPORTED_FUNCTIONS */
387 
388 /**@}*/ /* end of group RTC_Driver */
389 
390 /**@}*/ /* end of group Standard_Driver */
391 
392 #ifdef __cplusplus
393 }
394 #endif
395 
396 #endif /* __RTC_H__ */
397