1 /*
2  * Copyright 2017-2019 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 #ifndef _FSL_WDOG8_H_
8 #define _FSL_WDOG8_H_
9 
10 #include "fsl_common.h"
11 
12 /*!
13  * @addtogroup wdog8
14  * @{
15  */
16 
17 /*******************************************************************************
18  * Definitions
19  *******************************************************************************/
20 /*@}*/
21 /*! @name Driver version */
22 /*@{*/
23 /*! @brief WDOG8 driver version 2.0.1. */
24 #define FSL_WDOG8_DRIVER_VERSION (MAKE_VERSION(2, 0, 1))
25 /*@}*/
26 
27 /*! @brief Describes WDOG8 clock source. */
28 typedef enum _wdog8_clock_source
29 {
30     kWDOG8_ClockSource0 = 0U, /*!< Clock source 0 */
31     kWDOG8_ClockSource1 = 1U, /*!< Clock source 1 */
32     kWDOG8_ClockSource2 = 2U, /*!< Clock source 2 */
33     kWDOG8_ClockSource3 = 3U, /*!< Clock source 3 */
34 } wdog8_clock_source_t;
35 
36 /*! @brief Describes the selection of the clock prescaler. */
37 typedef enum _wdog8_clock_prescaler
38 {
39     kWDOG8_ClockPrescalerDivide1   = 0x0U, /*!< Divided by 1 */
40     kWDOG8_ClockPrescalerDivide256 = 0x1U, /*!< Divided by 256 */
41 } wdog8_clock_prescaler_t;
42 
43 /*! @brief Defines WDOG8 work mode. */
44 typedef struct _wdog8_work_mode
45 {
46     bool enableWait;  /*!< Enables or disables WDOG8 in wait mode */
47     bool enableStop;  /*!< Enables or disables WDOG8 in stop mode */
48     bool enableDebug; /*!< Enables or disables WDOG8 in debug mode */
49 } wdog8_work_mode_t;
50 
51 /*! @brief Describes WDOG8 test mode. */
52 typedef enum _wdog8_test_mode
53 {
54     kWDOG8_TestModeDisabled = 0U, /*!< Test Mode disabled */
55     kWDOG8_UserModeEnabled  = 1U, /*!< User Mode enabled */
56     kWDOG8_LowByteTest      = 2U, /*!< Test Mode enabled, only low byte is used */
57     kWDOG8_HighByteTest     = 3U, /*!< Test Mode enabled, only high byte is used */
58 } wdog8_test_mode_t;
59 
60 /*! @brief Describes WDOG8 configuration structure. */
61 typedef struct _wdog8_config
62 {
63     bool enableWdog8;                  /*!< Enables or disables WDOG8 */
64     wdog8_clock_source_t clockSource;  /*!< Clock source select */
65     wdog8_clock_prescaler_t prescaler; /*!< Clock prescaler value */
66     wdog8_work_mode_t workMode;        /*!< Configures WDOG8 work mode in debug stop and wait mode */
67     wdog8_test_mode_t testMode;        /*!< Configures WDOG8 test mode */
68     bool enableUpdate;                 /*!< Update write-once register enable */
69     bool enableInterrupt;              /*!< Enables or disables WDOG8 interrupt */
70     bool enableWindowMode;             /*!< Enables or disables WDOG8 window mode */
71     uint16_t windowValue;              /*!< Window value */
72     uint16_t timeoutValue;             /*!< Timeout value */
73 } wdog8_config_t;
74 
75 /*!
76  * @brief WDOG8 interrupt configuration structure.
77  *
78  * This structure contains the settings for all of the WDOG8 interrupt configurations.
79  */
80 enum _wdog8_interrupt_enable_t
81 {
82     kWDOG8_InterruptEnable = WDOG_CS1_INT_MASK, /*!< Interrupt is generated before forcing a reset */
83 };
84 
85 /*!
86  * @brief WDOG8 status flags.
87  *
88  * This structure contains the WDOG8 status flags for use in the WDOG8 functions.
89  */
90 enum _wdog8_status_flags_t
91 {
92     kWDOG8_RunningFlag   = WDOG_CS1_EN_MASK,  /*!< Running flag, set when WDOG8 is enabled */
93     kWDOG8_InterruptFlag = WDOG_CS2_FLG_MASK, /*!< Interrupt flag, set when interrupt occurs */
94 };
95 
96 /*! @} */
97 
98 /*******************************************************************************
99  * API
100  *******************************************************************************/
101 
102 #if defined(__cplusplus)
103 extern "C" {
104 #endif /* __cplusplus */
105 
106 /*!
107  * @addtogroup wdog8
108  * @{
109  */
110 
111 /*!
112  * @name WDOG8 Initialization and De-initialization
113  * @{
114  */
115 
116 /*!
117  * @brief Initializes the WDOG8 configuration structure.
118  *
119  * This function initializes the WDOG8 configuration structure to default values. The default
120  * values are:
121  * @code
122  *   wdog8Config->enableWdog8 = true;
123  *   wdog8Config->clockSource = kWDOG8_ClockSource1;
124  *   wdog8Config->prescaler = kWDOG8_ClockPrescalerDivide1;
125  *   wdog8Config->workMode.enableWait = true;
126  *   wdog8Config->workMode.enableStop = false;
127  *   wdog8Config->workMode.enableDebug = false;
128  *   wdog8Config->testMode = kWDOG8_TestModeDisabled;
129  *   wdog8Config->enableUpdate = true;
130  *   wdog8Config->enableInterrupt = false;
131  *   wdog8Config->enableWindowMode = false;
132  *   wdog8Config->windowValue = 0U;
133  *   wdog8Config->timeoutValue = 0xFFFFU;
134  * @endcode
135  *
136  * @param config Pointer to the WDOG8 configuration structure.
137  * @see wdog8_config_t
138  */
139 void WDOG8_GetDefaultConfig(wdog8_config_t *config);
140 
141 /*!
142  * @brief Initializes the WDOG8 module.
143  *
144  * This function initializes the WDOG8.
145  * To reconfigure the WDOG8 without forcing a reset first, enableUpdate must be set to true
146  * in the configuration.
147  *
148  * Example:
149  * @code
150  *   wdog8_config_t config;
151  *   WDOG8_GetDefaultConfig(&config);
152  *   config.timeoutValue = 0x7ffU;
153  *   config.enableUpdate = true;
154  *   WDOG8_Init(wdog_base,&config);
155  * @endcode
156  *
157  * @param base   WDOG8 peripheral base address.
158  * @param config The configuration of the WDOG8.
159  */
160 void WDOG8_Init(WDOG_Type *base, const wdog8_config_t *config);
161 
162 /*!
163  * @brief De-initializes the WDOG8 module.
164  *
165  * This function shuts down the WDOG8.
166  * Ensure that the WDOG_CS1.UPDATE is 1, which means that the register update is enabled.
167  *
168  * @param base   WDOG8 peripheral base address.
169  */
170 void WDOG8_Deinit(WDOG_Type *base);
171 
172 /* @} */
173 
174 /*!
175  * @name WDOG8 functional Operation
176  * @{
177  */
178 
179 /*!
180  * @brief Enables the WDOG8 module.
181  *
182  * This function writes a value into the WDOG_CS1 register to enable the WDOG8.
183  * The WDOG_CS1 register is a write-once register. Ensure that the WCT window is still open and
184  * this register has not been written in this WCT while the function is called.
185  *
186  * @param base WDOG8 peripheral base address.
187  */
WDOG8_Enable(WDOG_Type * base)188 static inline void WDOG8_Enable(WDOG_Type *base)
189 {
190     base->CS1 |= WDOG_CS1_EN_MASK;
191 }
192 
193 /*!
194  * @brief Disables the WDOG8 module.
195  *
196  * This function writes a value into the WDOG_CS1 register to disable the WDOG8.
197  * The WDOG_CS1 register is a write-once register. Ensure that the WCT window is still open and
198  * this register has not been written in this WCT while the function is called.
199  *
200  * @param base WDOG8 peripheral base address
201  */
WDOG8_Disable(WDOG_Type * base)202 static inline void WDOG8_Disable(WDOG_Type *base)
203 {
204     base->CS1 &= (uint8_t)~WDOG_CS1_EN_MASK;
205 }
206 
207 /*!
208  * @brief Enables the WDOG8 interrupt.
209  *
210  * This function writes a value into the WDOG_CS1 register to enable the WDOG8 interrupt.
211  * The WDOG_CS1 register is a write-once register. Ensure that the WCT window is still open and
212  * this register has not been written in this WCT while the function is called.
213  *
214  * @param base WDOG8 peripheral base address.
215  * @param mask The interrupts to enable.
216  *        The parameter can be a combination of the following source if defined:
217  *        @arg kWDOG8_InterruptEnable
218  */
WDOG8_EnableInterrupts(WDOG_Type * base,uint8_t mask)219 static inline void WDOG8_EnableInterrupts(WDOG_Type *base, uint8_t mask)
220 {
221     base->CS1 |= mask;
222 }
223 
224 /*!
225  * @brief Disables the WDOG8 interrupt.
226  *
227  * This function writes a value into the WDOG_CS register to disable the WDOG8 interrupt.
228  * The WDOG_CS register is a write-once register. Ensure that the WCT window is still open and
229  * this register has not been written in this WCT while the function is called.
230  *
231  * @param base WDOG8 peripheral base address.
232  * @param mask The interrupts to disabled.
233  *        The parameter can be a combination of the following source if defined:
234  *        @arg kWDOG8_InterruptEnable
235  */
WDOG8_DisableInterrupts(WDOG_Type * base,uint8_t mask)236 static inline void WDOG8_DisableInterrupts(WDOG_Type *base, uint8_t mask)
237 {
238     base->CS1 &= (uint8_t)~mask;
239 }
240 
241 /*!
242  * @brief Gets the WDOG8 all status flags.
243  *
244  * This function gets all status flags.
245  *
246  * Example to get the running flag:
247  * @code
248  *   uint32_t status;
249  *   status = WDOG8_GetStatusFlags(wdog_base) & kWDOG8_RunningFlag;
250  * @endcode
251  * @param base        WDOG8 peripheral base address
252  * @return            State of the status flag: asserted (true) or not-asserted (false). @see _wdog8_status_flags_t
253  *                    - true: related status flag has been set.
254  *                    - false: related status flag is not set.
255  */
WDOG8_GetStatusFlags(WDOG_Type * base)256 static inline uint8_t WDOG8_GetStatusFlags(WDOG_Type *base)
257 {
258     return (base->CS1 & WDOG_CS1_EN_MASK) | (base->CS2 & WDOG_CS2_FLG_MASK);
259 }
260 
261 /*!
262  * @brief Clears the WDOG8 flag.
263  *
264  * This function clears the WDOG8 status flag.
265  *
266  * Example to clear an interrupt flag:
267  * @code
268  *   WDOG8_ClearStatusFlags(wdog_base,kWDOG8_InterruptFlag);
269  * @endcode
270  * @param base        WDOG8 peripheral base address.
271  * @param mask        The status flags to clear.
272  *                    The parameter can be any combination of the following values:
273  *                    @arg kWDOG8_InterruptFlag
274  */
275 void WDOG8_ClearStatusFlags(WDOG_Type *base, uint8_t mask);
276 
277 /*!
278  * @brief Sets the WDOG8 timeout value.
279  *
280  * This function writes a timeout value into the WDOG_TOVALH/L register.
281  * The WDOG_TOVALH/L register is a write-once register. Ensure that the WCT window is still open and
282  * this register has not been written in this WCT while the function is called.
283  *
284  * @param base WDOG8 peripheral base address
285  * @param timeoutCount WDOG8 timeout value, count of WDOG8 clock ticks.
286  */
WDOG8_SetTimeoutValue(WDOG_Type * base,uint16_t timeoutCount)287 static inline void WDOG8_SetTimeoutValue(WDOG_Type *base, uint16_t timeoutCount)
288 {
289     base->TOVAL8B.TOVALH = (uint8_t)((timeoutCount >> 8U) & 0xFFU);
290     base->TOVAL8B.TOVALL = (uint8_t)((timeoutCount)&0xFFU);
291 }
292 
293 /*!
294  * @brief Sets the WDOG8 window value.
295  *
296  * This function writes a window value into the WDOG_WINH/L register.
297  * The WDOG_WINH/L register is a write-once register. Ensure that the WCT window is still open and
298  * this register has not been written in this WCT while the function is called.
299  *
300  * @param base WDOG8 peripheral base address.
301  * @param windowValue WDOG8 window value.
302  */
WDOG8_SetWindowValue(WDOG_Type * base,uint16_t windowValue)303 static inline void WDOG8_SetWindowValue(WDOG_Type *base, uint16_t windowValue)
304 {
305     base->WIN8B.WINH = (uint8_t)((windowValue >> 8U) & 0xFFU);
306     base->WIN8B.WINL = (uint8_t)((windowValue)&0xFFU);
307 }
308 
309 /*!
310  * @brief Unlocks the WDOG8 register written.
311  *
312  * This function unlocks the WDOG8 register written.
313  *
314  * Before starting the unlock sequence and following the configuration, disable the global interrupts.
315  * Otherwise, an interrupt could effectively invalidate the unlock sequence and the WCT may expire.
316  * After the configuration finishes, re-enable the global interrupts.
317  *
318  * @param base WDOG8 peripheral base address
319  */
WDOG8_Unlock(WDOG_Type * base)320 static inline void WDOG8_Unlock(WDOG_Type *base)
321 {
322     base->CNT = WDOG_UPDATE_KEY1;
323     base->CNT = WDOG_UPDATE_KEY2;
324 }
325 
326 /*!
327  * @brief Refreshes the WDOG8 timer.
328  *
329  * This function feeds the WDOG8.
330  * This function should be called before the Watchdog timer is in timeout. Otherwise, a reset is asserted.
331  *
332  * @param base WDOG8 peripheral base address
333  */
WDOG8_Refresh(WDOG_Type * base)334 static inline void WDOG8_Refresh(WDOG_Type *base)
335 {
336     uint32_t primaskValue = 0U;
337 
338     /* Disable the global interrupts */
339     primaskValue = DisableGlobalIRQ();
340     base->CNT    = WDOG_REFRESH_KEY1;
341     base->CNT    = WDOG_REFRESH_KEY2;
342     EnableGlobalIRQ(primaskValue);
343 }
344 
345 /*!
346  * @brief Gets the WDOG8 counter value.
347  *
348  * This function gets the WDOG8 counter value.
349  *
350  * @param base WDOG8 peripheral base address.
351  * @return     Current WDOG8 counter value.
352  */
WDOG8_GetCounterValue(WDOG_Type * base)353 static inline uint16_t WDOG8_GetCounterValue(WDOG_Type *base)
354 {
355     return ((uint16_t)(base->CNT8B.CNTH) << 8U) + (uint16_t)(base->CNT8B.CNTL);
356 }
357 
358 /*@}*/
359 
360 #if defined(__cplusplus)
361 }
362 #endif /* __cplusplus */
363 
364 /*! @}*/
365 
366 #endif /* _FSL_WDOG8_H_ */
367