1 /*
2 * Copyright (c) 2015, Freescale Semiconductor, Inc.
3 * Copyright 2016-2019 NXP
4 * All rights reserved.
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8 #ifndef FSL_WDOG_H_
9 #define FSL_WDOG_H_
10
11 #include "fsl_common.h"
12
13 /*!
14 * @addtogroup wdog
15 * @{
16 */
17
18 /*******************************************************************************
19 * Definitions
20 *******************************************************************************/
21
22 /*! @name Driver version */
23 /*! @{ */
24 /*! @brief Defines WDOG driver version 2.0.1. */
25 #define FSL_WDOG_DRIVER_VERSION (MAKE_VERSION(2, 0, 1))
26 /*! @} */
27
28 /*! @name Unlock sequence */
29 /*! @{ */
30 #define WDOG_FIRST_WORD_OF_UNLOCK (0xC520U) /*!< First word of unlock sequence */
31 #define WDOG_SECOND_WORD_OF_UNLOCK (0xD928U) /*!< Second word of unlock sequence */
32 /*! @} */
33
34 /*! @name Refresh sequence */
35 /*! @{ */
36 #define WDOG_FIRST_WORD_OF_REFRESH (0xA602U) /*!< First word of refresh sequence */
37 #define WDOG_SECOND_WORD_OF_REFRESH (0xB480U) /*!< Second word of refresh sequence */
38 /*! @} */
39
40 /*! @brief Describes WDOG clock source. */
41 typedef enum _wdog_clock_source
42 {
43 kWDOG_LpoClockSource = 0U, /*!< WDOG clock sourced from LPO*/
44 kWDOG_AlternateClockSource = 1U, /*!< WDOG clock sourced from alternate clock source*/
45 } wdog_clock_source_t;
46
47 /*! @brief Defines WDOG work mode. */
48 typedef struct _wdog_work_mode
49 {
50 #if defined(FSL_FEATURE_WDOG_HAS_WAITEN) && FSL_FEATURE_WDOG_HAS_WAITEN
51 bool enableWait; /*!< Enables or disables WDOG in wait mode */
52 #endif /* FSL_FEATURE_WDOG_HAS_WAITEN */
53 bool enableStop; /*!< Enables or disables WDOG in stop mode */
54 bool enableDebug; /*!< Enables or disables WDOG in debug mode */
55 } wdog_work_mode_t;
56
57 /*! @brief Describes the selection of the clock prescaler. */
58 typedef enum _wdog_clock_prescaler
59 {
60 kWDOG_ClockPrescalerDivide1 = 0x0U, /*!< Divided by 1 */
61 kWDOG_ClockPrescalerDivide2 = 0x1U, /*!< Divided by 2 */
62 kWDOG_ClockPrescalerDivide3 = 0x2U, /*!< Divided by 3 */
63 kWDOG_ClockPrescalerDivide4 = 0x3U, /*!< Divided by 4 */
64 kWDOG_ClockPrescalerDivide5 = 0x4U, /*!< Divided by 5 */
65 kWDOG_ClockPrescalerDivide6 = 0x5U, /*!< Divided by 6 */
66 kWDOG_ClockPrescalerDivide7 = 0x6U, /*!< Divided by 7 */
67 kWDOG_ClockPrescalerDivide8 = 0x7U, /*!< Divided by 8 */
68 } wdog_clock_prescaler_t;
69
70 /*! @brief Describes WDOG configuration structure. */
71 typedef struct _wdog_config
72 {
73 bool enableWdog; /*!< Enables or disables WDOG */
74 wdog_clock_source_t clockSource; /*!< Clock source select */
75 wdog_clock_prescaler_t prescaler; /*!< Clock prescaler value */
76 wdog_work_mode_t workMode; /*!< Configures WDOG work mode in debug stop and wait mode */
77 bool enableUpdate; /*!< Update write-once register enable */
78 bool enableInterrupt; /*!< Enables or disables WDOG interrupt */
79 bool enableWindowMode; /*!< Enables or disables WDOG window mode */
80 uint32_t windowValue; /*!< Window value */
81 uint32_t timeoutValue; /*!< Timeout value */
82 } wdog_config_t;
83
84 /*! @brief Describes WDOG test mode. */
85 typedef enum _wdog_test_mode
86 {
87 kWDOG_QuickTest = 0U, /*!< Selects quick test */
88 kWDOG_ByteTest = 1U, /*!< Selects byte test */
89 } wdog_test_mode_t;
90
91 /*! @brief Describes WDOG tested byte selection in byte test mode. */
92 typedef enum _wdog_tested_byte
93 {
94 kWDOG_TestByte0 = 0U, /*!< Byte 0 selected in byte test mode */
95 kWDOG_TestByte1 = 1U, /*!< Byte 1 selected in byte test mode */
96 kWDOG_TestByte2 = 2U, /*!< Byte 2 selected in byte test mode */
97 kWDOG_TestByte3 = 3U, /*!< Byte 3 selected in byte test mode */
98 } wdog_tested_byte_t;
99
100 /*! @brief Describes WDOG test mode configuration structure. */
101 typedef struct _wdog_test_config
102 {
103 wdog_test_mode_t testMode; /*!< Selects test mode */
104 wdog_tested_byte_t testedByte; /*!< Selects tested byte in byte test mode */
105 uint32_t timeoutValue; /*!< Timeout value */
106 } wdog_test_config_t;
107
108 /*!
109 * @brief WDOG interrupt configuration structure, default settings all disabled.
110 *
111 * This structure contains the settings for all of the WDOG interrupt configurations.
112 */
113 enum _wdog_interrupt_enable_t
114 {
115 kWDOG_InterruptEnable = WDOG_STCTRLH_IRQRSTEN_MASK, /*!< WDOG timeout generates an interrupt before reset*/
116 };
117
118 /*!
119 * @brief WDOG status flags.
120 *
121 * This structure contains the WDOG status flags for use in the WDOG functions.
122 */
123 enum _wdog_status_flags_t
124 {
125 kWDOG_RunningFlag = WDOG_STCTRLH_WDOGEN_MASK, /*!< Running flag, set when WDOG is enabled*/
126 kWDOG_TimeoutFlag = WDOG_STCTRLL_INTFLG_MASK, /*!< Interrupt flag, set when an exception occurs*/
127 };
128
129 /*******************************************************************************
130 * API
131 *******************************************************************************/
132
133 #if defined(__cplusplus)
134 extern "C" {
135 #endif /* __cplusplus */
136
137 /*!
138 * @name WDOG Initialization and De-initialization
139 * @{
140 */
141
142 /*!
143 * @brief Initializes the WDOG configuration structure.
144 *
145 * This function initializes the WDOG configuration structure to default values. The default
146 * values are as follows.
147 * @code
148 * wdogConfig->enableWdog = true;
149 * wdogConfig->clockSource = kWDOG_LpoClockSource;
150 * wdogConfig->prescaler = kWDOG_ClockPrescalerDivide1;
151 * wdogConfig->workMode.enableWait = true;
152 * wdogConfig->workMode.enableStop = false;
153 * wdogConfig->workMode.enableDebug = false;
154 * wdogConfig->enableUpdate = true;
155 * wdogConfig->enableInterrupt = false;
156 * wdogConfig->enableWindowMode = false;
157 * wdogConfig->windowValue = 0;
158 * wdogConfig->timeoutValue = 0xFFFFU;
159 * @endcode
160 *
161 * @param config Pointer to the WDOG configuration structure.
162 * @see wdog_config_t
163 */
164 void WDOG_GetDefaultConfig(wdog_config_t *config);
165
166 /*!
167 * @brief Initializes the WDOG.
168 *
169 * This function initializes the WDOG. When called, the WDOG runs according to the configuration.
170 * To reconfigure WDOG without forcing a reset first, enableUpdate must be set to true
171 * in the configuration.
172 *
173 * This is an example.
174 * @code
175 * wdog_config_t config;
176 * WDOG_GetDefaultConfig(&config);
177 * config.timeoutValue = 0x7ffU;
178 * config.enableUpdate = true;
179 * WDOG_Init(wdog_base,&config);
180 * @endcode
181 *
182 * @param base WDOG peripheral base address
183 * @param config The configuration of WDOG
184 */
185 void WDOG_Init(WDOG_Type *base, const wdog_config_t *config);
186
187 /*!
188 * @brief Shuts down the WDOG.
189 *
190 * This function shuts down the WDOG.
191 * Ensure that the WDOG_STCTRLH.ALLOWUPDATE is 1 which indicates that the register update is enabled.
192 */
193 void WDOG_Deinit(WDOG_Type *base);
194
195 /*!
196 * @brief Configures the WDOG functional test.
197 *
198 * This function is used to configure the WDOG functional test. When called, the WDOG goes into test mode
199 * and runs according to the configuration.
200 * Ensure that the WDOG_STCTRLH.ALLOWUPDATE is 1 which means that the register update is enabled.
201 *
202 * This is an example.
203 * @code
204 * wdog_test_config_t test_config;
205 * test_config.testMode = kWDOG_QuickTest;
206 * test_config.timeoutValue = 0xfffffu;
207 * WDOG_SetTestModeConfig(wdog_base, &test_config);
208 * @endcode
209 * @param base WDOG peripheral base address
210 * @param config The functional test configuration of WDOG
211 */
212 void WDOG_SetTestModeConfig(WDOG_Type *base, wdog_test_config_t *config);
213
214 /*! @} */
215
216 /*!
217 * @name WDOG Functional Operation
218 * @{
219 */
220
221 /*!
222 * @brief Enables the WDOG module.
223 *
224 * This function write value into WDOG_STCTRLH register to enable the WDOG, it is a write-once register,
225 * make sure that the WCT window is still open and this register has not been written in this WCT
226 * while this function is called.
227 *
228 * @param base WDOG peripheral base address
229 */
WDOG_Enable(WDOG_Type * base)230 static inline void WDOG_Enable(WDOG_Type *base)
231 {
232 base->STCTRLH |= WDOG_STCTRLH_WDOGEN_MASK;
233 }
234
235 /*!
236 * @brief Disables the WDOG module.
237 *
238 * This function writes a value into the WDOG_STCTRLH register to disable the WDOG. It is a write-once register.
239 * Ensure that the WCT window is still open and that register has not been written to in this WCT
240 * while the function is called.
241 *
242 * @param base WDOG peripheral base address
243 */
WDOG_Disable(WDOG_Type * base)244 static inline void WDOG_Disable(WDOG_Type *base)
245 {
246 base->STCTRLH &= ~(uint16_t)WDOG_STCTRLH_WDOGEN_MASK;
247 }
248
249 /*!
250 * @brief Enables the WDOG interrupt.
251 *
252 * This function writes a value into the WDOG_STCTRLH register to enable the WDOG interrupt. It is a write-once
253 * register.
254 * Ensure that the WCT window is still open and the register has not been written to in this WCT
255 * while the function is called.
256 *
257 * @param base WDOG peripheral base address
258 * @param mask The interrupts to enable
259 * The parameter can be combination of the following source if defined.
260 * @arg kWDOG_InterruptEnable
261 */
WDOG_EnableInterrupts(WDOG_Type * base,uint32_t mask)262 static inline void WDOG_EnableInterrupts(WDOG_Type *base, uint32_t mask)
263 {
264 base->STCTRLH |= (uint16_t)mask;
265 }
266
267 /*!
268 * @brief Disables the WDOG interrupt.
269 *
270 * This function writes a value into the WDOG_STCTRLH register to disable the WDOG interrupt. It is a write-once
271 * register.
272 * Ensure that the WCT window is still open and the register has not been written to in this WCT
273 * while the function is called.
274 *
275 * @param base WDOG peripheral base address
276 * @param mask The interrupts to disable
277 * The parameter can be combination of the following source if defined.
278 * @arg kWDOG_InterruptEnable
279 */
WDOG_DisableInterrupts(WDOG_Type * base,uint32_t mask)280 static inline void WDOG_DisableInterrupts(WDOG_Type *base, uint32_t mask)
281 {
282 base->STCTRLH &= (uint16_t)~mask;
283 }
284
285 /*!
286 * @brief Gets the WDOG all status flags.
287 *
288 * This function gets all status flags.
289 *
290 * This is an example for getting the Running Flag.
291 * @code
292 * uint32_t status;
293 * status = WDOG_GetStatusFlags (wdog_base) & kWDOG_RunningFlag;
294 * @endcode
295 * @param base WDOG peripheral base address
296 * @return State of the status flag: asserted (true) or not-asserted (false).@see _wdog_status_flags_t
297 * - true: a related status flag has been set.
298 * - false: a related status flag is not set.
299 */
300 uint32_t WDOG_GetStatusFlags(WDOG_Type *base);
301
302 /*!
303 * @brief Clears the WDOG flag.
304 *
305 * This function clears the WDOG status flag.
306 *
307 * This is an example for clearing the timeout (interrupt) flag.
308 * @code
309 * WDOG_ClearStatusFlags(wdog_base,kWDOG_TimeoutFlag);
310 * @endcode
311 * @param base WDOG peripheral base address
312 * @param mask The status flags to clear.
313 * The parameter could be any combination of the following values.
314 * kWDOG_TimeoutFlag
315 */
316 void WDOG_ClearStatusFlags(WDOG_Type *base, uint32_t mask);
317
318 /*!
319 * @brief Sets the WDOG timeout value.
320 *
321 * This function sets the timeout value.
322 * It should be ensured that the time-out value for the WDOG is always greater than
323 * 2xWCT time + 20 bus clock cycles.
324 * This function writes a value into WDOG_TOVALH and WDOG_TOVALL registers which are wirte-once.
325 * Ensure the WCT window is still open and the two registers have not been written to in this WCT
326 * while the function is called.
327 *
328 * @param base WDOG peripheral base address
329 * @param timeoutCount WDOG timeout value; count of WDOG clock tick.
330 */
WDOG_SetTimeoutValue(WDOG_Type * base,uint32_t timeoutCount)331 static inline void WDOG_SetTimeoutValue(WDOG_Type *base, uint32_t timeoutCount)
332 {
333 base->TOVALH = (uint16_t)((timeoutCount >> 16U) & 0xFFFFU);
334 base->TOVALL = (uint16_t)((timeoutCount)&0xFFFFU);
335 }
336
337 /*!
338 * @brief Sets the WDOG window value.
339 *
340 * This function sets the WDOG window value.
341 * This function writes a value into WDOG_WINH and WDOG_WINL registers which are wirte-once.
342 * Ensure the WCT window is still open and the two registers have not been written to in this WCT
343 * while the function is called.
344 *
345 * @param base WDOG peripheral base address
346 * @param windowValue WDOG window value.
347 */
WDOG_SetWindowValue(WDOG_Type * base,uint32_t windowValue)348 static inline void WDOG_SetWindowValue(WDOG_Type *base, uint32_t windowValue)
349 {
350 base->WINH = (uint16_t)((windowValue >> 16U) & 0xFFFFU);
351 base->WINL = (uint16_t)((windowValue)&0xFFFFU);
352 }
353
354 /*!
355 * @brief Unlocks the WDOG register written.
356 *
357 * This function unlocks the WDOG register written.
358 * Before starting the unlock sequence and following configuration, disable the global interrupts.
359 * Otherwise, an interrupt may invalidate the unlocking sequence and the WCT may expire.
360 * After the configuration finishes, re-enable the global interrupts.
361 *
362 * @param base WDOG peripheral base address
363 */
WDOG_Unlock(WDOG_Type * base)364 static inline void WDOG_Unlock(WDOG_Type *base)
365 {
366 base->UNLOCK = WDOG_FIRST_WORD_OF_UNLOCK;
367 base->UNLOCK = WDOG_SECOND_WORD_OF_UNLOCK;
368 }
369
370 /*!
371 * @brief Refreshes the WDOG timer.
372 *
373 * This function feeds the WDOG.
374 * This function should be called before the WDOG timer is in timeout. Otherwise, a reset is asserted.
375 *
376 * @param base WDOG peripheral base address
377 */
378 void WDOG_Refresh(WDOG_Type *base);
379
380 /*!
381 * @brief Gets the WDOG reset count.
382 *
383 * This function gets the WDOG reset count value.
384 *
385 * @param base WDOG peripheral base address
386 * @return WDOG reset count value.
387 */
WDOG_GetResetCount(WDOG_Type * base)388 static inline uint16_t WDOG_GetResetCount(WDOG_Type *base)
389 {
390 return base->RSTCNT;
391 }
392 /*!
393 * @brief Clears the WDOG reset count.
394 *
395 * This function clears the WDOG reset count value.
396 *
397 * @param base WDOG peripheral base address
398 */
WDOG_ClearResetCount(WDOG_Type * base)399 static inline void WDOG_ClearResetCount(WDOG_Type *base)
400 {
401 base->RSTCNT |= (uint16_t)UINT16_MAX;
402 }
403
404 /*! @} */
405
406 #if defined(__cplusplus)
407 }
408 #endif /* __cplusplus */
409
410 /*! @}*/
411
412 #endif /* FSL_WDOG_H_ */
413