2 ******************************************************************************
3 * @file stm32l1xx_hal_wwdg.c
4 * @author MCD Application Team
6 * @date 5-September-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
14 ==============================================================================
15 ##### WWDG specific features #####
16 ==============================================================================
18 Once enabled the WWDG generates a system reset on expiry of a programmed
19 time period, unless the program refreshes the counter (downcounter)
20 before reaching 0x3F value (i.e. a reset is generated when the counter
21 value rolls over from 0x40 to 0x3F).
23 (+) An MCU reset is also generated if the counter value is refreshed
24 before the counter has reached the refresh window value. This
25 implies that the counter must be refreshed in a limited window.
26 (+) Once enabled the WWDG cannot be disabled except by a system reset.
27 (+) WWDGRST flag in RCC_CSR register can be used to inform when a WWDG
29 (+) The WWDG counter input clock is derived from the APB clock divided
30 by a programmable prescaler.
31 (+) WWDG clock (Hz) = PCLK1 / (4096 * Prescaler)
32 (+) WWDG timeout (mS) = 1000 * Counter / WWDG clock
33 (+) WWDG Counter refresh is allowed between the following limits :
34 (++) min time (mS) = 1000 * (Counter
\96 Window) / WWDG clock
35 (++) max time (mS) = 1000 * (Counter
\96 0x40) / WWDG clock
37 (+) Min-max timeout value at @32MHz (PCLK1): ~128us / ~65.6ms.
40 ##### How to use this driver #####
41 ==============================================================================
43 (+) Enable WWDG APB1 clock using __WWDG_CLK_ENABLE().
44 (+) Set the WWDG prescaler, refresh window and counter value
45 using HAL_WWDG_Init() function.
46 (+) Start the WWDG using HAL_WWDG_Start() function.
47 When the WWDG is enabled the counter value should be configured to
48 a value greater than 0x40 to prevent generating an immediate reset.
49 (+) Optionally you can enable the Early Wakeup Interrupt (EWI) which is
50 generated when the counter reaches 0x40, and then start the WWDG using
51 HAL_WWDG_Start_IT(). At EWI HAL_WWDG_WakeupCallback is executed and user can
52 add his own code by customization of function pointer HAL_WWDG_WakeupCallback
53 Once enabled, EWI interrupt cannot be disabled except by a system reset.
54 (+) Then the application program must refresh the WWDG counter at regular
55 intervals during normal operation to prevent an MCU reset, using
56 HAL_WWDG_Refresh() function. This operation must occur only when
57 the counter is lower than the refresh window value already programmed.
59 *** WWDG HAL driver macros list ***
60 ==================================
62 Below the list of most used macros in WWDG HAL driver.
64 (+) __HAL_WWDG_ENABLE: Enable the WWDG peripheral
65 (+) __HAL_WWDG_GET_FLAG: Get the selected WWDG's flag status
66 (+) __HAL_WWDG_CLEAR_FLAG: Clear the WWDG's pending flags
67 (+) __HAL_WWDG_ENABLE_IT: Enables the WWDG early wakeup interrupt
70 ******************************************************************************
73 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
75 * Redistribution and use in source and binary forms, with or without modification,
76 * are permitted provided that the following conditions are met:
77 * 1. Redistributions of source code must retain the above copyright notice,
78 * this list of conditions and the following disclaimer.
79 * 2. Redistributions in binary form must reproduce the above copyright notice,
80 * this list of conditions and the following disclaimer in the documentation
81 * and/or other materials provided with the distribution.
82 * 3. Neither the name of STMicroelectronics nor the names of its contributors
83 * may be used to endorse or promote products derived from this software
84 * without specific prior written permission.
86 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
87 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
88 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
89 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
90 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
91 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
92 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
93 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
94 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
95 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
97 ******************************************************************************
100 /* Includes ------------------------------------------------------------------*/
101 #include "stm32l1xx_hal.h"
103 /** @addtogroup STM32L1xx_HAL_Driver
107 /** @defgroup WWDG WWDG
108 * @brief WWDG HAL module driver.
112 #ifdef HAL_WWDG_MODULE_ENABLED
114 /* Private typedef -----------------------------------------------------------*/
115 /* Private define ------------------------------------------------------------*/
116 /* Private macro -------------------------------------------------------------*/
117 /* Private variables ---------------------------------------------------------*/
118 /* Private function prototypes -----------------------------------------------*/
119 /* Private functions ---------------------------------------------------------*/
121 /** @defgroup WWDG_Exported_Functions WWDG Exported Functions
125 /** @defgroup WWDG_Exported_Functions_Group1 Initialization and de-initialization functions
126 * @brief Initialization and Configuration functions.
129 ==============================================================================
130 ##### Initialization and de-initialization functions #####
131 ==============================================================================
133 This section provides functions allowing to:
134 (+) Initialize the WWDG according to the specified parameters
135 in the WWDG_InitTypeDef and create the associated handle
136 (+) DeInitialize the WWDG peripheral
137 (+) Initialize the WWDG MSP
138 (+) DeInitialize the WWDG MSP
145 * @brief Initializes the WWDG according to the specified
146 * parameters in the WWDG_InitTypeDef and creates the associated handle.
147 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
148 * the configuration information for the specified WWDG module.
151 HAL_StatusTypeDef HAL_WWDG_Init(WWDG_HandleTypeDef *hwwdg)
153 /* Check the WWDG handle allocation */
154 if(hwwdg == HAL_NULL)
159 /* Check the parameters */
160 assert_param(IS_WWDG_ALL_INSTANCE(hwwdg->Instance));
161 assert_param(IS_WWDG_PRESCALER(hwwdg->Init.Prescaler));
162 assert_param(IS_WWDG_WINDOW(hwwdg->Init.Window));
163 assert_param(IS_WWDG_COUNTER(hwwdg->Init.Counter));
165 if(hwwdg->State == HAL_WWDG_STATE_RESET)
167 /* Init the low level hardware */
168 HAL_WWDG_MspInit(hwwdg);
171 /* Change WWDG peripheral state */
172 hwwdg->State = HAL_WWDG_STATE_BUSY;
174 /* Set WWDG Prescaler and Window */
175 MODIFY_REG(hwwdg->Instance->CFR, (WWDG_CFR_WDGTB | WWDG_CFR_W), (hwwdg->Init.Prescaler | hwwdg->Init.Window));
177 /* Set WWDG Counter */
178 MODIFY_REG(hwwdg->Instance->CR, WWDG_CR_T, hwwdg->Init.Counter);
180 /* Change WWDG peripheral state */
181 hwwdg->State = HAL_WWDG_STATE_READY;
183 /* Return function status */
188 * @brief DeInitializes the WWDG peripheral.
189 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
190 * the configuration information for the specified WWDG module.
193 HAL_StatusTypeDef HAL_WWDG_DeInit(WWDG_HandleTypeDef *hwwdg)
195 /* Check the parameters */
196 assert_param(IS_WWDG_ALL_INSTANCE(hwwdg->Instance));
198 /* Change WWDG peripheral state */
199 hwwdg->State = HAL_WWDG_STATE_BUSY;
201 /* DeInit the low level hardware */
202 HAL_WWDG_MspDeInit(hwwdg);
204 /* Reset WWDG Control register */
205 hwwdg->Instance->CR = (uint32_t)0x0000007F;
207 /* Reset WWDG Configuration register */
208 hwwdg->Instance->CFR = (uint32_t)0x0000007F;
210 /* Reset WWDG Status register */
211 hwwdg->Instance->SR = 0;
213 /* Change WWDG peripheral state */
214 hwwdg->State = HAL_WWDG_STATE_RESET;
219 /* Return function status */
224 * @brief Initializes the WWDG MSP.
225 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
226 * the configuration information for the specified WWDG module.
229 __weak void HAL_WWDG_MspInit(WWDG_HandleTypeDef *hwwdg)
231 /* NOTE: This function Should not be modified, when the callback is needed,
232 the HAL_WWDG_MspInit could be implemented in the user file
237 * @brief DeInitializes the WWDG MSP.
238 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
239 * the configuration information for the specified WWDG module.
242 __weak void HAL_WWDG_MspDeInit(WWDG_HandleTypeDef *hwwdg)
244 /* NOTE: This function Should not be modified, when the callback is needed,
245 the HAL_WWDG_MspDeInit could be implemented in the user file
253 /** @defgroup WWDG_Exported_Functions_Group2 IO operation functions
254 * @brief IO operation functions
257 ==============================================================================
258 ##### IO operation functions #####
259 ==============================================================================
261 This section provides functions allowing to:
263 (+) Refresh the WWDG.
264 (+) Handle WWDG interrupt request.
271 * @brief Starts the WWDG.
272 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
273 * the configuration information for the specified WWDG module.
276 HAL_StatusTypeDef HAL_WWDG_Start(WWDG_HandleTypeDef *hwwdg)
281 /* Change WWDG peripheral state */
282 hwwdg->State = HAL_WWDG_STATE_BUSY;
284 /* Enable the peripheral */
285 __HAL_WWDG_ENABLE(hwwdg);
287 /* Change WWDG peripheral state */
288 hwwdg->State = HAL_WWDG_STATE_READY;
290 /* Process Unlocked */
293 /* Return function status */
298 * @brief Starts the WWDG with interrupt enabled.
299 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
300 * the configuration information for the specified WWDG module.
303 HAL_StatusTypeDef HAL_WWDG_Start_IT(WWDG_HandleTypeDef *hwwdg)
308 /* Change WWDG peripheral state */
309 hwwdg->State = HAL_WWDG_STATE_BUSY;
311 /* Enable the Early Wakeup Interrupt */
312 __HAL_WWDG_ENABLE_IT(WWDG_IT_EWI);
314 /* Enable the peripheral */
315 __HAL_WWDG_ENABLE(hwwdg);
317 /* Return function status */
322 * @brief Refreshes the WWDG.
323 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
324 * the configuration information for the specified WWDG module.
325 * @param Counter: value of counter to put in WWDG counter
328 HAL_StatusTypeDef HAL_WWDG_Refresh(WWDG_HandleTypeDef *hwwdg, uint32_t Counter)
333 /* Change WWDG peripheral state */
334 hwwdg->State = HAL_WWDG_STATE_BUSY;
336 /* Check the parameters */
337 assert_param(IS_WWDG_COUNTER(Counter));
339 /* Write to WWDG CR the WWDG Counter value to refresh with */
340 MODIFY_REG(hwwdg->Instance->CR, (uint32_t)WWDG_CR_T, Counter);
342 /* Change WWDG peripheral state */
343 hwwdg->State = HAL_WWDG_STATE_READY;
345 /* Process Unlocked */
348 /* Return function status */
353 * @brief Handles WWDG interrupt request.
354 * @note The Early Wakeup Interrupt (EWI) can be used if specific safety operations
355 * or data logging must be performed before the actual reset is generated.
356 * The EWI interrupt is enabled using __HAL_WWDG_ENABLE_IT() macro.
357 * When the downcounter reaches the value 0x40, and EWI interrupt is
358 * generated and the corresponding Interrupt Service Routine (ISR) can
359 * be used to trigger specific actions (such as communications or data
360 * logging), before resetting the device.
361 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
362 * the configuration information for the specified WWDG module.
365 void HAL_WWDG_IRQHandler(WWDG_HandleTypeDef *hwwdg)
367 /* WWDG Early Wakeup Interrupt occurred */
368 if(__HAL_WWDG_GET_FLAG(hwwdg, WWDG_FLAG_EWIF) != RESET)
370 /* Early Wakeup callback */
371 HAL_WWDG_WakeupCallback(hwwdg);
373 /* Change WWDG peripheral state */
374 hwwdg->State = HAL_WWDG_STATE_READY;
376 /* Clear the WWDG Data Ready flag */
377 __HAL_WWDG_CLEAR_IT(hwwdg, WWDG_FLAG_EWIF);
379 /* Process Unlocked */
385 * @brief Early Wakeup WWDG callback.
386 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
387 * the configuration information for the specified WWDG module.
390 __weak void HAL_WWDG_WakeupCallback(WWDG_HandleTypeDef* hwwdg)
392 /* NOTE: This function Should not be modified, when the callback is needed,
393 the HAL_WWDG_WakeupCallback could be implemented in the user file
401 /** @defgroup WWDG_Exported_Functions_Group3 Peripheral State functions
402 * @brief Peripheral State functions.
405 ==============================================================================
406 ##### Peripheral State functions #####
407 ==============================================================================
409 This subsection permits to get in run-time the status of the peripheral
417 * @brief Returns the WWDG state.
418 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
419 * the configuration information for the specified WWDG module.
422 HAL_WWDG_StateTypeDef HAL_WWDG_GetState(WWDG_HandleTypeDef *hwwdg)
435 #endif /* HAL_WWDG_MODULE_ENABLED */
444 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/