1 /**************************************************************************//**
2  * @file     wdt.h
3  * @version  V3.00
4  * @brief    M480 series WDT 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 __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   * @note       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 /* Declare these inline functions here to avoid MISRA C 2004 rule 8.1 error */
152 __STATIC_INLINE void WDT_Close(void);
153 __STATIC_INLINE void WDT_EnableInt(void);
154 __STATIC_INLINE void WDT_DisableInt(void);
155 
156 /**
157   * @brief      Stop WDT Counting
158   *
159   * @param      None
160   *
161   * @return     None
162   *
163   * @details    This function will stop WDT counting and disable WDT module.
164   */
WDT_Close(void)165 __STATIC_INLINE void WDT_Close(void)
166 {
167     WDT->CTL = 0UL;
168     return;
169 }
170 
171 /**
172   * @brief      Enable WDT Time-out Interrupt
173   *
174   * @param      None
175   *
176   * @return     None
177   *
178   * @details    This function will enable the WDT time-out interrupt function.
179   */
WDT_EnableInt(void)180 __STATIC_INLINE void WDT_EnableInt(void)
181 {
182     WDT->CTL |= WDT_CTL_INTEN_Msk;
183     return;
184 }
185 
186 /**
187   * @brief      Disable WDT Time-out Interrupt
188   *
189   * @param      None
190   *
191   * @return     None
192   *
193   * @details    This function will disable the WDT time-out interrupt function.
194   */
WDT_DisableInt(void)195 __STATIC_INLINE void WDT_DisableInt(void)
196 {
197     /* Do not touch another write 1 clear bits */
198     WDT->CTL &= ~(WDT_CTL_INTEN_Msk | WDT_CTL_RSTF_Msk | WDT_CTL_IF_Msk | WDT_CTL_WKF_Msk);
199     return;
200 }
201 
202 void WDT_Open(uint32_t u32TimeoutInterval, uint32_t u32ResetDelay, uint32_t u32EnableReset, uint32_t u32EnableWakeup);
203 
204 /*@}*/ /* end of group WDT_EXPORTED_FUNCTIONS */
205 
206 /*@}*/ /* end of group WDT_Driver */
207 
208 /*@}*/ /* end of group Standard_Driver */
209 
210 #ifdef __cplusplus
211 }
212 #endif
213 
214 #endif /* __WDT_H__ */
215 
216 /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/
217