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
31 #include "fsl_wdog.h"
32
33 /*******************************************************************************
34 * Code
35 ******************************************************************************/
36
WDOG_GetDefaultConfig(wdog_config_t * config)37 void WDOG_GetDefaultConfig(wdog_config_t *config)
38 {
39 assert(config);
40
41 config->enableWdog = true;
42 config->clockSource = kWDOG_LpoClockSource;
43 config->prescaler = kWDOG_ClockPrescalerDivide1;
44 #if defined(FSL_FEATURE_WDOG_HAS_WAITEN) && FSL_FEATURE_WDOG_HAS_WAITEN
45 config->workMode.enableWait = true;
46 #endif /* FSL_FEATURE_WDOG_HAS_WAITEN */
47 config->workMode.enableStop = false;
48 config->workMode.enableDebug = false;
49 config->enableUpdate = true;
50 config->enableInterrupt = false;
51 config->enableWindowMode = false;
52 config->windowValue = 0U;
53 config->timeoutValue = 0xFFFFU;
54 }
55
WDOG_Init(WDOG_Type * base,const wdog_config_t * config)56 void WDOG_Init(WDOG_Type *base, const wdog_config_t *config)
57 {
58 assert(config);
59
60 uint32_t value = 0U;
61 uint32_t primaskValue = 0U;
62
63 value = WDOG_STCTRLH_WDOGEN(config->enableWdog) | WDOG_STCTRLH_CLKSRC(config->clockSource) |
64 WDOG_STCTRLH_IRQRSTEN(config->enableInterrupt) | WDOG_STCTRLH_WINEN(config->enableWindowMode) |
65 WDOG_STCTRLH_ALLOWUPDATE(config->enableUpdate) | WDOG_STCTRLH_DBGEN(config->workMode.enableDebug) |
66 WDOG_STCTRLH_STOPEN(config->workMode.enableStop) |
67 #if defined(FSL_FEATURE_WDOG_HAS_WAITEN) && FSL_FEATURE_WDOG_HAS_WAITEN
68 WDOG_STCTRLH_WAITEN(config->workMode.enableWait) |
69 #endif /* FSL_FEATURE_WDOG_HAS_WAITEN */
70 WDOG_STCTRLH_DISTESTWDOG(1U);
71
72 /* Disable the global interrupts. Otherwise, an interrupt could effectively invalidate the unlock sequence
73 * and the WCT may expire. After the configuration finishes, re-enable the global interrupts. */
74 primaskValue = DisableGlobalIRQ();
75 WDOG_Unlock(base);
76 /* Wait one bus clock cycle */
77 base->RSTCNT = 0U;
78 /* Set configruation */
79 base->PRESC = WDOG_PRESC_PRESCVAL(config->prescaler);
80 base->WINH = (uint16_t)((config->windowValue >> 16U) & 0xFFFFU);
81 base->WINL = (uint16_t)((config->windowValue) & 0xFFFFU);
82 base->TOVALH = (uint16_t)((config->timeoutValue >> 16U) & 0xFFFFU);
83 base->TOVALL = (uint16_t)((config->timeoutValue) & 0xFFFFU);
84 base->STCTRLH = value;
85 EnableGlobalIRQ(primaskValue);
86 }
87
WDOG_Deinit(WDOG_Type * base)88 void WDOG_Deinit(WDOG_Type *base)
89 {
90 uint32_t primaskValue = 0U;
91
92 /* Disable the global interrupts */
93 primaskValue = DisableGlobalIRQ();
94 WDOG_Unlock(base);
95 /* Wait one bus clock cycle */
96 base->RSTCNT = 0U;
97 WDOG_Disable(base);
98 EnableGlobalIRQ(primaskValue);
99 WDOG_ClearResetCount(base);
100 }
101
WDOG_SetTestModeConfig(WDOG_Type * base,wdog_test_config_t * config)102 void WDOG_SetTestModeConfig(WDOG_Type *base, wdog_test_config_t *config)
103 {
104 assert(config);
105
106 uint32_t value = 0U;
107 uint32_t primaskValue = 0U;
108
109 value = WDOG_STCTRLH_DISTESTWDOG(0U) | WDOG_STCTRLH_TESTWDOG(1U) | WDOG_STCTRLH_TESTSEL(config->testMode) |
110 WDOG_STCTRLH_BYTESEL(config->testedByte) | WDOG_STCTRLH_IRQRSTEN(0U) | WDOG_STCTRLH_WDOGEN(1U) |
111 WDOG_STCTRLH_ALLOWUPDATE(1U);
112
113 /* Disable the global interrupts. Otherwise, an interrupt could effectively invalidate the unlock sequence
114 * and the WCT may expire. After the configuration finishes, re-enable the global interrupts. */
115 primaskValue = DisableGlobalIRQ();
116 WDOG_Unlock(base);
117 /* Wait one bus clock cycle */
118 base->RSTCNT = 0U;
119 /* Set configruation */
120 base->TOVALH = (uint16_t)((config->timeoutValue >> 16U) & 0xFFFFU);
121 base->TOVALL = (uint16_t)((config->timeoutValue) & 0xFFFFU);
122 base->STCTRLH = value;
123 EnableGlobalIRQ(primaskValue);
124 }
125
WDOG_GetStatusFlags(WDOG_Type * base)126 uint32_t WDOG_GetStatusFlags(WDOG_Type *base)
127 {
128 uint32_t status_flag = 0U;
129
130 status_flag |= (base->STCTRLH & WDOG_STCTRLH_WDOGEN_MASK);
131 status_flag |= (base->STCTRLL & WDOG_STCTRLL_INTFLG_MASK);
132
133 return status_flag;
134 }
135
WDOG_ClearStatusFlags(WDOG_Type * base,uint32_t mask)136 void WDOG_ClearStatusFlags(WDOG_Type *base, uint32_t mask)
137 {
138 if (mask & kWDOG_TimeoutFlag)
139 {
140 base->STCTRLL |= WDOG_STCTRLL_INTFLG_MASK;
141 }
142 }
143
WDOG_Refresh(WDOG_Type * base)144 void WDOG_Refresh(WDOG_Type *base)
145 {
146 uint32_t primaskValue = 0U;
147
148 /* Disable the global interrupt to protect refresh sequence */
149 primaskValue = DisableGlobalIRQ();
150 base->REFRESH = WDOG_FIRST_WORD_OF_REFRESH;
151 base->REFRESH = WDOG_SECOND_WORD_OF_REFRESH;
152 EnableGlobalIRQ(primaskValue);
153 }
154