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.96µs
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.25µs
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.76µs
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 /* Includes ------------------------------------------------------------------*/
137 #include "stm32h7xx_hal.h"
138
139 /** @addtogroup STM32H7xx_HAL_Driver
140 * @{
141 */
142
143 #ifdef HAL_WWDG_MODULE_ENABLED
144 /** @defgroup WWDG WWDG
145 * @brief WWDG HAL module driver.
146 * @{
147 */
148
149 /* Private typedef -----------------------------------------------------------*/
150 /* Private define ------------------------------------------------------------*/
151 /* Private macro -------------------------------------------------------------*/
152 /* Private variables ---------------------------------------------------------*/
153 /* Private function prototypes -----------------------------------------------*/
154 /* Exported functions --------------------------------------------------------*/
155
156 /** @defgroup WWDG_Exported_Functions WWDG Exported Functions
157 * @{
158 */
159
160 /** @defgroup WWDG_Exported_Functions_Group1 Initialization and Configuration functions
161 * @brief Initialization and Configuration functions.
162 *
163 @verbatim
164 ==============================================================================
165 ##### Initialization and Configuration functions #####
166 ==============================================================================
167 [..]
168 This section provides functions allowing to:
169 (+) Initialize and start the WWDG according to the specified parameters
170 in the WWDG_InitTypeDef of associated handle.
171 (+) Initialize the WWDG MSP.
172
173 @endverbatim
174 * @{
175 */
176
177 /**
178 * @brief Initialize the WWDG according to the specified.
179 * parameters in the WWDG_InitTypeDef of associated handle.
180 * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
181 * the configuration information for the specified WWDG module.
182 * @retval HAL status
183 */
HAL_WWDG_Init(WWDG_HandleTypeDef * hwwdg)184 HAL_StatusTypeDef HAL_WWDG_Init(WWDG_HandleTypeDef *hwwdg)
185 {
186 /* Check the WWDG handle allocation */
187 if (hwwdg == NULL)
188 {
189 return HAL_ERROR;
190 }
191
192 /* Check the parameters */
193 assert_param(IS_WWDG_ALL_INSTANCE(hwwdg->Instance));
194 assert_param(IS_WWDG_PRESCALER(hwwdg->Init.Prescaler));
195 assert_param(IS_WWDG_WINDOW(hwwdg->Init.Window));
196 assert_param(IS_WWDG_COUNTER(hwwdg->Init.Counter));
197 assert_param(IS_WWDG_EWI_MODE(hwwdg->Init.EWIMode));
198
199 #if (USE_HAL_WWDG_REGISTER_CALLBACKS == 1)
200 /* Reset Callback pointers */
201 if (hwwdg->EwiCallback == NULL)
202 {
203 hwwdg->EwiCallback = HAL_WWDG_EarlyWakeupCallback;
204 }
205
206 if (hwwdg->MspInitCallback == NULL)
207 {
208 hwwdg->MspInitCallback = HAL_WWDG_MspInit;
209 }
210
211 /* Init the low level hardware */
212 hwwdg->MspInitCallback(hwwdg);
213 #else
214 /* Init the low level hardware */
215 HAL_WWDG_MspInit(hwwdg);
216 #endif /* USE_HAL_WWDG_REGISTER_CALLBACKS */
217
218 /* Set WWDG Counter */
219 WRITE_REG(hwwdg->Instance->CR, (WWDG_CR_WDGA | hwwdg->Init.Counter));
220
221 /* Set WWDG Prescaler and Window */
222 WRITE_REG(hwwdg->Instance->CFR, (hwwdg->Init.EWIMode | hwwdg->Init.Prescaler | hwwdg->Init.Window));
223
224 /* Return function status */
225 return HAL_OK;
226 }
227
228
229 /**
230 * @brief Initialize the WWDG MSP.
231 * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
232 * the configuration information for the specified WWDG module.
233 * @note When rewriting this function in user file, mechanism may be added
234 * to avoid multiple initialize when HAL_WWDG_Init function is called
235 * again to change parameters.
236 * @retval None
237 */
HAL_WWDG_MspInit(WWDG_HandleTypeDef * hwwdg)238 __weak void HAL_WWDG_MspInit(WWDG_HandleTypeDef *hwwdg)
239 {
240 /* Prevent unused argument(s) compilation warning */
241 UNUSED(hwwdg);
242
243 /* NOTE: This function should not be modified, when the callback is needed,
244 the HAL_WWDG_MspInit could be implemented in the user file
245 */
246 }
247
248
249 #if (USE_HAL_WWDG_REGISTER_CALLBACKS == 1)
250 /**
251 * @brief Register a User WWDG Callback
252 * To be used instead of the weak (surcharged) predefined callback
253 * @param hwwdg WWDG handle
254 * @param CallbackID ID of the callback to be registered
255 * This parameter can be one of the following values:
256 * @arg @ref HAL_WWDG_EWI_CB_ID Early WakeUp Interrupt Callback ID
257 * @arg @ref HAL_WWDG_MSPINIT_CB_ID MspInit callback ID
258 * @param pCallback pointer to the Callback function
259 * @retval status
260 */
HAL_WWDG_RegisterCallback(WWDG_HandleTypeDef * hwwdg,HAL_WWDG_CallbackIDTypeDef CallbackID,pWWDG_CallbackTypeDef pCallback)261 HAL_StatusTypeDef HAL_WWDG_RegisterCallback(WWDG_HandleTypeDef *hwwdg, HAL_WWDG_CallbackIDTypeDef CallbackID,
262 pWWDG_CallbackTypeDef pCallback)
263 {
264 HAL_StatusTypeDef status = HAL_OK;
265
266 if (pCallback == NULL)
267 {
268 status = HAL_ERROR;
269 }
270 else
271 {
272 switch (CallbackID)
273 {
274 case HAL_WWDG_EWI_CB_ID:
275 hwwdg->EwiCallback = pCallback;
276 break;
277
278 case HAL_WWDG_MSPINIT_CB_ID:
279 hwwdg->MspInitCallback = pCallback;
280 break;
281
282 default:
283 status = HAL_ERROR;
284 break;
285 }
286 }
287
288 return status;
289 }
290
291
292 /**
293 * @brief Unregister a WWDG Callback
294 * WWDG Callback is redirected to the weak (surcharged) predefined callback
295 * @param hwwdg WWDG handle
296 * @param CallbackID ID of the callback to be registered
297 * This parameter can be one of the following values:
298 * @arg @ref HAL_WWDG_EWI_CB_ID Early WakeUp Interrupt Callback ID
299 * @arg @ref HAL_WWDG_MSPINIT_CB_ID MspInit callback ID
300 * @retval status
301 */
HAL_WWDG_UnRegisterCallback(WWDG_HandleTypeDef * hwwdg,HAL_WWDG_CallbackIDTypeDef CallbackID)302 HAL_StatusTypeDef HAL_WWDG_UnRegisterCallback(WWDG_HandleTypeDef *hwwdg, HAL_WWDG_CallbackIDTypeDef CallbackID)
303 {
304 HAL_StatusTypeDef status = HAL_OK;
305
306 switch (CallbackID)
307 {
308 case HAL_WWDG_EWI_CB_ID:
309 hwwdg->EwiCallback = HAL_WWDG_EarlyWakeupCallback;
310 break;
311
312 case HAL_WWDG_MSPINIT_CB_ID:
313 hwwdg->MspInitCallback = HAL_WWDG_MspInit;
314 break;
315
316 default:
317 status = HAL_ERROR;
318 break;
319 }
320
321 return status;
322 }
323 #endif /* USE_HAL_WWDG_REGISTER_CALLBACKS */
324
325 /**
326 * @}
327 */
328
329 /** @defgroup WWDG_Exported_Functions_Group2 IO operation functions
330 * @brief IO operation functions
331 *
332 @verbatim
333 ==============================================================================
334 ##### IO operation functions #####
335 ==============================================================================
336 [..]
337 This section provides functions allowing to:
338 (+) Refresh the WWDG.
339 (+) Handle WWDG interrupt request and associated function callback.
340
341 @endverbatim
342 * @{
343 */
344
345 /**
346 * @brief Refresh the WWDG.
347 * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
348 * the configuration information for the specified WWDG module.
349 * @retval HAL status
350 */
HAL_WWDG_Refresh(WWDG_HandleTypeDef * hwwdg)351 HAL_StatusTypeDef HAL_WWDG_Refresh(WWDG_HandleTypeDef *hwwdg)
352 {
353 /* Write to WWDG CR the WWDG Counter value to refresh with */
354 WRITE_REG(hwwdg->Instance->CR, (hwwdg->Init.Counter));
355
356 /* Return function status */
357 return HAL_OK;
358 }
359
360 /**
361 * @brief Handle WWDG interrupt request.
362 * @note The Early Wakeup Interrupt (EWI) can be used if specific safety operations
363 * or data logging must be performed before the actual reset is generated.
364 * The EWI interrupt is enabled by calling HAL_WWDG_Init function with
365 * EWIMode set to WWDG_EWI_ENABLE.
366 * When the downcounter reaches the value 0x40, and EWI interrupt is
367 * generated and the corresponding Interrupt Service Routine (ISR) can
368 * be used to trigger specific actions (such as communications or data
369 * logging), before resetting the device.
370 * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
371 * the configuration information for the specified WWDG module.
372 * @retval None
373 */
HAL_WWDG_IRQHandler(WWDG_HandleTypeDef * hwwdg)374 void HAL_WWDG_IRQHandler(WWDG_HandleTypeDef *hwwdg)
375 {
376 /* Check if Early Wakeup Interrupt is enable */
377 if (__HAL_WWDG_GET_IT_SOURCE(hwwdg, WWDG_IT_EWI) != RESET)
378 {
379 /* Check if WWDG Early Wakeup Interrupt occurred */
380 if (__HAL_WWDG_GET_FLAG(hwwdg, WWDG_FLAG_EWIF) != RESET)
381 {
382 /* Clear the WWDG Early Wakeup flag */
383 __HAL_WWDG_CLEAR_FLAG(hwwdg, WWDG_FLAG_EWIF);
384
385 #if (USE_HAL_WWDG_REGISTER_CALLBACKS == 1)
386 /* Early Wakeup registered callback */
387 hwwdg->EwiCallback(hwwdg);
388 #else
389 /* Early Wakeup callback */
390 HAL_WWDG_EarlyWakeupCallback(hwwdg);
391 #endif /* USE_HAL_WWDG_REGISTER_CALLBACKS */
392 }
393 }
394 }
395
396
397 /**
398 * @brief WWDG Early Wakeup callback.
399 * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
400 * the configuration information for the specified WWDG module.
401 * @retval None
402 */
HAL_WWDG_EarlyWakeupCallback(WWDG_HandleTypeDef * hwwdg)403 __weak void HAL_WWDG_EarlyWakeupCallback(WWDG_HandleTypeDef *hwwdg)
404 {
405 /* Prevent unused argument(s) compilation warning */
406 UNUSED(hwwdg);
407
408 /* NOTE: This function should not be modified, when the callback is needed,
409 the HAL_WWDG_EarlyWakeupCallback could be implemented in the user file
410 */
411 }
412
413 /**
414 * @}
415 */
416
417 /**
418 * @}
419 */
420
421 #endif /* HAL_WWDG_MODULE_ENABLED */
422 /**
423 * @}
424 */
425
426 /**
427 * @}
428 */
429