1 /**************************************************************************//**
2  * @file     rtc.h
3  * @version  V3.00
4  * @brief    M480 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 #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_WAIT_COUNT          0xFFFFFFFFUL      /*!< Initial Time-out Value \hideinitializer */
70 #define RTC_YEAR2000            2000UL            /*!< RTC Reference for compute year data \hideinitializer */
71 #define RTC_FCR_REFERENCE       32761UL           /*!< RTC Reference for frequency compensation \hideinitializer */
72 
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 MAX_TAMPER_PIN_NUM 6ul              /*!< Tamper Pin number \hideinitializer */
81 
82 #define RTC_TAMPER_HIGH_LEVEL_DETECT 1ul    /*!< Tamper pin detect voltage level is high \hideinitializer */
83 #define RTC_TAMPER_LOW_LEVEL_DETECT  0ul    /*!< Tamper pin detect voltage level is low  \hideinitializer */
84 
85 #define RTC_TAMPER_DEBOUNCE_ENABLE   1ul    /*!< Enable RTC tamper pin de-bounce function \hideinitializer */
86 #define RTC_TAMPER_DEBOUNCE_DISABLE  0ul    /*!< Disable 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 MAX_PAIR_NUM     3ul                /*!< Pair number \hideinitializer */
92 
93 #define RTC_2POW10_CLK  (0x0 << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 RTC clock cycles \hideinitializer */
94 #define RTC_2POW11_CLK  (0x1 << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 2 RTC clock cycles \hideinitializer */
95 #define RTC_2POW12_CLK  (0x2 << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 4 RTC clock cycles \hideinitializer */
96 #define RTC_2POW13_CLK  (0x3 << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 6 RTC clock cycles \hideinitializer */
97 #define RTC_2POW14_CLK  (0x4 << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 8 RTC clock cycles \hideinitializer */
98 #define RTC_2POW15_CLK  (0x5 << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 16 RTC clock cycles \hideinitializer */
99 #define RTC_2POW16_CLK  (0x6 << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 32 RTC clock cycles \hideinitializer */
100 #define RTC_2POW17_CLK  (0x7 << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 64 RTC clock cycles \hideinitializer */
101 
102 #define REF_RANDOM_PATTERN    0x0 /*!< The new reference pattern is generated by random number generator when the reference pattern run out \hideinitializer */
103 #define REF_PREVIOUS_PATTERN  0x1 /*!< The new reference pattern is repeated previous random value when the reference pattern run out \hideinitializer */
104 #define REF_SEED              0x3 /*!< The new reference pattern is repeated from SEED (RTC_TAMPSEED[31:0]) when the reference pattern run out \hideinitializer */
105 
106 /*@}*/ /* end of group RTC_EXPORTED_CONSTANTS */
107 
108 
109 /** @addtogroup RTC_EXPORTED_STRUCTS RTC Exported Structs
110   @{
111 */
112 /**
113   * @details    RTC define Time Data Struct
114   */
115 typedef struct
116 {
117     uint32_t u32Year;           /*!< Year value */
118     uint32_t u32Month;          /*!< Month value */
119     uint32_t u32Day;            /*!< Day value */
120     uint32_t u32DayOfWeek;      /*!< Day of week value */
121     uint32_t u32Hour;           /*!< Hour value */
122     uint32_t u32Minute;         /*!< Minute value */
123     uint32_t u32Second;         /*!< Second value */
124     uint32_t u32TimeScale;      /*!< 12-Hour, 24-Hour */
125     uint32_t u32AmPm;           /*!< Only Time Scale select 12-hr used */
126 } S_RTC_TIME_DATA_T;
127 
128 /*@}*/ /* end of group RTC_EXPORTED_STRUCTS */
129 
130 
131 /** @addtogroup RTC_EXPORTED_FUNCTIONS RTC Exported Functions
132   @{
133 */
134 
135 /**
136   * @brief      Indicate is Leap Year or not
137   *
138   * @param      None
139   *
140   * @retval     0   This year is not a leap year
141   * @retval     1   This year is a leap year
142   *
143   * @details    According to current date, return this year is leap year or not.
144   * \hideinitializer
145   */
146 #define RTC_IS_LEAP_YEAR()              (RTC->LEAPYEAR & RTC_LEAPYEAR_LEAPYEAR_Msk ? 1:0)
147 
148 /**
149   * @brief      Clear RTC Alarm Interrupt Flag
150   *
151   * @param      None
152   *
153   * @return     None
154   *
155   * @details    This macro is used to clear RTC alarm interrupt flag.
156   * \hideinitializer
157   */
158 #define RTC_CLEAR_ALARM_INT_FLAG()      (RTC->INTSTS = RTC_INTSTS_ALMIF_Msk)
159 
160 /**
161   * @brief      Clear RTC Tick Interrupt Flag
162   *
163   * @param      None
164   *
165   * @return     None
166   *
167   * @details    This macro is used to clear RTC tick interrupt flag.
168   * \hideinitializer
169   */
170 #define RTC_CLEAR_TICK_INT_FLAG()       (RTC->INTSTS = RTC_INTSTS_TICKIF_Msk)
171 
172 /**
173   * @brief      Clear RTC Tamper Interrupt Flag
174   *
175   * @param      u32TamperFlag   Tamper interrupt flag. It consists of:    \n
176   *                             - \ref RTC_INTSTS_TAMP0IF_Msk    \n
177   *                             - \ref RTC_INTSTS_TAMP1IF_Msk    \n
178   *                             - \ref RTC_INTSTS_TAMP2IF_Msk    \n
179   *                             - \ref RTC_INTSTS_TAMP3IF_Msk    \n
180   *                             - \ref RTC_INTSTS_TAMP4IF_Msk    \n
181   *                             - \ref RTC_INTSTS_TAMP5IF_Msk
182   *
183   * @return     None
184   *
185   * @details    This macro is used to clear RTC snooper pin interrupt flag.
186   * \hideinitializer
187   */
188 #define RTC_CLEAR_TAMPER_INT_FLAG(u32TamperFlag)    (RTC->INTSTS = (u32TamperFlag))
189 
190 /**
191   * @brief      Get RTC Alarm Interrupt Flag
192   *
193   * @param      None
194   *
195   * @retval     0   RTC alarm interrupt did not occur
196   * @retval     1   RTC alarm interrupt occurred
197   *
198   * @details    This macro indicates RTC alarm interrupt occurred or not.
199   * \hideinitializer
200   */
201 #define RTC_GET_ALARM_INT_FLAG()        ((RTC->INTSTS & RTC_INTSTS_ALMIF_Msk)? 1:0)
202 
203 /**
204   * @brief      Get RTC Time Tick Interrupt Flag
205   *
206   * @param      None
207   *
208   * @retval     0   RTC time tick interrupt did not occur
209   * @retval     1   RTC time tick interrupt occurred
210   *
211   * @details    This macro indicates RTC time tick interrupt occurred or not.
212   * \hideinitializer
213   */
214 #define RTC_GET_TICK_INT_FLAG()         ((RTC->INTSTS & RTC_INTSTS_TICKIF_Msk)? 1:0)
215 
216 /**
217   * @brief      Get RTC Tamper Interrupt Flag
218   *
219   * @param      None
220   *
221   * @retval     0   RTC snooper pin interrupt did not occur
222   * @retval     1   RTC snooper pin interrupt occurred
223   *
224   * @details    This macro indicates RTC snooper pin interrupt occurred or not.
225   * \hideinitializer
226   */
227 #define RTC_GET_TAMPER_INT_FLAG()      ((RTC->INTSTS & (0x3F00))? 1:0)
228 
229 /**
230   * @brief      Get RTC TAMPER Interrupt Status
231   *
232   * @param      None
233   *
234   * @retval     RTC_INTSTS_TAMP0IF_Msk    Tamper 0 interrupt flag is generated
235   * @retval     RTC_INTSTS_TAMP1IF_Msk    Tamper 1 interrupt flag is generated
236   * @retval     RTC_INTSTS_TAMP2IF_Msk    Tamper 2 interrupt flag is generated
237   * @retval     RTC_INTSTS_TAMP3IF_Msk    Tamper 3 interrupt flag is generated
238   * @retval     RTC_INTSTS_TAMP4IF_Msk    Tamper 4 interrupt flag is generated
239   * @retval     RTC_INTSTS_TAMP5IF_Msk    Tamper 5 interrupt flag is generated
240   *
241   * @details    This macro indicates RTC snooper pin interrupt occurred or not.
242   * \hideinitializer
243   */
244 #define RTC_GET_TAMPER_INT_STATUS()      ((RTC->INTSTS & (0x3F00)))
245 
246 /**
247   * @brief      Read Spare Register
248   *
249   * @param[in]  u32RegNum   The spare register number, 0~19.
250   *
251   * @return     Spare register content
252   *
253   * @details    Read the specify spare register content.
254   * @note       The returned value is valid only when SPRRDY(SPRCTL[7] SPR Register Ready) bit is set. \n
255   *             And its controlled by RTC Access Enable Register.
256   * \hideinitializer
257   */
258 #define RTC_READ_SPARE_REGISTER(u32RegNum)                  (RTC->SPR[(u32RegNum)])
259 
260 /**
261   * @brief      Write Spare Register
262   *
263   * @param[in]  u32RegNum    The spare register number, 0~19.
264   * @param[in]  u32RegValue  The spare register value.
265   *
266   * @return     None
267   *
268   * @details    Write specify data to spare register.
269   * @note       This macro is effect only when SPRRDY(SPRCTL[7] SPR Register Ready) bit is set. \n
270   *             And its controlled by RTC Access Enable Register(RTC_RWEN).
271   * \hideinitializer
272   */
273 #define RTC_WRITE_SPARE_REGISTER(u32RegNum, u32RegValue)    (RTC->SPR[(u32RegNum)] = (u32RegValue))
274 
275 /* Declare these inline functions here to avoid MISRA C 2004 rule 8.1 error */
276 __STATIC_INLINE void RTC_WaitAccessEnable(void);
277 
278 /**
279   * @brief      Wait RTC Access Enable
280   *
281   * @param      None
282   *
283   * @return     None
284   *
285   * @details    This function is used to enable the maximum RTC read/write accessible time.
286   */
RTC_WaitAccessEnable(void)287 __STATIC_INLINE void RTC_WaitAccessEnable(void)
288 {
289     while((RTC->RWEN & RTC_RWEN_RTCBUSY_Msk) == RTC_RWEN_RTCBUSY_Msk)
290     {
291     }
292 
293     if(!(SYS->CSERVER & 0x1))
294     {
295         /* To wait RWENF bit is cleared and enable RWENF bit (Access Enable bit) again */
296         RTC->RWEN = RTC_WRITE_KEY;
297     }
298 
299     /* To wait RWENF bit is set and user can access the protected-register of RTC from now on */
300     while((RTC->RWEN & RTC_RWEN_RWENF_Msk) == (uint32_t)0x0)
301     {
302     }
303 }
304 
305 void RTC_Open(S_RTC_TIME_DATA_T *sPt);
306 void RTC_Close(void);
307 void RTC_32KCalibration(int32_t i32FrequencyX10000);
308 void RTC_GetDateAndTime(S_RTC_TIME_DATA_T *sPt);
309 void RTC_GetAlarmDateAndTime(S_RTC_TIME_DATA_T *sPt);
310 void RTC_SetDateAndTime(S_RTC_TIME_DATA_T *sPt);
311 void RTC_SetAlarmDateAndTime(S_RTC_TIME_DATA_T *sPt);
312 void RTC_SetDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day, uint32_t u32DayOfWeek);
313 void RTC_SetTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm);
314 void RTC_SetAlarmDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day);
315 void RTC_SetAlarmTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm);
316 void RTC_SetAlarmDateMask(uint8_t u8IsTenYMsk, uint8_t u8IsYMsk, uint8_t u8IsTenMMsk, uint8_t u8IsMMsk, uint8_t u8IsTenDMsk, uint8_t u8IsDMsk);
317 void RTC_SetAlarmTimeMask(uint8_t u8IsTenHMsk, uint8_t u8IsHMsk, uint8_t u8IsTenMMsk, uint8_t u8IsMMsk, uint8_t u8IsTenSMsk, uint8_t u8IsSMsk);
318 uint32_t RTC_GetDayOfWeek(void);
319 void RTC_SetTickPeriod(uint32_t u32TickSelection);
320 void RTC_EnableInt(uint32_t u32IntFlagMask);
321 void RTC_DisableInt(uint32_t u32IntFlagMask);
322 void RTC_EnableSpareAccess(void);
323 void RTC_DisableSpareRegister(void);
324 void RTC_StaticTamperEnable(uint32_t u32TamperSelect, uint32_t u32DetecLevel, uint32_t u32DebounceEn);
325 void RTC_StaticTamperDisable(uint32_t u32TamperSelect);
326 void RTC_DynamicTamperEnable(uint32_t u32PairSel, uint32_t u32DebounceEn, uint32_t u32Pair1Source, uint32_t u32Pair2Source);
327 void RTC_DynamicTamperDisable(uint32_t u32PairSel);
328 void RTC_DynamicTamperConfig(uint32_t u32ChangeRate, uint32_t u32SeedReload, uint32_t u32RefPattern, uint32_t u32Seed);
329 
330 /*@}*/ /* end of group RTC_EXPORTED_FUNCTIONS */
331 
332 /*@}*/ /* end of group RTC_Driver */
333 
334 /*@}*/ /* end of group Standard_Driver */
335 
336 #ifdef __cplusplus
337 }
338 #endif
339 
340 #endif /* __RTC_H__ */
341 
342 /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/
343