2 ******************************************************************************
\r
3 * @file stm32f7xx_hal_wwdg.c
\r
4 * @author MCD Application Team
\r
6 * @date 06-March-2015
\r
7 * @brief WWDG HAL module driver.
\r
8 * This file provides firmware functions to manage the following
\r
9 * functionalities of the Window Watchdog (WWDG) peripheral:
\r
10 * + Initialization and de-initialization functions
\r
11 * + IO operation functions
\r
12 * + Peripheral State functions
\r
14 ==============================================================================
\r
15 ##### WWDG specific features #####
\r
16 ==============================================================================
\r
18 Once enabled the WWDG generates a system reset on expiry of a programmed
\r
19 time period, unless the program refreshes the counter (downcounter)
\r
20 before reaching 0x3F value (i.e. a reset is generated when the counter
\r
21 value rolls over from 0x40 to 0x3F).
\r
23 (+) An MCU reset is also generated if the counter value is refreshed
\r
24 before the counter has reached the refresh window value. This
\r
25 implies that the counter must be refreshed in a limited window.
\r
26 (+) Once enabled the WWDG cannot be disabled except by a system reset.
\r
27 (+) WWDGRST flag in RCC_CSR register can be used to inform when a WWDG
\r
29 (+) The WWDG counter input clock is derived from the APB clock divided
\r
30 by a programmable prescaler.
\r
31 (+) WWDG clock (Hz) = PCLK1 / (4096 * Prescaler)
\r
32 (+) WWDG timeout (mS) = 1000 * Counter / WWDG clock
\r
33 (+) WWDG Counter refresh is allowed between the following limits :
\r
34 (++) min time (mS) = 1000 * (Counter
\96 Window) / WWDG clock
\r
35 (++) max time (mS) = 1000 * (Counter
\96 0x40) / WWDG clock
\r
37 (+) Min-max timeout value at 50 MHz(PCLK1): 81.9 us / 41.9 ms
\r
40 ##### How to use this driver #####
\r
41 ==============================================================================
\r
43 (+) Enable WWDG APB1 clock using __HAL_RCC_WWDG_CLK_ENABLE().
\r
44 (+) Set the WWDG prescaler, refresh window and counter value
\r
45 using HAL_WWDG_Init() function.
\r
46 (+) Start the WWDG using HAL_WWDG_Start() function.
\r
47 When the WWDG is enabled the counter value should be configured to
\r
48 a value greater than 0x40 to prevent generating an immediate reset.
\r
49 (+) Optionally you can enable the Early Wakeup Interrupt (EWI) which is
\r
50 generated when the counter reaches 0x40, and then start the WWDG using
\r
51 HAL_WWDG_Start_IT(). At EWI HAL_WWDG_WakeupCallback is executed and user can
\r
52 add his own code by customization of function pointer HAL_WWDG_WakeupCallback
\r
53 Once enabled, EWI interrupt cannot be disabled except by a system reset.
\r
54 (+) Then the application program must refresh the WWDG counter at regular
\r
55 intervals during normal operation to prevent an MCU reset, using
\r
56 HAL_WWDG_Refresh() function. This operation must occur only when
\r
57 the counter is lower than the refresh window value already programmed.
\r
59 *** WWDG HAL driver macros list ***
\r
60 ==================================
\r
62 Below the list of most used macros in WWDG HAL driver.
\r
64 (+) __HAL_WWDG_ENABLE: Enable the WWDG peripheral
\r
65 (+) __HAL_WWDG_GET_FLAG: Get the selected WWDG's flag status
\r
66 (+) __HAL_WWDG_CLEAR_FLAG: Clear the WWDG's pending flags
\r
67 (+) __HAL_WWDG_ENABLE_IT: Enables the WWDG early wake-up interrupt
\r
70 ******************************************************************************
\r
73 * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
\r
75 * Redistribution and use in source and binary forms, with or without modification,
\r
76 * are permitted provided that the following conditions are met:
\r
77 * 1. Redistributions of source code must retain the above copyright notice,
\r
78 * this list of conditions and the following disclaimer.
\r
79 * 2. Redistributions in binary form must reproduce the above copyright notice,
\r
80 * this list of conditions and the following disclaimer in the documentation
\r
81 * and/or other materials provided with the distribution.
\r
82 * 3. Neither the name of STMicroelectronics nor the names of its contributors
\r
83 * may be used to endorse or promote products derived from this software
\r
84 * without specific prior written permission.
\r
86 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
\r
87 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
\r
88 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
\r
89 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
\r
90 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
\r
91 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
\r
92 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
\r
93 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
\r
94 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
\r
95 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
97 ******************************************************************************
\r
100 /* Includes ------------------------------------------------------------------*/
\r
101 #include "stm32f7xx_hal.h"
\r
103 /** @addtogroup STM32F7xx_HAL_Driver
\r
107 /** @defgroup WWDG WWDG
\r
108 * @brief WWDG HAL module driver.
\r
112 #ifdef HAL_WWDG_MODULE_ENABLED
\r
114 /* Private typedef -----------------------------------------------------------*/
\r
115 /* Private define ------------------------------------------------------------*/
\r
116 /* Private macro -------------------------------------------------------------*/
\r
117 /* Private variables ---------------------------------------------------------*/
\r
118 /* Private function prototypes -----------------------------------------------*/
\r
119 /* Exported functions --------------------------------------------------------*/
\r
120 /** @defgroup WWDG_Exported_Functions WWDG Exported Functions
\r
124 /** @defgroup WWDG_Exported_Functions_Group1 Initialization and de-initialization functions
\r
125 * @brief Initialization and Configuration functions.
\r
128 ==============================================================================
\r
129 ##### Initialization and de-initialization functions #####
\r
130 ==============================================================================
\r
132 This section provides functions allowing to:
\r
133 (+) Initialize the WWDG according to the specified parameters
\r
134 in the WWDG_InitTypeDef and create the associated handle
\r
135 (+) DeInitialize the WWDG peripheral
\r
136 (+) Initialize the WWDG MSP
\r
137 (+) DeInitialize the WWDG MSP
\r
144 * @brief Initializes the WWDG according to the specified
\r
145 * parameters in the WWDG_InitTypeDef and creates the associated handle.
\r
146 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
\r
147 * the configuration information for the specified WWDG module.
\r
148 * @retval HAL status
\r
150 HAL_StatusTypeDef HAL_WWDG_Init(WWDG_HandleTypeDef *hwwdg)
\r
152 /* Check the WWDG handle allocation */
\r
158 /* Check the parameters */
\r
159 assert_param(IS_WWDG_ALL_INSTANCE(hwwdg->Instance));
\r
160 assert_param(IS_WWDG_PRESCALER(hwwdg->Init.Prescaler));
\r
161 assert_param(IS_WWDG_WINDOW(hwwdg->Init.Window));
\r
162 assert_param(IS_WWDG_COUNTER(hwwdg->Init.Counter));
\r
164 if(hwwdg->State == HAL_WWDG_STATE_RESET)
\r
166 /* Init the low level hardware */
\r
167 HAL_WWDG_MspInit(hwwdg);
\r
170 /* Change WWDG peripheral state */
\r
171 hwwdg->State = HAL_WWDG_STATE_BUSY;
\r
173 /* Set WWDG Prescaler and Window */
\r
174 MODIFY_REG(hwwdg->Instance->CFR, (WWDG_CFR_WDGTB | WWDG_CFR_W), (hwwdg->Init.Prescaler | hwwdg->Init.Window));
\r
175 /* Set WWDG Counter */
\r
176 MODIFY_REG(hwwdg->Instance->CR, WWDG_CR_T, hwwdg->Init.Counter);
\r
178 /* Change WWDG peripheral state */
\r
179 hwwdg->State = HAL_WWDG_STATE_READY;
\r
181 /* Return function status */
\r
186 * @brief DeInitializes the WWDG peripheral.
\r
187 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
\r
188 * the configuration information for the specified WWDG module.
\r
189 * @retval HAL status
\r
191 HAL_StatusTypeDef HAL_WWDG_DeInit(WWDG_HandleTypeDef *hwwdg)
\r
193 /* Check the WWDG handle allocation */
\r
199 /* Check the parameters */
\r
200 assert_param(IS_WWDG_ALL_INSTANCE(hwwdg->Instance));
\r
202 /* Change WWDG peripheral state */
\r
203 hwwdg->State = HAL_WWDG_STATE_BUSY;
\r
205 /* DeInit the low level hardware */
\r
206 HAL_WWDG_MspDeInit(hwwdg);
\r
208 /* Reset WWDG Control register */
\r
209 hwwdg->Instance->CR = (uint32_t)0x0000007F;
\r
211 /* Reset WWDG Configuration register */
\r
212 hwwdg->Instance->CFR = (uint32_t)0x0000007F;
\r
214 /* Reset WWDG Status register */
\r
215 hwwdg->Instance->SR = 0;
\r
217 /* Change WWDG peripheral state */
\r
218 hwwdg->State = HAL_WWDG_STATE_RESET;
\r
221 __HAL_UNLOCK(hwwdg);
\r
223 /* Return function status */
\r
228 * @brief Initializes the WWDG MSP.
\r
229 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
\r
230 * the configuration information for the specified WWDG module.
\r
233 __weak void HAL_WWDG_MspInit(WWDG_HandleTypeDef *hwwdg)
\r
235 /* NOTE: This function Should not be modified, when the callback is needed,
\r
236 the HAL_WWDG_MspInit could be implemented in the user file
\r
241 * @brief DeInitializes the WWDG MSP.
\r
242 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
\r
243 * the configuration information for the specified WWDG module.
\r
246 __weak void HAL_WWDG_MspDeInit(WWDG_HandleTypeDef *hwwdg)
\r
248 /* NOTE: This function Should not be modified, when the callback is needed,
\r
249 the HAL_WWDG_MspDeInit could be implemented in the user file
\r
257 /** @defgroup WWDG_Exported_Functions_Group2 IO operation functions
\r
258 * @brief IO operation functions
\r
261 ==============================================================================
\r
262 ##### IO operation functions #####
\r
263 ==============================================================================
\r
265 This section provides functions allowing to:
\r
266 (+) Start the WWDG.
\r
267 (+) Refresh the WWDG.
\r
268 (+) Handle WWDG interrupt request.
\r
275 * @brief Starts the WWDG.
\r
276 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
\r
277 * the configuration information for the specified WWDG module.
\r
278 * @retval HAL status
\r
280 HAL_StatusTypeDef HAL_WWDG_Start(WWDG_HandleTypeDef *hwwdg)
\r
282 /* Process Locked */
\r
283 __HAL_LOCK(hwwdg);
\r
285 /* Change WWDG peripheral state */
\r
286 hwwdg->State = HAL_WWDG_STATE_BUSY;
\r
288 /* Enable the peripheral */
\r
289 __HAL_WWDG_ENABLE(hwwdg);
\r
291 /* Change WWDG peripheral state */
\r
292 hwwdg->State = HAL_WWDG_STATE_READY;
\r
294 /* Process Unlocked */
\r
295 __HAL_UNLOCK(hwwdg);
\r
297 /* Return function status */
\r
302 * @brief Starts the WWDG with interrupt enabled.
\r
303 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
\r
304 * the configuration information for the specified WWDG module.
\r
305 * @retval HAL status
\r
307 HAL_StatusTypeDef HAL_WWDG_Start_IT(WWDG_HandleTypeDef *hwwdg)
\r
309 /* Process Locked */
\r
310 __HAL_LOCK(hwwdg);
\r
312 /* Change WWDG peripheral state */
\r
313 hwwdg->State = HAL_WWDG_STATE_BUSY;
\r
315 /* Enable the Early Wakeup Interrupt */
\r
316 __HAL_WWDG_ENABLE_IT(hwwdg, WWDG_IT_EWI);
\r
318 /* Enable the peripheral */
\r
319 __HAL_WWDG_ENABLE(hwwdg);
\r
321 /* Return function status */
\r
326 * @brief Refreshes the WWDG.
\r
327 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
\r
328 * the configuration information for the specified WWDG module.
\r
329 * @param Counter: value of counter to put in WWDG counter
\r
330 * @retval HAL status
\r
332 HAL_StatusTypeDef HAL_WWDG_Refresh(WWDG_HandleTypeDef *hwwdg, uint32_t Counter)
\r
334 /* Process Locked */
\r
335 __HAL_LOCK(hwwdg);
\r
337 /* Change WWDG peripheral state */
\r
338 hwwdg->State = HAL_WWDG_STATE_BUSY;
\r
340 /* Check the parameters */
\r
341 assert_param(IS_WWDG_COUNTER(Counter));
\r
343 /* Write to WWDG CR the WWDG Counter value to refresh with */
\r
344 MODIFY_REG(hwwdg->Instance->CR, (uint32_t)WWDG_CR_T, Counter);
\r
346 /* Change WWDG peripheral state */
\r
347 hwwdg->State = HAL_WWDG_STATE_READY;
\r
349 /* Process Unlocked */
\r
350 __HAL_UNLOCK(hwwdg);
\r
352 /* Return function status */
\r
357 * @brief Handles WWDG interrupt request.
\r
358 * @note The Early Wakeup Interrupt (EWI) can be used if specific safety operations
\r
359 * or data logging must be performed before the actual reset is generated.
\r
360 * The EWI interrupt is enabled using __HAL_WWDG_ENABLE_IT() macro.
\r
361 * When the downcounter reaches the value 0x40, and EWI interrupt is
\r
362 * generated and the corresponding Interrupt Service Routine (ISR) can
\r
363 * be used to trigger specific actions (such as communications or data
\r
364 * logging), before resetting the device.
\r
365 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
\r
366 * the configuration information for the specified WWDG module.
\r
369 void HAL_WWDG_IRQHandler(WWDG_HandleTypeDef *hwwdg)
\r
371 /* Check if Early Wakeup Interrupt is enable */
\r
372 if(__HAL_WWDG_GET_IT_SOURCE(hwwdg, WWDG_IT_EWI) != RESET)
\r
374 /* Check if WWDG Early Wakeup Interrupt occurred */
\r
375 if(__HAL_WWDG_GET_FLAG(hwwdg, WWDG_FLAG_EWIF) != RESET)
\r
377 /* Early Wakeup callback */
\r
378 HAL_WWDG_WakeupCallback(hwwdg);
\r
380 /* Change WWDG peripheral state */
\r
381 hwwdg->State = HAL_WWDG_STATE_READY;
\r
383 /* Clear the WWDG Early Wakeup flag */
\r
384 __HAL_WWDG_CLEAR_FLAG(hwwdg, WWDG_FLAG_EWIF);
\r
386 /* Process Unlocked */
\r
387 __HAL_UNLOCK(hwwdg);
\r
393 * @brief Early Wakeup WWDG callback.
\r
394 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
\r
395 * the configuration information for the specified WWDG module.
\r
398 __weak void HAL_WWDG_WakeupCallback(WWDG_HandleTypeDef* hwwdg)
\r
400 /* NOTE: This function Should not be modified, when the callback is needed,
\r
401 the HAL_WWDG_WakeupCallback could be implemented in the user file
\r
409 /** @defgroup WWDG_Exported_Functions_Group3 Peripheral State functions
\r
410 * @brief Peripheral State functions.
\r
413 ==============================================================================
\r
414 ##### Peripheral State functions #####
\r
415 ==============================================================================
\r
417 This subsection permits to get in run-time the status of the peripheral
\r
425 * @brief Returns the WWDG state.
\r
426 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
\r
427 * the configuration information for the specified WWDG module.
\r
428 * @retval HAL state
\r
430 HAL_WWDG_StateTypeDef HAL_WWDG_GetState(WWDG_HandleTypeDef *hwwdg)
\r
432 return hwwdg->State;
\r
443 #endif /* HAL_WWDG_MODULE_ENABLED */
\r
452 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
\r