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) 2021 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 /*---------------------------------------------------------------------------------------------------------*/
56 /* WDT Define Error Code                                                                                   */
57 /*---------------------------------------------------------------------------------------------------------*/
58 #define WDT_TIMEOUT         SystemCoreClock     /*!< WDT time-out counter (1 second time-out) \hideinitializer */
59 #define WDT_OK              ( 0L)               /*!< WDT operation OK \hideinitializer */
60 #define WDT_FAIL            (-1L)               /*!< WDT operation failed \hideinitializer */
61 #define WDT_ERR_TIMEOUT     (-2L)               /*!< WDT operation abort due to timeout error \hideinitializer */
62 
63 /**@}*/ /* end of group WDT_EXPORTED_CONSTANTS */
64 
65 /** @addtogroup WDT_EXPORTED_FUNCTIONS WDT Exported Functions
66   @{
67 */
68 
69 /**
70   * @brief      Clear WDT Reset System Flag
71   *
72   * @param      None
73   *
74   * @return     None
75   *
76   * @details    This macro clears WDT time-out reset system flag.
77   * \hideinitializer
78   */
79 #define WDT_CLEAR_RESET_FLAG()          (WDT->CTL = (WDT->CTL & ~(WDT_CTL_IF_Msk | WDT_CTL_WKF_Msk)) | WDT_CTL_RSTF_Msk)
80 
81 /**
82   * @brief      Clear WDT Time-out Interrupt Flag
83   *
84   * @param      None
85   *
86   * @return     None
87   *
88   * @details    This macro clears WDT time-out interrupt flag.
89   * \hideinitializer
90   */
91 #define WDT_CLEAR_TIMEOUT_INT_FLAG()    (WDT->CTL = (WDT->CTL & ~(WDT_CTL_RSTF_Msk | WDT_CTL_WKF_Msk)) | WDT_CTL_IF_Msk)
92 
93 /**
94   * @brief      Clear WDT Wake-up Flag
95   *
96   * @param      None
97   *
98   * @return     None
99   *
100   * @details    This macro clears WDT time-out wake-up system flag.
101   * \hideinitializer
102   */
103 #define WDT_CLEAR_TIMEOUT_WAKEUP_FLAG() (WDT->CTL = (WDT->CTL & ~(WDT_CTL_RSTF_Msk | WDT_CTL_IF_Msk)) | WDT_CTL_WKF_Msk)
104 
105 /**
106   * @brief      Get WDT Time-out Reset Flag
107   *
108   * @param      None
109   *
110   * @retval     0   WDT time-out reset system did not occur
111   * @retval     1   WDT time-out reset system occurred
112   *
113   * @details    This macro indicates system has been reset by WDT time-out reset or not.
114   * \hideinitializer
115   */
116 #define WDT_GET_RESET_FLAG()            ((WDT->CTL & WDT_CTL_RSTF_Msk)? 1UL : 0UL)
117 
118 /**
119   * @brief      Get WDT Time-out Interrupt Flag
120   *
121   * @param      None
122   *
123   * @retval     0   WDT time-out interrupt did not occur
124   * @retval     1   WDT time-out interrupt occurred
125   *
126   * @details    This macro indicates WDT time-out interrupt occurred or not.
127   * \hideinitializer
128   */
129 #define WDT_GET_TIMEOUT_INT_FLAG()      ((WDT->CTL & WDT_CTL_IF_Msk)? 1UL : 0UL)
130 
131 /**
132   * @brief      Get WDT Time-out Wake-up Flag
133   *
134   * @param      None
135   *
136   * @retval     0   WDT time-out interrupt does not cause CPU wake-up
137   * @retval     1   WDT time-out interrupt event cause CPU wake-up
138   *
139   * @details    This macro indicates WDT time-out interrupt event has waked up system or not.
140   * \hideinitializer
141   */
142 #define WDT_GET_TIMEOUT_WAKEUP_FLAG()   ((WDT->CTL & WDT_CTL_WKF_Msk)? 1UL : 0UL)
143 
144 /**
145   * @brief      Reset WDT Counter
146   *
147   * @param      None
148   *
149   * @return     None
150   *
151   * @details    This macro is used to reset the internal 20-bit WDT up counter value.
152   * @note       If WDT is activated and time-out reset system function is enabled also, user should \n
153   *             reset the 20-bit WDT up counter value to avoid generate WDT time-out reset signal to \n
154   *             reset system before the WDT time-out reset delay period expires.
155   * \hideinitializer
156   */
157 #define WDT_RESET_COUNTER()             (WDT->RSTCNT = WDT_RESET_COUNTER_KEYWORD)
158 
159 
160 /*---------------------------------------------------------------------------------------------------------*/
161 /* static inline functions                                                                                 */
162 /*---------------------------------------------------------------------------------------------------------*/
163 /* Declare these inline functions here to avoid MISRA C 2004 rule 8.1 error */
164 __STATIC_INLINE void WDT_Close(void);
165 __STATIC_INLINE void WDT_EnableInt(void);
166 __STATIC_INLINE void WDT_DisableInt(void);
167 
168 /**
169   * @brief      Stop WDT Counting
170   *
171   * @param      None
172   *
173   * @return     None
174   *
175   * @details    This function will stop WDT counting and disable WDT module.
176   */
WDT_Close(void)177 __STATIC_INLINE void WDT_Close(void)
178 {
179     uint32_t u32TimeOutCnt = WDT_TIMEOUT;
180 
181     WDT->CTL = 0UL;
182     while(WDT->CTL & WDT_CTL_SYNC_Msk) /* Wait disable WDTEN bit completed, it needs 2 * WDT_CLK. */
183     {
184         if(--u32TimeOutCnt == 0) break;
185     }
186 }
187 
188 /**
189   * @brief      Enable WDT Time-out Interrupt
190   *
191   * @param      None
192   *
193   * @return     None
194   *
195   * @details    This function will enable the WDT time-out interrupt function.
196   */
WDT_EnableInt(void)197 __STATIC_INLINE void WDT_EnableInt(void)
198 {
199     WDT->CTL |= WDT_CTL_INTEN_Msk;
200 }
201 
202 /**
203   * @brief      Disable WDT Time-out Interrupt
204   *
205   * @param      None
206   *
207   * @return     None
208   *
209   * @details    This function will disable the WDT time-out interrupt function.
210   */
WDT_DisableInt(void)211 __STATIC_INLINE void WDT_DisableInt(void)
212 {
213     /* Do not touch another write 1 clear bits */
214     WDT->CTL &= ~(WDT_CTL_INTEN_Msk | WDT_CTL_RSTF_Msk | WDT_CTL_IF_Msk | WDT_CTL_WKF_Msk);
215 }
216 
217 int32_t WDT_Open(uint32_t u32TimeoutInterval, uint32_t u32ResetDelay, uint32_t u32EnableReset, uint32_t u32EnableWakeup);
218 
219 /**@}*/ /* end of group WDT_EXPORTED_FUNCTIONS */
220 
221 /**@}*/ /* end of group WDT_Driver */
222 
223 /**@}*/ /* end of group Standard_Driver */
224 
225 #ifdef __cplusplus
226 }
227 #endif
228 
229 #endif /* __WDT_H__ */
230