2 ******************************************************************************
3 * @file stm32f3xx_hal_iwdg.c
4 * @author MCD Application Team
7 * @brief IWDG HAL module driver.
9 * This file provides firmware functions to manage the following
10 * functionalities of the Independent Watchdog (IWDG) peripheral:
11 * + Initialization and Configuration functions
12 * + IO operation functions
13 * + Peripheral State functions
17 ================================================================================
18 ##### IWDG specific features #####
19 ================================================================================
21 (+) The IWDG can be started by either software or hardware (configurable
23 (+) The IWDG is clocked by its own dedicated Low-Speed clock (LSI) and
24 thus stays active even if the main clock fails.
25 (+) Once the IWDG is started, the LSI is forced ON and cannot be disabled
26 (LSI cannot be disabled too), and the counter starts counting down from
27 the reset value of 0xFFF. When it reaches the end of count value (0x000)
28 a system reset is generated.
29 (+) The IWDG counter should be refreshed at regular intervals, otherwise the
30 watchdog generates an MCU reset when the counter reaches 0.
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
35 (+) Min-max timeout value @41KHz (LSI): ~0.1ms / ~25.5s
36 The IWDG timeout may vary due to LSI frequency dispersion. STM32F30x
37 devices provide the capability to measure the LSI frequency (LSI clock
38 connected internally to TIM16 CH1 input capture). The measured value
39 can be used to have an IWDG timeout with an acceptable accuracy.
40 For more information, please refer to the STM32F3xx Reference manual.
42 ##### How to use this driver #####
43 ==============================================================================
45 (#) if Window option is disabled
46 (+) Use IWDG using HAL_IWDG_Init() function to :
47 (++) Enable write access to IWDG_PR, IWDG_RLR.
48 (++) Configure the IWDG prescaler, counter reload value.
49 This reload value will be loaded in the IWDG counter each time the counter
50 is reloaded, then the IWDG will start counting down from this value.
51 (+) Use IWDG using HAL_IWDG_Start() function to :
52 (++) Reload IWDG counter with value defined in the IWDG_RLR register.
53 (++) Start the IWDG, when the IWDG is used in software mode (no need
54 to enable the LSI, it will be enabled by hardware).
55 (+) Then the application program must refresh the IWDG counter at regular
56 intervals during normal operation to prevent an MCU reset, using
57 HAL_IWDG_Refresh() function.
58 (#) if Window option is enabled:
59 (+) Use IWDG using HAL_IWDG_Start() function to enable IWDG downcounter
60 (+) Use IWDG using HAL_IWDG_Init() function to :
61 (++) Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers.
62 (++) Configure the IWDG prescaler, reload value and window value.
63 (+) Then the application program must refresh the IWDG counter at regular
64 intervals during normal operation to prevent an MCU reset, using
65 HAL_IWDG_Refresh() function.
67 *** IWDG HAL driver macros list ***
68 ====================================
70 Below the list of most used macros in IWDG HAL driver.
72 (+) __HAL_IWDG_START: Enable the IWDG peripheral
73 (+) __HAL_IWDG_RELOAD_COUNTER: Reloads IWDG counter with value defined in the reload register
74 (+) __HAL_IWDG_ENABLE_WRITE_ACCESS : Enable write access to IWDG_PR and IWDG_RLR registers
75 (+) __HAL_IWDG_DISABLE_WRITE_ACCESS : Disable write access to IWDG_PR and IWDG_RLR registers
76 (+) __HAL_IWDG_GET_FLAG: Get the selected IWDG's flag status
79 ******************************************************************************
82 * <h2><center>© COPYRIGHT(c) 2014 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
116 /** @defgroup IWDG IWDG HAL module driver
117 * @brief IWDG HAL module driver.
121 #ifdef HAL_IWDG_MODULE_ENABLED
123 /* Private typedef -----------------------------------------------------------*/
124 /* Private define ------------------------------------------------------------*/
126 /** @defgroup IWDG_Private_Defines IWDG Private Defines
129 #define HAL_IWDG_DEFAULT_TIMEOUT (uint32_t)1000
134 /* Private macro -------------------------------------------------------------*/
135 /* Private variables ---------------------------------------------------------*/
136 /* Private function prototypes -----------------------------------------------*/
137 /* Private functions ---------------------------------------------------------*/
139 /** @defgroup IWDG_Exported_Functions IWDG Exported Functions
143 /** @defgroup IWDG_Exported_Functions_Group1 Initialization and de-initialization functions
144 * @brief Initialization and Configuration functions.
147 ===============================================================================
148 ##### Initialization functions #####
149 ===============================================================================
150 [..] This section provides functions allowing to:
151 (+) Initialize the IWDG according to the specified parameters
152 in the IWDG_InitTypeDef and create the associated handle
153 (+) Manage Window option
154 (+) Initialize the IWDG MSP
161 * @brief Initializes the IWDG according to the specified
162 * parameters in the IWDG_InitTypeDef and creates the associated handle.
163 * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
164 * the configuration information for the specified IWDG module.
167 HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
169 uint32_t tickstart = 0;
171 /* Check the IWDG handle allocation */
172 if(hiwdg == HAL_NULL)
177 /* Check the parameters */
178 assert_param(IS_IWDG_ALL_INSTANCE(hiwdg->Instance));
179 assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler));
180 assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload));
181 assert_param(IS_IWDG_WINDOW(hiwdg->Init.Window));
183 /* Check pending flag, if previous update not done, return error */
184 if((__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_PVU) != RESET)
185 &&(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_RVU) != RESET)
186 &&(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_WVU) != RESET))
191 if(hiwdg->State == HAL_IWDG_STATE_RESET)
193 /* Init the low level hardware */
194 HAL_IWDG_MspInit(hiwdg);
197 /* Change IWDG peripheral state */
198 hiwdg->State = HAL_IWDG_STATE_BUSY;
200 /* Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers */
201 /* by writing 0x5555 in KR */
202 __HAL_IWDG_ENABLE_WRITE_ACCESS(hiwdg);
204 /* Write to IWDG registers the IWDG_Prescaler & IWDG_Reload values to work with */
205 MODIFY_REG(hiwdg->Instance->PR, IWDG_PR_PR, hiwdg->Init.Prescaler);
206 MODIFY_REG(hiwdg->Instance->RLR, IWDG_RLR_RL, hiwdg->Init.Reload);
208 /* check if window option is enabled */
209 if (((hiwdg->Init.Window) != IWDG_WINDOW_DISABLE) || ((hiwdg->Instance->WINR) != IWDG_WINDOW_DISABLE))
211 tickstart = HAL_GetTick();
213 /* Wait for register to be updated */
214 while((uint32_t)(hiwdg->Instance->SR) != RESET)
216 if((HAL_GetTick()-tickstart) > HAL_IWDG_DEFAULT_TIMEOUT)
219 hiwdg->State = HAL_IWDG_STATE_TIMEOUT;
224 /* Write to IWDG WINR the IWDG_Window value to compare with */
225 MODIFY_REG(hiwdg->Instance->WINR, IWDG_WINR_WIN, hiwdg->Init.Window);
228 /* Change IWDG peripheral state */
229 hiwdg->State = HAL_IWDG_STATE_READY;
231 /* Return function status */
236 * @brief Initializes the IWDG MSP.
237 * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
238 * the configuration information for the specified IWDG module.
241 __weak void HAL_IWDG_MspInit(IWDG_HandleTypeDef *hiwdg)
243 /* NOTE : This function Should not be modified, when the callback is needed,
244 the HAL_IWDG_MspInit could be implemented in the user file
252 /** @defgroup IWDG_Exported_Functions_Group2 Input and Output operation functions
253 * @brief IO operation functions
256 ===============================================================================
257 ##### IO operation functions #####
258 ===============================================================================
259 [..] This section provides functions allowing to:
261 (+) Refresh the IWDG.
268 * @brief Starts the IWDG.
269 * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
270 * the configuration information for the specified IWDG module.
273 HAL_StatusTypeDef HAL_IWDG_Start(IWDG_HandleTypeDef *hiwdg)
275 uint32_t tickstart = 0;
280 /* Change IWDG peripheral state */
281 hiwdg->State = HAL_IWDG_STATE_BUSY;
283 /* Reload IWDG counter with value defined in the RLR register */
284 if ((hiwdg->Init.Window) == IWDG_WINDOW_DISABLE)
286 __HAL_IWDG_RELOAD_COUNTER(hiwdg);
289 /* Enable the IWDG peripheral */
290 __HAL_IWDG_START(hiwdg);
292 tickstart = HAL_GetTick();
294 /* Wait until PVU, RVU, WVU flag are RESET */
295 while( (__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_PVU) != RESET)
296 &&(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_RVU) != RESET)
297 &&(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_WVU) != RESET) )
299 if((HAL_GetTick()-tickstart) > HAL_IWDG_DEFAULT_TIMEOUT)
302 hiwdg->State = HAL_IWDG_STATE_TIMEOUT;
304 /* Process unlocked */
311 /* Change IWDG peripheral state */
312 hiwdg->State = HAL_IWDG_STATE_READY;
314 /* Process Unlocked */
317 /* Return function status */
322 * @brief Refreshes the IWDG.
323 * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
324 * the configuration information for the specified IWDG module.
327 HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
329 uint32_t tickstart = 0;
334 /* Change IWDG peripheral state */
335 hiwdg->State = HAL_IWDG_STATE_BUSY;
337 tickstart = HAL_GetTick();
339 /* Wait until RVU flag is RESET */
340 while(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_RVU) != RESET)
342 if((HAL_GetTick()-tickstart) > HAL_IWDG_DEFAULT_TIMEOUT)
345 hiwdg->State = HAL_IWDG_STATE_TIMEOUT;
347 /* Process unlocked */
354 /* Reload IWDG counter with value defined in the reload register */
355 __HAL_IWDG_RELOAD_COUNTER(hiwdg);
357 /* Change IWDG peripheral state */
358 hiwdg->State = HAL_IWDG_STATE_READY;
360 /* Process Unlocked */
363 /* Return function status */
371 /** @defgroup IWDG_Exported_Functions_Group3 Peripheral State functions
372 * @brief Peripheral State functions.
375 ===============================================================================
376 ##### Peripheral State functions #####
377 ===============================================================================
379 This subsection permits to get in run-time the status of the peripheral
387 * @brief Returns the IWDG state.
388 * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
389 * the configuration information for the specified IWDG module.
392 HAL_IWDG_StateTypeDef HAL_IWDG_GetState(IWDG_HandleTypeDef *hiwdg)
405 #endif /* HAL_IWDG_MODULE_ENABLED */
414 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/