2 ******************************************************************************
3 * @file stm32l0xx_hal_iwdg.c
4 * @author MCD Application Team
6 * @date 06-February-2015
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): ~0.512ms / ~32.0s
38 The IWDG timeout may vary due to LSI frequency dispersion. STM32L0xx
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
49 (+) Use IWDG using HAL_IWDG_Init() function to :
50 (++) Enable write access to IWDG_PR, IWDG_RLR.
51 (++) Configure the IWDG prescaler, counter reload value.
52 This reload value will be loaded in the IWDG counter each time the counter
53 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 (+) 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) 2015 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 "stm32l0xx_hal.h"
117 /** @addtogroup STM32L0xx_HAL_Driver
122 * @brief IWDG HAL module driver.
126 #ifdef HAL_IWDG_MODULE_ENABLED
129 #define HAL_IWDG_DEFAULT_TIMEOUT (uint32_t)1000
131 /* Local define used to check the SR status register */
132 #define IWDG_SR_FLAGS (IWDG_FLAG_PVU | IWDG_FLAG_RVU | IWDG_FLAG_WVU)
134 /** @addtogroup IWDG_Exported_Functions
138 /** @addtogroup IWDG_Exported_Functions_Group1
139 * @brief Initialization and Configuration functions.
142 ===============================================================================
143 ##### Initialization and de-initialization functions #####
144 ===============================================================================
145 [..] This section provides functions allowing to:
146 (+) Initialize the IWDG according to the specified parameters
147 in the IWDG_InitTypeDef and create the associated handle
148 (+) Manage Window option
149 (+) Initialize the IWDG MSP
156 * @brief Initializes the IWDG according to the specified
157 * parameters in the IWDG_InitTypeDef and creates the associated handle.
159 * When using the 'window option', the function HAL_IWDG_Start() must
160 * be called before calling this function
162 * @param hiwdg : pointer to a IWDG_HandleTypeDef structure that contains
163 * 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 */
177 /* Check the parameters */
178 assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler));
179 assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload));
180 assert_param(IS_IWDG_WINDOW(hiwdg->Init.Window));
182 /* Check pending flag, if previous update not done, return error */
183 if(((hiwdg->Instance->SR) & IWDG_SR_FLAGS) != 0)
188 if(hiwdg->State == HAL_IWDG_STATE_RESET)
190 /* Init the low level hardware */
191 HAL_IWDG_MspInit(hiwdg);
194 /* Change IWDG peripheral state */
195 hiwdg->State = HAL_IWDG_STATE_BUSY;
197 /* Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers */
198 /* by writing 0x5555 in KR */
199 IWDG_ENABLE_WRITE_ACCESS(hiwdg);
201 /* Write to IWDG registers the IWDG_Prescaler & IWDG_Reload values to work with */
202 MODIFY_REG(hiwdg->Instance->PR, (uint32_t)IWDG_PR_PR, hiwdg->Init.Prescaler);
203 MODIFY_REG(hiwdg->Instance->RLR, (uint32_t)IWDG_RLR_RL, hiwdg->Init.Reload);
205 /* check if window option is enabled */
206 if (((hiwdg->Init.Window) != IWDG_WINDOW_DISABLE) || ((hiwdg->Instance->WINR) != IWDG_WINDOW_DISABLE))
208 tickstart = HAL_GetTick();
210 /* Wait for register to be updated */
211 while (((hiwdg->Instance->SR) & IWDG_SR_FLAGS) != 0)
213 if((HAL_GetTick()-tickstart) > HAL_IWDG_DEFAULT_TIMEOUT)
216 hiwdg->State = HAL_IWDG_STATE_TIMEOUT;
221 /* Write to IWDG WINR the IWDG_Window value to compare with */
222 MODIFY_REG(hiwdg->Instance->WINR, (uint32_t)IWDG_WINR_WIN, hiwdg->Init.Window);
225 /* Change IWDG peripheral state */
226 hiwdg->State = HAL_IWDG_STATE_READY;
228 /* Return function status */
233 * @brief Initializes the IWDG MSP.
234 * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
235 * the configuration information for the specified IWDG module.
238 __weak void HAL_IWDG_MspInit(IWDG_HandleTypeDef *hiwdg)
240 /* NOTE : This function Should not be modified, when the callback is needed,
241 the HAL_IWDG_MspInit could be implemented in the user file
249 /** @addtogroup IWDG_Exported_Functions_Group2
250 * @brief IO operation functions
253 ===============================================================================
254 ##### IO operation functions #####
255 ===============================================================================
256 [..] This section provides functions allowing to:
258 (+) Refresh the IWDG.
265 * @brief Starts the IWDG.
266 * @param hiwdg : pointer to a IWDG_HandleTypeDef structure that contains
267 * the configuration information for the specified IWDG module.
270 HAL_StatusTypeDef HAL_IWDG_Start(IWDG_HandleTypeDef *hiwdg)
272 uint32_t tickstart = 0;
277 /* Change IWDG peripheral state */
278 hiwdg->State = HAL_IWDG_STATE_BUSY;
280 /* Enable the IWDG peripheral */
281 __HAL_IWDG_START(hiwdg);
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 tickstart = HAL_GetTick();
291 /* Wait until PVU, RVU, WVU flag are RESET */
292 while (((hiwdg->Instance->SR) & IWDG_SR_FLAGS) != 0)
294 if((HAL_GetTick()-tickstart) > HAL_IWDG_DEFAULT_TIMEOUT)
297 hiwdg->State = HAL_IWDG_STATE_TIMEOUT;
299 /* Process unlocked */
305 /* Change IWDG peripheral state */
306 hiwdg->State = HAL_IWDG_STATE_READY;
308 /* Process Unlocked */
311 /* Return function status */
316 * @brief Refreshes the IWDG.
317 * @param hiwdg : pointer to a IWDG_HandleTypeDef structure that contains
318 * the configuration information for the specified IWDG module.
321 HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
323 uint32_t tickstart = 0;
328 /* Change IWDG peripheral state */
329 hiwdg->State = HAL_IWDG_STATE_BUSY;
331 tickstart = HAL_GetTick();
333 /* Wait until RVU flag is RESET */
334 while(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_RVU) != RESET)
336 if((HAL_GetTick()-tickstart) > HAL_IWDG_DEFAULT_TIMEOUT)
339 hiwdg->State = HAL_IWDG_STATE_TIMEOUT;
341 /* Process unlocked */
348 /* Reload IWDG counter with value defined in the reload register */
349 __HAL_IWDG_RELOAD_COUNTER(hiwdg);
351 /* Change IWDG peripheral state */
352 hiwdg->State = HAL_IWDG_STATE_READY;
354 /* Process Unlocked */
357 /* Return function status */
365 /** @addtogroup IWDG_Exported_Functions_Group3
366 * @brief Peripheral State functions.
369 ===============================================================================
370 ##### Peripheral State functions #####
371 ===============================================================================
373 This subsection permits to get in run-time the status of the peripheral
381 * @brief Returns the IWDG state.
382 * @param hiwdg : pointer to a IWDG_HandleTypeDef structure that contains
383 * the configuration information for the specified IWDG module.
386 HAL_IWDG_StateTypeDef HAL_IWDG_GetState(IWDG_HandleTypeDef *hiwdg)
399 #endif /* HAL_IWDG_MODULE_ENABLED */
408 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/