1 /**
2 ******************************************************************************
3 * @file stm32h7xx_hal_wwdg.c
4 * @author MCD Application Team
5 * @brief WWDG HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the Window Watchdog (WWDG) peripheral:
8 * + Initialization and Configuration functions
9 * + IO operation functions
10 ******************************************************************************
11 * @attention
12 *
13 * Copyright (c) 2017 STMicroelectronics.
14 * All rights reserved.
15 *
16 * This software is licensed under terms that can be found in the LICENSE file
17 * in the root directory of this software component.
18 * If no LICENSE file comes with this software, it is provided AS-IS.
19 *
20 ******************************************************************************
21 @verbatim
22 ==============================================================================
23 ##### WWDG Specific features #####
24 ==============================================================================
25 [..]
26 Once enabled the WWDG generates a system reset on expiry of a programmed
27 time period, unless the program refreshes the counter (T[6;0] downcounter)
28 before reaching 0x3F value (i.e. a reset is generated when the counter
29 value rolls down from 0x40 to 0x3F).
30
31 (+) An MCU reset is also generated if the counter value is refreshed
32 before the counter has reached the refresh window value. This
33 implies that the counter must be refreshed in a limited window.
34 (+) Once enabled the WWDG cannot be disabled except by a system reset.
35 (+) If required by application, an Early Wakeup Interrupt can be triggered
36 in order to be warned before WWDG expiration. The Early Wakeup Interrupt
37 (EWI) can be used if specific safety operations or data logging must
38 be performed before the actual reset is generated. When the downcounter
39 reaches 0x40, interrupt occurs. This mechanism requires WWDG interrupt
40 line to be enabled in NVIC. Once enabled, EWI interrupt cannot be
41 disabled except by a system reset.
42 (+) WWDGRST flag in RCC CSR register can be used to inform when a WWDG
43 reset occurs.
44 (+) The WWDG counter input clock is derived from the APB clock divided
45 by a programmable prescaler.
46 (+) WWDG clock (Hz) = PCLK1 / (4096 * Prescaler)
47 (+) WWDG timeout (mS) = 1000 * (T[5;0] + 1) / WWDG clock (Hz)
48 where T[5;0] are the lowest 6 bits of Counter.
49 (+) WWDG Counter refresh is allowed between the following limits :
50 (++) min time (mS) = 1000 * (Counter - Window) / WWDG clock
51 (++) max time (mS) = 1000 * (Counter - 0x40) / WWDG clock
52 (+) Typical values (case of STM32H74x/5x devices):
53 (++) Counter min (T[5;0] = 0x00) @100MHz (PCLK1) with zero prescaler:
54 max timeout before reset: approximately 40.96us
55 (++) Counter max (T[5;0] = 0x3F) @100MHz (PCLK1) with prescaler dividing by 128:
56 max timeout before reset: approximately 335.54ms
57 (+) Typical values (case of STM32H7Ax/Bx devices):
58 (++) Counter min (T[5;0] = 0x00) @140MHz (PCLK1) with zero prescaler:
59 max timeout before reset: approximately 29.25us
60 (++) Counter max (T[5;0] = 0x3F) @140MHz (PCLK1) with prescaler dividing by 128:
61 max timeout before reset: approximately 239.67ms
62 (+) Typical values (case of STM32H72x/3x devices):
63 (++) Counter min (T[5;0] = 0x00) @125MHz (PCLK1) with zero prescaler:
64 max timeout before reset: approximately 32.76us
65 (++) Counter max (T[5;0] = 0x3F) @125MHz (PCLK1) with prescaler dividing by 128:
66 max timeout before reset: approximately 268.43ms
67
68 ##### How to use this driver #####
69 ==============================================================================
70
71 *** Common driver usage ***
72 ===========================
73
74 [..]
75 (+) Enable WWDG APB1 clock using __HAL_RCC_WWDG_CLK_ENABLE().
76 (+) Configure the WWDG prescaler, refresh window value, counter value and early
77 interrupt status using HAL_WWDG_Init() function. This will automatically
78 enable WWDG and start its downcounter. Time reference can be taken from
79 function exit. Care must be taken to provide a counter value
80 greater than 0x40 to prevent generation of immediate reset.
81 (+) If the Early Wakeup Interrupt (EWI) feature is enabled, an interrupt is
82 generated when the counter reaches 0x40. When HAL_WWDG_IRQHandler is
83 triggered by the interrupt service routine, flag will be automatically
84 cleared and HAL_WWDG_WakeupCallback user callback will be executed. User
85 can add his own code by customization of callback HAL_WWDG_WakeupCallback.
86 (+) Then the application program must refresh the WWDG counter at regular
87 intervals during normal operation to prevent an MCU reset, using
88 HAL_WWDG_Refresh() function. This operation must occur only when
89 the counter is lower than the refresh window value already programmed.
90
91 *** Callback registration ***
92 =============================
93
94 [..]
95 The compilation define USE_HAL_WWDG_REGISTER_CALLBACKS when set to 1 allows
96 the user to configure dynamically the driver callbacks. Use Functions
97 HAL_WWDG_RegisterCallback() to register a user callback.
98
99 (+) Function HAL_WWDG_RegisterCallback() allows to register following
100 callbacks:
101 (++) EwiCallback : callback for Early WakeUp Interrupt.
102 (++) MspInitCallback : WWDG MspInit.
103 This function takes as parameters the HAL peripheral handle, the Callback ID
104 and a pointer to the user callback function.
105
106 (+) Use function HAL_WWDG_UnRegisterCallback() to reset a callback to
107 the default weak (surcharged) function. HAL_WWDG_UnRegisterCallback()
108 takes as parameters the HAL peripheral handle and the Callback ID.
109 This function allows to reset following callbacks:
110 (++) EwiCallback : callback for Early WakeUp Interrupt.
111 (++) MspInitCallback : WWDG MspInit.
112
113 [..]
114 When calling HAL_WWDG_Init function, callbacks are reset to the
115 corresponding legacy weak (surcharged) functions:
116 HAL_WWDG_EarlyWakeupCallback() and HAL_WWDG_MspInit() only if they have
117 not been registered before.
118
119 [..]
120 When compilation define USE_HAL_WWDG_REGISTER_CALLBACKS is set to 0 or
121 not defined, the callback registering feature is not available
122 and weak (surcharged) callbacks are used.
123
124 *** WWDG HAL driver macros list ***
125 ===================================
126 [..]
127 Below the list of available macros in WWDG HAL driver.
128 (+) __HAL_WWDG_ENABLE: Enable the WWDG peripheral
129 (+) __HAL_WWDG_GET_FLAG: Get the selected WWDG's flag status
130 (+) __HAL_WWDG_CLEAR_FLAG: Clear the WWDG's pending flags
131 (+) __HAL_WWDG_ENABLE_IT: Enable the WWDG early wakeup interrupt
132
133 @endverbatim
134 ******************************************************************************
135 */
136
137 /* Includes ------------------------------------------------------------------*/
138 #include "stm32h7xx_hal.h"
139
140 /** @addtogroup STM32H7xx_HAL_Driver
141 * @{
142 */
143
144 #ifdef HAL_WWDG_MODULE_ENABLED
145 /** @defgroup WWDG WWDG
146 * @brief WWDG HAL module driver.
147 * @{
148 */
149
150 /* Private typedef -----------------------------------------------------------*/
151 /* Private define ------------------------------------------------------------*/
152 /* Private macro -------------------------------------------------------------*/
153 /* Private variables ---------------------------------------------------------*/
154 /* Private function prototypes -----------------------------------------------*/
155 /* Exported functions --------------------------------------------------------*/
156
157 /** @defgroup WWDG_Exported_Functions WWDG Exported Functions
158 * @{
159 */
160
161 /** @defgroup WWDG_Exported_Functions_Group1 Initialization and Configuration functions
162 * @brief Initialization and Configuration functions.
163 *
164 @verbatim
165 ==============================================================================
166 ##### Initialization and Configuration functions #####
167 ==============================================================================
168 [..]
169 This section provides functions allowing to:
170 (+) Initialize and start the WWDG according to the specified parameters
171 in the WWDG_InitTypeDef of associated handle.
172 (+) Initialize the WWDG MSP.
173
174 @endverbatim
175 * @{
176 */
177
178 /**
179 * @brief Initialize the WWDG according to the specified.
180 * parameters in the WWDG_InitTypeDef of associated handle.
181 * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
182 * the configuration information for the specified WWDG module.
183 * @retval HAL status
184 */
HAL_WWDG_Init(WWDG_HandleTypeDef * hwwdg)185 HAL_StatusTypeDef HAL_WWDG_Init(WWDG_HandleTypeDef *hwwdg)
186 {
187 /* Check the WWDG handle allocation */
188 if (hwwdg == NULL)
189 {
190 return HAL_ERROR;
191 }
192
193 /* Check the parameters */
194 assert_param(IS_WWDG_ALL_INSTANCE(hwwdg->Instance));
195 assert_param(IS_WWDG_PRESCALER(hwwdg->Init.Prescaler));
196 assert_param(IS_WWDG_WINDOW(hwwdg->Init.Window));
197 assert_param(IS_WWDG_COUNTER(hwwdg->Init.Counter));
198 assert_param(IS_WWDG_EWI_MODE(hwwdg->Init.EWIMode));
199
200 #if (USE_HAL_WWDG_REGISTER_CALLBACKS == 1)
201 /* Reset Callback pointers */
202 if (hwwdg->EwiCallback == NULL)
203 {
204 hwwdg->EwiCallback = HAL_WWDG_EarlyWakeupCallback;
205 }
206
207 if (hwwdg->MspInitCallback == NULL)
208 {
209 hwwdg->MspInitCallback = HAL_WWDG_MspInit;
210 }
211
212 /* Init the low level hardware */
213 hwwdg->MspInitCallback(hwwdg);
214 #else
215 /* Init the low level hardware */
216 HAL_WWDG_MspInit(hwwdg);
217 #endif /* USE_HAL_WWDG_REGISTER_CALLBACKS */
218
219 /* Set WWDG Counter */
220 WRITE_REG(hwwdg->Instance->CR, (WWDG_CR_WDGA | hwwdg->Init.Counter));
221
222 /* Set WWDG Prescaler and Window */
223 WRITE_REG(hwwdg->Instance->CFR, (hwwdg->Init.EWIMode | hwwdg->Init.Prescaler | hwwdg->Init.Window));
224
225 /* Return function status */
226 return HAL_OK;
227 }
228
229
230 /**
231 * @brief Initialize the WWDG MSP.
232 * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
233 * the configuration information for the specified WWDG module.
234 * @note When rewriting this function in user file, mechanism may be added
235 * to avoid multiple initialize when HAL_WWDG_Init function is called
236 * again to change parameters.
237 * @retval None
238 */
HAL_WWDG_MspInit(WWDG_HandleTypeDef * hwwdg)239 __weak void HAL_WWDG_MspInit(WWDG_HandleTypeDef *hwwdg)
240 {
241 /* Prevent unused argument(s) compilation warning */
242 UNUSED(hwwdg);
243
244 /* NOTE: This function should not be modified, when the callback is needed,
245 the HAL_WWDG_MspInit could be implemented in the user file
246 */
247 }
248
249
250 #if (USE_HAL_WWDG_REGISTER_CALLBACKS == 1)
251 /**
252 * @brief Register a User WWDG Callback
253 * To be used instead of the weak (surcharged) predefined callback
254 * @param hwwdg WWDG handle
255 * @param CallbackID ID of the callback to be registered
256 * This parameter can be one of the following values:
257 * @arg @ref HAL_WWDG_EWI_CB_ID Early WakeUp Interrupt Callback ID
258 * @arg @ref HAL_WWDG_MSPINIT_CB_ID MspInit callback ID
259 * @param pCallback pointer to the Callback function
260 * @retval status
261 */
HAL_WWDG_RegisterCallback(WWDG_HandleTypeDef * hwwdg,HAL_WWDG_CallbackIDTypeDef CallbackID,pWWDG_CallbackTypeDef pCallback)262 HAL_StatusTypeDef HAL_WWDG_RegisterCallback(WWDG_HandleTypeDef *hwwdg, HAL_WWDG_CallbackIDTypeDef CallbackID,
263 pWWDG_CallbackTypeDef pCallback)
264 {
265 HAL_StatusTypeDef status = HAL_OK;
266
267 if (pCallback == NULL)
268 {
269 status = HAL_ERROR;
270 }
271 else
272 {
273 switch (CallbackID)
274 {
275 case HAL_WWDG_EWI_CB_ID:
276 hwwdg->EwiCallback = pCallback;
277 break;
278
279 case HAL_WWDG_MSPINIT_CB_ID:
280 hwwdg->MspInitCallback = pCallback;
281 break;
282
283 default:
284 status = HAL_ERROR;
285 break;
286 }
287 }
288
289 return status;
290 }
291
292
293 /**
294 * @brief Unregister a WWDG Callback
295 * WWDG Callback is redirected to the weak (surcharged) predefined callback
296 * @param hwwdg WWDG handle
297 * @param CallbackID ID of the callback to be registered
298 * This parameter can be one of the following values:
299 * @arg @ref HAL_WWDG_EWI_CB_ID Early WakeUp Interrupt Callback ID
300 * @arg @ref HAL_WWDG_MSPINIT_CB_ID MspInit callback ID
301 * @retval status
302 */
HAL_WWDG_UnRegisterCallback(WWDG_HandleTypeDef * hwwdg,HAL_WWDG_CallbackIDTypeDef CallbackID)303 HAL_StatusTypeDef HAL_WWDG_UnRegisterCallback(WWDG_HandleTypeDef *hwwdg, HAL_WWDG_CallbackIDTypeDef CallbackID)
304 {
305 HAL_StatusTypeDef status = HAL_OK;
306
307 switch (CallbackID)
308 {
309 case HAL_WWDG_EWI_CB_ID:
310 hwwdg->EwiCallback = HAL_WWDG_EarlyWakeupCallback;
311 break;
312
313 case HAL_WWDG_MSPINIT_CB_ID:
314 hwwdg->MspInitCallback = HAL_WWDG_MspInit;
315 break;
316
317 default:
318 status = HAL_ERROR;
319 break;
320 }
321
322 return status;
323 }
324 #endif /* USE_HAL_WWDG_REGISTER_CALLBACKS */
325
326 /**
327 * @}
328 */
329
330 /** @defgroup WWDG_Exported_Functions_Group2 IO operation functions
331 * @brief IO operation functions
332 *
333 @verbatim
334 ==============================================================================
335 ##### IO operation functions #####
336 ==============================================================================
337 [..]
338 This section provides functions allowing to:
339 (+) Refresh the WWDG.
340 (+) Handle WWDG interrupt request and associated function callback.
341
342 @endverbatim
343 * @{
344 */
345
346 /**
347 * @brief Refresh the WWDG.
348 * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
349 * the configuration information for the specified WWDG module.
350 * @retval HAL status
351 */
HAL_WWDG_Refresh(WWDG_HandleTypeDef * hwwdg)352 HAL_StatusTypeDef HAL_WWDG_Refresh(WWDG_HandleTypeDef *hwwdg)
353 {
354 /* Write to WWDG CR the WWDG Counter value to refresh with */
355 WRITE_REG(hwwdg->Instance->CR, (hwwdg->Init.Counter));
356
357 /* Return function status */
358 return HAL_OK;
359 }
360
361 /**
362 * @brief Handle WWDG interrupt request.
363 * @note The Early Wakeup Interrupt (EWI) can be used if specific safety operations
364 * or data logging must be performed before the actual reset is generated.
365 * The EWI interrupt is enabled by calling HAL_WWDG_Init function with
366 * EWIMode set to WWDG_EWI_ENABLE.
367 * When the downcounter reaches the value 0x40, and EWI interrupt is
368 * generated and the corresponding Interrupt Service Routine (ISR) can
369 * be used to trigger specific actions (such as communications or data
370 * logging), before resetting the device.
371 * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
372 * the configuration information for the specified WWDG module.
373 * @retval None
374 */
HAL_WWDG_IRQHandler(WWDG_HandleTypeDef * hwwdg)375 void HAL_WWDG_IRQHandler(WWDG_HandleTypeDef *hwwdg)
376 {
377 /* Check if Early Wakeup Interrupt is enable */
378 if (__HAL_WWDG_GET_IT_SOURCE(hwwdg, WWDG_IT_EWI) != RESET)
379 {
380 /* Check if WWDG Early Wakeup Interrupt occurred */
381 if (__HAL_WWDG_GET_FLAG(hwwdg, WWDG_FLAG_EWIF) != RESET)
382 {
383 /* Clear the WWDG Early Wakeup flag */
384 __HAL_WWDG_CLEAR_FLAG(hwwdg, WWDG_FLAG_EWIF);
385
386 #if (USE_HAL_WWDG_REGISTER_CALLBACKS == 1)
387 /* Early Wakeup registered callback */
388 hwwdg->EwiCallback(hwwdg);
389 #else
390 /* Early Wakeup callback */
391 HAL_WWDG_EarlyWakeupCallback(hwwdg);
392 #endif /* USE_HAL_WWDG_REGISTER_CALLBACKS */
393 }
394 }
395 }
396
397
398 /**
399 * @brief WWDG Early Wakeup callback.
400 * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
401 * the configuration information for the specified WWDG module.
402 * @retval None
403 */
HAL_WWDG_EarlyWakeupCallback(WWDG_HandleTypeDef * hwwdg)404 __weak void HAL_WWDG_EarlyWakeupCallback(WWDG_HandleTypeDef *hwwdg)
405 {
406 /* Prevent unused argument(s) compilation warning */
407 UNUSED(hwwdg);
408
409 /* NOTE: This function should not be modified, when the callback is needed,
410 the HAL_WWDG_EarlyWakeupCallback could be implemented in the user file
411 */
412 }
413
414 /**
415 * @}
416 */
417
418 /**
419 * @}
420 */
421
422 #endif /* HAL_WWDG_MODULE_ENABLED */
423 /**
424 * @}
425 */
426
427 /**
428 * @}
429 */
430