1 /***************************************************************************//**
2 * \file cy_wdt_b.c
3 * \version 1.60
4 *
5 *  This file provides the source code to the API for the WDT version B 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_MXS40SRSS) && (CY_IP_MXS40SRSS_VERSION >= 3)
28 
29 #include "cy_wdt.h"
30 
31 #if defined(__cplusplus)
32 extern "C" {
33 #endif
34 
35 
36 /*******************************************************************************
37 * Function Name: Cy_WDT_Init
38 ****************************************************************************//**
39 *
40 * Initializes the Watchdog timer to its default state.
41 *
42 * The given default setting of the WDT:
43 * The WDT is unlocked and disabled.
44 * The WDT Lower Limit value is set to 0 and Lower action to None.
45 * The WDT Warn Limit value is set to 0 and Warn action to None.
46 * The WDT Upper Limit value is set to 32000 (1 second) and Upper action to Reset.
47 * The WDT is locked again.
48 *
49 *******************************************************************************/
Cy_WDT_Init(void)50 void Cy_WDT_Init(void)
51 {
52     /* Unlock the WDT registers by two writes */
53     Cy_WDT_Unlock();
54 
55     Cy_WDT_Disable();
56     /* Set upper limit = 32000 (1sec) */
57     Cy_WDT_SetLowerLimit(CY_WDT_DEFAULT_LOWER_LIMIT);
58     Cy_WDT_SetUpperLimit(CY_WDT_DEFAULT_UPPER_LIMIT);
59     Cy_WDT_SetWarnLimit(CY_WDT_DEFAULT_WARN_LIMIT);
60     Cy_WDT_SetLowerAction(CY_WDT_LOW_UPPER_LIMIT_ACTION_NONE);
61     Cy_WDT_SetUpperAction(CY_WDT_LOW_UPPER_LIMIT_ACTION_RESET);
62     Cy_WDT_SetWarnAction(CY_WDT_WARN_ACTION_NONE);
63 
64     /* Lock the WDT registers */
65     Cy_WDT_Lock();
66 }
67 
68 /*******************************************************************************
69 * Function Name: Cy_WDT_Lock
70 ****************************************************************************//**
71 *
72 * Locks out configuration changes to the Watchdog Timer register.
73 *
74 * After this function is called, the WDT configuration cannot be changed until
75 * Cy_WDT_Unlock() is called.
76 *
77 * \warning
78 * The WDT lock state is not retained during system Deep Sleep. After the wakeup
79 * from system Deep Sleep the WDT is locked.
80 *
81 *******************************************************************************/
Cy_WDT_Lock(void)82 void Cy_WDT_Lock(void)
83 {
84     SRSS_WDT_LOCK |= _VAL2FLD(WDT_LOCK_WDT_LOCK, CY_SRSS_WDT_LOCK_BITS);
85 }
86 
87 
88 /*******************************************************************************
89 * Function Name: Cy_WDT_Locked
90 ****************************************************************************//**
91 *
92 * Returns the WDT lock state.
93 *
94 * \return
95 * True - if WDT is locked.
96 * False - if WDT is unlocked.
97 *
98 *******************************************************************************/
Cy_WDT_Locked(void)99 bool Cy_WDT_Locked(void)
100 {
101     /* Prohibits writing to the WDT registers and other CLK_LF */
102     return (0u != _FLD2VAL(WDT_LOCK_WDT_LOCK, SRSS_WDT_LOCK));
103 }
104 
105 
106 /*******************************************************************************
107 * Function Name: Cy_WDT_Unlock
108 ****************************************************************************//**
109 *
110 * Unlocks the Watchdog Timer configuration register.
111 *
112 * \warning
113 * The WDT lock state is not retained during system Deep Sleep. After the wakeup
114 * from system Deep Sleep the WDT is locked.
115 *
116 *******************************************************************************/
Cy_WDT_Unlock(void)117 void Cy_WDT_Unlock(void)
118 {
119     /* The WDT lock is to be removed by two writes */
120     SRSS_WDT_LOCK = ((SRSS_WDT_LOCK & (uint32_t)(~WDT_LOCK_WDT_LOCK_Msk)) | CY_SRSS_WDT_LOCK_BIT0);
121 
122     SRSS_WDT_LOCK |= CY_SRSS_WDT_LOCK_BIT1;
123 }
124 
125 /*******************************************************************************
126 * Function Name: Cy_WDT_ClearInterrupt
127 ****************************************************************************//**
128 *
129 * Clears the WDT match flag which is set as configured by WDT action and limits.
130 *
131 *******************************************************************************/
Cy_WDT_ClearInterrupt(void)132 void Cy_WDT_ClearInterrupt(void)
133 {
134     SRSS_WDT_INTR = _VAL2FLD(WDT_INTR_WDT, 1U);
135 
136     /* Read the interrupt register to ensure that the initial clearing write has
137     * been flushed out to the hardware.
138     */
139     (void) SRSS_WDT_INTR;
140 }
141 
142 /*******************************************************************************
143 * Function Name: Cy_WDT_ClearWatchdog
144 ****************************************************************************//**
145 *
146 * Clears ("feeds") the watchdog, to prevent a XRES device reset.
147 * This function simply call Cy_WDT_SetService() function.
148 *
149 *******************************************************************************/
Cy_WDT_ClearWatchdog(void)150 void Cy_WDT_ClearWatchdog(void)
151 {
152     Cy_WDT_Unlock();
153     Cy_WDT_SetService();
154     Cy_WDT_Lock();
155 }
156 
157 /*******************************************************************************
158 * Function Name: Cy_WDT_SetLowerLimit
159 ****************************************************************************//**
160 *
161 * Set lower limit for watchdog.
162 *
163 * \param match - Value to be matched
164 *
165 * \note
166 * This API is available for CAT1C devices.
167 *
168 *******************************************************************************/
Cy_WDT_SetLowerLimit(uint32_t match)169 void Cy_WDT_SetLowerLimit(uint32_t match)
170 {
171     SRSS_WDT_LOWER_LIMIT = match;
172 }
173 
174 /*******************************************************************************
175 * Function Name: Cy_WDT_SetUpperLimit
176 ****************************************************************************//**
177 *
178 * Set upper limit for watchdog.
179 *
180 * \param match - Value to be matched
181 *
182 * \note
183 * This API is available for CAT1C devices.
184 *
185 *******************************************************************************/
Cy_WDT_SetUpperLimit(uint32_t match)186 void Cy_WDT_SetUpperLimit(uint32_t match)
187 {
188     SRSS_WDT_UPPER_LIMIT = match;
189 }
190 
191 /*******************************************************************************
192 * Function Name: Cy_WDT_SetWarnLimit
193 ****************************************************************************//**
194 *
195 * Set warn limit for watchdog.
196 *
197 * \param match - Value to be matched
198 *
199 * \note
200 * This API is available for CAT1C devices.
201 *
202 *******************************************************************************/
Cy_WDT_SetWarnLimit(uint32_t match)203 void Cy_WDT_SetWarnLimit(uint32_t match)
204 {
205     SRSS_WDT_WARN_LIMIT = match;
206 }
207 
208 /*******************************************************************************
209 * Function Name: Cy_WDT_SetLowerAction
210 ****************************************************************************//**
211 *
212 * Set action taken if this watchdog is serviced before LOWER_LIMIT is reached.
213 *
214 * \param action
215 * \ref cy_en_wdt_lower_upper_action_t
216 *
217 * \note
218 * This API is available for CAT1C devices.
219 *
220 *******************************************************************************/
Cy_WDT_SetLowerAction(cy_en_wdt_lower_upper_action_t action)221 void Cy_WDT_SetLowerAction(cy_en_wdt_lower_upper_action_t action)
222 {
223     CY_REG32_CLR_SET(SRSS_WDT_CONFIG, WDT_CONFIG_LOWER_ACTION, action);
224 }
225 
226 /*******************************************************************************
227 * Function Name: Cy_WDT_SetUpperAction
228 ****************************************************************************//**
229 *
230 * Set action taken if this watchdog is not serviced before UPPER_LIMIT is reached.
231 *
232 * \param action
233 * \ref cy_en_wdt_lower_upper_action_t
234 *
235 * \note
236 * This API is available for CAT1C devices.
237 *
238 *******************************************************************************/
Cy_WDT_SetUpperAction(cy_en_wdt_lower_upper_action_t action)239 void Cy_WDT_SetUpperAction(cy_en_wdt_lower_upper_action_t action)
240 {
241     CY_REG32_CLR_SET(SRSS_WDT_CONFIG, WDT_CONFIG_UPPER_ACTION, action);
242 }
243 
244 /*******************************************************************************
245 * Function Name: Cy_WDT_SetWarnAction
246 ****************************************************************************//**
247 *
248 * Set action taken when the count value reaches WARN_LIMIT.
249 *
250 * \param action
251 * \ref cy_en_wdt_warn_action_t
252 *
253 * \note
254 * This API is available for CAT1C devices.
255 *
256 *******************************************************************************/
Cy_WDT_SetWarnAction(cy_en_wdt_warn_action_t action)257 void Cy_WDT_SetWarnAction(cy_en_wdt_warn_action_t action)
258 {
259     CY_REG32_CLR_SET(SRSS_WDT_CONFIG, WDT_CONFIG_WARN_ACTION, action);
260 }
261 
262 /*******************************************************************************
263 * Function Name: Cy_WDT_SetAutoService
264 ****************************************************************************//**
265 *
266 * Set automatically service when the count value reaches WARN_LIMIT.
267 * This function is used for WARN_LIMIT.
268 *
269 * \param enable
270 * \ref cy_en_wdt_enable_t
271 *
272 * \note
273 * This API is available for CAT1C devices.
274 *
275 *******************************************************************************/
Cy_WDT_SetAutoService(cy_en_wdt_enable_t enable)276 void Cy_WDT_SetAutoService(cy_en_wdt_enable_t enable)
277 {
278     CY_REG32_CLR_SET(SRSS_WDT_CONFIG, WDT_CONFIG_AUTO_SERVICE, enable);
279 }
280 
281 /*******************************************************************************
282 * Function Name: Cy_WDT_SetDeepSleepPause
283 ****************************************************************************//**
284 *
285 * Set whether the WDT counter runs/pauses when the system is in DEEPSLEEP.
286 *
287 * \param enable
288 * \ref cy_en_wdt_enable_t
289 *
290 * \note
291 * This API is available for CAT1C devices.
292 *
293 *******************************************************************************/
Cy_WDT_SetDeepSleepPause(cy_en_wdt_enable_t enable)294 void Cy_WDT_SetDeepSleepPause(cy_en_wdt_enable_t enable)
295 {
296     CY_REG32_CLR_SET(SRSS_WDT_CONFIG, WDT_CONFIG_DPSLP_PAUSE, enable);
297 }
298 
299 /*******************************************************************************
300 * Function Name: Cy_WDT_SetHibarnatePause
301 ****************************************************************************//**
302 *
303 * Set whether the WDT counter runs/pauses when the system is in HIBERNATE.
304 *
305 * \param enable
306 * \ref cy_en_wdt_enable_t
307 *
308 * \note
309 * This API is available for CAT1C devices.
310 *
311 *******************************************************************************/
Cy_WDT_SetHibernatePause(cy_en_wdt_enable_t enable)312 void Cy_WDT_SetHibernatePause(cy_en_wdt_enable_t enable)
313 {
314     CY_REG32_CLR_SET(SRSS_WDT_CONFIG, WDT_CONFIG_HIB_PAUSE, enable);
315 }
316 
317 /*******************************************************************************
318 * Function Name: Cy_WDT_SetDebugRun
319 ****************************************************************************//**
320 *
321 * Set the debug run value. It needs when using debugger.
322 *
323 * \param enable
324 * \ref cy_en_wdt_enable_t
325 *
326 * \note
327 * This API is available for CAT1C devices.
328 *
329 *******************************************************************************/
Cy_WDT_SetDebugRun(cy_en_wdt_enable_t enable)330 void Cy_WDT_SetDebugRun(cy_en_wdt_enable_t enable)
331 {
332     CY_REG32_CLR_SET(SRSS_WDT_CONFIG, WDT_CONFIG_DEBUG_RUN, enable);
333 }
334 
335 /*******************************************************************************
336 * Function Name: Cy_WDT_SetService
337 ****************************************************************************//**
338 *
339 * Set service of the watchdog. This resets the count value to zero.
340 *
341 * \note
342 * This API is available for CAT1C devices.
343 *
344 *******************************************************************************/
Cy_WDT_SetService(void)345 void Cy_WDT_SetService(void)
346 {
347     SRSS_WDT_SERVICE = WDT_SERVICE_SERVICE_Msk;
348 }
349 
350 #if defined(__cplusplus)
351 }
352 #endif
353 
354 #endif /* */
355 
356 /* [] END OF FILE */
357