Set blackbox file handler to NULL after closing file
[inav.git] / lib / main / STM32H7 / Drivers / STM32H7xx_HAL_Driver / Src / stm32h7xx_hal_iwdg.c
blob02baf0ac1eca778c2f1aa7c0e394d371fd8cda9d
1 /**
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
11 @verbatim
12 ==============================================================================
13 ##### IWDG Generic features #####
14 ==============================================================================
15 [..]
16 (+) The IWDG can be started by either software or hardware (configurable
17 through option byte).
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
29 prevented.
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
34 reset occurs.
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 frequency dispersion. STM32H7xx
44 devices provide the capability to measure the LSI frequency (LSI clock
45 connected internally to TIM16 CH1 input capture). The measured value
46 can be used to have an IWDG timeout with an acceptable accuracy.
48 ##### How to use this driver #####
49 ==============================================================================
50 [..]
51 (#) Use IWDG using HAL_IWDG_Init() function to :
52 (++) Enable instance by writing Start keyword in IWDG_KEY register. LSI
53 clock is forced ON and IWDG counter starts counting down.
54 (++) Enable write access to configuration registers:
55 IWDG_PR, IWDG_RLR and IWDG_WINR.
56 (++) Configure the IWDG prescaler and counter reload value. This reload
57 value will be loaded in the IWDG counter each time the watchdog is
58 reloaded, then the IWDG will start counting down from this value.
59 (++) Wait for status flags to be reset.
60 (++) Depending on window parameter:
61 (+++) If Window Init parameter is same as Window register value,
62 nothing more is done but reload counter value in order to exit
63 function with exact time base.
64 (+++) Else modify Window register. This will automatically reload
65 watchdog counter.
67 (#) Then the application program must refresh the IWDG counter at regular
68 intervals during normal operation to prevent an MCU reset, using
69 HAL_IWDG_Refresh() function.
71 *** IWDG HAL driver macros list ***
72 ====================================
73 [..]
74 Below the list of most used macros in IWDG HAL driver:
75 (+) __HAL_IWDG_START: Enable the IWDG peripheral
76 (+) __HAL_IWDG_RELOAD_COUNTER: Reloads IWDG counter with value defined in
77 the reload register
79 @endverbatim
80 ******************************************************************************
81 * @attention
83 * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
84 * All rights reserved.</center></h2>
86 * This software component is licensed by ST under BSD 3-Clause license,
87 * the "License"; You may not use this file except in compliance with the
88 * License. You may obtain a copy of the License at:
89 * opensource.org/licenses/BSD-3-Clause
91 ******************************************************************************
94 /* Includes ------------------------------------------------------------------*/
95 #include "stm32h7xx_hal.h"
97 /** @addtogroup STM32H7xx_HAL_Driver
98 * @{
101 #ifdef HAL_IWDG_MODULE_ENABLED
102 /** @addtogroup IWDG
103 * @brief IWDG HAL module driver.
104 * @{
107 /* Private typedef -----------------------------------------------------------*/
108 /* Private define ------------------------------------------------------------*/
109 /** @defgroup IWDG_Private_Defines IWDG Private Defines
110 * @{
112 /* Status register need 5 RC LSI divided by prescaler clock to be updated. With
113 higher prescaler (256), and according to LSI variation, we need to wait at
114 least 6 cycles so 48 ms. */
115 #define HAL_IWDG_DEFAULT_TIMEOUT 48u
117 * @}
120 /* Private macro -------------------------------------------------------------*/
121 /* Private variables ---------------------------------------------------------*/
122 /* Private function prototypes -----------------------------------------------*/
123 /* Exported functions --------------------------------------------------------*/
125 /** @addtogroup IWDG_Exported_Functions
126 * @{
129 /** @addtogroup IWDG_Exported_Functions_Group1
130 * @brief Initialization and Start functions.
132 @verbatim
133 ===============================================================================
134 ##### Initialization and Start functions #####
135 ===============================================================================
136 [..] This section provides functions allowing to:
137 (+) Initialize the IWDG according to the specified parameters in the
138 IWDG_InitTypeDef of associated handle.
139 (+) Manage Window option.
140 (+) Once initialization is performed in HAL_IWDG_Init function, Watchdog
141 is reloaded in order to exit function with correct time base.
143 @endverbatim
144 * @{
148 * @brief Initialize the IWDG according to the specified parameters in the
149 * IWDG_InitTypeDef and start watchdog. Before exiting function,
150 * watchdog is refreshed in order to have correct time base.
151 * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
152 * the configuration information for the specified IWDG module.
153 * @retval HAL status
155 HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
157 uint32_t tickstart;
159 /* Check the IWDG handle allocation */
160 if (hiwdg == NULL)
162 return HAL_ERROR;
165 /* Check the parameters */
166 assert_param(IS_IWDG_ALL_INSTANCE(hiwdg->Instance));
167 assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler));
168 assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload));
169 assert_param(IS_IWDG_WINDOW(hiwdg->Init.Window));
171 /* Enable IWDG. LSI is turned on automatically */
172 __HAL_IWDG_START(hiwdg);
174 /* Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers by writing
175 0x5555 in KR */
176 IWDG_ENABLE_WRITE_ACCESS(hiwdg);
178 /* Write to IWDG registers the Prescaler & Reload values to work with */
179 hiwdg->Instance->PR = hiwdg->Init.Prescaler;
180 hiwdg->Instance->RLR = hiwdg->Init.Reload;
182 /* Check pending flag, if previous update not done, return timeout */
183 tickstart = HAL_GetTick();
185 /* Wait for register to be updated */
186 while (hiwdg->Instance->SR != 0x00u)
188 if ((HAL_GetTick() - tickstart) > HAL_IWDG_DEFAULT_TIMEOUT)
190 return HAL_TIMEOUT;
194 /* If window parameter is different than current value, modify window
195 register */
196 if (hiwdg->Instance->WINR != hiwdg->Init.Window)
198 /* Write to IWDG WINR the IWDG_Window value to compare with. In any case,
199 even if window feature is disabled, Watchdog will be reloaded by writing
200 windows register */
201 hiwdg->Instance->WINR = hiwdg->Init.Window;
203 else
205 /* Reload IWDG counter with value defined in the reload register */
206 __HAL_IWDG_RELOAD_COUNTER(hiwdg);
209 /* Return function status */
210 return HAL_OK;
214 * @}
218 /** @addtogroup IWDG_Exported_Functions_Group2
219 * @brief IO operation functions
221 @verbatim
222 ===============================================================================
223 ##### IO operation functions #####
224 ===============================================================================
225 [..] This section provides functions allowing to:
226 (+) Refresh the IWDG.
228 @endverbatim
229 * @{
234 * @brief Refresh the IWDG.
235 * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
236 * the configuration information for the specified IWDG module.
237 * @retval HAL status
239 HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
241 /* Reload IWDG counter with value defined in the reload register */
242 __HAL_IWDG_RELOAD_COUNTER(hiwdg);
244 /* Return function status */
245 return HAL_OK;
249 * @}
253 * @}
256 #endif /* HAL_IWDG_MODULE_ENABLED */
258 * @}
262 * @}
265 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/