2 ******************************************************************************
3 * @file stm32l1xx_hal_iwdg.c
4 * @author MCD Application Team
6 * @date 5-September-2014
7 * @brief IWDG HAL module driver.
9 * This file provides firmware functions to manage the following
10 * functionalities of the 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
36 (+) Min-max timeout value @37KHz (LSI): ~108us / ~28.3s
37 The IWDG timeout may vary due to LSI frequency dispersion. STM32L1xx
38 devices provide the capability to measure the LSI frequency (LSI clock
39 connected internally to TIM10 CH1 input capture). The measured value
40 can be used to have an IWDG timeout with an acceptable accuracy.
41 For more information, please refer to the STM32L1xx Reference manual.
43 ##### How to use this driver #####
44 ==============================================================================
46 (+) Set the IWDG prescaler and reload value
47 using HAL_IWDG_Init() function.
48 (+) Use IWDG using HAL_IWDG_Start() function to:
49 (++) Enable write access to IWDG_PR and IWDG_RLR registers.
50 (++) Configure the IWDG prescaler and counter reload values.
51 (++) Reload IWDG counter with value defined in the IWDG_RLR register.
52 (++) Start the IWDG, when the IWDG is used in software mode (no need
53 to enable the LSI, it will be enabled by hardware).
54 (+) Then the application program must refresh the IWDG counter at regular
55 intervals during normal operation to prevent an MCU reset, using
56 HAL_IWDG_Refresh() function.
58 *** IWDG HAL driver macros list ***
59 ====================================
61 Below the list of most used macros in IWDG HAL driver.
63 (+) __HAL_IWDG_START: Enable the IWDG peripheral
64 (+) __HAL_IWDG_RELOAD_COUNTER: Reloads IWDG counter with value defined in the reload register
65 (+) __HAL_IWDG_ENABLE_WRITE_ACCESS : Enable write access to IWDG_PR and IWDG_RLR registers
66 (+) __HAL_IWDG_DISABLE_WRITE_ACCESS : Disable write access to IWDG_PR and IWDG_RLR registers
67 (+) __HAL_IWDG_GET_FLAG: Get the selected IWDG's flag status
68 (+) __HAL_IWDG_CLEAR_FLAG: Clear the IWDG's pending flags
71 ******************************************************************************
74 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
76 * Redistribution and use in source and binary forms, with or without modification,
77 * are permitted provided that the following conditions are met:
78 * 1. Redistributions of source code must retain the above copyright notice,
79 * this list of conditions and the following disclaimer.
80 * 2. Redistributions in binary form must reproduce the above copyright notice,
81 * this list of conditions and the following disclaimer in the documentation
82 * and/or other materials provided with the distribution.
83 * 3. Neither the name of STMicroelectronics nor the names of its contributors
84 * may be used to endorse or promote products derived from this software
85 * without specific prior written permission.
87 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
88 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
89 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
90 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
91 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
92 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
93 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
94 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
95 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
96 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
98 ******************************************************************************
101 /* Includes ------------------------------------------------------------------*/
102 #include "stm32l1xx_hal.h"
104 /** @addtogroup STM32L1xx_HAL_Driver
108 /** @defgroup IWDG IWDG
109 * @brief IWDG HAL module driver.
113 #ifdef HAL_IWDG_MODULE_ENABLED
115 /* Private typedef -----------------------------------------------------------*/
116 /* Private define ------------------------------------------------------------*/
118 /** @defgroup IWDG_Private_Defines IWDG Private Defines
122 #define HAL_IWDG_DEFAULT_TIMEOUT (uint32_t)1000
128 /* Private macro -------------------------------------------------------------*/
129 /* Private variables ---------------------------------------------------------*/
130 /* Private function prototypes -----------------------------------------------*/
131 /* Private functions ---------------------------------------------------------*/
133 /** @defgroup IWDG_Exported_Functions IWDG Exported Functions
137 /** @defgroup IWDG_Exported_Functions_Group1 Initialization and de-initialization functions
138 * @brief Initialization and Configuration functions.
141 ===============================================================================
142 ##### Initialization and de-initialization functions #####
143 ===============================================================================
144 [..] This section provides functions allowing to:
145 (+) Initialize the IWDG according to the specified parameters
146 in the IWDG_InitTypeDef and create the associated handle
147 (+) Initialize the IWDG MSP
148 (+) DeInitialize IWDG MSP
155 * @brief Initializes the IWDG according to the specified
156 * parameters in the IWDG_InitTypeDef and creates the associated handle.
157 * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
158 * the configuration information for the specified IWDG module.
161 HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
163 /* Check the IWDG handle allocation */
164 if(hiwdg == HAL_NULL)
169 /* Check the parameters */
170 assert_param(IS_IWDG_ALL_INSTANCE(hiwdg->Instance));
171 assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler));
172 assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload));
174 /* Check pending flag, if previous update not done, return error */
175 if((__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_PVU) != RESET)
176 &&(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_RVU) != RESET))
181 if(hiwdg->State == HAL_IWDG_STATE_RESET)
183 /* Init the low level hardware */
184 HAL_IWDG_MspInit(hiwdg);
187 /* Change IWDG peripheral state */
188 hiwdg->State = HAL_IWDG_STATE_BUSY;
190 /* Enable write access to IWDG_PR and IWDG_RLR registers */
191 __HAL_IWDG_ENABLE_WRITE_ACCESS(hiwdg);
193 /* Write to IWDG registers the IWDG_Prescaler & IWDG_Reload values to work with */
194 MODIFY_REG(hiwdg->Instance->PR, IWDG_PR_PR, hiwdg->Init.Prescaler);
195 MODIFY_REG(hiwdg->Instance->RLR, IWDG_RLR_RL, hiwdg->Init.Reload);
197 /* Change IWDG peripheral state */
198 hiwdg->State = HAL_IWDG_STATE_READY;
200 /* Return function status */
205 * @brief Initializes the IWDG MSP.
206 * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
207 * the configuration information for the specified IWDG module.
210 __weak void HAL_IWDG_MspInit(IWDG_HandleTypeDef *hiwdg)
212 /* NOTE : This function Should not be modified, when the callback is needed,
213 the HAL_IWDG_MspInit could be implemented in the user file
221 /** @defgroup IWDG_Exported_Functions_Group2 IO operation functions
222 * @brief IO operation functions
225 ===============================================================================
226 ##### IO operation functions #####
227 ===============================================================================
228 [..] This section provides functions allowing to:
230 (+) Refresh the IWDG.
237 * @brief Starts the IWDG.
238 * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
239 * the configuration information for the specified IWDG module.
242 HAL_StatusTypeDef HAL_IWDG_Start(IWDG_HandleTypeDef *hiwdg)
247 /* Change IWDG peripheral state */
248 hiwdg->State = HAL_IWDG_STATE_BUSY;
250 /* Start the IWDG peripheral */
251 __HAL_IWDG_START(hiwdg);
253 /* Reload IWDG counter with value defined in the RLR register */
254 __HAL_IWDG_RELOAD_COUNTER(hiwdg);
256 /* Change IWDG peripheral state */
257 hiwdg->State = HAL_IWDG_STATE_READY;
259 /* Process Unlocked */
262 /* Return function status */
267 * @brief Refreshes the IWDG.
268 * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
269 * the configuration information for the specified IWDG module.
272 HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
274 uint32_t tickstart = 0;
279 /* Change IWDG peripheral state */
280 hiwdg->State = HAL_IWDG_STATE_BUSY;
282 tickstart = HAL_GetTick();
284 /* Wait until RVU flag is RESET */
285 while(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_RVU) != RESET)
287 if((HAL_GetTick() - tickstart ) > HAL_IWDG_DEFAULT_TIMEOUT)
290 hiwdg->State = HAL_IWDG_STATE_TIMEOUT;
292 /* Process unlocked */
299 /* Reload IWDG counter with value defined in the reload register */
300 __HAL_IWDG_RELOAD_COUNTER(hiwdg);
302 /* Change IWDG peripheral state */
303 hiwdg->State = HAL_IWDG_STATE_READY;
305 /* Process Unlocked */
308 /* Return function status */
316 /** @defgroup IWDG_Exported_Functions_Group3 Peripheral State functions
317 * @brief Peripheral State functions.
320 ===============================================================================
321 ##### Peripheral State functions #####
322 ===============================================================================
324 This subsection permits to get in run-time the status of the peripheral
332 * @brief Returns the IWDG state.
333 * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
334 * the configuration information for the specified IWDG module.
337 HAL_IWDG_StateTypeDef HAL_IWDG_GetState(IWDG_HandleTypeDef *hiwdg)
350 #endif /* HAL_IWDG_MODULE_ENABLED */
359 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/