1 /**************************************************************************//**
2  * @file     wdt.h
3  * @version  V3.00
4  * @brief    Watchdog Timer(WDT) 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 __WDT_H__
10 #define __WDT_H__
11 
12 #ifdef __cplusplus
13 extern "C"
14 {
15 #endif
16 
17 
18 /** @addtogroup Standard_Driver Standard Driver
19   @{
20 */
21 
22 /** @addtogroup WDT_Driver WDT Driver
23   @{
24 */
25 
26 /** @addtogroup WDT_EXPORTED_CONSTANTS WDT Exported Constants
27   @{
28 */
29 /*---------------------------------------------------------------------------------------------------------*/
30 /*  WDT Time-out Interval Period Constant Definitions                                                      */
31 /*---------------------------------------------------------------------------------------------------------*/
32 #define WDT_TIMEOUT_2POW4           (0UL << WDT_CTL_TOUTSEL_Pos) /*!< Setting WDT time-out interval to 2^4 * WDT clocks \hideinitializer */
33 #define WDT_TIMEOUT_2POW6           (1UL << WDT_CTL_TOUTSEL_Pos) /*!< Setting WDT time-out interval to 2^6 * WDT clocks \hideinitializer */
34 #define WDT_TIMEOUT_2POW8           (2UL << WDT_CTL_TOUTSEL_Pos) /*!< Setting WDT time-out interval to 2^8 * WDT clocks \hideinitializer */
35 #define WDT_TIMEOUT_2POW10          (3UL << WDT_CTL_TOUTSEL_Pos) /*!< Setting WDT time-out interval to 2^10 * WDT clocks \hideinitializer */
36 #define WDT_TIMEOUT_2POW12          (4UL << WDT_CTL_TOUTSEL_Pos) /*!< Setting WDT time-out interval to 2^12 * WDT clocks \hideinitializer */
37 #define WDT_TIMEOUT_2POW14          (5UL << WDT_CTL_TOUTSEL_Pos) /*!< Setting WDT time-out interval to 2^14 * WDT clocks \hideinitializer */
38 #define WDT_TIMEOUT_2POW16          (6UL << WDT_CTL_TOUTSEL_Pos) /*!< Setting WDT time-out interval to 2^16 * WDT clocks \hideinitializer */
39 #define WDT_TIMEOUT_2POW18          (7UL << WDT_CTL_TOUTSEL_Pos) /*!< Setting WDT time-out interval to 2^18 * WDT clocks \hideinitializer */
40 #define WDT_TIMEOUT_2POW20          (8UL << WDT_CTL_TOUTSEL_Pos) /*!< Setting WDT time-out interval to 2^20 * WDT clocks \hideinitializer */
41 
42 /*---------------------------------------------------------------------------------------------------------*/
43 /*  WDT  Reset Delay Period Constant Definitions                                                           */
44 /*---------------------------------------------------------------------------------------------------------*/
45 #define WDT_RESET_DELAY_1026CLK     (0UL << WDT_ALTCTL_RSTDSEL_Pos) /*!< Setting WDT reset delay period to 1026 * WDT clocks \hideinitializer */
46 #define WDT_RESET_DELAY_130CLK      (1UL << WDT_ALTCTL_RSTDSEL_Pos) /*!< Setting WDT reset delay period to 130 * WDT clocks \hideinitializer */
47 #define WDT_RESET_DELAY_18CLK       (2UL << WDT_ALTCTL_RSTDSEL_Pos) /*!< Setting WDT reset delay period to 18 * WDT clocks \hideinitializer */
48 #define WDT_RESET_DELAY_3CLK        (3UL << WDT_ALTCTL_RSTDSEL_Pos) /*!< Setting WDT reset delay period to 3 * WDT clocks \hideinitializer */
49 
50 /*---------------------------------------------------------------------------------------------------------*/
51 /*  WDT Free Reset Counter Keyword Constant Definitions                                                    */
52 /*---------------------------------------------------------------------------------------------------------*/
53 #define WDT_RESET_COUNTER_KEYWORD   (0x00005AA5UL)    /*!< Fill this value to WDT_RSTCNT register to free reset WDT counter \hideinitializer */
54 
55 /**@}*/ /* end of group WDT_EXPORTED_CONSTANTS */
56 
57 
58 /** @addtogroup WDT_EXPORTED_FUNCTIONS WDT Exported Functions
59   @{
60 */
61 
62 /**
63   * @brief      Clear WDT Reset System Flag
64   *
65   * @param      None
66   *
67   * @return     None
68   *
69   * @details    This macro clears WDT time-out reset system flag.
70   * \hideinitializer
71   */
72 #define WDT_CLEAR_RESET_FLAG()          (WDT->CTL = (WDT->CTL & ~(WDT_CTL_IF_Msk | WDT_CTL_WKF_Msk)) | WDT_CTL_RSTF_Msk)
73 
74 /**
75   * @brief      Clear WDT Time-out Interrupt Flag
76   *
77   * @param      None
78   *
79   * @return     None
80   *
81   * @details    This macro clears WDT time-out interrupt flag.
82   * \hideinitializer
83   */
84 #define WDT_CLEAR_TIMEOUT_INT_FLAG()    (WDT->CTL = (WDT->CTL & ~(WDT_CTL_RSTF_Msk | WDT_CTL_WKF_Msk)) | WDT_CTL_IF_Msk)
85 
86 /**
87   * @brief      Clear WDT Wake-up Flag
88   *
89   * @param      None
90   *
91   * @return     None
92   *
93   * @details    This macro clears WDT time-out wake-up system flag.
94   * \hideinitializer
95   */
96 #define WDT_CLEAR_TIMEOUT_WAKEUP_FLAG() (WDT->CTL = (WDT->CTL & ~(WDT_CTL_RSTF_Msk | WDT_CTL_IF_Msk)) | WDT_CTL_WKF_Msk)
97 
98 /**
99   * @brief      Get WDT Time-out Reset Flag
100   *
101   * @param      None
102   *
103   * @retval     0   WDT time-out reset system did not occur
104   * @retval     1   WDT time-out reset system occurred
105   *
106   * @details    This macro indicates system has been reset by WDT time-out reset or not.
107   * \hideinitializer
108   */
109 #define WDT_GET_RESET_FLAG()            ((WDT->CTL & WDT_CTL_RSTF_Msk)? 1UL : 0UL)
110 
111 /**
112   * @brief      Get WDT Time-out Interrupt Flag
113   *
114   * @param      None
115   *
116   * @retval     0   WDT time-out interrupt did not occur
117   * @retval     1   WDT time-out interrupt occurred
118   *
119   * @details    This macro indicates WDT time-out interrupt occurred or not.
120   * \hideinitializer
121   */
122 #define WDT_GET_TIMEOUT_INT_FLAG()      ((WDT->CTL & WDT_CTL_IF_Msk)? 1UL : 0UL)
123 
124 /**
125   * @brief      Get WDT Time-out Wake-up Flag
126   *
127   * @param      None
128   *
129   * @retval     0   WDT time-out interrupt does not cause CPU wake-up
130   * @retval     1   WDT time-out interrupt event cause CPU wake-up
131   *
132   * @details    This macro indicates WDT time-out interrupt event has waked up system or not.
133   * \hideinitializer
134   */
135 #define WDT_GET_TIMEOUT_WAKEUP_FLAG()   ((WDT->CTL & WDT_CTL_WKF_Msk)? 1UL : 0UL)
136 
137 /**
138   * @brief      Reset WDT Counter
139   *
140   * @param      None
141   *
142   * @return     None
143   *
144   * @details    This macro is used to reset the internal 20-bit WDT up counter value.
145   * @note       If WDT is activated and time-out reset system function is enabled also, user should \n
146   *             reset the 20-bit WDT up counter value to avoid generate WDT time-out reset signal to \n
147   *             reset system before the WDT time-out reset delay period expires.
148   * \hideinitializer
149   */
150 #define WDT_RESET_COUNTER()             (WDT->RSTCNT = WDT_RESET_COUNTER_KEYWORD)
151 
152 
153 /*---------------------------------------------------------------------------------------------------------*/
154 /* static inline functions                                                                                 */
155 /*---------------------------------------------------------------------------------------------------------*/
156 /* Declare these inline functions here to avoid MISRA C 2004 rule 8.1 error */
157 __STATIC_INLINE void WDT_Close(void);
158 __STATIC_INLINE void WDT_EnableInt(void);
159 __STATIC_INLINE void WDT_DisableInt(void);
160 
161 /**
162   * @brief      Stop WDT Counting
163   *
164   * @param      None
165   *
166   * @return     None
167   *
168   * @details    This function will stop WDT counting and disable WDT module.
169   */
WDT_Close(void)170 __STATIC_INLINE void WDT_Close(void)
171 {
172     WDT->CTL = 0UL;
173     while(WDT->CTL & WDT_CTL_SYNC_Msk) {} /* Wait disable WDTEN bit completed, it needs 2 * WDT_CLK. */
174 }
175 
176 /**
177   * @brief      Enable WDT Time-out Interrupt
178   *
179   * @param      None
180   *
181   * @return     None
182   *
183   * @details    This function will enable the WDT time-out interrupt function.
184   */
WDT_EnableInt(void)185 __STATIC_INLINE void WDT_EnableInt(void)
186 {
187     WDT->CTL |= WDT_CTL_INTEN_Msk;
188 }
189 
190 /**
191   * @brief      Disable WDT Time-out Interrupt
192   *
193   * @param      None
194   *
195   * @return     None
196   *
197   * @details    This function will disable the WDT time-out interrupt function.
198   */
WDT_DisableInt(void)199 __STATIC_INLINE void WDT_DisableInt(void)
200 {
201     /* Do not touch another write 1 clear bits */
202     WDT->CTL &= ~(WDT_CTL_INTEN_Msk | WDT_CTL_RSTF_Msk | WDT_CTL_IF_Msk | WDT_CTL_WKF_Msk);
203 }
204 
205 void WDT_Open(uint32_t u32TimeoutInterval, uint32_t u32ResetDelay, uint32_t u32EnableReset, uint32_t u32EnableWakeup);
206 
207 /**@}*/ /* end of group WDT_EXPORTED_FUNCTIONS */
208 
209 /**@}*/ /* end of group WDT_Driver */
210 
211 /**@}*/ /* end of group Standard_Driver */
212 
213 #ifdef __cplusplus
214 }
215 #endif
216 
217 #endif /* __WDT_H__ */
218