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