2 ******************************************************************************
3 * @file stm32f7xx_hal_iwdg.c
4 * @author MCD Application Team
5 * @brief IWDG HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the Independent Watchdog (IWDG) peripheral:
8 * + Initialization and Start functions
9 * + IO operation functions
12 ==============================================================================
13 ##### IWDG Generic features #####
14 ==============================================================================
16 (+) The IWDG can be started by either software or hardware (configurable
19 (+) The IWDG is clocked by Low-Speed clock (LSI) and thus stays active even
20 if the main clock fails.
22 (+) Once the IWDG is started, the LSI is forced ON and both can not be
23 disabled. The counter starts counting down from the reset value (0xFFF).
24 When it reaches the end of count value (0x000) a reset signal is
25 generated (IWDG reset).
27 (+) Whenever the key value 0x0000 AAAA is written in the IWDG_KR register,
28 the IWDG_RLR value is reloaded in the counter and the watchdog reset is
31 (+) The IWDG is implemented in the VDD voltage domain that is still functional
32 in STOP and STANDBY mode (IWDG reset can wake-up from STANDBY).
33 IWDGRST flag in RCC_CSR register can be used to inform when an IWDG
36 (+) Debug mode : When the microcontroller enters debug mode (core halted),
37 the IWDG counter either continues to work normally or stops, depending
38 on DBG_IWDG_STOP configuration bit in DBG module, accessible through
39 __HAL_DBGMCU_FREEZE_IWDG() and __HAL_DBGMCU_UNFREEZE_IWDG() macros.
41 [..] Min-max timeout value @32KHz (LSI): ~125us / ~32.7s
42 The IWDG timeout may vary due to LSI frequency dispersion. STM32F7xx
43 devices provide the capability to measure the LSI frequency (LSI clock
44 connected internally to TIM16 CH1 input capture). The measured value
45 can be used to have an IWDG timeout with an acceptable accuracy.
47 ##### How to use this driver #####
48 ==============================================================================
50 (#) Use IWDG using HAL_IWDG_Init() function to :
51 (+) Enable instance by writing Start keyword in IWDG_KEY register. LSI
52 clock is forced ON and IWDG counter starts counting down.
53 (+) Enable write access to configuration registers:
54 IWDG_PR, IWDG_RLR and IWDG_WINR.
55 (+) Configure the IWDG prescaler and counter reload value. This reload
56 value will be loaded in the IWDG counter each time the watchdog is
57 reloaded, then the IWDG will start counting down from this value.
58 (+) Wait for status flags to be reset.
59 (+) Depending on window parameter:
60 (++) If Window Init parameter is same as Window register value,
61 nothing more is done but reload counter value in order to exit
62 function with exact time base.
63 (++) Else modify Window register. This will automatically reload
66 (#) Then the application program must refresh the IWDG counter at regular
67 intervals during normal operation to prevent an MCU reset, using
68 HAL_IWDG_Refresh() function.
70 *** IWDG HAL driver macros list ***
71 ====================================
73 Below the list of most used macros in IWDG HAL driver:
74 (+) __HAL_IWDG_START: Enable the IWDG peripheral
75 (+) __HAL_IWDG_RELOAD_COUNTER: Reloads IWDG counter with value defined in
79 ******************************************************************************
82 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
83 * All rights reserved.</center></h2>
85 * This software component is licensed by ST under BSD 3-Clause license,
86 * the "License"; You may not use this file except in compliance with the
87 * License. You may obtain a copy of the License at:
88 * opensource.org/licenses/BSD-3-Clause
90 ******************************************************************************
93 /* Includes ------------------------------------------------------------------*/
94 #include "stm32f7xx_hal.h"
96 /** @addtogroup STM32F7xx_HAL_Driver
100 #ifdef HAL_IWDG_MODULE_ENABLED
102 * @brief IWDG HAL module driver.
106 /* Private typedef -----------------------------------------------------------*/
107 /* Private define ------------------------------------------------------------*/
108 /** @defgroup IWDG_Private_Defines IWDG Private Defines
111 /* Status register need 5 RC LSI divided by prescaler clock to be updated. With
112 higher prescaler (256), and according to LSI variation, we need to wait at
113 least 6 cycles so 48 ms. */
114 #define HAL_IWDG_DEFAULT_TIMEOUT 48u
119 /* Private macro -------------------------------------------------------------*/
120 /* Private variables ---------------------------------------------------------*/
121 /* Private function prototypes -----------------------------------------------*/
122 /* Exported functions --------------------------------------------------------*/
124 /** @addtogroup IWDG_Exported_Functions
128 /** @addtogroup IWDG_Exported_Functions_Group1
129 * @brief Initialization and Start functions.
132 ===============================================================================
133 ##### Initialization and Start functions #####
134 ===============================================================================
135 [..] This section provides functions allowing to:
136 (+) Initialize the IWDG according to the specified parameters in the
137 IWDG_InitTypeDef of associated handle.
138 (+) Manage Window option.
139 (+) Once initialization is performed in HAL_IWDG_Init function, Watchdog
140 is reloaded in order to exit function with correct time base.
147 * @brief Initialize the IWDG according to the specified parameters in the
148 * IWDG_InitTypeDef and start watchdog. Before exiting function,
149 * watchdog is refreshed in order to have correct time base.
150 * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
151 * the configuration information for the specified IWDG module.
154 HAL_StatusTypeDef
HAL_IWDG_Init(IWDG_HandleTypeDef
*hiwdg
)
158 /* Check the IWDG handle allocation */
164 /* Check the parameters */
165 assert_param(IS_IWDG_ALL_INSTANCE(hiwdg
->Instance
));
166 assert_param(IS_IWDG_PRESCALER(hiwdg
->Init
.Prescaler
));
167 assert_param(IS_IWDG_RELOAD(hiwdg
->Init
.Reload
));
168 assert_param(IS_IWDG_WINDOW(hiwdg
->Init
.Window
));
170 /* Enable IWDG. LSI is turned on automatically */
171 __HAL_IWDG_START(hiwdg
);
173 /* Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers by writing
175 IWDG_ENABLE_WRITE_ACCESS(hiwdg
);
177 /* Write to IWDG registers the Prescaler & Reload values to work with */
178 hiwdg
->Instance
->PR
= hiwdg
->Init
.Prescaler
;
179 hiwdg
->Instance
->RLR
= hiwdg
->Init
.Reload
;
181 /* Check pending flag, if previous update not done, return timeout */
182 tickstart
= HAL_GetTick();
184 /* Wait for register to be updated */
185 while (hiwdg
->Instance
->SR
!= 0x00u
)
187 if ((HAL_GetTick() - tickstart
) > HAL_IWDG_DEFAULT_TIMEOUT
)
193 /* If window parameter is different than current value, modify window
195 if (hiwdg
->Instance
->WINR
!= hiwdg
->Init
.Window
)
197 /* Write to IWDG WINR the IWDG_Window value to compare with. In any case,
198 even if window feature is disabled, Watchdog will be reloaded by writing
200 hiwdg
->Instance
->WINR
= hiwdg
->Init
.Window
;
204 /* Reload IWDG counter with value defined in the reload register */
205 __HAL_IWDG_RELOAD_COUNTER(hiwdg
);
208 /* Return function status */
217 /** @addtogroup IWDG_Exported_Functions_Group2
218 * @brief IO operation functions
221 ===============================================================================
222 ##### IO operation functions #####
223 ===============================================================================
224 [..] This section provides functions allowing to:
225 (+) Refresh the IWDG.
233 * @brief Refresh the IWDG.
234 * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
235 * the configuration information for the specified IWDG module.
238 HAL_StatusTypeDef
HAL_IWDG_Refresh(IWDG_HandleTypeDef
*hiwdg
)
240 /* Reload IWDG counter with value defined in the reload register */
241 __HAL_IWDG_RELOAD_COUNTER(hiwdg
);
243 /* Return function status */
255 #endif /* HAL_IWDG_MODULE_ENABLED */
264 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/