2 ******************************************************************************
3 * @file stm32f4xx_hal_iwdg.c
4 * @author MCD Application Team
7 * @brief IWDG HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the Independent Watchdog (IWDG) peripheral:
10 * + Initialization and de-initialization functions
11 * + IO operation functions
12 * + Peripheral State functions
15 ==============================================================================
16 ##### IWDG Generic features #####
17 ==============================================================================
19 (+) The IWDG can be started by either software or hardware (configurable
22 (+) The IWDG is clocked by its own dedicated Low-Speed clock (LSI) and
23 thus stays active even if the main clock fails.
24 Once the IWDG is started, the LSI is forced ON and cannot be disabled
25 (LSI cannot be disabled too), and the counter starts counting down from
26 the reset value of 0xFFF. When it reaches the end of count value (0x000)
27 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.
32 (+) The IWDG is implemented in the VDD voltage domain that is still functional
33 in STOP and STANDBY mode (IWDG reset can wake-up from STANDBY).
34 IWDGRST flag in RCC_CSR register can be used to inform when an IWDG
37 (+) Min-max timeout value @32KHz (LSI): ~125us / ~32.7s
38 The IWDG timeout may vary due to LSI frequency dispersion. STM32F4xx
39 devices provide the capability to measure the LSI frequency (LSI clock
40 connected internally to TIM5 CH4 input capture). The measured value
41 can be used to have an IWDG timeout with an acceptable accuracy.
44 ##### How to use this driver #####
45 ==============================================================================
47 If Window option is disabled
48 (+) Use IWDG using HAL_IWDG_Init() function to :
49 (++) Enable write access to IWDG_PR, IWDG_RLR.
50 (++) Configure the IWDG prescaler, counter reload value.
51 This reload value will be loaded in the IWDG counter each time the counter
52 is reloaded, then the IWDG will start counting down from this value.
54 (+) Use IWDG using HAL_IWDG_Start() function to:
55 (++) Reload IWDG counter with value defined in the IWDG_RLR register.
56 (++) Start the IWDG, when the IWDG is used in software mode (no need
57 to enable the LSI, it will be enabled by hardware).
58 (+) Then the application program must refresh the IWDG counter at regular
59 intervals during normal operation to prevent an MCU reset, using
60 HAL_IWDG_Refresh() function.
62 if Window option is enabled:
64 (+) Use IWDG using HAL_IWDG_Start() function to enable IWDG downcounter
65 (+) Use IWDG using HAL_IWDG_Init() function to :
66 (++) Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers.
67 (++) Configure the IWDG prescaler, reload value and window value.
68 (+) Then the application program must refresh the IWDG counter at regular
69 intervals during normal operation to prevent an MCU reset, using
70 HAL_IWDG_Refresh() function.
72 *** IWDG HAL driver macros list ***
73 ====================================
75 Below the list of most used macros in IWDG HAL driver.
77 (+) __HAL_IWDG_START: Enable the IWDG peripheral
78 (+) __HAL_IWDG_RELOAD_COUNTER: Reloads IWDG counter with value defined in the reload register
79 (+) __HAL_IWDG_ENABLE_WRITE_ACCESS : Enable write access to IWDG_PR and IWDG_RLR registers
80 (+) __HAL_IWDG_DISABLE_WRITE_ACCESS : Disable write access to IWDG_PR and IWDG_RLR registers
81 (+) __HAL_IWDG_GET_FLAG: Get the selected IWDG's flag status
84 ******************************************************************************
87 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
89 * Redistribution and use in source and binary forms, with or without modification,
90 * are permitted provided that the following conditions are met:
91 * 1. Redistributions of source code must retain the above copyright notice,
92 * this list of conditions and the following disclaimer.
93 * 2. Redistributions in binary form must reproduce the above copyright notice,
94 * this list of conditions and the following disclaimer in the documentation
95 * and/or other materials provided with the distribution.
96 * 3. Neither the name of STMicroelectronics nor the names of its contributors
97 * may be used to endorse or promote products derived from this software
98 * without specific prior written permission.
100 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
101 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
102 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
103 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
104 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
105 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
106 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
107 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
108 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
109 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
111 ******************************************************************************
114 /* Includes ------------------------------------------------------------------*/
115 #include "stm32f4xx_hal.h"
117 /** @addtogroup STM32F4xx_HAL_Driver
122 * @brief IWDG HAL module driver.
126 #ifdef HAL_IWDG_MODULE_ENABLED
128 /* Private typedef -----------------------------------------------------------*/
129 /* Private define ------------------------------------------------------------*/
130 #define IWDG_TIMEOUT_FLAG ((uint32_t)1000) /* 1 s */
131 /* Private macro -------------------------------------------------------------*/
132 /* Private variables ---------------------------------------------------------*/
133 /* Private function prototypes -----------------------------------------------*/
134 /* Private functions ---------------------------------------------------------*/
136 /** @defgroup IWDG_Private_Functions
140 /** @defgroup IWDG_Group1 Initialization and de-initialization functions
141 * @brief Initialization and Configuration functions.
144 ===============================================================================
145 ##### Initialization and de-initialization functions #####
146 ===============================================================================
147 [..] This section provides functions allowing to:
148 (+) Initialize the IWDG according to the specified parameters
149 in the IWDG_InitTypeDef and create the associated handle
150 (+) Initialize the IWDG MSP
151 (+) DeInitialize IWDG MSP
158 * @brief Initializes the IWDG according to the specified
159 * parameters in the IWDG_InitTypeDef and creates the associated handle.
160 * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
161 * the configuration information for the specified IWDG module.
164 HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
166 /* Check the IWDG handle allocation */
167 if(hiwdg == HAL_NULL)
172 /* Check the parameters */
173 assert_param(IS_IWDG_ALL_INSTANCE(hiwdg->Instance));
174 assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler));
175 assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload));
177 if(hiwdg->State == HAL_IWDG_STATE_RESET)
179 /* Init the low level hardware */
180 HAL_IWDG_MspInit(hiwdg);
183 /* Change IWDG peripheral state */
184 hiwdg->State = HAL_IWDG_STATE_BUSY;
186 /* Enable write access to IWDG_PR and IWDG_RLR registers */
187 __HAL_IWDG_ENABLE_WRITE_ACCESS(hiwdg);
189 /* Write to IWDG registers the IWDG_Prescaler & IWDG_Reload values to work with */
190 MODIFY_REG(hiwdg->Instance->PR, IWDG_PR_PR, hiwdg->Init.Prescaler);
191 MODIFY_REG(hiwdg->Instance->RLR, IWDG_RLR_RL, hiwdg->Init.Reload);
193 /* Change IWDG peripheral state */
194 hiwdg->State = HAL_IWDG_STATE_READY;
196 /* Return function status */
201 * @brief Initializes the IWDG MSP.
202 * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
203 * the configuration information for the specified IWDG module.
206 __weak void HAL_IWDG_MspInit(IWDG_HandleTypeDef *hiwdg)
208 /* NOTE : This function Should not be modified, when the callback is needed,
209 the HAL_IWDG_MspInit could be implemented in the user file
217 /** @defgroup IWDG_Group2 IO operation functions
218 * @brief IO operation functions
221 ===============================================================================
222 ##### IO operation functions #####
223 ===============================================================================
224 [..] This section provides functions allowing to:
226 (+) Refresh the IWDG.
233 * @brief Starts the IWDG.
234 * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
235 * the configuration information for the specified IWDG module.
238 HAL_StatusTypeDef HAL_IWDG_Start(IWDG_HandleTypeDef *hiwdg)
243 /* Change IWDG peripheral state */
244 hiwdg->State = HAL_IWDG_STATE_BUSY;
246 /* Start the IWDG peripheral */
247 __HAL_IWDG_START(hiwdg);
249 /* Reload IWDG counter with value defined in the RLR register */
250 __HAL_IWDG_RELOAD_COUNTER(hiwdg);
252 /* Change IWDG peripheral state */
253 hiwdg->State = HAL_IWDG_STATE_READY;
255 /* Process Unlocked */
258 /* Return function status */
263 * @brief Refreshes the IWDG.
264 * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
265 * the configuration information for the specified IWDG module.
268 HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
270 uint32_t tickstart = 0;
275 /* Change IWDG peripheral state */
276 hiwdg->State = HAL_IWDG_STATE_BUSY;
278 tickstart = HAL_GetTick();
280 /* Wait until RVU flag is RESET */
281 while(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_RVU) != RESET)
283 if((HAL_GetTick() - tickstart ) > IWDG_TIMEOUT_FLAG)
286 hiwdg->State = HAL_IWDG_STATE_TIMEOUT;
288 /* Process unlocked */
295 /* Reload IWDG counter with value defined in the reload register */
296 __HAL_IWDG_RELOAD_COUNTER(hiwdg);
298 /* Change IWDG peripheral state */
299 hiwdg->State = HAL_IWDG_STATE_READY;
301 /* Process Unlocked */
304 /* Return function status */
312 /** @defgroup IWDG_Group3 Peripheral State functions
313 * @brief Peripheral State functions.
316 ===============================================================================
317 ##### Peripheral State functions #####
318 ===============================================================================
320 This subsection permits to get in run-time the status of the peripheral
328 * @brief Returns the IWDG state.
329 * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
330 * the configuration information for the specified IWDG module.
333 HAL_IWDG_StateTypeDef HAL_IWDG_GetState(IWDG_HandleTypeDef *hiwdg)
346 #endif /* HAL_IWDG_MODULE_ENABLED */
355 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/