2 ******************************************************************************
\r
3 * @file stm32f7xx_hal_iwdg.c
\r
4 * @author MCD Application Team
\r
6 * @date 06-March-2015
\r
7 * @brief IWDG HAL module driver.
\r
9 * This file provides firmware functions to manage the following
\r
10 * functionalities of the Independent Watchdog (IWDG) peripheral:
\r
11 * + Initialization and de-initialization functions
\r
12 * + IO operation functions
\r
13 * + Peripheral State functions
\r
16 ==============================================================================
\r
17 ##### IWDG Generic features #####
\r
18 ==============================================================================
\r
20 (+) The IWDG can be started by either software or hardware (configurable
\r
21 through option byte).
\r
23 (+) The IWDG is clocked by its own dedicated Low-Speed clock (LSI) and
\r
24 thus stays active even if the main clock fails.
\r
25 Once the IWDG is started, the LSI is forced ON and cannot be disabled
\r
26 (LSI cannot be disabled too), and the counter starts counting down from
\r
27 the reset value of 0xFFF. When it reaches the end of count value (0x000)
\r
28 a system reset is generated.
\r
30 (+) The IWDG counter should be refreshed at regular intervals, otherwise the
\r
31 watchdog generates an MCU reset when the counter reaches 0.
\r
33 (+) The IWDG is implemented in the VDD voltage domain that is still functional
\r
34 in STOP and STANDBY mode (IWDG reset can wake-up from STANDBY).
\r
35 IWDGRST flag in RCC_CSR register can be used to inform when an IWDG
\r
38 [..] Min-max timeout value @32KHz (LSI): ~125us / ~32.7s
\r
39 The IWDG timeout may vary due to LSI frequency dispersion. STM32L4xx
\r
40 devices provide the capability to measure the LSI frequency (LSI clock
\r
41 connected internally to TIM16 CH1 input capture). The measured value
\r
42 can be used to have an IWDG timeout with an acceptable accuracy.
\r
45 ##### How to use this driver #####
\r
46 ==============================================================================
\r
48 If Window option is disabled
\r
50 (+) Use IWDG using HAL_IWDG_Init() function to :
\r
51 (++) Enable write access to IWDG_PR, IWDG_RLR.
\r
52 (++) Configure the IWDG prescaler, counter reload value.
\r
53 This reload value will be loaded in the IWDG counter each time the counter
\r
54 is reloaded, then the IWDG will start counting down from this value.
\r
55 (+) Use IWDG using HAL_IWDG_Start() function to :
\r
56 (++) Reload IWDG counter with value defined in the IWDG_RLR register.
\r
57 (++) Start the IWDG, when the IWDG is used in software mode (no need
\r
58 to enable the LSI, it will be enabled by hardware).
\r
59 (+) Then the application program must refresh the IWDG counter at regular
\r
60 intervals during normal operation to prevent an MCU reset, using
\r
61 HAL_IWDG_Refresh() function.
\r
63 if Window option is enabled:
\r
65 (+) Use IWDG using HAL_IWDG_Start() function to enable IWDG downcounter
\r
66 (+) Use IWDG using HAL_IWDG_Init() function to :
\r
67 (++) Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers.
\r
68 (++) Configure the IWDG prescaler, reload value and window value.
\r
69 (+) Then the application program must refresh the IWDG counter at regular
\r
70 intervals during normal operation to prevent an MCU reset, using
\r
71 HAL_IWDG_Refresh() function.
\r
73 *** IWDG HAL driver macros list ***
\r
74 ====================================
\r
76 Below the list of most used macros in IWDG HAL driver.
\r
78 (+) __HAL_IWDG_START: Enable the IWDG peripheral
\r
79 (+) __HAL_IWDG_RELOAD_COUNTER: Reloads IWDG counter with value defined in the reload register
\r
80 (+) __HAL_IWDG_GET_FLAG: Get the selected IWDG's flag status
\r
81 (+) IWDG_ENABLE_WRITE_ACCESS : Enable write access to IWDG_PR and IWDG_RLR registers
\r
82 (+) IWDG_DISABLE_WRITE_ACCESS : Disable write access to IWDG_PR and IWDG_RLR registers
\r
85 ******************************************************************************
\r
88 * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
\r
90 * Redistribution and use in source and binary forms, with or without modification,
\r
91 * are permitted provided that the following conditions are met:
\r
92 * 1. Redistributions of source code must retain the above copyright notice,
\r
93 * this list of conditions and the following disclaimer.
\r
94 * 2. Redistributions in binary form must reproduce the above copyright notice,
\r
95 * this list of conditions and the following disclaimer in the documentation
\r
96 * and/or other materials provided with the distribution.
\r
97 * 3. Neither the name of STMicroelectronics nor the names of its contributors
\r
98 * may be used to endorse or promote products derived from this software
\r
99 * without specific prior written permission.
\r
101 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
\r
102 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
\r
103 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
\r
104 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
\r
105 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
\r
106 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
\r
107 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
\r
108 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
\r
109 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
\r
110 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
112 ******************************************************************************
\r
115 /* Includes ------------------------------------------------------------------*/
\r
116 #include "stm32f7xx_hal.h"
\r
118 /** @addtogroup STM32F7xx_HAL_Driver
\r
122 /** @defgroup IWDG IWDG HAL module driver
\r
123 * @brief IWDG HAL module driver.
\r
127 #ifdef HAL_IWDG_MODULE_ENABLED
\r
129 /* Private typedef -----------------------------------------------------------*/
\r
130 /* Private define ------------------------------------------------------------*/
\r
131 /** @defgroup IWDG_Private_Defines IWDG Private Defines
\r
135 #define HAL_IWDG_DEFAULT_TIMEOUT (uint32_t)1000
\r
140 /* Private macro -------------------------------------------------------------*/
\r
141 /* Private variables ---------------------------------------------------------*/
\r
142 /* Private function prototypes -----------------------------------------------*/
\r
143 /* Exported functions --------------------------------------------------------*/
\r
145 /** @defgroup IWDG_Exported_Functions IWDG Exported Functions
\r
149 /** @defgroup IWDG_Exported_Functions_Group1 Initialization and de-initialization functions
\r
150 * @brief Initialization and Configuration functions.
\r
153 ===============================================================================
\r
154 ##### Initialization and de-initialization functions #####
\r
155 ===============================================================================
\r
156 [..] This section provides functions allowing to:
\r
157 (+) Initialize the IWDG according to the specified parameters
\r
158 in the IWDG_InitTypeDef and create the associated handle
\r
159 (+) Manage Window option
\r
160 (+) Initialize the IWDG MSP
\r
161 (+) DeInitialize IWDG MSP
\r
168 * @brief Initializes the IWDG according to the specified
\r
169 * parameters in the IWDG_InitTypeDef and creates the associated handle.
\r
170 * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
\r
171 * the configuration information for the specified IWDG module.
\r
172 * @retval HAL status
\r
174 HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
\r
176 uint32_t tickstart = 0;
\r
178 /* Check the IWDG handle allocation */
\r
184 /* Check the parameters */
\r
185 assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler));
\r
186 assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload));
\r
187 assert_param(IS_IWDG_WINDOW(hiwdg->Init.Window));
\r
189 /* Check pending flag, if previous update not done, return error */
\r
190 if((__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_PVU) != RESET)
\r
191 &&(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_RVU) != RESET)
\r
192 &&(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_WVU) != RESET))
\r
197 if(hiwdg->State == HAL_IWDG_STATE_RESET)
\r
199 /* Allocate lock resource and initialize it */
\r
200 hiwdg->Lock = HAL_UNLOCKED;
\r
202 /* Init the low level hardware */
\r
203 HAL_IWDG_MspInit(hiwdg);
\r
206 /* Change IWDG peripheral state */
\r
207 hiwdg->State = HAL_IWDG_STATE_BUSY;
\r
209 /* Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers */
\r
210 /* by writing 0x5555 in KR */
\r
211 IWDG_ENABLE_WRITE_ACCESS(hiwdg);
\r
213 /* Write to IWDG registers the IWDG_Prescaler & IWDG_Reload values to work with */
\r
214 MODIFY_REG(hiwdg->Instance->PR, (uint32_t)IWDG_PR_PR, hiwdg->Init.Prescaler);
\r
215 MODIFY_REG(hiwdg->Instance->RLR, (uint32_t)IWDG_RLR_RL, hiwdg->Init.Reload);
\r
217 /* check if window option is enabled */
\r
218 if (((hiwdg->Init.Window) != IWDG_WINDOW_DISABLE) || ((hiwdg->Instance->WINR) != IWDG_WINDOW_DISABLE))
\r
220 tickstart = HAL_GetTick();
\r
222 /* Wait for register to be updated */
\r
223 while((uint32_t)(hiwdg->Instance->SR) != RESET)
\r
225 if((HAL_GetTick() - tickstart ) > HAL_IWDG_DEFAULT_TIMEOUT)
\r
227 /* Set IWDG state */
\r
228 hiwdg->State = HAL_IWDG_STATE_TIMEOUT;
\r
229 return HAL_TIMEOUT;
\r
233 /* Write to IWDG WINR the IWDG_Window value to compare with */
\r
234 MODIFY_REG(hiwdg->Instance->WINR, (uint32_t)IWDG_WINR_WIN, hiwdg->Init.Window);
\r
237 /* Change IWDG peripheral state */
\r
238 hiwdg->State = HAL_IWDG_STATE_READY;
\r
240 /* Return function status */
\r
245 * @brief Initializes the IWDG MSP.
\r
246 * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
\r
247 * the configuration information for the specified IWDG module.
\r
250 __weak void HAL_IWDG_MspInit(IWDG_HandleTypeDef *hiwdg)
\r
252 /* NOTE : This function should not be modified, when the callback is needed,
\r
253 the HAL_IWDG_MspInit could be implemented in the user file
\r
261 /** @defgroup IWDG_Exported_Functions_Group2 IO operation functions
\r
262 * @brief IO operation functions
\r
265 ===============================================================================
\r
266 ##### IO operation functions #####
\r
267 ===============================================================================
\r
268 [..] This section provides functions allowing to:
\r
269 (+) Start the IWDG.
\r
270 (+) Refresh the IWDG.
\r
277 * @brief Starts the IWDG.
\r
278 * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
\r
279 * the configuration information for the specified IWDG module.
\r
280 * @retval HAL status
\r
282 HAL_StatusTypeDef HAL_IWDG_Start(IWDG_HandleTypeDef *hiwdg)
\r
284 uint32_t tickstart = 0;
\r
286 /* Process locked */
\r
287 __HAL_LOCK(hiwdg);
\r
289 /* Change IWDG peripheral state */
\r
290 hiwdg->State = HAL_IWDG_STATE_BUSY;
\r
292 /* Reload IWDG counter with value defined in the RLR register */
\r
293 if ((hiwdg->Init.Window) == IWDG_WINDOW_DISABLE)
\r
295 __HAL_IWDG_RELOAD_COUNTER(hiwdg);
\r
298 /* Start the IWDG peripheral */
\r
299 __HAL_IWDG_START(hiwdg);
\r
301 tickstart = HAL_GetTick();
\r
303 /* Wait until PVU, RVU, WVU flag are RESET */
\r
304 while( (__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_PVU) != RESET)
\r
305 &&(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_RVU) != RESET)
\r
306 &&(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_WVU) != RESET) )
\r
309 if((HAL_GetTick() - tickstart ) > HAL_IWDG_DEFAULT_TIMEOUT)
\r
311 /* Set IWDG state */
\r
312 hiwdg->State = HAL_IWDG_STATE_TIMEOUT;
\r
314 /* Process unlocked */
\r
315 __HAL_UNLOCK(hiwdg);
\r
317 return HAL_TIMEOUT;
\r
321 /* Change IWDG peripheral state */
\r
322 hiwdg->State = HAL_IWDG_STATE_READY;
\r
324 /* Process Unlocked */
\r
325 __HAL_UNLOCK(hiwdg);
\r
327 /* Return function status */
\r
332 * @brief Refreshes the IWDG.
\r
333 * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
\r
334 * the configuration information for the specified IWDG module.
\r
335 * @retval HAL status
\r
337 HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
\r
339 uint32_t tickstart = 0;
\r
341 /* Process Locked */
\r
344 /* Change IWDG peripheral state */
\r
345 hiwdg->State = HAL_IWDG_STATE_BUSY;
\r
347 tickstart = HAL_GetTick();
\r
349 /* Wait until RVU flag is RESET */
\r
350 while(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_RVU) != RESET)
\r
352 if((HAL_GetTick() - tickstart ) > HAL_IWDG_DEFAULT_TIMEOUT)
\r
354 /* Set IWDG state */
\r
355 hiwdg->State = HAL_IWDG_STATE_TIMEOUT;
\r
357 /* Process unlocked */
\r
358 __HAL_UNLOCK(hiwdg);
\r
360 return HAL_TIMEOUT;
\r
364 /* Reload IWDG counter with value defined in the reload register */
\r
365 __HAL_IWDG_RELOAD_COUNTER(hiwdg);
\r
367 /* Change IWDG peripheral state */
\r
368 hiwdg->State = HAL_IWDG_STATE_READY;
\r
370 /* Process Unlocked */
\r
371 __HAL_UNLOCK(hiwdg);
\r
373 /* Return function status */
\r
381 /** @defgroup IWDG_Exported_Functions_Group3 Peripheral State functions
\r
382 * @brief Peripheral State functions.
\r
385 ===============================================================================
\r
386 ##### Peripheral State functions #####
\r
387 ===============================================================================
\r
389 This subsection permits to get in run-time the status of the peripheral.
\r
396 * @brief Returns the IWDG state.
\r
397 * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
\r
398 * the configuration information for the specified IWDG module.
\r
399 * @retval HAL state
\r
401 HAL_IWDG_StateTypeDef HAL_IWDG_GetState(IWDG_HandleTypeDef *hiwdg)
\r
403 return hiwdg->State;
\r
414 #endif /* HAL_IWDG_MODULE_ENABLED */
\r
423 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
\r