1 /***************************************************************************//**
2 * \file cy_wdt.c
3 * \version 1.30.1
4 *
5 *  This file provides the source code to the API for the WDT driver.
6 *
7 ********************************************************************************
8 * \copyright
9 * Copyright 2016-2020 Cypress Semiconductor Corporation
10 * SPDX-License-Identifier: Apache-2.0
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 *     http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *******************************************************************************/
24 
25 #include "cy_device.h"
26 
27 #if defined (CY_IP_MXS28SRSS) || defined (CY_IP_MXS40SRSS) || defined (CY_IP_MXS40SSRSS )
28 
29 #include "cy_wdt.h"
30 
31 #if defined(__cplusplus)
32 extern "C" {
33 #endif
34 
35 
36 
37 /*******************************************************************************
38 * Function Name: Cy_WDT_Init
39 ****************************************************************************//**
40 *
41 * Initializes the Watchdog timer to its default state.
42 *
43 * The given default setting of the WDT:
44 * The WDT is unlocked and disabled.
45 * The WDT match value is 4096.
46 * None of ignore bits are set: the whole WDT counter bits are checked against
47 * the match value.
48 *
49 * \sideeffect
50 * This function clears the WDT interrupt.
51 *
52 *******************************************************************************/
Cy_WDT_Init(void)53 void Cy_WDT_Init(void)
54 {
55     /* Unlock the WDT by two writes */
56     SRSS_WDT_CTL = ((SRSS_WDT_CTL & (uint32_t)(~SRSS_WDT_CTL_WDT_LOCK_Msk)) | CY_SRSS_WDT_LOCK_BIT0);
57     SRSS_WDT_CTL |= CY_SRSS_WDT_LOCK_BIT1;
58 
59     Cy_WDT_Disable();
60     Cy_WDT_SetMatch(CY_SRSS_WDT_DEFAULT_MATCH_VALUE);
61     Cy_WDT_SetIgnoreBits(CY_SRSS_WDT_DEFAULT_IGNORE_BITS);
62     Cy_WDT_ClearInterrupt();
63 }
64 
65 
66 /*******************************************************************************
67 * Function Name: Cy_WDT_Lock
68 ****************************************************************************//**
69 *
70 * Locks out configuration changes to the Watchdog Timer register.
71 *
72 * After this function is called, the WDT configuration cannot be changed until
73 * Cy_WDT_Unlock() is called.
74 *
75 * \warning
76 * The WDT lock state is not retained during system Deep Sleep. After the wakeup
77 * from system Deep Sleep the WDT is locked.
78 *
79 *******************************************************************************/
Cy_WDT_Lock(void)80 void Cy_WDT_Lock(void)
81 {
82     SRSS_WDT_CTL |= _VAL2FLD(SRSS_WDT_CTL_WDT_LOCK, CY_SRSS_WDT_LOCK_BITS);
83 }
84 
85 
86 /*******************************************************************************
87 * Function Name: Cy_WDT_Locked
88 ****************************************************************************//**
89 *
90 * Returns the WDT lock state.
91 *
92 * \return
93 * True - if WDT is locked.
94 * False - if WDT is unlocked.
95 *
96 *******************************************************************************/
Cy_WDT_Locked(void)97 bool Cy_WDT_Locked(void)
98 {
99     /* Prohibits writing to the WDT registers and other CLK_LF */
100     return (0u != _FLD2VAL(SRSS_WDT_CTL_WDT_LOCK, SRSS_WDT_CTL));
101 }
102 
103 
104 /*******************************************************************************
105 * Function Name: Cy_WDT_Unlock
106 ****************************************************************************//**
107 *
108 * Unlocks the Watchdog Timer configuration register.
109 *
110 * \warning
111 * The WDT lock state is not retained during system Deep Sleep. After the wakeup
112 * from system Deep Sleep the WDT is locked.
113 *
114 *******************************************************************************/
Cy_WDT_Unlock(void)115 void Cy_WDT_Unlock(void)
116 {
117     /* The WDT lock is to be removed by two writes */
118     SRSS_WDT_CTL = ((SRSS_WDT_CTL & (uint32_t)(~SRSS_WDT_CTL_WDT_LOCK_Msk)) | CY_SRSS_WDT_LOCK_BIT0);
119 
120     SRSS_WDT_CTL |= CY_SRSS_WDT_LOCK_BIT1;
121 }
122 
123 
124 /*******************************************************************************
125 * Function Name: Cy_WDT_SetMatch
126 ****************************************************************************//**
127 *
128 * Configures the WDT counter match comparison value. The Watchdog timer
129 * should be unlocked before changing the match value. Call the Cy_WDT_Unlock()
130 * function to unlock the WDT.
131 *
132 * \param match
133 * The valid valid range is [0-65535]. The value to be used to match
134 * against the counter.
135 *
136 *******************************************************************************/
Cy_WDT_SetMatch(uint32_t match)137 void Cy_WDT_SetMatch(uint32_t match)
138 {
139     CY_ASSERT_L2(CY_WDT_IS_MATCH_VAL_VALID(match));
140 
141     if (false == Cy_WDT_Locked())
142     {
143         SRSS_WDT_MATCH = _CLR_SET_FLD32U((SRSS_WDT_MATCH), SRSS_WDT_MATCH_MATCH, match);
144     }
145 }
146 
147 
148 /*******************************************************************************
149 * Function Name: Cy_WDT_SetIgnoreBits
150 ****************************************************************************//**
151 *
152 * Configures the number of the most significant bits of the Watchdog timer that
153 * are not checked against the match. Unlock the Watchdog timer before
154 * ignoring the bits setting. Call the Cy_WDT_Unlock() API to unlock the WDT.
155 *
156 * \param bitsNum
157 * The number of the most significant bits. The valid range is [0-15].
158 * The bitsNum over 12 are considered as 12.
159 *
160 * \details The value of bitsNum controls the time-to-reset of the Watchdog timer
161 * This happens after 3 successive matches.
162 *
163 * \warning This function changes the WDT interrupt period, therefore
164 * the device can go into an infinite WDT reset loop. This may happen
165 * if a WDT reset occurs faster that a device start-up.
166 *
167 *******************************************************************************/
Cy_WDT_SetIgnoreBits(uint32_t bitsNum)168 void Cy_WDT_SetIgnoreBits(uint32_t bitsNum)
169 {
170     CY_ASSERT_L2(CY_WDT_IS_IGNORE_BITS_VALID(bitsNum));
171 
172     if (false == Cy_WDT_Locked())
173     {
174         SRSS_WDT_MATCH = _CLR_SET_FLD32U((SRSS_WDT_MATCH), SRSS_WDT_MATCH_IGNORE_BITS, bitsNum);
175     }
176 }
177 
178 
179 /*******************************************************************************
180 * Function Name: Cy_WDT_ClearInterrupt
181 ****************************************************************************//**
182 *
183 * Clears the WDT match flag which is set every time the WDT counter reaches a
184 * WDT match value. Two unserviced interrupts lead to a system reset
185 * (i.e. at the third match).
186 *
187 *******************************************************************************/
Cy_WDT_ClearInterrupt(void)188 void Cy_WDT_ClearInterrupt(void)
189 {
190     #if CY_CPU_CORTEX_M4 && defined(CY_DEVICE_SECURE)
191         CY_PRA_REG32_SET(CY_PRA_INDX_SRSS_SRSS_INTR, _VAL2FLD(SRSS_SRSS_INTR_WDT_MATCH, 1U));
192     #else
193         SRSS_SRSS_INTR = _VAL2FLD(SRSS_SRSS_INTR_WDT_MATCH, 1U);
194     #endif
195 
196     /* Read the interrupt register to ensure that the initial clearing write has
197     * been flushed out to the hardware.
198     */
199     (void) SRSS_SRSS_INTR;
200 }
201 
202 
203 /*******************************************************************************
204 * Function Name: Cy_WDT_ClearWatchdog
205 ****************************************************************************//**
206 *
207 * Clears ("feeds") the watchdog, to prevent a XRES device reset.
208 * This function simply call Cy_WDT_ClearInterrupt() function.
209 *
210 *******************************************************************************/
Cy_WDT_ClearWatchdog(void)211 void Cy_WDT_ClearWatchdog(void)
212 {
213     Cy_WDT_ClearInterrupt();
214 }
215 
216 #if defined(__cplusplus)
217 }
218 #endif
219 
220 #endif /* CY_IP_MXS28SRSS, CY_IP_MXS40SRSS */
221 
222 /* [] END OF FILE */
223