2 ******************************************************************************
3 * @file stm32f3xx_hal_wwdg.c
4 * @author MCD Application Team
7 * @brief WWDG HAL module driver.
9 * This file provides firmware functions to manage the following
10 * functionalities of the Window Watchdog (WWDG) peripheral:
11 * + Initialization/de-initialization functions
12 * + I/O operation functions
13 * + Peripheral State functions
16 ==============================================================================
17 ##### WWDG specific features #####
18 ==============================================================================
19 [..] Once enabled the WWDG generates a system reset on expiry of a programmed
20 time period, unless the program refreshes the counter (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 counter clock = PCLK1 / Prescaler
33 WWDG timeout = (WWDG counter clock) * (counter value)
34 (+) Min-max timeout value @42 MHz(PCLK1): ~97.5 us / ~49.9 ms
36 ##### How to use this driver #####
37 ==============================================================================
39 (+) Enable WWDG APB1 clock using __WWDG_CLK_ENABLE().
40 (+) Set the WWDG prescaler, refresh window and counter value
41 using HAL_WWDG_Init() function.
42 (+) Start the WWDG using HAL_WWDG_Start() function.
43 When the WWDG is enabled the counter value should be configured to
44 a value greater than 0x40 to prevent generating an immediate reset.
45 (+) Optionally you can enable the Early Wakeup Interrupt (EWI) which is
46 generated when the counter reaches 0x40, and then start the WWDG using
48 Once enabled, EWI interrupt cannot be disabled except by a system reset.
49 (+) Then the application program must refresh the WWDG counter at regular
50 intervals during normal operation to prevent an MCU reset, using
51 HAL_WWDG_Refresh() function. This operation must occur only when
52 the counter is lower than the refresh window value already programmed.
55 ******************************************************************************
58 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
60 * Redistribution and use in source and binary forms, with or without modification,
61 * are permitted provided that the following conditions are met:
62 * 1. Redistributions of source code must retain the above copyright notice,
63 * this list of conditions and the following disclaimer.
64 * 2. Redistributions in binary form must reproduce the above copyright notice,
65 * this list of conditions and the following disclaimer in the documentation
66 * and/or other materials provided with the distribution.
67 * 3. Neither the name of STMicroelectronics nor the names of its contributors
68 * may be used to endorse or promote products derived from this software
69 * without specific prior written permission.
71 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
72 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
73 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
74 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
75 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
76 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
77 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
78 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
79 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
80 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
82 ******************************************************************************
85 /* Includes ------------------------------------------------------------------*/
86 #include "stm32f3xx_hal.h"
88 /** @addtogroup STM32F3xx_HAL_Driver
92 /** @defgroup WWDG WWDG HAL module driver.
93 * @brief WWDG HAL module driver.
97 #ifdef HAL_WWDG_MODULE_ENABLED
99 /* Private typedef -----------------------------------------------------------*/
100 /* Private define ------------------------------------------------------------*/
101 /* Private macro -------------------------------------------------------------*/
102 /* Private variables ---------------------------------------------------------*/
103 /* Private function prototypes -----------------------------------------------*/
104 /* Exported functions ---------------------------------------------------------*/
106 /** @defgroup WWDG_Exported_Functions WWDG Exported Functions
110 /** @defgroup WWDG_Exported_Functions_Group1 Initialization and de-initialization functions
111 * @brief Initialization and Configuration functions.
114 ===============================================================================
115 ##### Initialization/de-initialization functions #####
116 ===============================================================================
117 [..] This section provides functions allowing to:
118 (+) Initialize the WWDG according to the specified parameters
119 in the WWDG_InitTypeDef and create the associated handle
120 (+) DeInitialize the WWDG peripheral
121 (+) Initialize the WWDG MSP
122 (+) DeInitialize the WWDG MSP
129 * @brief Initializes the WWDG according to the specified
130 * parameters in the WWDG_InitTypeDef and creates the associated handle.
131 * @param hwwdg: WWDG handle
134 HAL_StatusTypeDef HAL_WWDG_Init(WWDG_HandleTypeDef *hwwdg)
136 /* Check the WWDG handle allocation */
137 if(hwwdg == HAL_NULL)
142 /* Check the parameters */
143 assert_param(IS_WWDG_ALL_INSTANCE(hwwdg->Instance));
144 assert_param(IS_WWDG_PRESCALER(hwwdg->Init.Prescaler));
145 assert_param(IS_WWDG_WINDOW(hwwdg->Init.Window));
146 assert_param(IS_WWDG_COUNTER(hwwdg->Init.Counter));
148 if(hwwdg->State == HAL_WWDG_STATE_RESET)
150 /* Init the low level hardware */
151 HAL_WWDG_MspInit(hwwdg);
154 /* Change WWDG peripheral state */
155 hwwdg->State = HAL_WWDG_STATE_BUSY;
157 /* Set WWDG Prescaler and Window */
158 MODIFY_REG(hwwdg->Instance->CFR, (WWDG_CFR_WDGTB | WWDG_CFR_W), (hwwdg->Init.Prescaler | hwwdg->Init.Window));
160 /* Set WWDG Counter */
161 MODIFY_REG(hwwdg->Instance->CR, WWDG_CR_T, hwwdg->Init.Counter);
163 /* Return function status */
168 * @brief DeInitializes the WWDG peripheral.
169 * @param hwwdg: WWDG handle
172 HAL_StatusTypeDef HAL_WWDG_DeInit(WWDG_HandleTypeDef *hwwdg)
174 /* Check the parameters */
175 assert_param(IS_WWDG_ALL_INSTANCE(hwwdg->Instance));
177 /* Change WWDG peripheral state */
178 hwwdg->State = HAL_WWDG_STATE_BUSY;
180 /* DeInit the low level hardware */
181 HAL_WWDG_MspDeInit(hwwdg);
183 /* Reset WWDG Control register */
184 hwwdg->Instance->CR = (uint32_t)0x0000007F;
186 /* Reset WWDG Configuration register */
187 hwwdg->Instance->CFR = (uint32_t)0x0000007F;
189 /* Reset WWDG Status register */
190 hwwdg->Instance->SR = 0;
192 /* Change WWDG peripheral state */
193 hwwdg->State = HAL_WWDG_STATE_RESET;
195 /* Return function status */
200 * @brief Initializes the WWDG MSP.
201 * @param hwwdg: WWDG handle
204 __weak void HAL_WWDG_MspInit(WWDG_HandleTypeDef *hwwdg)
206 /* NOTE : This function Should not be modified, when the callback is needed,
207 the HAL_WWDG_MspInit could be implemented in the user file
212 * @brief DeInitializes the WWDG MSP.
213 * @param hwwdg: WWDG handle
216 __weak void HAL_WWDG_MspDeInit(WWDG_HandleTypeDef *hwwdg)
218 /* NOTE : This function Should not be modified, when the callback is needed,
219 the HAL_WWDG_MspDeInit could be implemented in the user file
224 * @brief Early Wakeup WWDG callback.
225 * @param hwwdg: WWDG handle
228 __weak void HAL_WWDG_WakeupCallback(WWDG_HandleTypeDef* hwwdg)
230 /* NOTE : This function Should not be modified, when the callback is needed,
231 the HAL_WWDG_WakeupCallback could be implemented in the user file
239 /** @defgroup WWDG_Exported_Functions_Group2 Input and Output operation functions
240 * @brief I/O operation functions
243 ===============================================================================
244 ##### IO operation functions #####
245 ===============================================================================
246 [..] This section provides functions allowing to:
248 (+) Refresh the WWDG.
249 (+) Handle WWDG interrupt request.
256 * @brief Starts the WWDG
257 * @param hwwdg: WWDG handle
260 HAL_StatusTypeDef HAL_WWDG_Start(WWDG_HandleTypeDef *hwwdg)
265 /* Change WWDG peripheral state */
266 hwwdg->State = HAL_WWDG_STATE_BUSY;
268 /* Enable the Peripheral */
269 __HAL_WWDG_ENABLE(hwwdg);
271 /* Change WWDG peripheral state */
272 hwwdg->State = HAL_WWDG_STATE_READY;
274 /* Process unlocked */
277 /* Return function status */
282 * @brief Starts the WWDG with interrupt enabled.
283 * @param hwwdg: WWDG handle
286 HAL_StatusTypeDef HAL_WWDG_Start_IT(WWDG_HandleTypeDef *hwwdg)
291 /* Change WWDG peripheral state */
292 hwwdg->State = HAL_WWDG_STATE_BUSY;
294 /* Enable the Early Wakeup Interrupt */
295 __HAL_WWDG_ENABLE_IT(WWDG_IT_EWI);
297 /* Enable the Peripheral */
298 __HAL_WWDG_ENABLE(hwwdg);
300 /* Return function status */
305 * @brief Refreshes the WWDG.
306 * @param hwwdg: WWDG handle
307 * @param Counter: Counter value to refresh WWDG with
310 HAL_StatusTypeDef HAL_WWDG_Refresh(WWDG_HandleTypeDef *hwwdg, uint32_t Counter)
315 /* Change WWDG peripheral state */
316 hwwdg->State = HAL_WWDG_STATE_BUSY;
318 /* Check the parameters */
319 assert_param(IS_WWDG_COUNTER(Counter));
321 /* Write to WWDG CR the WWDG Counter value to refresh with */
322 MODIFY_REG(hwwdg->Instance->CR, WWDG_CR_T, Counter);
324 /* Change WWDG peripheral state */
325 hwwdg->State = HAL_WWDG_STATE_READY;
327 /* Process unlocked */
330 /* Return function status */
335 * @brief Handles WWDG interrupt request.
336 * @note The Early Wakeup Interrupt (EWI) can be used if specific safety operations
337 * or data logging must be performed before the actual reset is generated.
338 * The EWI interrupt is enabled using __HAL_WWDG_ENABLE_IT() macro.
339 * When the downcounter reaches the value 0x40, and EWI interrupt is
340 * generated and the corresponding Interrupt Service Routine (ISR) can
341 * be used to trigger specific actions (such as communications or data
342 * logging), before resetting the device.
343 * @param hwwdg: WWDG handle
346 void HAL_WWDG_IRQHandler(WWDG_HandleTypeDef *hwwdg)
348 /* WWDG Early Wakeup Interrupt occurred */
349 if(__HAL_WWDG_GET_FLAG(hwwdg, WWDG_FLAG_EWIF) != RESET)
351 /* Early Wakeup callback */
352 HAL_WWDG_WakeupCallback(hwwdg);
354 /* Change WWDG peripheral state */
355 hwwdg->State = HAL_WWDG_STATE_READY;
357 /* Clear the WWDG Data Ready flag */
358 __HAL_WWDG_CLEAR_FLAG(hwwdg, WWDG_FLAG_EWIF);
360 /* Process unlocked */
369 /** @defgroup WWDG_Exported_Functions_Group3 Peripheral State functions
370 * @brief Peripheral State functions.
373 ===============================================================================
374 ##### Peripheral State functions #####
375 ===============================================================================
377 This subsection permits to get in run-time the status of the peripheral
385 * @brief Returns the WWDG state.
386 * @param hwwdg: WWDG handle
389 HAL_WWDG_StateTypeDef HAL_WWDG_GetState(WWDG_HandleTypeDef *hwwdg)
401 #endif /* HAL_WWDG_MODULE_ENABLED */
410 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/