1 /*
2  * Copyright (c) 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2020 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 #ifndef FSL_WWDT_H_
9 #define FSL_WWDT_H_
10 
11 #include "fsl_common.h"
12 
13 /*!
14  * @addtogroup wwdt
15  * @{
16  */
17 
18 /*! @file */
19 
20 /*******************************************************************************
21  * Definitions
22  *******************************************************************************/
23 
24 /*! @name Driver version */
25 /*! @{ */
26 /*! @brief Defines WWDT driver version. */
27 #define FSL_WWDT_DRIVER_VERSION (MAKE_VERSION(2, 1, 9))
28 /*! @} */
29 
30 /*! @name Refresh sequence */
31 /*! @{ */
32 #define WWDT_FIRST_WORD_OF_REFRESH  (0xAAU) /*!< First word of refresh sequence */
33 #define WWDT_SECOND_WORD_OF_REFRESH (0x55U) /*!< Second word of refresh sequence */
34 /*! @} */
35 
36 /*! @brief Describes WWDT configuration structure. */
37 typedef struct _wwdt_config
38 {
39     bool enableWwdt;            /*!< Enables or disables WWDT */
40     bool enableWatchdogReset;   /*!< true: Watchdog timeout will cause a chip reset
41                                      false: Watchdog timeout will not cause a chip reset */
42     bool enableWatchdogProtect; /*!< true: Enable watchdog protect i.e timeout value can only be
43                                            changed after counter is below warning & window values
44                                      false: Disable watchdog protect; timeout value can be changed
45                                             at any time */
46 #if !(defined(FSL_FEATURE_WWDT_HAS_NO_OSCILLATOR_LOCK) && FSL_FEATURE_WWDT_HAS_NO_OSCILLATOR_LOCK)
47     bool enableLockOscillator; /*!< true: Disabling or powering down the watchdog oscillator is prevented
48                                           Once set, this bit can only be cleared by a reset
49                                     false: Do not lock oscillator */
50 #endif
51     uint32_t windowValue;  /*!< Window value, set this to 0xFFFFFF if windowing is not in effect */
52     uint32_t timeoutValue; /*!< Timeout value */
53     uint32_t warningValue; /*!< Watchdog time counter value that will generate a
54                                 warning interrupt. Set this to 0 for no warning */
55     uint32_t clockFreq_Hz; /*!< Watchdog clock source frequency. */
56 } wwdt_config_t;
57 
58 /*!
59  * @brief WWDT status flags.
60  *
61  * This structure contains the WWDT status flags for use in the WWDT functions.
62  */
63 enum _wwdt_status_flags_t
64 {
65     kWWDT_TimeoutFlag = WWDT_MOD_WDTOF_MASK, /*!< Time-out flag, set when the timer times out */
66     kWWDT_WarningFlag = WWDT_MOD_WDINT_MASK  /*!< Warning interrupt flag, set when timer is below the value WDWARNINT */
67 };
68 
69 /*******************************************************************************
70  * API
71  *******************************************************************************/
72 
73 #if defined(__cplusplus)
74 extern "C" {
75 #endif /* __cplusplus */
76 
77 /*!
78  * @name WWDT Initialization and De-initialization
79  * @{
80  */
81 
82 /*!
83  * @brief Initializes WWDT configure structure.
84  *
85  * This function initializes the WWDT configure structure to default value. The default
86  * value are:
87  * @code
88  *  config->enableWwdt = true;
89  *  config->enableWatchdogReset = false;
90  *  config->enableWatchdogProtect = false;
91  *  config->enableLockOscillator = false;
92  *  config->windowValue = 0xFFFFFFU;
93  *  config->timeoutValue = 0xFFFFFFU;
94  *  config->warningValue = 0;
95  * @endcode
96  *
97  * @param config Pointer to WWDT config structure.
98  * @see wwdt_config_t
99  */
100 void WWDT_GetDefaultConfig(wwdt_config_t *config);
101 
102 /*!
103  * @brief Initializes the WWDT.
104  *
105  * This function initializes the WWDT. When called, the WWDT runs according to the configuration.
106  *
107  * Example:
108  * @code
109  *   wwdt_config_t config;
110  *   WWDT_GetDefaultConfig(&config);
111  *   config.timeoutValue = 0x7ffU;
112  *   WWDT_Init(wwdt_base,&config);
113  * @endcode
114  *
115  * @param base   WWDT peripheral base address
116  * @param config The configuration of WWDT
117  */
118 void WWDT_Init(WWDT_Type *base, const wwdt_config_t *config);
119 
120 /*!
121  * @brief Shuts down the WWDT.
122  *
123  * This function shuts down the WWDT.
124  *
125  * @param base WWDT peripheral base address
126  */
127 void WWDT_Deinit(WWDT_Type *base);
128 
129 /*! @} */
130 
131 /*!
132  * @name WWDT Functional Operation
133  * @{
134  */
135 
136 /*!
137  * @brief Enables the WWDT module.
138  *
139  * This function write value into WWDT_MOD register to enable the WWDT, it is a write-once bit;
140  * once this bit is set to one and a watchdog feed is performed, the watchdog timer will run
141  * permanently.
142  *
143  * @param base WWDT peripheral base address
144  */
WWDT_Enable(WWDT_Type * base)145 static inline void WWDT_Enable(WWDT_Type *base)
146 {
147     base->MOD |= WWDT_MOD_WDEN_MASK;
148 }
149 
150 /*!
151  * @brief Disables the WWDT module.
152  * @deprecated Do not use this function.  It will be deleted in next release version, for
153  *  once the bit field of WDEN written with a 1, it can not be re-written with a 0.
154  *
155  * This function write value into WWDT_MOD register to disable the WWDT.
156  *
157  * @param base WWDT peripheral base address
158  */
WWDT_Disable(WWDT_Type * base)159 static inline void WWDT_Disable(WWDT_Type *base)
160 {
161     base->MOD &= ~WWDT_MOD_WDEN_MASK;
162 }
163 
164 /*!
165  * @brief Gets all WWDT status flags.
166  *
167  * This function gets all status flags.
168  *
169  * Example for getting Timeout Flag:
170  * @code
171  *   uint32_t status;
172  *   status = WWDT_GetStatusFlags(wwdt_base) & kWWDT_TimeoutFlag;
173  * @endcode
174  * @param base        WWDT peripheral base address
175  * @return The status flags. This is the logical OR of members of the
176  *         enumeration ::_wwdt_status_flags_t
177  */
WWDT_GetStatusFlags(WWDT_Type * base)178 static inline uint32_t WWDT_GetStatusFlags(WWDT_Type *base)
179 {
180 #if defined(FSL_FEATURE_WWDT_WDTRESET_FROM_PMC) && (FSL_FEATURE_WWDT_WDTRESET_FROM_PMC)
181     uint32_t status;
182     /* WDTOF is not set in case of WD reset - get info from PMC instead */
183     status = (base->MOD & (WWDT_MOD_WDTOF_MASK | WWDT_MOD_WDINT_MASK));
184     if (PMC->RESETCAUSE & PMC_RESETCAUSE_WDTRESET_MASK)
185     {
186         status |= kWWDT_TimeoutFlag;
187     }
188     return status;
189 #else
190     return (base->MOD & (WWDT_MOD_WDTOF_MASK | WWDT_MOD_WDINT_MASK));
191 #endif /*FSL_FEATURE_WWDT_WDTRESET_FROM_PMC*/
192 }
193 
194 /*!
195  * @brief Clear WWDT flag.
196  *
197  * This function clears WWDT status flag.
198  *
199  * Example for clearing warning flag:
200  * @code
201  *   WWDT_ClearStatusFlags(wwdt_base, kWWDT_WarningFlag);
202  * @endcode
203  * @param base WWDT peripheral base address
204  * @param mask The status flags to clear. This is a logical OR of members of the
205  *             enumeration ::_wwdt_status_flags_t
206  */
207 void WWDT_ClearStatusFlags(WWDT_Type *base, uint32_t mask);
208 
209 /*!
210  * @brief Set the WWDT warning value.
211  *
212  * The WDWARNINT register determines the watchdog timer counter value that will generate a watchdog
213  * interrupt. When the watchdog timer counter is no longer greater than the value defined by
214  * WARNINT, an interrupt will be generated after the subsequent WDCLK.
215  *
216  * @param base         WWDT peripheral base address
217  * @param warningValue WWDT warning value.
218  */
WWDT_SetWarningValue(WWDT_Type * base,uint32_t warningValue)219 static inline void WWDT_SetWarningValue(WWDT_Type *base, uint32_t warningValue)
220 {
221     base->WARNINT = WWDT_WARNINT_WARNINT(warningValue);
222 }
223 
224 /*!
225  * @brief Set the WWDT timeout value.
226  *
227  * This function sets the timeout value. Every time a feed sequence occurs the value in the TC
228  * register is loaded into the Watchdog timer. Writing a value below 0xFF will cause 0xFF to be
229  * loaded into the TC register. Thus the minimum time-out interval is TWDCLK*256*4.
230  * If enableWatchdogProtect flag is true in wwdt_config_t config structure, any attempt to change
231  * the timeout value before the watchdog counter is below the warning and window values
232  * will cause a watchdog reset and set the WDTOF flag.
233  *
234  * @param base WWDT peripheral base address
235  * @param timeoutCount WWDT timeout value, count of WWDT clock tick.
236  */
WWDT_SetTimeoutValue(WWDT_Type * base,uint32_t timeoutCount)237 static inline void WWDT_SetTimeoutValue(WWDT_Type *base, uint32_t timeoutCount)
238 {
239     base->TC = WWDT_TC_COUNT(timeoutCount);
240 }
241 
242 /*!
243  * @brief Sets the WWDT window value.
244  *
245  * The WINDOW register determines the highest TV value allowed when a watchdog feed is performed.
246  * If a feed sequence occurs when timer value is greater than the value in WINDOW, a watchdog
247  * event will occur. To disable windowing, set windowValue to 0xFFFFFF (maximum possible timer
248  * value) so windowing is not in effect.
249  *
250  * @param base        WWDT peripheral base address
251  * @param windowValue WWDT window value.
252  */
WWDT_SetWindowValue(WWDT_Type * base,uint32_t windowValue)253 static inline void WWDT_SetWindowValue(WWDT_Type *base, uint32_t windowValue)
254 {
255     base->WINDOW = WWDT_WINDOW_WINDOW(windowValue);
256 }
257 
258 /*!
259  * @brief Refreshes the WWDT timer.
260  *
261  * This function feeds the WWDT.
262  * This function should be called before WWDT timer is in timeout. Otherwise, a reset is asserted.
263  *
264  * @param base WWDT peripheral base address
265  */
266 void WWDT_Refresh(WWDT_Type *base);
267 
268 /*! @} */
269 
270 #if defined(__cplusplus)
271 }
272 #endif /* __cplusplus */
273 
274 /*! @}*/
275 
276 #endif /* FSL_WWDT_H_ */
277