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