2 ******************************************************************************
3 * @file stm32f1xx_hal_wwdg.c
4 * @author MCD Application Team
6 * @date 15-December-2014
7 * @brief WWDG HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the Window Watchdog (WWDG) peripheral:
10 * + Initialization and de-initialization functions
11 * + IO operation functions
12 * + Peripheral State functions
15 ==============================================================================
16 ##### WWDG specific features #####
17 ==============================================================================
19 Once enabled the WWDG generates a system reset on expiry of a programmed
20 time period, unless the program refreshes the Counter (T[6;0] downcounter)
21 before reaching 0x3F value (i.e. a reset is generated when the counter
22 value rolls over from 0x40 to 0x3F).
24 (+) An MCU reset is also generated if the counter value is refreshed
25 before the counter has reached the refresh window value. This
26 implies that the counter must be refreshed in a limited window.
27 (+) Once enabled the WWDG cannot be disabled except by a system reset.
28 (+) WWDGRST flag in RCC_CSR register can be used to inform when a WWDG
30 (+) The WWDG counter input clock is derived from the APB clock divided
31 by a programmable prescaler.
32 (+) WWDG clock (Hz) = PCLK / (4096 * Prescaler)
33 (+) WWDG timeout (mS) = 1000 * (T[5;0] + 1) / WWDG clock
34 where T[5;0] are the lowest 6 bits of Counter.
35 (+) WWDG Counter refresh is allowed between the following limits :
36 (++) min time (mS) = 1000 * (Counter - Window) / WWDG clock
37 (++) max time (mS) = 1000 * (Counter - 0x40) / WWDG clock
39 (+) Min-max timeout value @48 MHz(PCLK): ~85,3us / ~5,46 ms
41 ##### How to use this driver #####
42 ==============================================================================
44 (+) Enable WWDG APB1 clock using __HAL_RCC_WWDG_CLK_ENABLE().
45 (+) Set the WWDG prescaler, refresh window and counter value
46 using HAL_WWDG_Init() function.
47 (+) Start the WWDG using HAL_WWDG_Start() function.
48 When the WWDG is enabled the counter value should be configured to
49 a value greater than 0x40 to prevent generating an immediate reset.
50 (+) Optionally you can enable the Early Wakeup Interrupt (EWI) which is
51 generated when the counter reaches 0x40, and then start the WWDG using
52 HAL_WWDG_Start_IT(). At EWI HAL_WWDG_WakeupCallback is executed and user can
53 add his own code by customization of function pointer HAL_WWDG_WakeupCallback
54 Once enabled, EWI interrupt cannot be disabled except by a system reset.
55 (+) Then the application program must refresh the WWDG counter at regular
56 intervals during normal operation to prevent an MCU reset, using
57 HAL_WWDG_Refresh() function. This operation must occur only when
58 the counter is lower than the refresh window value already programmed.
60 *** WWDG HAL driver macros list ***
61 ==================================
63 Below the list of most used macros in WWDG HAL driver.
65 (+) __HAL_WWDG_ENABLE: Enable the WWDG peripheral
66 (+) __HAL_WWDG_GET_FLAG: Get the selected WWDG's flag status
67 (+) __HAL_WWDG_CLEAR_FLAG: Clear the WWDG's pending flags
68 (+) __HAL_WWDG_ENABLE_IT: Enables the WWDG early wakeup interrupt
71 ******************************************************************************
74 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
76 * Redistribution and use in source and binary forms, with or without modification,
77 * are permitted provided that the following conditions are met:
78 * 1. Redistributions of source code must retain the above copyright notice,
79 * this list of conditions and the following disclaimer.
80 * 2. Redistributions in binary form must reproduce the above copyright notice,
81 * this list of conditions and the following disclaimer in the documentation
82 * and/or other materials provided with the distribution.
83 * 3. Neither the name of STMicroelectronics nor the names of its contributors
84 * may be used to endorse or promote products derived from this software
85 * without specific prior written permission.
87 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
88 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
89 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
90 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
91 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
92 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
93 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
94 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
95 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
96 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
98 ******************************************************************************
101 /* Includes ------------------------------------------------------------------*/
102 #include "stm32f1xx_hal.h"
104 /** @addtogroup STM32F1xx_HAL_Driver
108 /** @defgroup WWDG WWDG
109 * @brief WWDG HAL module driver.
113 #ifdef HAL_WWDG_MODULE_ENABLED
115 /* Private typedef -----------------------------------------------------------*/
116 /* Private define ------------------------------------------------------------*/
117 /* Private macro -------------------------------------------------------------*/
118 /* Private variables ---------------------------------------------------------*/
119 /* Private function prototypes -----------------------------------------------*/
120 /* Exported functions --------------------------------------------------------*/
122 /** @defgroup WWDG_Exported_Functions WWDG Exported Functions
126 /** @defgroup WWDG_Exported_Functions_Group1 Initialization and de-initialization functions
127 * @brief Initialization and Configuration functions.
130 ==============================================================================
131 ##### Initialization and de-initialization functions #####
132 ==============================================================================
134 This section provides functions allowing to:
135 (+) Initialize the WWDG according to the specified parameters
136 in the WWDG_InitTypeDef and create the associated handle
137 (+) DeInitialize the WWDG peripheral
138 (+) Initialize the WWDG MSP
139 (+) DeInitialize the WWDG MSP
146 * @brief Initializes the WWDG according to the specified
147 * parameters in the WWDG_InitTypeDef and creates the associated handle.
148 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
149 * the configuration information for the specified WWDG module.
152 HAL_StatusTypeDef HAL_WWDG_Init(WWDG_HandleTypeDef *hwwdg)
154 /* Check the WWDG handle allocation */
160 /* Check the parameters */
161 assert_param(IS_WWDG_ALL_INSTANCE(hwwdg->Instance));
162 assert_param(IS_WWDG_PRESCALER(hwwdg->Init.Prescaler));
163 assert_param(IS_WWDG_WINDOW(hwwdg->Init.Window));
164 assert_param(IS_WWDG_COUNTER(hwwdg->Init.Counter));
166 if(hwwdg->State == HAL_WWDG_STATE_RESET)
168 /* Allocate lock resource and initialize it */
169 hwwdg-> Lock = HAL_UNLOCKED;
171 /* Init the low level hardware */
172 HAL_WWDG_MspInit(hwwdg);
175 /* Change WWDG peripheral state */
176 hwwdg->State = HAL_WWDG_STATE_BUSY;
178 /* Set WWDG Prescaler and Window */
179 MODIFY_REG(hwwdg->Instance->CFR, (WWDG_CFR_WDGTB | WWDG_CFR_W), (hwwdg->Init.Prescaler | hwwdg->Init.Window));
181 /* Set WWDG Counter */
182 MODIFY_REG(hwwdg->Instance->CR, WWDG_CR_T, hwwdg->Init.Counter);
184 /* Change WWDG peripheral state */
185 hwwdg->State = HAL_WWDG_STATE_READY;
187 /* Return function status */
192 * @brief DeInitializes the WWDG peripheral.
193 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
194 * the configuration information for the specified WWDG module.
197 HAL_StatusTypeDef HAL_WWDG_DeInit(WWDG_HandleTypeDef *hwwdg)
199 /* Check the WWDG handle allocation */
205 /* Check the parameters */
206 assert_param(IS_WWDG_ALL_INSTANCE(hwwdg->Instance));
208 /* Change WWDG peripheral state */
209 hwwdg->State = HAL_WWDG_STATE_BUSY;
211 /* DeInit the low level hardware */
212 HAL_WWDG_MspDeInit(hwwdg);
214 /* Reset WWDG Control register */
215 hwwdg->Instance->CR = (uint32_t)0x0000007F;
217 /* Reset WWDG Configuration register */
218 hwwdg->Instance->CFR = (uint32_t)0x0000007F;
220 /* Reset WWDG Status register */
221 hwwdg->Instance->SR = 0;
223 /* Change WWDG peripheral state */
224 hwwdg->State = HAL_WWDG_STATE_RESET;
229 /* Return function status */
234 * @brief Initializes the WWDG MSP.
235 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
236 * the configuration information for the specified WWDG module.
239 __weak void HAL_WWDG_MspInit(WWDG_HandleTypeDef *hwwdg)
241 /* NOTE: This function Should not be modified, when the callback is needed,
242 the HAL_WWDG_MspInit could be implemented in the user file
247 * @brief DeInitializes the WWDG MSP.
248 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
249 * the configuration information for the specified WWDG module.
252 __weak void HAL_WWDG_MspDeInit(WWDG_HandleTypeDef *hwwdg)
254 /* NOTE: This function Should not be modified, when the callback is needed,
255 the HAL_WWDG_MspDeInit could be implemented in the user file
263 /** @defgroup WWDG_Exported_Functions_Group2 IO operation functions
264 * @brief IO operation functions
267 ==============================================================================
268 ##### IO operation functions #####
269 ==============================================================================
271 This section provides functions allowing to:
273 (+) Refresh the WWDG.
274 (+) Handle WWDG interrupt request.
281 * @brief Starts the WWDG.
282 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
283 * the configuration information for the specified WWDG module.
286 HAL_StatusTypeDef HAL_WWDG_Start(WWDG_HandleTypeDef *hwwdg)
291 /* Change WWDG peripheral state */
292 hwwdg->State = HAL_WWDG_STATE_BUSY;
294 /* Enable the peripheral */
295 __HAL_WWDG_ENABLE(hwwdg);
297 /* Change WWDG peripheral state */
298 hwwdg->State = HAL_WWDG_STATE_READY;
300 /* Process Unlocked */
303 /* Return function status */
308 * @brief Starts the WWDG with interrupt enabled.
309 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
310 * the configuration information for the specified WWDG module.
313 HAL_StatusTypeDef HAL_WWDG_Start_IT(WWDG_HandleTypeDef *hwwdg)
318 /* Change WWDG peripheral state */
319 hwwdg->State = HAL_WWDG_STATE_BUSY;
321 /* Enable the Early Wakeup Interrupt */
322 __HAL_WWDG_ENABLE_IT(hwwdg, WWDG_IT_EWI);
324 /* Enable the peripheral */
325 __HAL_WWDG_ENABLE(hwwdg);
327 /* Return function status */
332 * @brief Refreshes the WWDG.
333 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
334 * the configuration information for the specified WWDG module.
335 * @param Counter: value of counter to put in WWDG counter
338 HAL_StatusTypeDef HAL_WWDG_Refresh(WWDG_HandleTypeDef *hwwdg, uint32_t Counter)
343 /* Change WWDG peripheral state */
344 hwwdg->State = HAL_WWDG_STATE_BUSY;
346 /* Check the parameters */
347 assert_param(IS_WWDG_COUNTER(Counter));
349 /* Write to WWDG CR the WWDG Counter value to refresh with */
350 MODIFY_REG(hwwdg->Instance->CR, (uint32_t)WWDG_CR_T, Counter);
352 /* Change WWDG peripheral state */
353 hwwdg->State = HAL_WWDG_STATE_READY;
355 /* Process Unlocked */
358 /* Return function status */
363 * @brief Handles WWDG interrupt request.
364 * @note The Early Wakeup Interrupt (EWI) can be used if specific safety operations
365 * or data logging must be performed before the actual reset is generated.
366 * The EWI interrupt is enabled when calling HAL_WWDG_Start_IT function.
367 * When the downcounter reaches the value 0x40, and EWI interrupt is
368 * generated and the corresponding Interrupt Service Routine (ISR) can
369 * be used to trigger specific actions (such as communications or data
370 * logging), before resetting the device.
371 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
372 * the configuration information for the specified WWDG module.
375 void HAL_WWDG_IRQHandler(WWDG_HandleTypeDef *hwwdg)
377 /* Check if Early Wakeup Interrupt is enable */
378 if(__HAL_WWDG_GET_IT_SOURCE(hwwdg, WWDG_IT_EWI) != RESET)
380 /* Wheck if WWDG Early Wakeup Interrupt occurred */
381 if(__HAL_WWDG_GET_FLAG(hwwdg, WWDG_FLAG_EWIF) != RESET)
383 /* Early Wakeup callback */
384 HAL_WWDG_WakeupCallback(hwwdg);
386 /* Change WWDG peripheral state */
387 hwwdg->State = HAL_WWDG_STATE_READY;
389 /* Clear the WWDG Early Wakeup flag */
390 __HAL_WWDG_CLEAR_FLAG(hwwdg, WWDG_FLAG_EWIF);
392 /* Process Unlocked */
399 * @brief Early Wakeup WWDG callback.
400 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
401 * the configuration information for the specified WWDG module.
404 __weak void HAL_WWDG_WakeupCallback(WWDG_HandleTypeDef* hwwdg)
406 /* NOTE: This function Should not be modified, when the callback is needed,
407 the HAL_WWDG_WakeupCallback could be implemented in the user file
415 /** @defgroup WWDG_Exported_Functions_Group3 Peripheral State functions
416 * @brief Peripheral State functions.
419 ==============================================================================
420 ##### Peripheral State functions #####
421 ==============================================================================
423 This subsection permits to get in run-time the status of the peripheral
431 * @brief Returns the WWDG state.
432 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
433 * the configuration information for the specified WWDG module.
436 HAL_WWDG_StateTypeDef HAL_WWDG_GetState(WWDG_HandleTypeDef *hwwdg)
449 #endif /* HAL_WWDG_MODULE_ENABLED */
458 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/