2 ******************************************************************************
3 * @file stm32h7xx_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 the Low-Speed Internal clock (LSI) and thus stays
20 active even if the main clock fails.
22 (+) Once the IWDG is started, the LSI is forced ON and both cannot 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 into the counter and the watchdog reset
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 the CPU 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_IWDG1() or __HAL_DBGMCU_FREEZE2_IWDG2() and
40 __HAL_DBGMCU_UnFreeze_IWDG1 or __HAL_DBGMCU_UnFreeze2_IWDG2() macros.
42 [..] Min-max timeout value @32KHz (LSI): ~125us / ~32.7s
43 The IWDG timeout may vary due to LSI clock frequency dispersion.
44 STM32H7xx devices provide the capability to measure the LSI clock
45 frequency (LSI clock is internally connected to TIM16 CH1 input capture).
46 The measured value can be used to have an IWDG timeout with an
49 [..] Default timeout value (necessary for IWDG_SR status register update):
50 Constant LSI_VALUE is defined based on the nominal LSI clock frequency.
51 This frequency being subject to variations as mentioned above, the
52 default timeout value (defined through constant HAL_IWDG_DEFAULT_TIMEOUT
53 below) may become too short or too long.
54 In such cases, this default timeout value can be tuned by redefining
55 the constant LSI_VALUE at user-application level (based, for instance,
56 on the measured LSI clock frequency as explained above).
58 ##### How to use this driver #####
59 ==============================================================================
61 (#) Use IWDG using HAL_IWDG_Init() function to :
62 (++) Enable instance by writing Start keyword in IWDG_KEY register. LSI
63 clock is forced ON and IWDG counter starts counting down.
64 (++) Enable write access to configuration registers:
65 IWDG_PR, IWDG_RLR and IWDG_WINR.
66 (++) Configure the IWDG prescaler and counter reload value. This reload
67 value will be loaded in the IWDG counter each time the watchdog is
68 reloaded, then the IWDG will start counting down from this value.
69 (++) Wait for status flags to be reset.
70 (++) Depending on window parameter:
71 (+++) If Window Init parameter is same as Window register value,
72 nothing more is done but reload counter value in order to exit
73 function with exact time base.
74 (+++) Else modify Window register. This will automatically reload
77 (#) Then the application program must refresh the IWDG counter at regular
78 intervals during normal operation to prevent an MCU reset, using
79 HAL_IWDG_Refresh() function.
81 *** IWDG HAL driver macros list ***
82 ====================================
84 Below the list of most used macros in IWDG HAL driver:
85 (+) __HAL_IWDG_START: Enable the IWDG peripheral
86 (+) __HAL_IWDG_RELOAD_COUNTER: Reloads IWDG counter with value defined in
90 ******************************************************************************
93 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
94 * All rights reserved.</center></h2>
96 * This software component is licensed by ST under BSD 3-Clause license,
97 * the "License"; You may not use this file except in compliance with the
98 * License. You may obtain a copy of the License at:
99 * opensource.org/licenses/BSD-3-Clause
101 ******************************************************************************
104 /* Includes ------------------------------------------------------------------*/
105 #include "stm32h7xx_hal.h"
107 /** @addtogroup STM32H7xx_HAL_Driver
111 #ifdef HAL_IWDG_MODULE_ENABLED
113 * @brief IWDG HAL module driver.
117 /* Private typedef -----------------------------------------------------------*/
118 /* Private define ------------------------------------------------------------*/
119 /** @defgroup IWDG_Private_Defines IWDG Private Defines
122 /* Status register needs up to 5 LSI clock periods divided by the clock
123 prescaler to be updated. The number of LSI clock periods is upper-rounded to
124 6 for the timeout value calculation.
125 The timeout value is also calculated using the highest prescaler (256) and
126 the LSI_VALUE constant. The value of this constant can be changed by the user
127 to take into account possible LSI clock period variations.
128 The timeout value is multiplied by 1000 to be converted in milliseconds. */
129 #define HAL_IWDG_DEFAULT_TIMEOUT ((6UL * 256UL * 1000UL) / LSI_VALUE)
134 /* Private macro -------------------------------------------------------------*/
135 /* Private variables ---------------------------------------------------------*/
136 /* Private function prototypes -----------------------------------------------*/
137 /* Exported functions --------------------------------------------------------*/
139 /** @addtogroup IWDG_Exported_Functions
143 /** @addtogroup IWDG_Exported_Functions_Group1
144 * @brief Initialization and Start functions.
147 ===============================================================================
148 ##### Initialization and Start functions #####
149 ===============================================================================
150 [..] This section provides functions allowing to:
151 (+) Initialize the IWDG according to the specified parameters in the
152 IWDG_InitTypeDef of associated handle.
153 (+) Manage Window option.
154 (+) Once initialization is performed in HAL_IWDG_Init function, Watchdog
155 is reloaded in order to exit function with correct time base.
162 * @brief Initialize the IWDG according to the specified parameters in the
163 * IWDG_InitTypeDef and start watchdog. Before exiting function,
164 * watchdog is refreshed in order to have correct time base.
165 * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
166 * the configuration information for the specified IWDG module.
169 HAL_StatusTypeDef
HAL_IWDG_Init(IWDG_HandleTypeDef
*hiwdg
)
173 /* Check the IWDG handle allocation */
179 /* Check the parameters */
180 assert_param(IS_IWDG_ALL_INSTANCE(hiwdg
->Instance
));
181 assert_param(IS_IWDG_PRESCALER(hiwdg
->Init
.Prescaler
));
182 assert_param(IS_IWDG_RELOAD(hiwdg
->Init
.Reload
));
183 assert_param(IS_IWDG_WINDOW(hiwdg
->Init
.Window
));
185 /* Enable IWDG. LSI is turned on automatically */
186 __HAL_IWDG_START(hiwdg
);
188 /* Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers by writing
190 IWDG_ENABLE_WRITE_ACCESS(hiwdg
);
192 /* Write to IWDG registers the Prescaler & Reload values to work with */
193 hiwdg
->Instance
->PR
= hiwdg
->Init
.Prescaler
;
194 hiwdg
->Instance
->RLR
= hiwdg
->Init
.Reload
;
196 /* Check pending flag, if previous update not done, return timeout */
197 tickstart
= HAL_GetTick();
199 /* Wait for register to be updated */
200 while (hiwdg
->Instance
->SR
!= 0x00u
)
202 if ((HAL_GetTick() - tickstart
) > HAL_IWDG_DEFAULT_TIMEOUT
)
208 /* If window parameter is different than current value, modify window
210 if (hiwdg
->Instance
->WINR
!= hiwdg
->Init
.Window
)
212 /* Write to IWDG WINR the IWDG_Window value to compare with. In any case,
213 even if window feature is disabled, Watchdog will be reloaded by writing
215 hiwdg
->Instance
->WINR
= hiwdg
->Init
.Window
;
219 /* Reload IWDG counter with value defined in the reload register */
220 __HAL_IWDG_RELOAD_COUNTER(hiwdg
);
223 /* Return function status */
232 /** @addtogroup IWDG_Exported_Functions_Group2
233 * @brief IO operation functions
236 ===============================================================================
237 ##### IO operation functions #####
238 ===============================================================================
239 [..] This section provides functions allowing to:
240 (+) Refresh the IWDG.
248 * @brief Refresh the IWDG.
249 * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
250 * the configuration information for the specified IWDG module.
253 HAL_StatusTypeDef
HAL_IWDG_Refresh(IWDG_HandleTypeDef
*hiwdg
)
255 /* Reload IWDG counter with value defined in the reload register */
256 __HAL_IWDG_RELOAD_COUNTER(hiwdg
);
258 /* Return function status */
270 #endif /* HAL_IWDG_MODULE_ENABLED */
279 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/