1 /**************************************************************************//**
2  * @file     ewdt.h
3  * @version  V3.00
4  * @brief    Extra Watchdog Timer(EWDT) 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 __EWDT_H__
10 #define __EWDT_H__
11 
12 #ifdef __cplusplus
13 extern "C"
14 {
15 #endif
16 
17 
18 /** @addtogroup Standard_Driver Standard Driver
19   @{
20 */
21 
22 /** @addtogroup EWDT_Driver EWDT Driver
23   @{
24 */
25 
26 /** @addtogroup EWDT_EXPORTED_CONSTANTS EWDT Exported Constants
27   @{
28 */
29 /*---------------------------------------------------------------------------------------------------------*/
30 /*  EWDT Time-out Interval Period Constant Definitions                                                     */
31 /*---------------------------------------------------------------------------------------------------------*/
32 #define EWDT_TIMEOUT_2POW4           (0UL << EWDT_CTL_TOUTSEL_Pos) /*!< Setting EWDT time-out interval to 2^4 * EWDT clocks \hideinitializer */
33 #define EWDT_TIMEOUT_2POW6           (1UL << EWDT_CTL_TOUTSEL_Pos) /*!< Setting EWDT time-out interval to 2^6 * EWDT clocks \hideinitializer */
34 #define EWDT_TIMEOUT_2POW8           (2UL << EWDT_CTL_TOUTSEL_Pos) /*!< Setting EWDT time-out interval to 2^8 * EWDT clocks \hideinitializer */
35 #define EWDT_TIMEOUT_2POW10          (3UL << EWDT_CTL_TOUTSEL_Pos) /*!< Setting EWDT time-out interval to 2^10 * EWDT clocks \hideinitializer */
36 #define EWDT_TIMEOUT_2POW12          (4UL << EWDT_CTL_TOUTSEL_Pos) /*!< Setting EWDT time-out interval to 2^12 * EWDT clocks \hideinitializer */
37 #define EWDT_TIMEOUT_2POW14          (5UL << EWDT_CTL_TOUTSEL_Pos) /*!< Setting EWDT time-out interval to 2^14 * EWDT clocks \hideinitializer */
38 #define EWDT_TIMEOUT_2POW16          (6UL << EWDT_CTL_TOUTSEL_Pos) /*!< Setting EWDT time-out interval to 2^16 * EWDT clocks \hideinitializer */
39 #define EWDT_TIMEOUT_2POW18          (7UL << EWDT_CTL_TOUTSEL_Pos) /*!< Setting EWDT time-out interval to 2^18 * EWDT clocks \hideinitializer */
40 #define EWDT_TIMEOUT_2POW20          (8UL << EWDT_CTL_TOUTSEL_Pos) /*!< Setting EWDT time-out interval to 2^20 * EWDT clocks \hideinitializer */
41 
42 /*---------------------------------------------------------------------------------------------------------*/
43 /*  EWDT Reset Delay Period Constant Definitions                                                           */
44 /*---------------------------------------------------------------------------------------------------------*/
45 #define EWDT_RESET_DELAY_1026CLK     (0UL << EWDT_ALTCTL_RSTDSEL_Pos) /*!< Setting EWDT reset delay period to 1026 * EWDT clocks \hideinitializer */
46 #define EWDT_RESET_DELAY_130CLK      (1UL << EWDT_ALTCTL_RSTDSEL_Pos) /*!< Setting EWDT reset delay period to 130 * EWDT clocks \hideinitializer */
47 #define EWDT_RESET_DELAY_18CLK       (2UL << EWDT_ALTCTL_RSTDSEL_Pos) /*!< Setting EWDT reset delay period to 18 * EWDT clocks \hideinitializer */
48 #define EWDT_RESET_DELAY_3CLK        (3UL << EWDT_ALTCTL_RSTDSEL_Pos) /*!< Setting EWDT reset delay period to 3 * EWDT clocks \hideinitializer */
49 
50 /*---------------------------------------------------------------------------------------------------------*/
51 /*  EWDT Free Reset Counter Keyword Constant Definitions                                                   */
52 /*---------------------------------------------------------------------------------------------------------*/
53 #define EWDT_RESET_COUNTER_KEYWORD   (0x00005AA5UL)    /*!< Fill this value to EWDT_RSTCNT register to free reset EWDT counter \hideinitializer */
54 
55 /**@}*/ /* end of group EWDT_EXPORTED_CONSTANTS */
56 
57 
58 /** @addtogroup EWDT_EXPORTED_FUNCTIONS EWDT Exported Functions
59   @{
60 */
61 
62 /**
63   * @brief      Clear EWDT Reset System Flag
64   *
65   * @param      None
66   *
67   * @return     None
68   *
69   * @details    This macro clears EWDT time-out reset system flag.
70   * \hideinitializer
71   */
72 #define EWDT_CLEAR_RESET_FLAG()          (EWDT->CTL = (EWDT->CTL & ~(EWDT_CTL_IF_Msk | EWDT_CTL_WKF_Msk)) | EWDT_CTL_RSTF_Msk)
73 
74 /**
75   * @brief      Clear EWDT Time-out Interrupt Flag
76   *
77   * @param      None
78   *
79   * @return     None
80   *
81   * @details    This macro clears EWDT time-out interrupt flag.
82   * \hideinitializer
83   */
84 #define EWDT_CLEAR_TIMEOUT_INT_FLAG()    (EWDT->CTL = (EWDT->CTL & ~(EWDT_CTL_RSTF_Msk | EWDT_CTL_WKF_Msk)) | EWDT_CTL_IF_Msk)
85 
86 /**
87   * @brief      Clear EWDT Wake-up Flag
88   *
89   * @param      None
90   *
91   * @return     None
92   *
93   * @details    This macro clears EWDT time-out wake-up system flag.
94   * \hideinitializer
95   */
96 #define EWDT_CLEAR_TIMEOUT_WAKEUP_FLAG() (EWDT->CTL = (EWDT->CTL & ~(EWDT_CTL_RSTF_Msk | EWDT_CTL_IF_Msk)) | EWDT_CTL_WKF_Msk)
97 
98 /**
99   * @brief      Get EWDT Time-out Reset Flag
100   *
101   * @param      None
102   *
103   * @retval     0   EWDT time-out reset system did not occur
104   * @retval     1   EWDT time-out reset system occurred
105   *
106   * @details    This macro indicates system has been reset by EWDT time-out reset or not.
107   * \hideinitializer
108   */
109 #define EWDT_GET_RESET_FLAG()            ((EWDT->CTL & EWDT_CTL_RSTF_Msk)? 1UL : 0UL)
110 
111 /**
112   * @brief      Get EWDT Time-out Interrupt Flag
113   *
114   * @param      None
115   *
116   * @retval     0   EWDT time-out interrupt did not occur
117   * @retval     1   EWDT time-out interrupt occurred
118   *
119   * @details    This macro indicates EWDT time-out interrupt occurred or not.
120   * \hideinitializer
121   */
122 #define EWDT_GET_TIMEOUT_INT_FLAG()      ((EWDT->CTL & EWDT_CTL_IF_Msk)? 1UL : 0UL)
123 
124 /**
125   * @brief      Get EWDT Time-out Wake-up Flag
126   *
127   * @param      None
128   *
129   * @retval     0   EWDT time-out interrupt does not cause CPU wake-up
130   * @retval     1   EWDT time-out interrupt event cause CPU wake-up
131   *
132   * @details    This macro indicates EWDT time-out interrupt event has waked up system or not.
133   * \hideinitializer
134   */
135 #define EWDT_GET_TIMEOUT_WAKEUP_FLAG()   ((EWDT->CTL & EWDT_CTL_WKF_Msk)? 1UL : 0UL)
136 
137 /**
138   * @brief      Reset EWDT Counter
139   *
140   * @param      None
141   *
142   * @return     None
143   *
144   * @details    This macro is used to reset the internal 20-bit EWDT up counter value.
145   * @note       If EWDT is activated and time-out reset system function is enabled also, user should \n
146   *             reset the 20-bit EWDT up counter value to avoid generate EWDT time-out reset signal to \n
147   *             reset system before the EWDT time-out reset delay period expires.
148   * \hideinitializer
149   */
150 #define EWDT_RESET_COUNTER()             (EWDT->RSTCNT = EWDT_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 EWDT_Close(void);
158 __STATIC_INLINE void EWDT_EnableInt(void);
159 __STATIC_INLINE void EWDT_DisableInt(void);
160 
161 /**
162   * @brief      Stop EWDT Counting
163   *
164   * @param      None
165   *
166   * @return     None
167   *
168   * @details    This function will stop EWDT counting and disable EWDT module.
169   */
EWDT_Close(void)170 __STATIC_INLINE void EWDT_Close(void)
171 {
172     EWDT->CTL = 0UL;
173     while((EWDT->CTL & EWDT_CTL_SYNC_Msk) == EWDT_CTL_SYNC_Msk) {} /* Wait disable WDTEN bit completed, it needs 2 * EWDT_CLK. */
174 }
175 
176 /**
177   * @brief      Enable EWDT Time-out Interrupt
178   *
179   * @param      None
180   *
181   * @return     None
182   *
183   * @details    This function will enable the EWDT time-out interrupt function.
184   */
EWDT_EnableInt(void)185 __STATIC_INLINE void EWDT_EnableInt(void)
186 {
187     EWDT->CTL |= EWDT_CTL_INTEN_Msk;
188 }
189 
190 /**
191   * @brief      Disable EWDT Time-out Interrupt
192   *
193   * @param      None
194   *
195   * @return     None
196   *
197   * @details    This function will disable the EWDT time-out interrupt function.
198   */
EWDT_DisableInt(void)199 __STATIC_INLINE void EWDT_DisableInt(void)
200 {
201     /* Do not touch another write 1 clear bits */
202     EWDT->CTL &= ~(EWDT_CTL_INTEN_Msk | EWDT_CTL_RSTF_Msk | EWDT_CTL_IF_Msk | EWDT_CTL_WKF_Msk);
203 }
204 
205 void EWDT_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 /* __EWDT_H__ */
218