1 /**
2 ******************************************************************************
3 * @file stm32f1xx_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 de-initialization functions
9 * + IO operation functions
10 * + Peripheral State functions
11 @verbatim
12 ==============================================================================
13 ##### WWDG specific features #####
14 ==============================================================================
15 [..]
16 Once enabled the WWDG generates a system reset on expiry of a programmed
17 time period, unless the program refreshes the counter (downcounter)
18 before reaching 0x3F value (i.e. a reset is generated when the counter
19 value rolls over from 0x40 to 0x3F).
20
21 (+) An MCU reset is also generated if the counter value is refreshed
22 before the counter has reached the refresh window value. This
23 implies that the counter must be refreshed in a limited window.
24 (+) Once enabled the WWDG cannot be disabled except by a system reset.
25 (+) WWDGRST flag in RCC_CSR register can be used to inform when a WWDG
26 reset occurs.
27 (+) The WWDG counter input clock is derived from the APB clock divided
28 by a programmable prescaler.
29 (+) WWDG clock (Hz) = PCLK1 / (4096 * Prescaler)
30 (+) WWDG timeout (mS) = 1000 * Counter / WWDG clock
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
35 (+) Min-max timeout value at 36 MHz(PCLK1): 910 us / 58.25 ms
36
37 (+) The Early Wakeup Interrupt (EWI) can be used if specific safety
38 operations or data logging must be performed before the actual reset is
39 generated. When the downcounter reaches the value 0x40, an EWI interrupt
40 is generated and the corresponding interrupt service routine (ISR) can
41 be used to trigger specific actions (such as communications or data
42 logging), before resetting the device.
43 In some applications, the EWI interrupt can be used to manage a software
44 system check and/or system recovery/graceful degradation, without
45 generating a WWDG reset. In this case, the corresponding interrupt
46 service routine (ISR) should reload the WWDG counter to avoid the WWDG
47 reset, then trigger the required actions.
48 Note:When the EWI interrupt cannot be served, e.g. due to a system lock
49 in a higher priority task, the WWDG reset will eventually be generated.
50
51 (+) Debug mode : When the microcontroller enters debug mode (core halted),
52 the WWDG counter either continues to work normally or stops, depending
53 on DBG_WWDG_STOP configuration bit in DBG module, accessible through
54 __HAL_DBGMCU_FREEZE_WWDG() and __HAL_DBGMCU_UNFREEZE_WWDG() macros
55
56 ##### How to use this driver #####
57 ==============================================================================
58 [..]
59 (+) Enable WWDG APB1 clock using __HAL_RCC_WWDG_CLK_ENABLE().
60
61 (+) Set the WWDG prescaler, refresh window, counter value and Early Wakeup
62 Interrupt mode using using HAL_WWDG_Init() function.
63 This enables WWDG peripheral and the downcounter starts downcounting
64 from given counter value.
65 Init function can be called again to modify all watchdog parameters,
66 however if EWI mode has been set once, it can't be clear until next
67 reset.
68
69 (+) 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 window value already programmed.
73
74 (+) if Early Wakeup Interrupt mode is enable an interrupt is generated when
75 the counter reaches 0x40. User can add his own code in weak function
76 HAL_WWDG_EarlyWakeupCallback().
77
78 *** WWDG HAL driver macros list ***
79 ==================================
80 [..]
81 Below the list of most used macros in WWDG HAL driver.
82
83 (+) __HAL_WWDG_GET_IT_SOURCE: Check the selected WWDG's interrupt source.
84 (+) __HAL_WWDG_GET_FLAG: Get the selected WWDG's flag status.
85 (+) __HAL_WWDG_CLEAR_FLAG: Clear the WWDG's pending flags.
86
87 @endverbatim
88 ******************************************************************************
89 * @attention
90 *
91 * <h2><center>© Copyright (c) 2016 STMicroelectronics.
92 * All rights reserved.</center></h2>
93 *
94 * This software component is licensed by ST under BSD 3-Clause license,
95 * the "License"; You may not use this file except in compliance with the
96 * License. You may obtain a copy of the License at:
97 * opensource.org/licenses/BSD-3-Clause
98 *
99 ******************************************************************************
100 */
101
102 /* Includes ------------------------------------------------------------------*/
103 #include "stm32f1xx_hal.h"
104
105 /** @addtogroup STM32F1xx_HAL_Driver
106 * @{
107 */
108
109 #ifdef HAL_WWDG_MODULE_ENABLED
110 /** @defgroup WWDG WWDG
111 * @brief WWDG HAL module driver.
112 * @{
113 */
114
115 /* Private typedef -----------------------------------------------------------*/
116 /* Private define ------------------------------------------------------------*/
117 /* Private macro -------------------------------------------------------------*/
118 /* Private variables ---------------------------------------------------------*/
119 /* Private function prototypes -----------------------------------------------*/
120 /* Exported functions --------------------------------------------------------*/
121
122 /** @defgroup WWDG_Exported_Functions WWDG Exported Functions
123 * @{
124 */
125
126 /** @defgroup WWDG_Exported_Functions_Group1 Initialization and Configuration functions
127 * @brief Initialization and Configuration functions.
128 *
129 @verbatim
130 ==============================================================================
131 ##### Initialization and Configuration functions #####
132 ==============================================================================
133 [..]
134 This section provides functions allowing to:
135 (+) Initialize and start the WWDG according to the specified parameters
136 in the WWDG_InitTypeDef of associated handle.
137 (+) Initialize the WWDG MSP.
138
139 @endverbatim
140 * @{
141 */
142
143 /**
144 * @brief Initialize the WWDG according to the specified.
145 * parameters in the WWDG_InitTypeDef of associated handle.
146 * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
147 * the configuration information for the specified WWDG module.
148 * @retval HAL status
149 */
HAL_WWDG_Init(WWDG_HandleTypeDef * hwwdg)150 HAL_StatusTypeDef HAL_WWDG_Init(WWDG_HandleTypeDef *hwwdg)
151 {
152 /* Check the WWDG handle allocation */
153 if (hwwdg == NULL)
154 {
155 return HAL_ERROR;
156 }
157
158 /* Check the parameters */
159 assert_param(IS_WWDG_ALL_INSTANCE(hwwdg->Instance));
160 assert_param(IS_WWDG_PRESCALER(hwwdg->Init.Prescaler));
161 assert_param(IS_WWDG_WINDOW(hwwdg->Init.Window));
162 assert_param(IS_WWDG_COUNTER(hwwdg->Init.Counter));
163 assert_param(IS_WWDG_EWI_MODE(hwwdg->Init.EWIMode));
164
165 #if (USE_HAL_WWDG_REGISTER_CALLBACKS == 1)
166 /* Reset Callback pointers */
167 if(hwwdg->EwiCallback == NULL)
168 {
169 hwwdg->EwiCallback = HAL_WWDG_EarlyWakeupCallback;
170 }
171
172 if(hwwdg->MspInitCallback == NULL)
173 {
174 hwwdg->MspInitCallback = HAL_WWDG_MspInit;
175 }
176
177 /* Init the low level hardware */
178 hwwdg->MspInitCallback(hwwdg);
179 #else
180 /* Init the low level hardware */
181 HAL_WWDG_MspInit(hwwdg);
182 #endif
183
184 /* Set WWDG Counter */
185 WRITE_REG(hwwdg->Instance->CR, (WWDG_CR_WDGA | hwwdg->Init.Counter));
186
187 /* Set WWDG Prescaler and Window */
188 WRITE_REG(hwwdg->Instance->CFR, (hwwdg->Init.EWIMode | hwwdg->Init.Prescaler | hwwdg->Init.Window));
189
190 /* Return function status */
191 return HAL_OK;
192 }
193
194 /**
195 * @brief Initialize the WWDG MSP.
196 * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
197 * the configuration information for the specified WWDG module.
198 * @note When rewriting this function in user file, mechanism may be added
199 * to avoid multiple initialize when HAL_WWDG_Init function is called
200 * again to change parameters.
201 * @retval None
202 */
HAL_WWDG_MspInit(WWDG_HandleTypeDef * hwwdg)203 __weak void HAL_WWDG_MspInit(WWDG_HandleTypeDef *hwwdg)
204 {
205 /* Prevent unused argument(s) compilation warning */
206 UNUSED(hwwdg);
207
208 /* NOTE: This function should not be modified, when the callback is needed,
209 the HAL_WWDG_MspInit could be implemented in the user file
210 */
211 }
212
213
214 #if (USE_HAL_WWDG_REGISTER_CALLBACKS == 1)
215 /**
216 * @brief Register a User WWDG Callback
217 * To be used instead of the weak (surcharged) predefined callback
218 * @param hwwdg WWDG handle
219 * @param CallbackID ID of the callback to be registered
220 * This parameter can be one of the following values:
221 * @arg @ref HAL_WWDG_EWI_CB_ID Early WakeUp Interrupt Callback ID
222 * @arg @ref HAL_WWDG_MSPINIT_CB_ID MspInit callback ID
223 * @param pCallback pointer to the Callback function
224 * @retval status
225 */
HAL_WWDG_RegisterCallback(WWDG_HandleTypeDef * hwwdg,HAL_WWDG_CallbackIDTypeDef CallbackID,pWWDG_CallbackTypeDef pCallback)226 HAL_StatusTypeDef HAL_WWDG_RegisterCallback(WWDG_HandleTypeDef *hwwdg, HAL_WWDG_CallbackIDTypeDef CallbackID, pWWDG_CallbackTypeDef pCallback)
227 {
228 HAL_StatusTypeDef status = HAL_OK;
229
230 if(pCallback == NULL)
231 {
232 status = HAL_ERROR;
233 }
234 else
235 {
236 switch(CallbackID)
237 {
238 case HAL_WWDG_EWI_CB_ID:
239 hwwdg->EwiCallback = pCallback;
240 break;
241
242 case HAL_WWDG_MSPINIT_CB_ID:
243 hwwdg->MspInitCallback = pCallback;
244 break;
245
246 default:
247 status = HAL_ERROR;
248 break;
249 }
250 }
251
252 return status;
253 }
254
255
256 /**
257 * @brief Unregister a WWDG Callback
258 * WWDG Callback is redirected to the weak (surcharged) predefined callback
259 * @param hwwdg WWDG handle
260 * @param CallbackID ID of the callback to be registered
261 * This parameter can be one of the following values:
262 * @arg @ref HAL_WWDG_EWI_CB_ID Early WakeUp Interrupt Callback ID
263 * @arg @ref HAL_WWDG_MSPINIT_CB_ID MspInit callback ID
264 * @retval status
265 */
HAL_WWDG_UnRegisterCallback(WWDG_HandleTypeDef * hwwdg,HAL_WWDG_CallbackIDTypeDef CallbackID)266 HAL_StatusTypeDef HAL_WWDG_UnRegisterCallback(WWDG_HandleTypeDef *hwwdg, HAL_WWDG_CallbackIDTypeDef CallbackID)
267 {
268 HAL_StatusTypeDef status = HAL_OK;
269
270 switch(CallbackID)
271 {
272 case HAL_WWDG_EWI_CB_ID:
273 hwwdg->EwiCallback = HAL_WWDG_EarlyWakeupCallback;
274 break;
275
276 case HAL_WWDG_MSPINIT_CB_ID:
277 hwwdg->MspInitCallback = HAL_WWDG_MspInit;
278 break;
279
280 default:
281 status = HAL_ERROR;
282 break;
283 }
284
285 return status;
286 }
287 #endif
288
289 /**
290 * @}
291 */
292
293 /** @defgroup WWDG_Exported_Functions_Group2 IO operation functions
294 * @brief IO operation functions
295 *
296 @verbatim
297 ==============================================================================
298 ##### IO operation functions #####
299 ==============================================================================
300 [..]
301 This section provides functions allowing to:
302 (+) Refresh the WWDG.
303 (+) Handle WWDG interrupt request and associated function callback.
304
305 @endverbatim
306 * @{
307 */
308
309 /**
310 * @brief Refresh the WWDG.
311 * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
312 * the configuration information for the specified WWDG module.
313 * @retval HAL status
314 */
HAL_WWDG_Refresh(WWDG_HandleTypeDef * hwwdg)315 HAL_StatusTypeDef HAL_WWDG_Refresh(WWDG_HandleTypeDef *hwwdg)
316 {
317 /* Write to WWDG CR the WWDG Counter value to refresh with */
318 WRITE_REG(hwwdg->Instance->CR, (hwwdg->Init.Counter));
319
320 /* Return function status */
321 return HAL_OK;
322 }
323
324 /**
325 * @brief Handle WWDG interrupt request.
326 * @note The Early Wakeup Interrupt (EWI) can be used if specific safety operations
327 * or data logging must be performed before the actual reset is generated.
328 * The EWI interrupt is enabled by calling HAL_WWDG_Init function with
329 * EWIMode set to WWDG_EWI_ENABLE.
330 * When the downcounter reaches the value 0x40, and EWI interrupt is
331 * generated and the corresponding Interrupt Service Routine (ISR) can
332 * be used to trigger specific actions (such as communications or data
333 * logging), before resetting the device.
334 * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
335 * the configuration information for the specified WWDG module.
336 * @retval None
337 */
HAL_WWDG_IRQHandler(WWDG_HandleTypeDef * hwwdg)338 void HAL_WWDG_IRQHandler(WWDG_HandleTypeDef *hwwdg)
339 {
340 /* Check if Early Wakeup Interrupt is enable */
341 if (__HAL_WWDG_GET_IT_SOURCE(hwwdg, WWDG_IT_EWI) != RESET)
342 {
343 /* Check if WWDG Early Wakeup Interrupt occurred */
344 if (__HAL_WWDG_GET_FLAG(hwwdg, WWDG_FLAG_EWIF) != RESET)
345 {
346 /* Clear the WWDG Early Wakeup flag */
347 __HAL_WWDG_CLEAR_FLAG(hwwdg, WWDG_FLAG_EWIF);
348
349 #if (USE_HAL_WWDG_REGISTER_CALLBACKS == 1)
350 /* Early Wakeup registered callback */
351 hwwdg->EwiCallback(hwwdg);
352 #else
353 /* Early Wakeup callback */
354 HAL_WWDG_EarlyWakeupCallback(hwwdg);
355 #endif
356 }
357 }
358 }
359
360 /**
361 * @brief WWDG Early Wakeup callback.
362 * @param hwwdg : pointer to a WWDG_HandleTypeDef structure that contains
363 * the configuration information for the specified WWDG module.
364 * @retval None
365 */
HAL_WWDG_EarlyWakeupCallback(WWDG_HandleTypeDef * hwwdg)366 __weak void HAL_WWDG_EarlyWakeupCallback(WWDG_HandleTypeDef *hwwdg)
367 {
368 /* Prevent unused argument(s) compilation warning */
369 UNUSED(hwwdg);
370
371 /* NOTE: This function should not be modified, when the callback is needed,
372 the HAL_WWDG_EarlyWakeupCallback could be implemented in the user file
373 */
374 }
375
376 /**
377 * @}
378 */
379
380 /**
381 * @}
382 */
383
384 #endif /* HAL_WWDG_MODULE_ENABLED */
385 /**
386 * @}
387 */
388
389 /**
390 * @}
391 */
392
393 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
394