2 ******************************************************************************
3 * @file stm32f7xx_hal_iwdg.c
4 * @author MCD Application Team
7 * @brief IWDG HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the Independent Watchdog (IWDG) peripheral:
10 * + Initialization and Start functions
11 * + IO operation functions
14 ==============================================================================
15 ##### IWDG Generic features #####
16 ==============================================================================
18 (+) The IWDG can be started by either software or hardware (configurable
21 (+) The IWDG is clocked by Low-Speed clock (LSI) and thus stays active even
22 if the main clock fails.
24 (+) Once the IWDG is started, the LSI is forced ON and both can not be
25 disabled. The counter starts counting down from the reset value (0xFFF).
26 When it reaches the end of count value (0x000) a reset signal is
27 generated (IWDG reset).
29 (+) Whenever the key value 0x0000 AAAA is written in the IWDG_KR register,
30 the IWDG_RLR value is reloaded in the counter and the watchdog reset is
33 (+) The IWDG is implemented in the VDD voltage domain that is still functional
34 in STOP and STANDBY mode (IWDG reset can wake-up from STANDBY).
35 IWDGRST flag in RCC_CSR register can be used to inform when an IWDG
38 (+) Debug mode : When the microcontroller enters debug mode (core halted),
39 the IWDG counter either continues to work normally or stops, depending
40 on DBG_IWDG_STOP configuration bit in DBG module, accessible through
41 __HAL_DBGMCU_FREEZE_IWDG() and __HAL_DBGMCU_UNFREEZE_IWDG() macros
43 [..] Min-max timeout value @32KHz (LSI): ~125us / ~32.7s
44 The IWDG timeout may vary due to LSI frequency dispersion. STM32F7xx
45 devices provide the capability to measure the LSI frequency (LSI clock
46 connected internally to TIM16 CH1 input capture). The measured value
47 can be used to have an IWDG timeout with an acceptable accuracy.
49 ##### How to use this driver #####
50 ==============================================================================
52 (#) Use IWDG using HAL_IWDG_Init() function to :
53 (++) Enable instance by writing Start keyword in IWDG_KEY register. LSI
54 clock is forced ON and IWDG counter starts downcounting.
55 (++) Enable write access to configuration register: IWDG_PR, IWDG_RLR &
57 (++) Configure the IWDG prescaler and counter reload value. This reload
58 value will be loaded in the IWDG counter each time the watchdog is
59 reloaded, then the IWDG will start counting down from this value.
60 (++) wait for status flags to be reset
61 (++) Depending on window parameter:
62 (+++) If Window Init parameter is same as Window register value,
63 nothing more is done but reload counter value in order to exit
64 function withy exact time base.
65 (+++) Else modify Window register. This will automatically reload
68 (#) Then the application program must refresh the IWDG counter at regular
69 intervals during normal operation to prevent an MCU reset, using
70 HAL_IWDG_Refresh() function.
72 *** IWDG HAL driver macros list ***
73 ====================================
75 Below the list of most used macros in IWDG HAL driver:
76 (+) __HAL_IWDG_START: Enable the IWDG peripheral
77 (+) __HAL_IWDG_RELOAD_COUNTER: Reloads IWDG counter with value defined in
81 ******************************************************************************
84 * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
86 * Redistribution and use in source and binary forms, with or without modification,
87 * are permitted provided that the following conditions are met:
88 * 1. Redistributions of source code must retain the above copyright notice,
89 * this list of conditions and the following disclaimer.
90 * 2. Redistributions in binary form must reproduce the above copyright notice,
91 * this list of conditions and the following disclaimer in the documentation
92 * and/or other materials provided with the distribution.
93 * 3. Neither the name of STMicroelectronics nor the names of its contributors
94 * may be used to endorse or promote products derived from this software
95 * without specific prior written permission.
97 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
98 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
99 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
100 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
101 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
102 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
103 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
104 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
105 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
106 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
108 ******************************************************************************
111 /* Includes ------------------------------------------------------------------*/
112 #include "stm32f7xx_hal.h"
114 /** @addtogroup STM32F7xx_HAL_Driver
118 #ifdef HAL_IWDG_MODULE_ENABLED
120 * @brief IWDG HAL module driver.
124 /* Private typedef -----------------------------------------------------------*/
125 /* Private define ------------------------------------------------------------*/
126 /** @defgroup IWDG_Private_Defines IWDG Private Defines
129 /* Status register need 5 RC LSI divided by prescaler clock to be updated. With
130 higher prescaler (256), and according to LSI variation, we need to wait at
131 least 6 cycles so 48 ms. */
132 #define HAL_IWDG_DEFAULT_TIMEOUT 48u
137 /* Private macro -------------------------------------------------------------*/
138 /* Private variables ---------------------------------------------------------*/
139 /* Private function prototypes -----------------------------------------------*/
140 /* Exported functions --------------------------------------------------------*/
142 /** @addtogroup IWDG_Exported_Functions
146 /** @addtogroup IWDG_Exported_Functions_Group1
147 * @brief Initialization and Start functions.
150 ===============================================================================
151 ##### Initialization and Start functions #####
152 ===============================================================================
153 [..] This section provides functions allowing to:
154 (+) Initialize the IWDG according to the specified parameters in the
155 IWDG_InitTypeDef of associated handle.
156 (+) Manage Window option.
157 (+) Once initialization is performed in HAL_IWDG_Init function, Watchdog
158 is reloaded in order to exit function with correct time base.
165 * @brief Initialize the IWDG according to the specified parameters in the
166 * IWDG_InitTypeDef and start watchdog. Before exiting function,
167 * watchdog is refreshed in order to have correct time base.
168 * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
169 * the configuration information for the specified IWDG module.
172 HAL_StatusTypeDef
HAL_IWDG_Init(IWDG_HandleTypeDef
*hiwdg
)
176 /* Check the IWDG handle allocation */
182 /* Check the parameters */
183 assert_param(IS_IWDG_ALL_INSTANCE(hiwdg
->Instance
));
184 assert_param(IS_IWDG_PRESCALER(hiwdg
->Init
.Prescaler
));
185 assert_param(IS_IWDG_RELOAD(hiwdg
->Init
.Reload
));
186 assert_param(IS_IWDG_WINDOW(hiwdg
->Init
.Window
));
188 /* Enable IWDG. LSI is turned on automaticaly */
189 __HAL_IWDG_START(hiwdg
);
191 /* Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers by writing
193 IWDG_ENABLE_WRITE_ACCESS(hiwdg
);
195 /* Write to IWDG registers the Prescaler & Reload values to work with */
196 hiwdg
->Instance
->PR
= hiwdg
->Init
.Prescaler
;
197 hiwdg
->Instance
->RLR
= hiwdg
->Init
.Reload
;
199 /* Check pending flag, if previous update not done, return timeout */
200 tickstart
= HAL_GetTick();
202 /* Wait for register to be updated */
203 while(hiwdg
->Instance
->SR
!= RESET
)
205 if((HAL_GetTick() - tickstart
) > HAL_IWDG_DEFAULT_TIMEOUT
)
211 /* If window parameter is different than current value, modify window
213 if(hiwdg
->Instance
->WINR
!= hiwdg
->Init
.Window
)
215 /* Write to IWDG WINR the IWDG_Window value to compare with. In any case,
216 even if window feature is disabled, Watchdog will be reloaded by writing
218 hiwdg
->Instance
->WINR
= hiwdg
->Init
.Window
;
222 /* Reload IWDG counter with value defined in the reload register */
223 __HAL_IWDG_RELOAD_COUNTER(hiwdg
);
226 /* Return function status */
235 /** @addtogroup IWDG_Exported_Functions_Group2
236 * @brief IO operation functions
239 ===============================================================================
240 ##### IO operation functions #####
241 ===============================================================================
242 [..] This section provides functions allowing to:
243 (+) Refresh the IWDG.
251 * @brief Refresh the IWDG.
252 * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
253 * the configuration information for the specified IWDG module.
256 HAL_StatusTypeDef
HAL_IWDG_Refresh(IWDG_HandleTypeDef
*hiwdg
)
258 /* Reload IWDG counter with value defined in the reload register */
259 __HAL_IWDG_RELOAD_COUNTER(hiwdg
);
261 /* Return function status */
273 #endif /* HAL_IWDG_MODULE_ENABLED */
282 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/