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) 2016-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 
41 /*---------------------------------------------------------------------------------------------------------*/
42 /*  WDT  Reset Delay Period Constant Definitions                                                           */
43 /*---------------------------------------------------------------------------------------------------------*/
44 #define WDT_RESET_DELAY_1026CLK     (0UL << WDT_ALTCTL_RSTDSEL_Pos) /*!< Setting WDT reset delay period to 1026 * WDT clocks \hideinitializer */
45 #define WDT_RESET_DELAY_130CLK      (1UL << WDT_ALTCTL_RSTDSEL_Pos) /*!< Setting WDT reset delay period to 130 * WDT clocks \hideinitializer */
46 #define WDT_RESET_DELAY_18CLK       (2UL << WDT_ALTCTL_RSTDSEL_Pos) /*!< Setting WDT reset delay period to 18 * WDT clocks \hideinitializer */
47 #define WDT_RESET_DELAY_3CLK        (3UL << WDT_ALTCTL_RSTDSEL_Pos) /*!< Setting WDT reset delay period to 3 * WDT clocks \hideinitializer */
48 
49 /*---------------------------------------------------------------------------------------------------------*/
50 /*  WDT Free Reset Counter Keyword Constant Definitions                                                    */
51 /*---------------------------------------------------------------------------------------------------------*/
52 #define WDT_RESET_COUNTER_KEYWORD   (0x00005AA5UL)    /*!< Fill this value to WDT_RSTCNT register to free reset WDT counter \hideinitializer */
53 
54 /*@}*/ /* end of group WDT_EXPORTED_CONSTANTS */
55 
56 
57 /** @addtogroup WDT_EXPORTED_FUNCTIONS WDT Exported Functions
58   @{
59 */
60 
61 /**
62   * @brief      Clear WDT Reset System Flag
63   *
64   * @param      None
65   *
66   * @return     None
67   *
68   * @details    This macro clears WDT time-out reset system flag.
69   * \hideinitializer
70   */
71 #define WDT_CLEAR_RESET_FLAG()          (WDT->CTL = (WDT->CTL & ~(WDT_CTL_IF_Msk | WDT_CTL_WKF_Msk)) | WDT_CTL_RSTF_Msk)
72 
73 /**
74   * @brief      Clear WDT Time-out Interrupt Flag
75   *
76   * @param      None
77   *
78   * @return     None
79   *
80   * @details    This macro clears WDT time-out interrupt flag.
81   * \hideinitializer
82   */
83 #define WDT_CLEAR_TIMEOUT_INT_FLAG()    (WDT->CTL = (WDT->CTL & ~(WDT_CTL_RSTF_Msk | WDT_CTL_WKF_Msk)) | WDT_CTL_IF_Msk)
84 
85 /**
86   * @brief      Clear WDT Wake-up Flag
87   *
88   * @param      None
89   *
90   * @return     None
91   *
92   * @details    This macro clears WDT time-out wake-up system flag.
93   * \hideinitializer
94   */
95 #define WDT_CLEAR_TIMEOUT_WAKEUP_FLAG() (WDT->CTL = (WDT->CTL & ~(WDT_CTL_RSTF_Msk | WDT_CTL_IF_Msk)) | WDT_CTL_WKF_Msk)
96 
97 /**
98   * @brief      Get WDT Time-out Reset Flag
99   *
100   * @param      None
101   *
102   * @retval     0   WDT time-out reset system did not occur
103   * @retval     1   WDT time-out reset system occurred
104   *
105   * @details    This macro indicates system has been reset by WDT time-out reset or not.
106   * \hideinitializer
107   */
108 #define WDT_GET_RESET_FLAG()            ((WDT->CTL & WDT_CTL_RSTF_Msk)? 1UL : 0UL)
109 
110 /**
111   * @brief      Get WDT Time-out Interrupt Flag
112   *
113   * @param      None
114   *
115   * @retval     0   WDT time-out interrupt did not occur
116   * @retval     1   WDT time-out interrupt occurred
117   *
118   * @details    This macro indicates WDT time-out interrupt occurred or not.
119   * \hideinitializer
120   */
121 #define WDT_GET_TIMEOUT_INT_FLAG()      ((WDT->CTL & WDT_CTL_IF_Msk)? 1UL : 0UL)
122 
123 /**
124   * @brief      Get WDT Time-out Wake-up Flag
125   *
126   * @param      None
127   *
128   * @retval     0   WDT time-out interrupt does not cause CPU wake-up
129   * @retval     1   WDT time-out interrupt event cause CPU wake-up
130   *
131   * @details    This macro indicates WDT time-out interrupt event has waked up system or not.
132   * \hideinitializer
133   */
134 #define WDT_GET_TIMEOUT_WAKEUP_FLAG()   ((WDT->CTL & WDT_CTL_WKF_Msk)? 1UL : 0UL)
135 
136 /**
137   * @brief      Reset WDT Counter
138   *
139   * @param      None
140   *
141   * @return     None
142   *
143   * @details    This macro is used to reset the internal 18-bit WDT up counter value.
144         If WDT is activated and time-out reset system function is enabled also, user should \n
145   *             reset the 18-bit WDT up counter value to avoid generate WDT time-out reset signal to \n
146   *             reset system before the WDT time-out reset delay period expires.
147   * \hideinitializer
148   */
149 #define WDT_RESET_COUNTER()             (WDT->RSTCNT = WDT_RESET_COUNTER_KEYWORD)
150 
151 
152 /*---------------------------------------------------------------------------------------------------------*/
153 /* static inline functions                                                                                 */
154 /*---------------------------------------------------------------------------------------------------------*/
155 /* Declare these inline functions here to avoid MISRA C 2004 rule 8.1 error */
156 __STATIC_INLINE void WDT_Close(void);
157 __STATIC_INLINE void WDT_EnableInt(void);
158 __STATIC_INLINE void WDT_DisableInt(void);
159 
160 /**
161   * @brief      Stop WDT Counting
162   *
163   * @param      None
164   *
165   * @return     None
166   *
167   * @details    This function will stop WDT counting and disable WDT module.
168   */
WDT_Close(void)169 __STATIC_INLINE void WDT_Close(void)
170 {
171     WDT->CTL = 0UL;
172     while(WDT->CTL & WDT_CTL_SYNC_Msk) {} /* Wait disable WDTEN bit completed, it needs 2 * WDT_CLK. */
173 }
174 
175 /**
176   * @brief      Enable WDT Time-out Interrupt
177   *
178   * @param      None
179   *
180   * @return     None
181   *
182   * @details    This function will enable the WDT time-out interrupt function.
183   */
WDT_EnableInt(void)184 __STATIC_INLINE void WDT_EnableInt(void)
185 {
186     WDT->CTL |= WDT_CTL_INTEN_Msk;
187     while(WDT->CTL & WDT_CTL_SYNC_Msk) {} /* Wait enable WDTEN bit completed, it needs 2 * WDT_CLK. */
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 
219