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 
9 #include "fsl_wdog.h"
10 
11 /* Component ID definition, used by tools. */
12 #ifndef FSL_COMPONENT_ID
13 #define FSL_COMPONENT_ID "platform.drivers.wdog"
14 #endif
15 
16 /*******************************************************************************
17  * Code
18  ******************************************************************************/
19 
20 /*!
21  * brief Initializes the WDOG configuration structure.
22  *
23  * This function initializes the WDOG configuration structure to default values. The default
24  * values are as follows.
25  * code
26  *   wdogConfig->enableWdog = true;
27  *   wdogConfig->clockSource = kWDOG_LpoClockSource;
28  *   wdogConfig->prescaler = kWDOG_ClockPrescalerDivide1;
29  *   wdogConfig->workMode.enableWait = true;
30  *   wdogConfig->workMode.enableStop = false;
31  *   wdogConfig->workMode.enableDebug = false;
32  *   wdogConfig->enableUpdate = true;
33  *   wdogConfig->enableInterrupt = false;
34  *   wdogConfig->enableWindowMode = false;
35  *   wdogConfig->windowValue = 0;
36  *   wdogConfig->timeoutValue = 0xFFFFU;
37  * endcode
38  *
39  * param config Pointer to the WDOG configuration structure.
40  * see wdog_config_t
41  */
WDOG_GetDefaultConfig(wdog_config_t * config)42 void WDOG_GetDefaultConfig(wdog_config_t *config)
43 {
44     assert(NULL != config);
45 
46     /* Initializes the configure structure to zero. */
47     (void)memset(config, 0, sizeof(*config));
48 
49     config->enableWdog  = true;
50     config->clockSource = kWDOG_LpoClockSource;
51     config->prescaler   = kWDOG_ClockPrescalerDivide1;
52 #if defined(FSL_FEATURE_WDOG_HAS_WAITEN) && FSL_FEATURE_WDOG_HAS_WAITEN
53     config->workMode.enableWait = true;
54 #endif /* FSL_FEATURE_WDOG_HAS_WAITEN */
55     config->workMode.enableStop  = false;
56     config->workMode.enableDebug = false;
57     config->enableUpdate         = true;
58     config->enableInterrupt      = false;
59     config->enableWindowMode     = false;
60     config->windowValue          = 0U;
61     config->timeoutValue         = 0xFFFFU;
62 }
63 
64 /*!
65  * brief Initializes the WDOG.
66  *
67  * This function initializes the WDOG. When called, the WDOG runs according to the configuration.
68  * To reconfigure WDOG without forcing a reset first, enableUpdate must be set to true
69  * in the configuration.
70  *
71  * This is an example.
72  * code
73  *   wdog_config_t config;
74  *   WDOG_GetDefaultConfig(&config);
75  *   config.timeoutValue = 0x7ffU;
76  *   config.enableUpdate = true;
77  *   WDOG_Init(wdog_base,&config);
78  * endcode
79  *
80  * param base   WDOG peripheral base address
81  * param config The configuration of WDOG
82  */
WDOG_Init(WDOG_Type * base,const wdog_config_t * config)83 void WDOG_Init(WDOG_Type *base, const wdog_config_t *config)
84 {
85     assert(NULL != config);
86 
87     uint16_t value        = 0U;
88     uint32_t primaskValue = 0U;
89 
90     value = WDOG_STCTRLH_WDOGEN(config->enableWdog) | WDOG_STCTRLH_CLKSRC(config->clockSource) |
91             WDOG_STCTRLH_IRQRSTEN(config->enableInterrupt) | WDOG_STCTRLH_WINEN(config->enableWindowMode) |
92             WDOG_STCTRLH_ALLOWUPDATE(config->enableUpdate) | WDOG_STCTRLH_DBGEN(config->workMode.enableDebug) |
93             WDOG_STCTRLH_STOPEN(config->workMode.enableStop) |
94 #if defined(FSL_FEATURE_WDOG_HAS_WAITEN) && FSL_FEATURE_WDOG_HAS_WAITEN
95             WDOG_STCTRLH_WAITEN(config->workMode.enableWait) |
96 #endif /* FSL_FEATURE_WDOG_HAS_WAITEN */
97             WDOG_STCTRLH_DISTESTWDOG(1U);
98 
99     /* Disable the global interrupts. Otherwise, an interrupt could effectively invalidate the unlock sequence
100      * and the WCT may expire. After the configuration finishes, re-enable the global interrupts. */
101     primaskValue = DisableGlobalIRQ();
102     WDOG_Unlock(base);
103     /* Wait one bus clock cycle */
104     base->RSTCNT = 0U;
105     /* Set configuration */
106     base->PRESC   = WDOG_PRESC_PRESCVAL(config->prescaler);
107     base->WINH    = (uint16_t)((config->windowValue >> 16U) & 0xFFFFU);
108     base->WINL    = (uint16_t)((config->windowValue) & 0xFFFFU);
109     base->TOVALH  = (uint16_t)((config->timeoutValue >> 16U) & 0xFFFFU);
110     base->TOVALL  = (uint16_t)((config->timeoutValue) & 0xFFFFU);
111     base->STCTRLH = value;
112     EnableGlobalIRQ(primaskValue);
113 }
114 
115 /*!
116  * brief Shuts down the WDOG.
117  *
118  * This function shuts down the WDOG.
119  * Ensure that the WDOG_STCTRLH.ALLOWUPDATE is 1 which indicates that the register update is enabled.
120  */
WDOG_Deinit(WDOG_Type * base)121 void WDOG_Deinit(WDOG_Type *base)
122 {
123     uint32_t primaskValue = 0U;
124 
125     /* Disable the global interrupts */
126     primaskValue = DisableGlobalIRQ();
127     WDOG_Unlock(base);
128     /* Wait one bus clock cycle */
129     base->RSTCNT = 0U;
130     WDOG_Disable(base);
131     EnableGlobalIRQ(primaskValue);
132     WDOG_ClearResetCount(base);
133 }
134 
135 /*!
136  * brief Configures the WDOG functional test.
137  *
138  * This function is used to configure the WDOG functional test. When called, the WDOG goes into test mode
139  * and runs according to the configuration.
140  * Ensure that the WDOG_STCTRLH.ALLOWUPDATE is 1 which means that the register update is enabled.
141  *
142  * This is an example.
143  * code
144  *   wdog_test_config_t test_config;
145  *   test_config.testMode = kWDOG_QuickTest;
146  *   test_config.timeoutValue = 0xfffffu;
147  *   WDOG_SetTestModeConfig(wdog_base, &test_config);
148  * endcode
149  * param base   WDOG peripheral base address
150  * param config The functional test configuration of WDOG
151  */
WDOG_SetTestModeConfig(WDOG_Type * base,wdog_test_config_t * config)152 void WDOG_SetTestModeConfig(WDOG_Type *base, wdog_test_config_t *config)
153 {
154     assert(NULL != config);
155 
156     uint16_t value        = 0U;
157     uint32_t primaskValue = 0U;
158 
159     value = WDOG_STCTRLH_DISTESTWDOG(0U) | WDOG_STCTRLH_TESTWDOG(1U) | WDOG_STCTRLH_TESTSEL(config->testMode) |
160             WDOG_STCTRLH_BYTESEL(config->testedByte) | WDOG_STCTRLH_IRQRSTEN(0U) | WDOG_STCTRLH_WDOGEN(1U) |
161             WDOG_STCTRLH_ALLOWUPDATE(1U);
162 
163     /* Disable the global interrupts. Otherwise, an interrupt could effectively invalidate the unlock sequence
164      * and the WCT may expire. After the configuration finishes, re-enable the global interrupts. */
165     primaskValue = DisableGlobalIRQ();
166     WDOG_Unlock(base);
167     /* Wait one bus clock cycle */
168     base->RSTCNT = 0U;
169     /* Set configuration */
170     base->TOVALH  = (uint16_t)((config->timeoutValue >> 16U) & 0xFFFFU);
171     base->TOVALL  = (uint16_t)((config->timeoutValue) & 0xFFFFU);
172     base->STCTRLH = value;
173     EnableGlobalIRQ(primaskValue);
174 }
175 
176 /*!
177  * brief Gets the WDOG all status flags.
178  *
179  * This function gets all status flags.
180  *
181  * This is an example for getting the Running Flag.
182  * code
183  *   uint32_t status;
184  *   status = WDOG_GetStatusFlags (wdog_base) & kWDOG_RunningFlag;
185  * endcode
186  * param base        WDOG peripheral base address
187  * return            State of the status flag: asserted (true) or not-asserted (false).see _wdog_status_flags_t
188  *                    - true: a related status flag has been set.
189  *                    - false: a related status flag is not set.
190  */
WDOG_GetStatusFlags(WDOG_Type * base)191 uint32_t WDOG_GetStatusFlags(WDOG_Type *base)
192 {
193     uint32_t status_flag = 0U;
194 
195     status_flag |= ((uint32_t)base->STCTRLH & (uint32_t)WDOG_STCTRLH_WDOGEN_MASK);
196     status_flag |= ((uint32_t)base->STCTRLL & (uint32_t)WDOG_STCTRLL_INTFLG_MASK);
197 
198     return status_flag;
199 }
200 
201 /*!
202  * brief Clears the WDOG flag.
203  *
204  * This function clears the WDOG status flag.
205  *
206  * This is an example for clearing the timeout (interrupt) flag.
207  * code
208  *   WDOG_ClearStatusFlags(wdog_base,kWDOG_TimeoutFlag);
209  * endcode
210  * param base        WDOG peripheral base address
211  * param mask        The status flags to clear.
212  *                    The parameter could be any combination of the following values.
213  *                    kWDOG_TimeoutFlag
214  */
WDOG_ClearStatusFlags(WDOG_Type * base,uint32_t mask)215 void WDOG_ClearStatusFlags(WDOG_Type *base, uint32_t mask)
216 {
217     if (0U != (mask & (uint32_t)kWDOG_TimeoutFlag))
218     {
219         base->STCTRLL |= WDOG_STCTRLL_INTFLG_MASK;
220     }
221 }
222 
223 /*!
224  * brief Refreshes the WDOG timer.
225  *
226  * This function feeds the WDOG.
227  * This function should be called before the WDOG timer is in timeout. Otherwise, a reset is asserted.
228  *
229  * param base WDOG peripheral base address
230  */
WDOG_Refresh(WDOG_Type * base)231 void WDOG_Refresh(WDOG_Type *base)
232 {
233     uint32_t primaskValue = 0U;
234 
235     /* Disable the global interrupt to protect refresh sequence */
236     primaskValue  = DisableGlobalIRQ();
237     base->REFRESH = WDOG_FIRST_WORD_OF_REFRESH;
238     base->REFRESH = WDOG_SECOND_WORD_OF_REFRESH;
239     EnableGlobalIRQ(primaskValue);
240 }
241