1 /**************************************************************************//**
2  * @file     rtc.h
3  * @version  V3.00
4  * @brief    M2L31 series RTC driver header file
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  * @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 
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_WAIT_COUNT          0xFFFFFFFFUL      /*!< Initial Time-out Value \hideinitializer */
69 #define RTC_YEAR2000            2000UL            /*!< RTC Reference for compute year data \hideinitializer */
70 #define RTC_FCR_REFERENCE       32761UL           /*!< RTC Reference for frequency compensation \hideinitializer */
71 
72 /*---------------------------------------------------------------------------------------------------------*/
73 /*  RTC Tamper Constant Definitions                                                                        */
74 /*---------------------------------------------------------------------------------------------------------*/
75 #define RTC_TAMPER0_SELECT (0x1ul << 0)     /*!< Select Tamper 0 \hideinitializer */
76 #define RTC_TAMPER1_SELECT (0x1ul << 1)     /*!< Select Tamper 1 \hideinitializer */
77 #define RTC_TAMPER2_SELECT (0x1ul << 2)     /*!< Select Tamper 2 \hideinitializer */
78 #define RTC_TAMPER3_SELECT (0x1ul << 3)     /*!< Select Tamper 3 \hideinitializer */
79 #define RTC_TAMPER4_SELECT (0x1ul << 4)     /*!< Select Tamper 4 \hideinitializer */
80 #define RTC_TAMPER5_SELECT (0x1ul << 5)     /*!< Select Tamper 5 \hideinitializer */
81 #define RTC_MAX_TAMPER_PIN_NUM  6UL             /*!< Tamper Pin number \hideinitializer */
82 
83 #define RTC_TAMPER_HIGH_LEVEL_DETECT 1ul    /*!< Tamper pin detect voltage level is high \hideinitializer */
84 #define RTC_TAMPER_LOW_LEVEL_DETECT  0ul    /*!< Tamper pin detect voltage level is low  \hideinitializer */
85 
86 #define RTC_TAMPER_DEBOUNCE_ENABLE   1ul    /*!< Enable RTC tamper pin de-bounce function \hideinitializer */
87 #define RTC_TAMPER_DEBOUNCE_DISABLE  0ul    /*!< Disable RTC tamper pin de-bounce function \hideinitializer */
88 
89 
90 
91 /*---------------------------------------------------------------------------------------------------------*/
92 /*  RTC Clock Source Constant Definitions                                                                  */
93 /*---------------------------------------------------------------------------------------------------------*/
94 #define RTC_CLOCK_SOURCE_LXT        0UL /*!< Set RTC clock source as external LXT \hideinitializer */
95 #define RTC_CLOCK_SOURCE_LIRC       1UL /*!< Set RTC clock source as LIRC \hideinitializer */
96 #define RTC_CLOCK_SOURCE_LIRC32K    2UL /*!< Set RTC clock source as LIRC32K \hideinitializer */
97 
98 /*---------------------------------------------------------------------------------------------------------*/
99 /*  RTC GPIO_MODE Constant Definitions                                                                     */
100 /*---------------------------------------------------------------------------------------------------------*/
101 #define RTC_IO_MODE_INPUT       0x0UL   /*!< Input Mode */
102 #define RTC_IO_MODE_OUTPUT      0x1UL   /*!< Output Mode */
103 #define RTC_IO_MODE_OPEN_DRAIN  0x2UL   /*!< Open-Drain Mode */
104 #define RTC_IO_MODE_QUASI       0x3UL   /*!< Quasi-bidirectional Mode */
105 
106 #define RTC_IO_DIGITAL_ENABLE   0UL     /*!< I/O digital path is enabled */
107 #define RTC_IO_DIGITAL_DISABLE  1UL     /*!< I/O digital path is disabled */
108 
109 #define RTC_IO_PULL_UP_DOWN_DISABLE     0x0UL   /*!< I/O pull-up and pull-down is disabled */
110 #define RTC_IO_PULL_UP_ENABLE           0x1UL   /*!< I/O pull-up is enabled */
111 #define RTC_IO_PULL_DOWN_ENABLE         0x2UL   /*!< I/O pull-down is enabled */
112 
113 /*---------------------------------------------------------------------------------------------------------*/
114 /* RTC Define Error Code                                                                                   */
115 /*---------------------------------------------------------------------------------------------------------*/
116 #define RTC_TIMEOUT         SystemCoreClock     /*!< RTC time-out counter (1 second time-out) */
117 #define RTC_OK              ( 0L)               /*!< RTC operation OK */
118 #define RTC_ERR_FAIL        (-1L)               /*!< RTC operation failed */
119 #define RTC_ERR_TIMEOUT     (-2L)               /*!< RTC operation abort due to timeout error */
120 
121 /*@}*/ /* end of group RTC_EXPORTED_CONSTANTS */
122 
123 
124 /** @addtogroup RTC_EXPORTED_STRUCTS RTC Exported Structs
125   @{
126 */
127 /**
128   * @details    RTC define Time Data Struct
129   */
130 typedef struct
131 {
132     uint32_t u32Year;           /*!< Year value */
133     uint32_t u32Month;          /*!< Month value */
134     uint32_t u32Day;            /*!< Day value */
135     uint32_t u32DayOfWeek;      /*!< Day of week value */
136     uint32_t u32Hour;           /*!< Hour value */
137     uint32_t u32Minute;         /*!< Minute value */
138     uint32_t u32Second;         /*!< Second value */
139     uint32_t u32TimeScale;      /*!< 12-Hour, 24-Hour */
140     uint32_t u32AmPm;           /*!< Only Time Scale select 12-hr used */
141 } S_RTC_TIME_DATA_T;
142 
143 /*@}*/ /* end of group RTC_EXPORTED_STRUCTS */
144 
145 
146 /** @addtogroup RTC_EXPORTED_FUNCTIONS RTC Exported Functions
147   @{
148 */
149 
150 /**
151   * @brief      Indicate is Leap Year or not
152   *
153   * @param      None
154   *
155   * @retval     0   This year is not a leap year
156   * @retval     1   This year is a leap year
157   *
158   * @details    According to current date, return this year is leap year or not.
159   * \hideinitializer
160   */
161 #define RTC_IS_LEAP_YEAR()              (RTC->LEAPYEAR & RTC_LEAPYEAR_LEAPYEAR_Msk ? 1:0)
162 
163 /**
164   * @brief      Clear RTC Alarm Interrupt Flag
165   *
166   * @param      None
167   *
168   * @return     None
169   *
170   * @details    This macro is used to clear RTC alarm interrupt flag.
171   * \hideinitializer
172   */
173 #define RTC_CLEAR_ALARM_INT_FLAG()      (RTC->INTSTS = RTC_INTSTS_ALMIF_Msk)
174 
175 /**
176   * @brief      Clear RTC Tick Interrupt Flag
177   *
178   * @param      None
179   *
180   * @return     None
181   *
182   * @details    This macro is used to clear RTC tick interrupt flag.
183   * \hideinitializer
184   */
185 #define RTC_CLEAR_TICK_INT_FLAG()       (RTC->INTSTS = RTC_INTSTS_TICKIF_Msk)
186 
187 /**
188   * @brief      Clear RTC Tamper Interrupt Flag
189   *
190   * @param      u32TamperFlag   Tamper interrupt flag. It consists of:    \n
191   *                             - \ref RTC_INTSTS_TAMP0IF_Msk    \n
192   *                             - \ref RTC_INTSTS_TAMP1IF_Msk    \n
193   *                             - \ref RTC_INTSTS_TAMP2IF_Msk    \n
194   *
195   * @return     None
196   *
197   * @details    This macro is used to clear RTC snooper pin interrupt flag.
198   * \hideinitializer
199   */
200 #define RTC_CLEAR_TAMPER_INT_FLAG(u32TamperFlag)    (RTC->INTSTS = (u32TamperFlag))
201 
202 /**
203   * @brief      Get RTC Alarm Interrupt Flag
204   *
205   * @param      None
206   *
207   * @retval     0   RTC alarm interrupt did not occur
208   * @retval     1   RTC alarm interrupt occurred
209   *
210   * @details    This macro indicates RTC alarm interrupt occurred or not.
211   * \hideinitializer
212   */
213 #define RTC_GET_ALARM_INT_FLAG()        ((RTC->INTSTS & RTC_INTSTS_ALMIF_Msk)? 1:0)
214 
215 /**
216   * @brief      Get RTC Time Tick Interrupt Flag
217   *
218   * @param      None
219   *
220   * @retval     0   RTC time tick interrupt did not occur
221   * @retval     1   RTC time tick interrupt occurred
222   *
223   * @details    This macro indicates RTC time tick interrupt occurred or not.
224   * \hideinitializer
225   */
226 #define RTC_GET_TICK_INT_FLAG()         ((RTC->INTSTS & RTC_INTSTS_TICKIF_Msk)? 1:0)
227 
228 /**
229   * @brief      Set I/O Control By GPIO
230   *
231   * @param[in]  rtc         The pointer of RTC module.
232   *
233   * @return     None
234   *
235   * @details    This macro sets the PF.4~11 pin I/O is controlled by GPIO module.
236   * \hideinitializer
237   */
238 #define RTC_SET_IOCTL_BY_GPIO()             (RTC->LXTCTL &= ~RTC_LXTCTL_IOCTLSEL_Msk)
239 
240 /**
241   * @brief      Set I/O Control By RTC
242   *
243   * @param[in]  rtc         The pointer of RTC module.
244   *
245   * @return     None
246   *
247   * @details    This macro sets the PF.4~11 pin I/O is controlled by RTC module.
248   * \hideinitializer
249   */
250 #define RTC_SET_IOCTL_BY_RTC()              (RTC->LXTCTL |= RTC_LXTCTL_IOCTLSEL_Msk)
251 
252 /**
253   * @brief      Get I/O Control Property
254   *
255   * @param[in]  rtc         The pointer of RTC module.
256   *
257   * @retval     0   PF.4~11 pin I/O is controlled by GPIO module
258   * @retval     1   PF.4~11 pin I/O is controlled by RTC module
259   *
260   * @details    This macro indicates the PF.4~11 pin I/O control property.
261   * \hideinitializer
262   */
263 #define RTC_GET_IOCTL_PROPERTY()             ((RTC->LXTCTL & RTC_LXTCTL_IOCTLSEL_Msk)? 1:0)
264 
265 /**
266   * @brief      Get RTC Tamper Interrupt Flag
267   *
268   * @param      None
269   *
270   * @retval     0   RTC snooper pin interrupt did not occur
271   * @retval     1   RTC snooper pin interrupt occurred
272   *
273   * @details    This macro indicates RTC snooper pin interrupt occurred or not.
274   * \hideinitializer
275   */
276 #define RTC_GET_TAMPER_INT_FLAG()      ((RTC->INTSTS & (0x3F00))? 1:0)
277 
278 /**
279   * @brief      Get RTC TAMPER Interrupt Status
280   *
281   * @param      None
282   *
283   * @retval     RTC_INTSTS_TAMP0IF_Msk    Tamper 0 interrupt flag is generated
284   * @retval     RTC_INTSTS_TAMP1IF_Msk    Tamper 1 interrupt flag is generated
285   * @retval     RTC_INTSTS_TAMP2IF_Msk    Tamper 2 interrupt flag is generated
286   *
287   * @details    This macro indicates RTC snooper pin interrupt occurred or not.
288   * \hideinitializer
289   */
290 #define RTC_GET_TAMPER_INT_STATUS()      ((RTC->INTSTS & (0x3F00)))
291 
292 /**
293   * @brief      Enable RTC Tick Wake-up Function
294   *
295   * @param[in]  rtc         The pointer of RTC module.
296   *
297   * @return     None
298   *
299   * @details    This macro is used to enable RTC tick interrupt wake-up function.
300   * \hideinitializer
301   */
302 #define RTC_ENABLE_TICK_WAKEUP()            (RTC->INTEN |= RTC_INTEN_TICKIEN_Msk);
303 
304 /**
305   * @brief      Disable RTC Tick Wake-up Function
306   *
307   * @param[in]  rtc         The pointer of RTC module.
308   *
309   * @return     None
310   *
311   * @details    This macro is used to disable RTC tick interrupt wake-up function.
312   * \hideinitializer
313   */
314 #define RTC_DISABLE_TICK_WAKEUP()           (RTC->INTEN &= ~RTC_INTEN_TICKIEN_Msk);
315 
316 /**
317   * @brief      Read Spare Register
318   *
319   * @param[in]  u32RegNum   The spare register number, 0~19.
320   *
321   * @return     Spare register content
322   *
323   * @details    Read the specify spare register content.
324   * @note       The returned value is valid only when SPRRDY(SPRCTL[7] SPR Register Ready) bit is set. \n
325   *             And its controlled by RTC Access Enable Register.
326   * \hideinitializer
327   */
328 #define RTC_READ_SPARE_REGISTER(u32RegNum)                  (RTC->SPR[(u32RegNum)])
329 
330 /**
331   * @brief      Write Spare Register
332   *
333   * @param[in]  u32RegNum    The spare register number, 0~19.
334   * @param[in]  u32RegValue  The spare register value.
335   *
336   * @return     None
337   *
338   * @details    Write specify data to spare register.
339   * @note       This macro is effect only when SPRRDY(SPRCTL[7] SPR Register Ready) bit is set. \n
340   *             And its controlled by RTC Access Enable Register(RTC_RWEN).
341   * \hideinitializer
342   */
343 #define RTC_WRITE_SPARE_REGISTER(u32RegNum, u32RegValue)    (RTC->SPR[(u32RegNum)] = (u32RegValue))
344 
345 int32_t RTC_Open(S_RTC_TIME_DATA_T *sPt);
346 void RTC_Close(void);
347 int32_t RTC_32KCalibration(int32_t i32FrequencyX10000);
348 void RTC_GetDateAndTime(S_RTC_TIME_DATA_T *sPt);
349 void RTC_GetAlarmDateAndTime(S_RTC_TIME_DATA_T *sPt);
350 void RTC_SetDateAndTime(S_RTC_TIME_DATA_T *sPt);
351 void RTC_SetAlarmDateAndTime(S_RTC_TIME_DATA_T *sPt);
352 void RTC_SetDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day, uint32_t u32DayOfWeek);
353 void RTC_SetTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm);
354 void RTC_SetAlarmDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day);
355 void RTC_SetAlarmTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm);
356 void RTC_SetAlarmDateMask(uint8_t u8IsTenYMsk, uint8_t u8IsYMsk, uint8_t u8IsTenMMsk, uint8_t u8IsMMsk, uint8_t u8IsTenDMsk, uint8_t u8IsDMsk);
357 void RTC_SetAlarmTimeMask(uint8_t u8IsTenHMsk, uint8_t u8IsHMsk, uint8_t u8IsTenMMsk, uint8_t u8IsMMsk, uint8_t u8IsTenSMsk, uint8_t u8IsSMsk);
358 uint32_t RTC_GetDayOfWeek(void);
359 void RTC_SetTickPeriod(uint32_t u32TickSelection);
360 void RTC_EnableInt(uint32_t u32IntFlagMask);
361 void RTC_DisableInt(uint32_t u32IntFlagMask);
362 void RTC_EnableSpareAccess(void);
363 void RTC_DisableSpareRegister(void);
364 void RTC_TamperEnable(uint32_t u32TamperSelect, uint32_t u32DetecLevel, uint32_t u32DebounceEn);
365 void RTC_TamperDisable(uint32_t u32TamperSelect);
366 uint32_t RTC_SetClockSource(uint32_t u32ClkSrc);
367 void RTC_SetGPIOMode(uint32_t u32PFPin, uint32_t u32Mode, uint32_t u32DigitalCtl, uint32_t u32PullCtl, uint32_t u32OutputLevel);
368 void RTC_SetGPIOLevel(uint32_t u32PFPin, uint32_t u32OutputLevel);
369 
370 /*@}*/ /* end of group RTC_EXPORTED_FUNCTIONS */
371 
372 /*@}*/ /* end of group RTC_Driver */
373 
374 /*@}*/ /* end of group Standard_Driver */
375 
376 #ifdef __cplusplus
377 }
378 #endif
379 
380 #endif /* __RTC_H__ */
381 
382 /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/
383