1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2020 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 #ifndef _FSL_WDOG32_H_
9 #define _FSL_WDOG32_H_
10 
11 #include "fsl_common.h"
12 
13 /*!
14  * @addtogroup wdog32
15  * @{
16  */
17 
18 /*******************************************************************************
19  * Definitions
20  *******************************************************************************/
21 /*! @name Unlock sequence */
22 /*@{*/
23 #define WDOG_FIRST_WORD_OF_UNLOCK  (WDOG_UPDATE_KEY & 0xFFFFU)          /*!< First word of unlock sequence */
24 #define WDOG_SECOND_WORD_OF_UNLOCK ((WDOG_UPDATE_KEY >> 16U) & 0xFFFFU) /*!< Second word of unlock sequence */
25 /*@}*/
26 
27 /*! @name Refresh sequence */
28 /*@{*/
29 #define WDOG_FIRST_WORD_OF_REFRESH  (WDOG_REFRESH_KEY & 0xFFFFU)          /*!< First word of refresh sequence */
30 #define WDOG_SECOND_WORD_OF_REFRESH ((WDOG_REFRESH_KEY >> 16U) & 0xFFFFU) /*!< Second word of refresh sequence */
31 /*@}*/
32 /*! @name Driver version */
33 /*@{*/
34 /*! @brief WDOG32 driver version. */
35 #define FSL_WDOG32_DRIVER_VERSION (MAKE_VERSION(2, 0, 4))
36 /*@}*/
37 
38 /*! @brief Describes WDOG32 clock source. */
39 typedef enum _wdog32_clock_source
40 {
41     kWDOG32_ClockSource0 = 0U, /*!< Clock source 0 */
42     kWDOG32_ClockSource1 = 1U, /*!< Clock source 1 */
43     kWDOG32_ClockSource2 = 2U, /*!< Clock source 2 */
44     kWDOG32_ClockSource3 = 3U, /*!< Clock source 3 */
45 } wdog32_clock_source_t;
46 
47 /*! @brief Describes the selection of the clock prescaler. */
48 typedef enum _wdog32_clock_prescaler
49 {
50     kWDOG32_ClockPrescalerDivide1   = 0x0U, /*!< Divided by 1 */
51     kWDOG32_ClockPrescalerDivide256 = 0x1U, /*!< Divided by 256 */
52 } wdog32_clock_prescaler_t;
53 
54 /*! @brief Defines WDOG32 work mode. */
55 typedef struct _wdog32_work_mode
56 {
57     bool enableWait;  /*!< Enables or disables WDOG32 in wait mode */
58     bool enableStop;  /*!< Enables or disables WDOG32 in stop mode */
59     bool enableDebug; /*!< Enables or disables WDOG32 in debug mode */
60 } wdog32_work_mode_t;
61 
62 /*! @brief Describes WDOG32 test mode. */
63 typedef enum _wdog32_test_mode
64 {
65     kWDOG32_TestModeDisabled = 0U, /*!< Test Mode disabled */
66     kWDOG32_UserModeEnabled  = 1U, /*!< User Mode enabled */
67     kWDOG32_LowByteTest      = 2U, /*!< Test Mode enabled, only low byte is used */
68     kWDOG32_HighByteTest     = 3U, /*!< Test Mode enabled, only high byte is used */
69 } wdog32_test_mode_t;
70 
71 /*! @brief Describes WDOG32 configuration structure. */
72 typedef struct _wdog32_config
73 {
74     bool enableWdog32;                  /*!< Enables or disables WDOG32 */
75     wdog32_clock_source_t clockSource;  /*!< Clock source select */
76     wdog32_clock_prescaler_t prescaler; /*!< Clock prescaler value */
77     wdog32_work_mode_t workMode;        /*!< Configures WDOG32 work mode in debug stop and wait mode */
78     wdog32_test_mode_t testMode;        /*!< Configures WDOG32 test mode */
79     bool enableUpdate;                  /*!< Update write-once register enable */
80     bool enableInterrupt;               /*!< Enables or disables WDOG32 interrupt */
81     bool enableWindowMode;              /*!< Enables or disables WDOG32 window mode */
82     uint16_t windowValue;               /*!< Window value */
83     uint16_t timeoutValue;              /*!< Timeout value */
84 } wdog32_config_t;
85 
86 /*!
87  * @brief WDOG32 interrupt configuration structure.
88  *
89  * This structure contains the settings for all of the WDOG32 interrupt configurations.
90  */
91 enum _wdog32_interrupt_enable_t
92 {
93     kWDOG32_InterruptEnable = WDOG_CS_INT_MASK, /*!< Interrupt is generated before forcing a reset */
94 };
95 
96 /*!
97  * @brief WDOG32 status flags.
98  *
99  * This structure contains the WDOG32 status flags for use in the WDOG32 functions.
100  */
101 enum _wdog32_status_flags_t
102 {
103     kWDOG32_RunningFlag   = WDOG_CS_EN_MASK,  /*!< Running flag, set when WDOG32 is enabled */
104     kWDOG32_InterruptFlag = WDOG_CS_FLG_MASK, /*!< Interrupt flag, set when interrupt occurs */
105 };
106 
107 /*******************************************************************************
108  * API
109  *******************************************************************************/
110 
111 #if defined(__cplusplus)
112 extern "C" {
113 #endif /* __cplusplus */
114 
115 /*!
116  * @name WDOG32 Initialization and De-initialization
117  * @{
118  */
119 
120 /*!
121  * @brief Initializes the WDOG32 configuration structure.
122  *
123  * This function initializes the WDOG32 configuration structure to default values. The default
124  * values are:
125  * @code
126  *   wdog32Config->enableWdog32 = true;
127  *   wdog32Config->clockSource = kWDOG32_ClockSource1;
128  *   wdog32Config->prescaler = kWDOG32_ClockPrescalerDivide1;
129  *   wdog32Config->workMode.enableWait = true;
130  *   wdog32Config->workMode.enableStop = false;
131  *   wdog32Config->workMode.enableDebug = false;
132  *   wdog32Config->testMode = kWDOG32_TestModeDisabled;
133  *   wdog32Config->enableUpdate = true;
134  *   wdog32Config->enableInterrupt = false;
135  *   wdog32Config->enableWindowMode = false;
136  *   wdog32Config->windowValue = 0U;
137  *   wdog32Config->timeoutValue = 0xFFFFU;
138  * @endcode
139  *
140  * @param config Pointer to the WDOG32 configuration structure.
141  * @see wdog32_config_t
142  */
143 void WDOG32_GetDefaultConfig(wdog32_config_t *config);
144 
145 /*!
146  * @brief Initializes the WDOG32 module.
147  *
148  * This function initializes the WDOG32.
149  * To reconfigure the WDOG32 without forcing a reset first, enableUpdate must be set to true
150  * in the configuration.
151  *
152  * Example:
153  * @code
154  *   wdog32_config_t config;
155  *   WDOG32_GetDefaultConfig(&config);
156  *   config.timeoutValue = 0x7ffU;
157  *   config.enableUpdate = true;
158  *   WDOG32_Init(wdog_base,&config);
159  * @endcode
160  *
161  * @param base   WDOG32 peripheral base address.
162  * @param config The configuration of the WDOG32.
163  */
164 #if defined(DOXYGEN_OUTPUT) && DOXYGEN_OUTPUT
165 void WDOG32_Init(WDOG_Type *base, const wdog32_config_t *config);
166 #else
167 AT_QUICKACCESS_SECTION_CODE(void WDOG32_Init(WDOG_Type *base, const wdog32_config_t *config));
168 #endif
169 
170 /*!
171  * @brief De-initializes the WDOG32 module.
172  *
173  * This function shuts down the WDOG32.
174  * Ensure that the WDOG_CS.UPDATE is 1, which means that the register update is enabled.
175  *
176  * @param base   WDOG32 peripheral base address.
177  */
178 void WDOG32_Deinit(WDOG_Type *base);
179 
180 /* @} */
181 
182 /*!
183  * @name WDOG32 functional Operation
184  * @{
185  */
186 
187 /*!
188  * @brief Unlocks the WDOG32 register written.
189  *
190  * This function unlocks the WDOG32 register written.
191  *
192  * Before starting the unlock sequence and following the configuration, disable the global interrupts.
193  * Otherwise, an interrupt could effectively invalidate the unlock sequence and the WCT may expire.
194  * After the configuration finishes, re-enable the global interrupts.
195  *
196  * @param base WDOG32 peripheral base address
197  */
198 #if defined(DOXYGEN_OUTPUT) && DOXYGEN_OUTPUT
199 void WDOG32_Unlock(WDOG_Type *base);
200 #else
201 AT_QUICKACCESS_SECTION_CODE(void WDOG32_Unlock(WDOG_Type *base));
202 #endif
203 
204 /*!
205  * @brief Enables the WDOG32 module.
206  *
207  * This function writes a value into the WDOG_CS register to enable the WDOG32.
208  * The WDOG_CS register is a write-once register. Please check the enableUpdate is set to true for calling
209  * @ref WDOG32_Init to do wdog initialize.
210  * Before call the re-configuration APIs, ensure that the WCT window is still open and
211  * this register has not been written in this WCT while the function is called.
212  *
213  * @param base WDOG32 peripheral base address.
214  */
215 #if defined(DOXYGEN_OUTPUT) && DOXYGEN_OUTPUT
216 void WDOG32_Enable(WDOG_Type *base);
217 #else
218 AT_QUICKACCESS_SECTION_CODE(void WDOG32_Enable(WDOG_Type *base));
219 #endif
220 
221 /*!
222  * @brief Disables the WDOG32 module.
223  *
224  * This function writes a value into the WDOG_CS register to disable the WDOG32.
225  * The WDOG_CS register is a write-once register. Please check the enableUpdate is set to true for calling
226  * @ref WDOG32_Init to do wdog initialize.
227  * Before call the re-configuration APIs, ensure that the WCT window is still open and
228  * this register has not been written in this WCT while the function is called.
229  *
230  * @param base WDOG32 peripheral base address
231  */
232 #if defined(DOXYGEN_OUTPUT) && DOXYGEN_OUTPUT
233 void WDOG32_Disable(WDOG_Type *base);
234 #else
235 AT_QUICKACCESS_SECTION_CODE(void WDOG32_Disable(WDOG_Type *base));
236 #endif
237 
238 /*!
239  * @brief Enables the WDOG32 interrupt.
240  *
241  * This function writes a value into the WDOG_CS register to enable the WDOG32 interrupt.
242  * The WDOG_CS register is a write-once register. Please check the enableUpdate is set to true for calling
243  * @ref WDOG32_Init to do wdog initialize.
244  * Before call the re-configuration APIs, ensure that the WCT window is still open and
245  * this register has not been written in this WCT while the function is called.
246  *
247  * @param base WDOG32 peripheral base address.
248  * @param mask The interrupts to enable.
249  *        The parameter can be a combination of the following source if defined:
250  *        @arg kWDOG32_InterruptEnable
251  */
252 #if defined(DOXYGEN_OUTPUT) && DOXYGEN_OUTPUT
253 void WDOG32_EnableInterrupts(WDOG_Type *base, uint32_t mask);
254 #else
255 AT_QUICKACCESS_SECTION_CODE(void WDOG32_EnableInterrupts(WDOG_Type *base, uint32_t mask));
256 #endif
257 
258 /*!
259  * @brief Disables the WDOG32 interrupt.
260  *
261  * This function writes a value into the WDOG_CS register to disable the WDOG32 interrupt.
262  * The WDOG_CS register is a write-once register. Please check the enableUpdate is set to true for calling
263  * @ref WDOG32_Init to do wdog initialize.
264  * Before call the re-configuration APIs, ensure that the WCT window is still open and
265  * this register has not been written in this WCT while the function is called.
266  *
267  * @param base WDOG32 peripheral base address.
268  * @param mask The interrupts to disabled.
269  *        The parameter can be a combination of the following source if defined:
270  *        @arg kWDOG32_InterruptEnable
271  */
272 #if defined(DOXYGEN_OUTPUT) && DOXYGEN_OUTPUT
273 void WDOG32_DisableInterrupts(WDOG_Type *base, uint32_t mask);
274 #else
275 AT_QUICKACCESS_SECTION_CODE(void WDOG32_DisableInterrupts(WDOG_Type *base, uint32_t mask));
276 #endif
277 
278 /*!
279  * @brief Gets the WDOG32 all status flags.
280  *
281  * This function gets all status flags.
282  *
283  * Example to get the running flag:
284  * @code
285  *   uint32_t status;
286  *   status = WDOG32_GetStatusFlags(wdog_base) & kWDOG32_RunningFlag;
287  * @endcode
288  * @param base        WDOG32 peripheral base address
289  * @return            State of the status flag: asserted (true) or not-asserted (false). @see _wdog32_status_flags_t
290  *                    - true: related status flag has been set.
291  *                    - false: related status flag is not set.
292  */
WDOG32_GetStatusFlags(WDOG_Type * base)293 static inline uint32_t WDOG32_GetStatusFlags(WDOG_Type *base)
294 {
295     return (base->CS & (WDOG_CS_EN_MASK | WDOG_CS_FLG_MASK));
296 }
297 
298 /*!
299  * @brief Clears the WDOG32 flag.
300  *
301  * This function clears the WDOG32 status flag.
302  *
303  * Example to clear an interrupt flag:
304  * @code
305  *   WDOG32_ClearStatusFlags(wdog_base,kWDOG32_InterruptFlag);
306  * @endcode
307  * @param base        WDOG32 peripheral base address.
308  * @param mask        The status flags to clear.
309  *                    The parameter can be any combination of the following values:
310  *                    @arg kWDOG32_InterruptFlag
311  */
312 
313 #if defined(DOXYGEN_OUTPUT) && DOXYGEN_OUTPUT
314 void WDOG32_ClearStatusFlags(WDOG_Type *base, uint32_t mask);
315 #else
316 AT_QUICKACCESS_SECTION_CODE(void WDOG32_ClearStatusFlags(WDOG_Type *base, uint32_t mask));
317 #endif
318 
319 /*!
320  * @brief Sets the WDOG32 timeout value.
321  *
322  * This function writes a timeout value into the WDOG_TOVAL register.
323  * The WDOG_TOVAL register is a write-once register. To ensure the reconfiguration fits the timing of WCT, unlock
324  * function will be called inline.
325  *
326  * @param base WDOG32 peripheral base address
327  * @param timeoutCount WDOG32 timeout value, count of WDOG32 clock ticks.
328  */
329 #if defined(DOXYGEN_OUTPUT) && DOXYGEN_OUTPUT
330 void WDOG32_SetTimeoutValue(WDOG_Type *base, uint16_t timeoutCount);
331 #else
332 AT_QUICKACCESS_SECTION_CODE(void WDOG32_SetTimeoutValue(WDOG_Type *base, uint16_t timeoutCount));
333 #endif
334 
335 /*!
336  * @brief Sets the WDOG32 window value.
337  *
338  * This function writes a window value into the WDOG_WIN register.
339  * The WDOG_WIN register is a write-once register. Please check the enableUpdate is set to true for calling
340  * @ref WDOG32_Init to do wdog initialize.
341  * Before call the re-configuration APIs, ensure that the WCT window is still open and
342  * this register has not been written in this WCT while the function is called.
343  *
344  * @param base WDOG32 peripheral base address.
345  * @param windowValue WDOG32 window value.
346  */
347 #if defined(DOXYGEN_OUTPUT) && DOXYGEN_OUTPUT
348 void WDOG32_SetWindowValue(WDOG_Type *base, uint16_t windowValue);
349 #else
350 AT_QUICKACCESS_SECTION_CODE(void WDOG32_SetWindowValue(WDOG_Type *base, uint16_t windowValue));
351 #endif
352 
353 /*!
354  * @brief Refreshes the WDOG32 timer.
355  *
356  * This function feeds the WDOG32.
357  * This function should be called before the Watchdog timer is in timeout. Otherwise, a reset is asserted.
358  *
359  * @param base WDOG32 peripheral base address
360  */
WDOG32_Refresh(WDOG_Type * base)361 static inline void WDOG32_Refresh(WDOG_Type *base)
362 {
363     uint32_t primaskValue = 0U;
364 
365     /* Disable the global interrupt to protect refresh sequence */
366     primaskValue = DisableGlobalIRQ();
367     if (0U != ((base->CS) & WDOG_CS_CMD32EN_MASK))
368     {
369         base->CNT = WDOG_REFRESH_KEY;
370     }
371     else
372     {
373         base->CNT = WDOG_FIRST_WORD_OF_REFRESH;
374         base->CNT = WDOG_SECOND_WORD_OF_REFRESH;
375     }
376     EnableGlobalIRQ(primaskValue);
377 }
378 
379 /*!
380  * @brief Gets the WDOG32 counter value.
381  *
382  * This function gets the WDOG32 counter value.
383  *
384  * @param base WDOG32 peripheral base address.
385  * @return     Current WDOG32 counter value.
386  */
WDOG32_GetCounterValue(WDOG_Type * base)387 static inline uint16_t WDOG32_GetCounterValue(WDOG_Type *base)
388 {
389     return (uint16_t)base->CNT;
390 }
391 
392 /*@}*/
393 
394 #if defined(__cplusplus)
395 }
396 #endif /* __cplusplus */
397 
398 /*! @}*/
399 
400 #endif /* _FSL_WDOG32_H_ */
401