2 ******************************************************************************
3 * @file stm32f1xx_hal_iwdg.c
4 * @author MCD Application Team
6 * @date 15-December-2014
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 Configuration functions
11 * + IO operation functions
12 * + Peripheral State functions
14 ================================================================================
15 ##### IWDG specific features #####
16 ================================================================================
18 (+) The IWDG can be started by either software or hardware (configurable
20 (+) The IWDG is clocked by its own dedicated Low-Speed clock (LSI) and
21 thus stays active even if the main clock fails.
22 (+) Once the IWDG is started, the LSI is forced ON and cannot be disabled
23 (LSI cannot be disabled too), and the counter starts counting down from
24 the reset value of 0xFFF. When it reaches the end of count value (0x000)
25 a system reset is generated.
26 (+) The IWDG counter should be refreshed at regular intervals, otherwise the
27 watchdog generates an MCU reset when the counter reaches 0.
28 (+) The IWDG is implemented in the VDD voltage domain that is still functional
29 in STOP and STANDBY mode (IWDG reset can wake-up from STANDBY).
30 (+) IWDGRST flag in RCC_CSR register can be used to inform when an IWDG
33 (+) Min-max timeout value at 40KHz (LSI): 0.1us / 26.2s .
34 The IWDG timeout may vary due to LSI frequency dispersion. STM32F1xx
35 devices provide the capability to measure the LSI frequency (LSI clock
36 connected internally to TIM5 CH4 input capture). The measured value
37 can be used to have an IWDG timeout with an acceptable accuracy.
38 For more information, please refer to the STM32F1xx Reference manual.
39 Note: LSI Calibration is only available on: High density, XL-density and Connectivity line devices.
41 ##### How to use this driver #####
42 ==============================================================================
44 (+) Use IWDG using HAL_IWDG_Init() function to :
45 (++) Enable write access to IWDG_PR, IWDG_RLR.
46 (++) Configure the IWDG prescaler, counter reload value.
47 This reload value will be loaded in the IWDG counter each time the counter
48 is reloaded, then the IWDG will start counting down from this value.
49 (+) Use IWDG using HAL_IWDG_Start() function to :
50 (++) Reload IWDG counter with value defined in the IWDG_RLR register.
51 (++) Start the IWDG, when the IWDG is used in software mode (no need
52 to enable the LSI, it will be enabled by hardware).
53 (+) Then the application program must refresh the IWDG counter at regular
54 intervals during normal operation to prevent an MCU reset, using
55 HAL_IWDG_Refresh() function.
57 *** IWDG HAL driver macros list ***
58 ====================================
60 Below the list of most used macros in IWDG HAL driver.
62 (+) __HAL_IWDG_START: Enable the IWDG peripheral
63 (+) __HAL_IWDG_RELOAD_COUNTER: Reloads IWDG counter with value defined in the reload register
64 (+) __HAL_IWDG_GET_FLAG: Get the selected IWDG's flag status
67 ******************************************************************************
70 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
72 * Redistribution and use in source and binary forms, with or without modification,
73 * are permitted provided that the following conditions are met:
74 * 1. Redistributions of source code must retain the above copyright notice,
75 * this list of conditions and the following disclaimer.
76 * 2. Redistributions in binary form must reproduce the above copyright notice,
77 * this list of conditions and the following disclaimer in the documentation
78 * and/or other materials provided with the distribution.
79 * 3. Neither the name of STMicroelectronics nor the names of its contributors
80 * may be used to endorse or promote products derived from this software
81 * without specific prior written permission.
83 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
84 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
85 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
86 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
87 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
88 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
89 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
90 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
91 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
92 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
94 ******************************************************************************
97 /* Includes ------------------------------------------------------------------*/
98 #include "stm32f1xx_hal.h"
100 /** @addtogroup STM32F1xx_HAL_Driver
104 /** @defgroup IWDG IWDG
105 * @brief IWDG HAL module driver.
109 #ifdef HAL_IWDG_MODULE_ENABLED
111 /* Private typedef -----------------------------------------------------------*/
112 /* Private define ------------------------------------------------------------*/
114 /** @defgroup IWDG_Private_Constants IWDG Private Constants
118 #define IWDG_DEFAULT_TIMEOUT (uint32_t)1000
124 /* Private macro -------------------------------------------------------------*/
125 /* Private variables ---------------------------------------------------------*/
126 /* Private function prototypes -----------------------------------------------*/
127 /* Private functions ---------------------------------------------------------*/
129 /** @defgroup IWDG_Exported_Functions IWDG Exported Functions
133 /** @defgroup IWDG_Exported_Functions_Group1 Initialization and de-initialization functions
134 * @brief Initialization and Configuration functions.
137 ===============================================================================
138 ##### Initialization and de-initialization functions #####
139 ===============================================================================
140 [..] This section provides functions allowing to:
141 (+) Initialize the IWDG according to the specified parameters
142 in the IWDG_InitTypeDef and create the associated handle
143 (+) Initialize the IWDG MSP
144 (+) DeInitialize IWDG MSP
151 * @brief Initializes the IWDG according to the specified
152 * parameters in the IWDG_InitTypeDef and creates the associated handle.
153 * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
154 * the configuration information for the specified IWDG module.
157 HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
159 /* Check the IWDG handle allocation */
165 /* Check the parameters */
166 assert_param(IS_IWDG_ALL_INSTANCE(hiwdg->Instance));
167 assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler));
168 assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload));
170 /* Check pending flag, if previous update not done, return error */
171 if((__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_PVU) != RESET)
172 &&(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_RVU) != RESET))
177 if(hiwdg->State == HAL_IWDG_STATE_RESET)
179 /* Allocate lock resource and initialize it */
180 hiwdg-> Lock = HAL_UNLOCKED;
182 /* Init the low level hardware */
183 HAL_IWDG_MspInit(hiwdg);
186 /* Change IWDG peripheral state */
187 hiwdg->State = HAL_IWDG_STATE_BUSY;
189 /* Enable write access to IWDG_PR and IWDG_RLR registers */
190 IWDG_ENABLE_WRITE_ACCESS(hiwdg);
192 /* Write to IWDG registers the IWDG_Prescaler & IWDG_Reload values to work with */
193 MODIFY_REG(hiwdg->Instance->PR, IWDG_PR_PR, hiwdg->Init.Prescaler);
194 MODIFY_REG(hiwdg->Instance->RLR, IWDG_RLR_RL, hiwdg->Init.Reload);
196 /* Change IWDG peripheral state */
197 hiwdg->State = HAL_IWDG_STATE_READY;
199 /* Return function status */
204 * @brief Initializes the IWDG MSP.
205 * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
206 * the configuration information for the specified IWDG module.
209 __weak void HAL_IWDG_MspInit(IWDG_HandleTypeDef *hiwdg)
211 /* NOTE : This function Should not be modified, when the callback is needed,
212 the HAL_IWDG_MspInit could be implemented in the user file
220 /** @defgroup IWDG_Exported_Functions_Group2 IO operation functions
221 * @brief IO operation functions
224 ===============================================================================
225 ##### IO operation functions #####
226 ===============================================================================
227 [..] This section provides functions allowing to:
229 (+) Refresh the IWDG.
236 * @brief Starts the IWDG.
237 * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
238 * the configuration information for the specified IWDG module.
241 HAL_StatusTypeDef HAL_IWDG_Start(IWDG_HandleTypeDef *hiwdg)
246 /* Change IWDG peripheral state */
247 hiwdg->State = HAL_IWDG_STATE_BUSY;
249 /* Start the IWDG peripheral */
250 __HAL_IWDG_START(hiwdg);
252 /* Reload IWDG counter with value defined in the RLR register */
253 __HAL_IWDG_RELOAD_COUNTER(hiwdg);
255 /* Change IWDG peripheral state */
256 hiwdg->State = HAL_IWDG_STATE_READY;
258 /* Process Unlocked */
261 /* Return function status */
266 * @brief Refreshes the IWDG.
267 * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
268 * the configuration information for the specified IWDG module.
271 HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
273 uint32_t tickstart = 0;
278 /* Change IWDG peripheral state */
279 hiwdg->State = HAL_IWDG_STATE_BUSY;
281 tickstart = HAL_GetTick();
283 /* Wait until RVU flag is RESET */
284 while(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_RVU) != RESET)
286 if((HAL_GetTick() - tickstart ) > IWDG_DEFAULT_TIMEOUT)
289 hiwdg->State = HAL_IWDG_STATE_TIMEOUT;
291 /* Process unlocked */
298 /* Reload IWDG counter with value defined in the reload register */
299 __HAL_IWDG_RELOAD_COUNTER(hiwdg);
301 /* Change IWDG peripheral state */
302 hiwdg->State = HAL_IWDG_STATE_READY;
304 /* Process Unlocked */
307 /* Return function status */
315 /** @defgroup IWDG_Exported_Functions_Group3 Peripheral State functions
316 * @brief Peripheral State functions.
319 ===============================================================================
320 ##### Peripheral State functions #####
321 ===============================================================================
323 This subsection permits to get in run-time the status of the peripheral
331 * @brief Returns the IWDG state.
332 * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
333 * the configuration information for the specified IWDG module.
336 HAL_IWDG_StateTypeDef HAL_IWDG_GetState(IWDG_HandleTypeDef *hiwdg)
349 #endif /* HAL_IWDG_MODULE_ENABLED */
358 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/