Merge pull request #11189 from klutvott123/move-telemetry-displayport-init
[betaflight.git] / lib / main / STM32F3 / Drivers / STM32F3xx_HAL_Driver / Src / stm32f3xx_hal_iwdg.c
blob479f4d9507249f952e6a75549b7e61495d97a9fa
1 /**
2 ******************************************************************************
3 * @file stm32f3xx_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 (0xFFFU).
24 When it reaches the end of count value (0x000U) 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 @41KHz (LSI): ~0.1ms / ~25.5s
42 The IWDG timeout may vary due to LSI frequency dispersion. STM32F3xx
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 downcounting.
53 (++) Enable write access to configuration register: IWDG_PR, IWDG_RLR &
54 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 withy 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) 2016 STMicroelectronics</center></h2>
84 * Redistribution and use in source and binary forms, with or without modification,
85 * are permitted provided that the following conditions are met:
86 * 1. Redistributions of source code must retain the above copyright notice,
87 * this list of conditions and the following disclaimer.
88 * 2. Redistributions in binary form must reproduce the above copyright notice,
89 * this list of conditions and the following disclaimer in the documentation
90 * and/or other materials provided with the distribution.
91 * 3. Neither the name of STMicroelectronics nor the names of its contributors
92 * may be used to endorse or promote products derived from this software
93 * without specific prior written permission.
95 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
96 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
97 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
98 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
99 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
100 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
101 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
102 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
103 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
104 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
106 ******************************************************************************
109 /* Includes ------------------------------------------------------------------*/
110 #include "stm32f3xx_hal.h"
112 /** @addtogroup STM32F3xx_HAL_Driver
113 * @{
116 #ifdef HAL_IWDG_MODULE_ENABLED
117 /** @addtogroup IWDG
118 * @brief IWDG HAL module driver.
119 * @{
122 /* Private typedef -----------------------------------------------------------*/
123 /* Private define ------------------------------------------------------------*/
124 /** @defgroup IWDG_Private_Defines IWDG Private Defines
125 * @{
127 /* Status register need 5 RC LSI divided by prescaler clock to be updated. With
128 higher prescaler (256U), and according to HSI variation, we need to wait at
129 least 6 cycles so 48 ms. */
130 #define HAL_IWDG_DEFAULT_TIMEOUT 48u
132 * @}
135 /* Private macro -------------------------------------------------------------*/
136 /* Private variables ---------------------------------------------------------*/
137 /* Private function prototypes -----------------------------------------------*/
138 /* Exported functions --------------------------------------------------------*/
140 /** @addtogroup IWDG_Exported_Functions
141 * @{
144 /** @addtogroup IWDG_Exported_Functions_Group1
145 * @brief Initialization and Start functions.
147 @verbatim
148 ===============================================================================
149 ##### Initialization and Start functions #####
150 ===============================================================================
151 [..] This section provides functions allowing to:
152 (+) Initialize the IWDG according to the specified parameters in the
153 IWDG_InitTypeDef of associated handle.
154 (+) Manage Window option.
155 (+) Once initialization is performed in HAL_IWDG_Init function, Watchdog
156 is reloaded in order to exit function with correct time base.
158 @endverbatim
159 * @{
163 * @brief Initialize the IWDG according to the specified parameters in the
164 * IWDG_InitTypeDef and start watchdog. Before exiting function,
165 * watchdog is refreshed in order to have correct time base.
166 * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
167 * the configuration information for the specified IWDG module.
168 * @retval HAL status
170 HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
172 uint32_t tickstart;
174 /* Check the IWDG handle allocation */
175 if(hiwdg == NULL)
177 return HAL_ERROR;
180 /* Check the parameters */
181 assert_param(IS_IWDG_ALL_INSTANCE(hiwdg->Instance));
182 assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler));
183 assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload));
184 assert_param(IS_IWDG_WINDOW(hiwdg->Init.Window));
186 /* Enable IWDG. LSI is turned on automaticaly */
187 __HAL_IWDG_START(hiwdg);
189 /* Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers by writing
190 0x5555 in KR */
191 IWDG_ENABLE_WRITE_ACCESS(hiwdg);
193 /* Write to IWDG registers the Prescaler & Reload values to work with */
194 hiwdg->Instance->PR = hiwdg->Init.Prescaler;
195 hiwdg->Instance->RLR = hiwdg->Init.Reload;
197 /* Check pending flag, if previous update not done, return timeout */
198 tickstart = HAL_GetTick();
200 /* Wait for register to be updated */
201 while(hiwdg->Instance->SR != RESET)
203 if((HAL_GetTick() - tickstart ) > HAL_IWDG_DEFAULT_TIMEOUT)
205 return HAL_TIMEOUT;
209 /* If window parameter is different than current value, modify window
210 register */
211 if(hiwdg->Instance->WINR != hiwdg->Init.Window)
213 /* Write to IWDG WINR the IWDG_Window value to compare with. In any case,
214 even if window feature is disabled, Watchdog will be reloaded by writing
215 windows register */
216 hiwdg->Instance->WINR = hiwdg->Init.Window;
218 else
220 /* Reload IWDG counter with value defined in the reload register */
221 __HAL_IWDG_RELOAD_COUNTER(hiwdg);
224 /* Return function status */
225 return HAL_OK;
229 * @}
233 /** @addtogroup IWDG_Exported_Functions_Group2
234 * @brief IO operation functions
236 @verbatim
237 ===============================================================================
238 ##### IO operation functions #####
239 ===============================================================================
240 [..] This section provides functions allowing to:
241 (+) Refresh the IWDG.
243 @endverbatim
244 * @{
249 * @brief Refresh the IWDG.
250 * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
251 * the configuration information for the specified IWDG module.
252 * @retval HAL status
254 HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
256 /* Reload IWDG counter with value defined in the reload register */
257 __HAL_IWDG_RELOAD_COUNTER(hiwdg);
259 /* Return function status */
260 return HAL_OK;
264 * @}
268 * @}
271 #endif /* HAL_IWDG_MODULE_ENABLED */
273 * @}
277 * @}
280 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/