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
11 ==============================================================================
12 ##### WWDG Specific features #####
13 ==============================================================================
15 Once enabled the WWDG generates a system reset on expiry of a programmed
16 time period, unless the program refreshes the counter (T[6;0] downcounter)
17 before reaching 0x3F value (i.e. a reset is generated when the counter
18 value rolls down from 0x40 to 0x3F).
20 (+) An MCU reset is also generated if the counter value is refreshed
21 before the counter has reached the refresh window value. This
22 implies that the counter must be refreshed in a limited window.
23 (+) Once enabled the WWDG cannot be disabled except by a system reset.
24 (+) WWDGRST flag in RCC CSR register can be used to inform when a WWDG
26 (+) The WWDG counter input clock is derived from the APB clock divided
27 by a programmable prescaler.
28 (+) WWDG clock (Hz) = PCLK1 / (4096 * Prescaler)
29 (+) WWDG timeout (mS) = 1000 * (T[5;0] + 1) / WWDG clock (Hz)
30 where T[5;0] are the lowest 6 bits of Counter.
31 (+) WWDG Counter refresh is allowed between the following limits :
32 (++) min time (mS) = 1000 * (Counter - Window) / WWDG clock
33 (++) max time (mS) = 1000 * (Counter - 0x40) / WWDG clock
34 (+) Typical values (case of STM32H74x/5x devices):
35 (++) Counter min (T[5;0] = 0x00) @100MHz (PCLK1) with zero prescaler:
36 max timeout before reset: approximately 40.96µs
37 (++) Counter max (T[5;0] = 0x3F) @100MHz (PCLK1) with prescaler dividing by 128:
38 max timeout before reset: approximately 335.54ms
39 (+) Typical values (case of STM32H7Ax/Bx devices):
40 (++) Counter min (T[5;0] = 0x00) @140MHz (PCLK1) with zero prescaler:
41 max timeout before reset: approximately 29.25µs
42 (++) Counter max (T[5;0] = 0x3F) @140MHz (PCLK1) with prescaler dividing by 128:
43 max timeout before reset: approximately 239.67ms
44 (+) Typical values (case of STM32H72x/3x devices):
45 (++) Counter min (T[5;0] = 0x00) @125MHz (PCLK1) with zero prescaler:
46 max timeout before reset: approximately 32.76µs
47 (++) Counter max (T[5;0] = 0x3F) @125MHz (PCLK1) with prescaler dividing by 128:
48 max timeout before reset: approximately 268.43ms
50 ==============================================================================
51 ##### How to use this driver #####
52 ==============================================================================
54 *** Common driver usage ***
55 ===========================
58 (+) Enable WWDG APB1 clock using __HAL_RCC_WWDG_CLK_ENABLE().
59 (+) Set the WWDG prescaler, refresh window and counter value
60 using HAL_WWDG_Init() function.
61 (+) Start the WWDG using HAL_WWDG_Start() function.
62 When the WWDG is enabled the counter value should be configured to
63 a value greater than 0x40 to prevent generating an immediate reset.
64 (+) Optionally you can enable the Early Wakeup Interrupt (EWI) which is
65 generated when the counter reaches 0x40, and then start the WWDG using
66 HAL_WWDG_Start_IT(). At EWI HAL_WWDG_WakeupCallback is executed and user can
67 add his own code by customization of callback HAL_WWDG_WakeupCallback.
68 Once enabled, EWI interrupt cannot be disabled except by a system reset.
69 (+) Then the application program must refresh the WWDG counter at regular
70 intervals during normal operation to prevent an MCU reset, using
71 HAL_WWDG_Refresh() function. This operation must occur only when
72 the counter is lower than the refresh window value already programmed.
74 *** Callback registration ***
75 =============================
78 The compilation define USE_HAL_WWDG_REGISTER_CALLBACKS when set to 1 allows
79 the user to configure dynamically the driver callbacks. Use Functions
80 @ref HAL_WWDG_RegisterCallback() to register a user callback.
82 (+) Function @ref HAL_WWDG_RegisterCallback() allows to register following
84 (++) EwiCallback : callback for Early WakeUp Interrupt.
85 (++) MspInitCallback : WWDG MspInit.
86 This function takes as parameters the HAL peripheral handle, the Callback ID
87 and a pointer to the user callback function.
89 (+) Use function @ref HAL_WWDG_UnRegisterCallback() to reset a callback to
90 the default weak (surcharged) function. @ref HAL_WWDG_UnRegisterCallback()
91 takes as parameters the HAL peripheral handle and the Callback ID.
92 This function allows to reset following callbacks:
93 (++) EwiCallback : callback for Early WakeUp Interrupt.
94 (++) MspInitCallback : WWDG MspInit.
97 When calling @ref HAL_WWDG_Init function, callbacks are reset to the
98 corresponding legacy weak (surcharged) functions:
99 @ref HAL_WWDG_EarlyWakeupCallback() and HAL_WWDG_MspInit() only if they have
100 not been registered before.
103 When compilation define USE_HAL_WWDG_REGISTER_CALLBACKS is set to 0 or
104 not defined, the callback registering feature is not available
105 and weak (surcharged) callbacks are used.
107 *** WWDG HAL driver macros list ***
108 ===================================
110 Below the list of most used macros in WWDG HAL driver.
111 (+) __HAL_WWDG_ENABLE: Enable the WWDG peripheral
112 (+) __HAL_WWDG_GET_FLAG: Get the selected WWDG's flag status
113 (+) __HAL_WWDG_CLEAR_FLAG: Clear the WWDG's pending flags
114 (+) __HAL_WWDG_ENABLE_IT: Enable the WWDG early wakeup interrupt
117 ******************************************************************************
120 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
121 * All rights reserved.</center></h2>
123 * This software component is licensed by ST under BSD 3-Clause license,
124 * the "License"; You may not use this file except in compliance with the
125 * License. You may obtain a copy of the License at:
126 * opensource.org/licenses/BSD-3-Clause
128 ******************************************************************************
131 /* Includes ------------------------------------------------------------------*/
132 #include "stm32h7xx_hal.h"
134 /** @addtogroup STM32H7xx_HAL_Driver
138 #ifdef HAL_WWDG_MODULE_ENABLED
139 /** @defgroup WWDG WWDG
140 * @brief WWDG HAL module driver.
144 /* Private typedef -----------------------------------------------------------*/
145 /* Private define ------------------------------------------------------------*/
146 /* Private macro -------------------------------------------------------------*/
147 /* Private variables ---------------------------------------------------------*/
148 /* Private function prototypes -----------------------------------------------*/
149 /* Exported functions --------------------------------------------------------*/
151 /** @defgroup WWDG_Exported_Functions WWDG Exported Functions
155 /** @defgroup WWDG_Exported_Functions_Group1 Initialization and Configuration functions
156 * @brief Initialization and Configuration functions.
159 ==============================================================================
160 ##### Initialization and Configuration functions #####
161 ==============================================================================
163 This section provides functions allowing to:
164 (+) Initialize and start the WWDG according to the specified parameters
165 in the WWDG_InitTypeDef of associated handle.
166 (+) Initialize the WWDG MSP.
173 * @brief Initialize the WWDG according to the specified.
174 * parameters in the WWDG_InitTypeDef of associated handle.
175 * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
176 * the configuration information for the specified WWDG module.
179 HAL_StatusTypeDef
HAL_WWDG_Init(WWDG_HandleTypeDef
*hwwdg
)
181 /* Check the WWDG handle allocation */
187 /* Check the parameters */
188 assert_param(IS_WWDG_ALL_INSTANCE(hwwdg
->Instance
));
189 assert_param(IS_WWDG_PRESCALER(hwwdg
->Init
.Prescaler
));
190 assert_param(IS_WWDG_WINDOW(hwwdg
->Init
.Window
));
191 assert_param(IS_WWDG_COUNTER(hwwdg
->Init
.Counter
));
192 assert_param(IS_WWDG_EWI_MODE(hwwdg
->Init
.EWIMode
));
194 #if (USE_HAL_WWDG_REGISTER_CALLBACKS == 1)
195 /* Reset Callback pointers */
196 if (hwwdg
->EwiCallback
== NULL
)
198 hwwdg
->EwiCallback
= HAL_WWDG_EarlyWakeupCallback
;
201 if (hwwdg
->MspInitCallback
== NULL
)
203 hwwdg
->MspInitCallback
= HAL_WWDG_MspInit
;
206 /* Init the low level hardware */
207 hwwdg
->MspInitCallback(hwwdg
);
209 /* Init the low level hardware */
210 HAL_WWDG_MspInit(hwwdg
);
213 /* Set WWDG Counter */
214 WRITE_REG(hwwdg
->Instance
->CR
, (WWDG_CR_WDGA
| hwwdg
->Init
.Counter
));
216 /* Set WWDG Prescaler and Window */
217 WRITE_REG(hwwdg
->Instance
->CFR
, (hwwdg
->Init
.EWIMode
| hwwdg
->Init
.Prescaler
| hwwdg
->Init
.Window
));
219 /* Return function status */
225 * @brief Initialize the WWDG MSP.
226 * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
227 * the configuration information for the specified WWDG module.
228 * @note When rewriting this function in user file, mechanism may be added
229 * to avoid multiple initialize when HAL_WWDG_Init function is called
230 * again to change parameters.
233 __weak
void HAL_WWDG_MspInit(WWDG_HandleTypeDef
*hwwdg
)
235 /* Prevent unused argument(s) compilation warning */
238 /* NOTE: This function should not be modified, when the callback is needed,
239 the HAL_WWDG_MspInit could be implemented in the user file
244 #if (USE_HAL_WWDG_REGISTER_CALLBACKS == 1)
246 * @brief Register a User WWDG Callback
247 * To be used instead of the weak (surcharged) predefined callback
248 * @param hwwdg WWDG handle
249 * @param CallbackID ID of the callback to be registered
250 * This parameter can be one of the following values:
251 * @arg @ref HAL_WWDG_EWI_CB_ID Early WakeUp Interrupt Callback ID
252 * @arg @ref HAL_WWDG_MSPINIT_CB_ID MspInit callback ID
253 * @param pCallback pointer to the Callback function
256 HAL_StatusTypeDef
HAL_WWDG_RegisterCallback(WWDG_HandleTypeDef
*hwwdg
, HAL_WWDG_CallbackIDTypeDef CallbackID
, pWWDG_CallbackTypeDef pCallback
)
258 HAL_StatusTypeDef status
= HAL_OK
;
260 if (pCallback
== NULL
)
268 case HAL_WWDG_EWI_CB_ID
:
269 hwwdg
->EwiCallback
= pCallback
;
272 case HAL_WWDG_MSPINIT_CB_ID
:
273 hwwdg
->MspInitCallback
= pCallback
;
287 * @brief Unregister a WWDG Callback
288 * WWDG Callback is redirected to the weak (surcharged) predefined callback
289 * @param hwwdg WWDG handle
290 * @param CallbackID ID of the callback to be registered
291 * This parameter can be one of the following values:
292 * @arg @ref HAL_WWDG_EWI_CB_ID Early WakeUp Interrupt Callback ID
293 * @arg @ref HAL_WWDG_MSPINIT_CB_ID MspInit callback ID
296 HAL_StatusTypeDef
HAL_WWDG_UnRegisterCallback(WWDG_HandleTypeDef
*hwwdg
, HAL_WWDG_CallbackIDTypeDef CallbackID
)
298 HAL_StatusTypeDef status
= HAL_OK
;
302 case HAL_WWDG_EWI_CB_ID
:
303 hwwdg
->EwiCallback
= HAL_WWDG_EarlyWakeupCallback
;
306 case HAL_WWDG_MSPINIT_CB_ID
:
307 hwwdg
->MspInitCallback
= HAL_WWDG_MspInit
;
323 /** @defgroup WWDG_Exported_Functions_Group2 IO operation functions
324 * @brief IO operation functions
327 ==============================================================================
328 ##### IO operation functions #####
329 ==============================================================================
331 This section provides functions allowing to:
332 (+) Refresh the WWDG.
333 (+) Handle WWDG interrupt request and associated function callback.
340 * @brief Refresh the WWDG.
341 * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
342 * the configuration information for the specified WWDG module.
345 HAL_StatusTypeDef
HAL_WWDG_Refresh(WWDG_HandleTypeDef
*hwwdg
)
347 /* Write to WWDG CR the WWDG Counter value to refresh with */
348 WRITE_REG(hwwdg
->Instance
->CR
, (hwwdg
->Init
.Counter
));
350 /* Return function status */
355 * @brief Handle WWDG interrupt request.
356 * @note The Early Wakeup Interrupt (EWI) can be used if specific safety operations
357 * or data logging must be performed before the actual reset is generated.
358 * The EWI interrupt is enabled by calling HAL_WWDG_Init function with
359 * EWIMode set to WWDG_EWI_ENABLE.
360 * When the downcounter reaches the value 0x40, and EWI interrupt is
361 * generated and the corresponding Interrupt Service Routine (ISR) can
362 * be used to trigger specific actions (such as communications or data
363 * logging), before resetting the device.
364 * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
365 * the configuration information for the specified WWDG module.
368 void HAL_WWDG_IRQHandler(WWDG_HandleTypeDef
*hwwdg
)
370 /* Check if Early Wakeup Interrupt is enable */
371 if (__HAL_WWDG_GET_IT_SOURCE(hwwdg
, WWDG_IT_EWI
) != RESET
)
373 /* Check if WWDG Early Wakeup Interrupt occurred */
374 if (__HAL_WWDG_GET_FLAG(hwwdg
, WWDG_FLAG_EWIF
) != RESET
)
376 /* Clear the WWDG Early Wakeup flag */
377 __HAL_WWDG_CLEAR_FLAG(hwwdg
, WWDG_FLAG_EWIF
);
379 #if (USE_HAL_WWDG_REGISTER_CALLBACKS == 1)
380 /* Early Wakeup registered callback */
381 hwwdg
->EwiCallback(hwwdg
);
383 /* Early Wakeup callback */
384 HAL_WWDG_EarlyWakeupCallback(hwwdg
);
392 * @brief WWDG Early Wakeup callback.
393 * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
394 * the configuration information for the specified WWDG module.
397 __weak
void HAL_WWDG_EarlyWakeupCallback(WWDG_HandleTypeDef
*hwwdg
)
399 /* Prevent unused argument(s) compilation warning */
402 /* NOTE: This function should not be modified, when the callback is needed,
403 the HAL_WWDG_EarlyWakeupCallback could be implemented in the user file
415 #endif /* HAL_WWDG_MODULE_ENABLED */
424 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/