Merge pull request #11189 from klutvott123/move-telemetry-displayport-init
[betaflight.git] / lib / main / STM32G4 / Drivers / STM32G4xx_HAL_Driver / Src / stm32g4xx_hal_iwdg.c
blob76d71a3b10642b805706fd86092ef6323a6227a1
1 /**
2 ******************************************************************************
3 * @file stm32g4xx_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_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. STM32G4xx
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 ==============================================================================
49 [..]
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
64 watchdog counter.
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 ====================================
72 [..]
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
76 the reload register
78 @endverbatim
79 ******************************************************************************
80 * @attention
82 * <h2><center>&copy; 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 "stm32g4xx_hal.h"
96 /** @addtogroup STM32G4xx_HAL_Driver
97 * @{
100 #ifdef HAL_IWDG_MODULE_ENABLED
101 /** @addtogroup IWDG
102 * @brief IWDG HAL module driver.
103 * @{
106 /* Private typedef -----------------------------------------------------------*/
107 /* Private define ------------------------------------------------------------*/
108 /** @defgroup IWDG_Private_Defines IWDG Private Defines
109 * @{
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
116 * @}
119 /* Private macro -------------------------------------------------------------*/
120 /* Private variables ---------------------------------------------------------*/
121 /* Private function prototypes -----------------------------------------------*/
122 /* Exported functions --------------------------------------------------------*/
124 /** @addtogroup IWDG_Exported_Functions
125 * @{
128 /** @addtogroup IWDG_Exported_Functions_Group1
129 * @brief Initialization and Start functions.
131 @verbatim
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.
142 @endverbatim
143 * @{
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.
152 * @retval HAL status
154 HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
156 uint32_t tickstart;
158 /* Check the IWDG handle allocation */
159 if (hiwdg == NULL)
161 return HAL_ERROR;
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
174 0x5555 in KR */
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)
189 return HAL_TIMEOUT;
193 /* If window parameter is different than current value, modify window
194 register */
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
199 windows register */
200 hiwdg->Instance->WINR = hiwdg->Init.Window;
202 else
204 /* Reload IWDG counter with value defined in the reload register */
205 __HAL_IWDG_RELOAD_COUNTER(hiwdg);
208 /* Return function status */
209 return HAL_OK;
213 * @}
217 /** @addtogroup IWDG_Exported_Functions_Group2
218 * @brief IO operation functions
220 @verbatim
221 ===============================================================================
222 ##### IO operation functions #####
223 ===============================================================================
224 [..] This section provides functions allowing to:
225 (+) Refresh the IWDG.
227 @endverbatim
228 * @{
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.
236 * @retval HAL status
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 */
244 return HAL_OK;
248 * @}
252 * @}
255 #endif /* HAL_IWDG_MODULE_ENABLED */
257 * @}
261 * @}
264 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/