2 ******************************************************************************
3 * @file stm32f0xx_hal_wwdg.c
4 * @author MCD Application Team
6 * @date 11-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 __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 "stm32f0xx_hal.h"
104 /** @addtogroup STM32F0xx_HAL_Driver
108 /** @defgroup WWDG WWDG HAL module driver
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 /* Init the low level hardware */
169 HAL_WWDG_MspInit(hwwdg);
172 /* Change WWDG peripheral state */
173 hwwdg->State = HAL_WWDG_STATE_BUSY;
175 /* Set WWDG Prescaler and Window */
176 MODIFY_REG(hwwdg->Instance->CFR, (WWDG_CFR_WDGTB | WWDG_CFR_W), (hwwdg->Init.Prescaler | hwwdg->Init.Window));
178 /* Set WWDG Counter */
179 MODIFY_REG(hwwdg->Instance->CR, WWDG_CR_T, hwwdg->Init.Counter);
181 /* Change WWDG peripheral state */
182 hwwdg->State = HAL_WWDG_STATE_READY;
184 /* Return function status */
189 * @brief DeInitializes the WWDG peripheral.
190 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
191 * the configuration information for the specified WWDG module.
194 HAL_StatusTypeDef HAL_WWDG_DeInit(WWDG_HandleTypeDef *hwwdg)
196 /* Check the parameters */
197 assert_param(IS_WWDG_ALL_INSTANCE(hwwdg->Instance));
199 /* Change WWDG peripheral state */
200 hwwdg->State = HAL_WWDG_STATE_BUSY;
202 /* DeInit the low level hardware */
203 HAL_WWDG_MspDeInit(hwwdg);
205 /* Reset WWDG Control register */
206 hwwdg->Instance->CR = (uint32_t)0x0000007F;
208 /* Reset WWDG Configuration register */
209 hwwdg->Instance->CFR = (uint32_t)0x0000007F;
211 /* Reset WWDG Status register */
212 hwwdg->Instance->SR = 0;
214 /* Change WWDG peripheral state */
215 hwwdg->State = HAL_WWDG_STATE_RESET;
220 /* Return function status */
225 * @brief Initializes the WWDG MSP.
226 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
227 * the configuration information for the specified WWDG module.
230 __weak void HAL_WWDG_MspInit(WWDG_HandleTypeDef *hwwdg)
232 /* NOTE : This function Should not be modified, when the callback is needed,
233 the HAL_WWDG_MspInit could be implemented in the user file
238 * @brief DeInitializes the WWDG MSP.
239 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
240 * the configuration information for the specified WWDG module.
243 __weak void HAL_WWDG_MspDeInit(WWDG_HandleTypeDef *hwwdg)
245 /* NOTE : This function Should not be modified, when the callback is needed,
246 the HAL_WWDG_MspDeInit could be implemented in the user file
254 /** @defgroup WWDG_Exported_Functions_Group2 IO operation functions
255 * @brief IO operation functions
258 ==============================================================================
259 ##### IO operation functions #####
260 ==============================================================================
262 This section provides functions allowing to:
264 (+) Refresh the WWDG.
265 (+) Handle WWDG interrupt request.
272 * @brief Starts the WWDG.
273 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
274 * the configuration information for the specified WWDG module.
277 HAL_StatusTypeDef HAL_WWDG_Start(WWDG_HandleTypeDef *hwwdg)
282 /* Change WWDG peripheral state */
283 hwwdg->State = HAL_WWDG_STATE_BUSY;
285 /* Enable the peripheral */
286 __HAL_WWDG_ENABLE(hwwdg);
288 /* Change WWDG peripheral state */
289 hwwdg->State = HAL_WWDG_STATE_READY;
291 /* Process Unlocked */
294 /* Return function status */
299 * @brief Starts the WWDG with interrupt enabled.
300 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
301 * the configuration information for the specified WWDG module.
304 HAL_StatusTypeDef HAL_WWDG_Start_IT(WWDG_HandleTypeDef *hwwdg)
309 /* Change WWDG peripheral state */
310 hwwdg->State = HAL_WWDG_STATE_BUSY;
312 /* Enable the Early Wakeup Interrupt */
313 __HAL_WWDG_ENABLE_IT(hwwdg, WWDG_IT_EWI);
315 /* Enable the peripheral */
316 __HAL_WWDG_ENABLE(hwwdg);
318 /* Return function status */
323 * @brief Refreshes the WWDG.
324 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
325 * the configuration information for the specified WWDG module.
326 * @param Counter: value of counter to put in WWDG counter
329 HAL_StatusTypeDef HAL_WWDG_Refresh(WWDG_HandleTypeDef *hwwdg, uint32_t Counter)
334 /* Change WWDG peripheral state */
335 hwwdg->State = HAL_WWDG_STATE_BUSY;
337 /* Check the parameters */
338 assert_param(IS_WWDG_COUNTER(Counter));
340 /* Write to WWDG CR the WWDG Counter value to refresh with */
341 MODIFY_REG(hwwdg->Instance->CR, (uint32_t)WWDG_CR_T, Counter);
343 /* Change WWDG peripheral state */
344 hwwdg->State = HAL_WWDG_STATE_READY;
346 /* Process Unlocked */
349 /* Return function status */
354 * @brief Handles 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 when calling HAL_WWDG_Start_IT function.
358 * When the downcounter reaches the value 0x40, and EWI interrupt is
359 * generated and the corresponding Interrupt Service Routine (ISR) can
360 * be used to trigger specific actions (such as communications or data
361 * logging), before resetting the device.
362 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
363 * the configuration information for the specified WWDG module.
366 void HAL_WWDG_IRQHandler(WWDG_HandleTypeDef *hwwdg)
368 /* Check if Early Wakeup Interrupt is enable */
369 if(__HAL_WWDG_GET_IT_SOURCE(hwwdg, WWDG_IT_EWI) != RESET)
371 /* Wheck if WWDG Early Wakeup Interrupt occurred */
372 if(__HAL_WWDG_GET_FLAG(hwwdg, WWDG_FLAG_EWIF) != RESET)
374 /* Early Wakeup callback */
375 HAL_WWDG_WakeupCallback(hwwdg);
377 /* Change WWDG peripheral state */
378 hwwdg->State = HAL_WWDG_STATE_READY;
380 /* Clear the WWDG Early Wakeup flag */
381 __HAL_WWDG_CLEAR_FLAG(hwwdg, WWDG_FLAG_EWIF);
383 /* Process Unlocked */
390 * @brief Early Wakeup WWDG callback.
391 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
392 * the configuration information for the specified WWDG module.
395 __weak void HAL_WWDG_WakeupCallback(WWDG_HandleTypeDef* hwwdg)
397 /* NOTE : This function Should not be modified, when the callback is needed,
398 the HAL_WWDG_WakeupCallback could be implemented in the user file
406 /** @defgroup WWDG_Exported_Functions_Group3 Peripheral State functions
407 * @brief Peripheral State functions.
410 ==============================================================================
411 ##### Peripheral State functions #####
412 ==============================================================================
414 This subsection permits to get in run-time the status of the peripheral
422 * @brief Returns the WWDG state.
423 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
424 * the configuration information for the specified WWDG module.
427 HAL_WWDG_StateTypeDef HAL_WWDG_GetState(WWDG_HandleTypeDef *hwwdg)
440 #endif /* HAL_WWDG_MODULE_ENABLED */
449 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/