2 ******************************************************************************
3 * @file stm32g4xx_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
35 (++) Counter min (T[5;0] = 0x00) @56 MHz(PCLK1) with zero prescaler:
36 max timeout before reset: ~73.14 µs
37 (++) Counter max (T[5;0] = 0x3F) @56 MHz(PCLK1) with prescaler dividing by 128:
38 max timeout before reset: ~599.18 ms
40 ==============================================================================
41 ##### How to use this driver #####
42 ==============================================================================
44 *** Common driver usage ***
45 ===========================
46 (+) Enable WWDG APB1 clock using __HAL_RCC_WWDG_CLK_ENABLE().
47 (+) Set the WWDG prescaler, refresh window and counter value
48 using HAL_WWDG_Init() function.
49 (+) Start the WWDG using HAL_WWDG_Start() function.
50 When the WWDG is enabled the counter value should be configured to
51 a value greater than 0x40 to prevent generating an immediate reset.
52 (+) Optionally you can enable the Early Wakeup Interrupt (EWI) which is
53 generated when the counter reaches 0x40, and then start the WWDG using
54 HAL_WWDG_Start_IT(). At EWI HAL_WWDG_WakeupCallback is executed and user can
55 add his own code by customization of callback HAL_WWDG_WakeupCallback.
56 Once enabled, EWI interrupt cannot be disabled except by a system reset.
57 (+) Then the application program must refresh the WWDG counter at regular
58 intervals during normal operation to prevent an MCU reset, using
59 HAL_WWDG_Refresh() function. This operation must occur only when
60 the counter is lower than the refresh window value already programmed.
63 *** Callback registration ***
64 =============================
65 The compilation define USE_HAL_WWDG_REGISTER_CALLBACKS when set to 1 allows
66 the user to configure dynamically the driver callbacks. Use Functions
67 @ref HAL_WWDG_RegisterCallback() to register a user callback.
69 (+) Function @ref HAL_WWDG_RegisterCallback() allows to register following
71 (++) EwiCallback : callback for Early WakeUp Interrupt.
72 (++) MspInitCallback : WWDG MspInit.
73 This function takes as parameters the HAL peripheral handle, the Callback ID
74 and a pointer to the user callback function.
76 (+) Use function @ref HAL_WWDG_UnRegisterCallback() to reset a callback to
77 the default weak (surcharged) function. @ref HAL_WWDG_UnRegisterCallback()
78 takes as parameters the HAL peripheral handle and the Callback ID.
79 This function allows to reset following callbacks:
80 (++) EwiCallback : callback for Early WakeUp Interrupt.
81 (++) MspInitCallback : WWDG MspInit.
83 When calling @ref HAL_WWDG_Init function, callbacks are reset to the
84 corresponding legacy weak (surcharged) functions:
85 @ref HAL_WWDG_EarlyWakeupCallback() and HAL_WWDG_MspInit() only if they have
86 not been registered before.
88 When compilation define USE_HAL_WWDG_REGISTER_CALLBACKS is set to 0 or
89 not defined, the callback registering feature is not available
90 and weak (surcharged) callbacks are used.
92 *** WWDG HAL driver macros list ***
93 ===================================
95 Below the list of most used macros in WWDG HAL driver.
96 (+) __HAL_WWDG_ENABLE: Enable the WWDG peripheral
97 (+) __HAL_WWDG_GET_FLAG: Get the selected WWDG's flag status
98 (+) __HAL_WWDG_CLEAR_FLAG: Clear the WWDG's pending flags
99 (+) __HAL_WWDG_ENABLE_IT: Enable the WWDG early wakeup interrupt
102 ******************************************************************************
105 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
106 * All rights reserved.</center></h2>
108 * This software component is licensed by ST under BSD 3-Clause license,
109 * the "License"; You may not use this file except in compliance with the
110 * License. You may obtain a copy of the License at:
111 * opensource.org/licenses/BSD-3-Clause
113 ******************************************************************************
116 /* Includes ------------------------------------------------------------------*/
117 #include "stm32g4xx_hal.h"
119 /** @addtogroup STM32G4xx_HAL_Driver
123 #ifdef HAL_WWDG_MODULE_ENABLED
124 /** @defgroup WWDG WWDG
125 * @brief WWDG HAL module driver.
129 /* Private typedef -----------------------------------------------------------*/
130 /* Private define ------------------------------------------------------------*/
131 /* Private macro -------------------------------------------------------------*/
132 /* Private variables ---------------------------------------------------------*/
133 /* Private function prototypes -----------------------------------------------*/
134 /* Exported functions --------------------------------------------------------*/
136 /** @defgroup WWDG_Exported_Functions WWDG Exported Functions
140 /** @defgroup WWDG_Exported_Functions_Group1 Initialization and Configuration functions
141 * @brief Initialization and Configuration functions.
144 ==============================================================================
145 ##### Initialization and Configuration functions #####
146 ==============================================================================
148 This section provides functions allowing to:
149 (+) Initialize and start the WWDG according to the specified parameters
150 in the WWDG_InitTypeDef of associated handle.
151 (+) Initialize the WWDG MSP.
158 * @brief Initialize the WWDG according to the specified.
159 * parameters in the WWDG_InitTypeDef of associated handle.
160 * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
161 * the configuration information for the specified WWDG module.
164 HAL_StatusTypeDef
HAL_WWDG_Init(WWDG_HandleTypeDef
*hwwdg
)
166 /* Check the WWDG handle allocation */
172 /* Check the parameters */
173 assert_param(IS_WWDG_ALL_INSTANCE(hwwdg
->Instance
));
174 assert_param(IS_WWDG_PRESCALER(hwwdg
->Init
.Prescaler
));
175 assert_param(IS_WWDG_WINDOW(hwwdg
->Init
.Window
));
176 assert_param(IS_WWDG_COUNTER(hwwdg
->Init
.Counter
));
177 assert_param(IS_WWDG_EWI_MODE(hwwdg
->Init
.EWIMode
));
179 #if (USE_HAL_WWDG_REGISTER_CALLBACKS == 1)
180 /* Reset Callback pointers */
181 if(hwwdg
->EwiCallback
== NULL
)
183 hwwdg
->EwiCallback
= HAL_WWDG_EarlyWakeupCallback
;
186 if(hwwdg
->MspInitCallback
== NULL
)
188 hwwdg
->MspInitCallback
= HAL_WWDG_MspInit
;
191 /* Init the low level hardware */
192 hwwdg
->MspInitCallback(hwwdg
);
194 /* Init the low level hardware */
195 HAL_WWDG_MspInit(hwwdg
);
198 /* Set WWDG Counter */
199 WRITE_REG(hwwdg
->Instance
->CR
, (WWDG_CR_WDGA
| hwwdg
->Init
.Counter
));
201 /* Set WWDG Prescaler and Window */
202 WRITE_REG(hwwdg
->Instance
->CFR
, (hwwdg
->Init
.EWIMode
| hwwdg
->Init
.Prescaler
| hwwdg
->Init
.Window
));
204 /* Return function status */
210 * @brief Initialize the WWDG MSP.
211 * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
212 * the configuration information for the specified WWDG module.
213 * @note When rewriting this function in user file, mechanism may be added
214 * to avoid multiple initialize when HAL_WWDG_Init function is called
215 * again to change parameters.
218 __weak
void HAL_WWDG_MspInit(WWDG_HandleTypeDef
*hwwdg
)
220 /* Prevent unused argument(s) compilation warning */
223 /* NOTE: This function should not be modified, when the callback is needed,
224 the HAL_WWDG_MspInit could be implemented in the user file
229 #if (USE_HAL_WWDG_REGISTER_CALLBACKS == 1)
231 * @brief Register a User WWDG Callback
232 * To be used instead of the weak (surcharged) predefined callback
233 * @param hwwdg WWDG handle
234 * @param CallbackID ID of the callback to be registered
235 * This parameter can be one of the following values:
236 * @arg @ref HAL_WWDG_EWI_CB_ID Early WakeUp Interrupt Callback ID
237 * @arg @ref HAL_WWDG_MSPINIT_CB_ID MspInit callback ID
238 * @param pCallback pointer to the Callback function
241 HAL_StatusTypeDef
HAL_WWDG_RegisterCallback(WWDG_HandleTypeDef
*hwwdg
, HAL_WWDG_CallbackIDTypeDef CallbackID
, pWWDG_CallbackTypeDef pCallback
)
243 HAL_StatusTypeDef status
= HAL_OK
;
245 if(pCallback
== NULL
)
253 case HAL_WWDG_EWI_CB_ID
:
254 hwwdg
->EwiCallback
= pCallback
;
257 case HAL_WWDG_MSPINIT_CB_ID
:
258 hwwdg
->MspInitCallback
= pCallback
;
272 * @brief Unregister a WWDG Callback
273 * WWDG Callback is redirected to the weak (surcharged) predefined callback
274 * @param hwwdg WWDG handle
275 * @param CallbackID ID of the callback to be registered
276 * This parameter can be one of the following values:
277 * @arg @ref HAL_WWDG_EWI_CB_ID Early WakeUp Interrupt Callback ID
278 * @arg @ref HAL_WWDG_MSPINIT_CB_ID MspInit callback ID
281 HAL_StatusTypeDef
HAL_WWDG_UnRegisterCallback(WWDG_HandleTypeDef
*hwwdg
, HAL_WWDG_CallbackIDTypeDef CallbackID
)
283 HAL_StatusTypeDef status
= HAL_OK
;
287 case HAL_WWDG_EWI_CB_ID
:
288 hwwdg
->EwiCallback
= HAL_WWDG_EarlyWakeupCallback
;
291 case HAL_WWDG_MSPINIT_CB_ID
:
292 hwwdg
->MspInitCallback
= HAL_WWDG_MspInit
;
308 /** @defgroup WWDG_Exported_Functions_Group2 IO operation functions
309 * @brief IO operation functions
312 ==============================================================================
313 ##### IO operation functions #####
314 ==============================================================================
316 This section provides functions allowing to:
317 (+) Refresh the WWDG.
318 (+) Handle WWDG interrupt request and associated function callback.
325 * @brief Refresh the WWDG.
326 * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
327 * the configuration information for the specified WWDG module.
330 HAL_StatusTypeDef
HAL_WWDG_Refresh(WWDG_HandleTypeDef
*hwwdg
)
332 /* Write to WWDG CR the WWDG Counter value to refresh with */
333 WRITE_REG(hwwdg
->Instance
->CR
, (hwwdg
->Init
.Counter
));
335 /* Return function status */
340 * @brief Handle WWDG interrupt request.
341 * @note The Early Wakeup Interrupt (EWI) can be used if specific safety operations
342 * or data logging must be performed before the actual reset is generated.
343 * The EWI interrupt is enabled by calling HAL_WWDG_Init function with
344 * EWIMode set to WWDG_EWI_ENABLE.
345 * When the downcounter reaches the value 0x40, and EWI interrupt is
346 * generated and the corresponding Interrupt Service Routine (ISR) can
347 * be used to trigger specific actions (such as communications or data
348 * logging), before resetting the device.
349 * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
350 * the configuration information for the specified WWDG module.
353 void HAL_WWDG_IRQHandler(WWDG_HandleTypeDef
*hwwdg
)
355 /* Check if Early Wakeup Interrupt is enable */
356 if (__HAL_WWDG_GET_IT_SOURCE(hwwdg
, WWDG_IT_EWI
) != RESET
)
358 /* Check if WWDG Early Wakeup Interrupt occurred */
359 if (__HAL_WWDG_GET_FLAG(hwwdg
, WWDG_FLAG_EWIF
) != RESET
)
361 /* Clear the WWDG Early Wakeup flag */
362 __HAL_WWDG_CLEAR_FLAG(hwwdg
, WWDG_FLAG_EWIF
);
364 #if (USE_HAL_WWDG_REGISTER_CALLBACKS == 1)
365 /* Early Wakeup registered callback */
366 hwwdg
->EwiCallback(hwwdg
);
368 /* Early Wakeup callback */
369 HAL_WWDG_EarlyWakeupCallback(hwwdg
);
377 * @brief WWDG Early Wakeup callback.
378 * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
379 * the configuration information for the specified WWDG module.
382 __weak
void HAL_WWDG_EarlyWakeupCallback(WWDG_HandleTypeDef
*hwwdg
)
384 /* Prevent unused argument(s) compilation warning */
387 /* NOTE: This function should not be modified, when the callback is needed,
388 the HAL_WWDG_EarlyWakeupCallback could be implemented in the user file
400 #endif /* HAL_WWDG_MODULE_ENABLED */
409 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/