STM32F479xx HAL User Manual
stm32f4xx_hal_iwdg.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f4xx_hal_iwdg.c
00004   * @author  MCD Application Team
00005   * @brief   IWDG HAL module driver.
00006   *          This file provides firmware functions to manage the following
00007   *          functionalities of the Independent Watchdog (IWDG) peripheral:
00008   *           + Initialization and Start functions
00009   *           + IO operation functions
00010   *
00011   @verbatim
00012   ==============================================================================
00013                     ##### IWDG Generic features #####
00014   ==============================================================================
00015   [..]
00016     (+) The IWDG can be started by either software or hardware (configurable
00017         through option byte).
00018 
00019     (+) The IWDG is clocked by the Low-Speed Internal clock (LSI) and thus stays
00020         active even if the main clock fails.
00021 
00022     (+) Once the IWDG is started, the LSI is forced ON and both cannot be
00023         disabled. The counter starts counting down from the reset value (0xFFF).
00024         When it reaches the end of count value (0x000) a reset signal is
00025         generated (IWDG reset).
00026 
00027     (+) Whenever the key value 0x0000 AAAA is written in the IWDG_KR register,
00028         the IWDG_RLR value is reloaded into the counter and the watchdog reset
00029         is prevented.
00030 
00031     (+) The IWDG is implemented in the VDD voltage domain that is still functional
00032         in STOP and STANDBY mode (IWDG reset can wake up the CPU from STANDBY).
00033         IWDGRST flag in RCC_CSR register can be used to inform when an IWDG
00034         reset occurs.
00035 
00036     (+) Debug mode: When the microcontroller enters debug mode (core halted),
00037         the IWDG counter either continues to work normally or stops, depending
00038         on DBG_IWDG_STOP configuration bit in DBG module, accessible through
00039         __HAL_DBGMCU_FREEZE_IWDG() and __HAL_DBGMCU_UNFREEZE_IWDG() macros.
00040 
00041     [..] Min-max timeout value @32KHz (LSI): ~125us / ~32.7s
00042          The IWDG timeout may vary due to LSI clock frequency dispersion.
00043          STM32F4xx devices provide the capability to measure the LSI clock
00044          frequency (LSI clock is internally connected to TIM5 CH4 input capture).
00045          The measured value can be used to have an IWDG timeout with an
00046          acceptable accuracy.
00047 
00048     [..] Default timeout value (necessary for IWDG_SR status register update):
00049          Constant LSI_VALUE is defined based on the nominal LSI clock frequency.
00050          This frequency being subject to variations as mentioned above, the
00051          default timeout value (defined through constant HAL_IWDG_DEFAULT_TIMEOUT
00052          below) may become too short or too long.
00053          In such cases, this default timeout value can be tuned by redefining
00054          the constant LSI_VALUE at user-application level (based, for instance,
00055          on the measured LSI clock frequency as explained above).
00056 
00057                      ##### How to use this driver #####
00058   ==============================================================================
00059   [..]
00060     (#) Use IWDG using HAL_IWDG_Init() function to :
00061       (++) Enable instance by writing Start keyword in IWDG_KEY register. LSI
00062            clock is forced ON and IWDG counter starts counting down.
00063       (++) Enable write access to configuration registers:
00064           IWDG_PR and IWDG_RLR.
00065       (++) Configure the IWDG prescaler and counter reload value. This reload
00066            value will be loaded in the IWDG counter each time the watchdog is
00067            reloaded, then the IWDG will start counting down from this value.
00068       (++) Wait for status flags to be reset.
00069 
00070     (#) Then the application program must refresh the IWDG counter at regular
00071         intervals during normal operation to prevent an MCU reset, using
00072         HAL_IWDG_Refresh() function.
00073 
00074      *** IWDG HAL driver macros list ***
00075      ====================================
00076      [..]
00077        Below the list of most used macros in IWDG HAL driver:
00078       (+) __HAL_IWDG_START: Enable the IWDG peripheral
00079       (+) __HAL_IWDG_RELOAD_COUNTER: Reloads IWDG counter with value defined in
00080           the reload register
00081 
00082   @endverbatim
00083   ******************************************************************************
00084   * @attention
00085   *
00086   * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
00087   * All rights reserved.</center></h2>
00088   *
00089   * This software component is licensed by ST under BSD 3-Clause license,
00090   * the "License"; You may not use this file except in compliance with the
00091   * License. You may obtain a copy of the License at:
00092   *                        opensource.org/licenses/BSD-3-Clause
00093   *
00094   ******************************************************************************
00095   */
00096 
00097 /* Includes ------------------------------------------------------------------*/
00098 #include "stm32f4xx_hal.h"
00099 
00100 /** @addtogroup STM32F4xx_HAL_Driver
00101   * @{
00102   */
00103 
00104 #ifdef HAL_IWDG_MODULE_ENABLED
00105 /** @addtogroup IWDG
00106   * @brief IWDG HAL module driver.
00107   * @{
00108   */
00109 
00110 /* Private typedef -----------------------------------------------------------*/
00111 /* Private define ------------------------------------------------------------*/
00112 /** @defgroup IWDG_Private_Defines IWDG Private Defines
00113   * @{
00114   */
00115 /* Status register needs up to 5 LSI clock periods divided by the clock
00116    prescaler to be updated. The number of LSI clock periods is upper-rounded to
00117    6 for the timeout value calculation.
00118    The timeout value is calculated using the highest prescaler (256) and
00119    the LSI_VALUE constant. The value of this constant can be changed by the user
00120    to take into account possible LSI clock period variations.
00121    The timeout value is multiplied by 1000 to be converted in milliseconds.
00122    LSI startup time is also considered here by adding LSI_STARTUP_TIMEOUT
00123    converted in milliseconds. */
00124 #define HAL_IWDG_DEFAULT_TIMEOUT        (((6UL * 256UL * 1000UL) / LSI_VALUE) + ((LSI_STARTUP_TIME / 1000UL) + 1UL))
00125 #define IWDG_KERNEL_UPDATE_FLAGS        (IWDG_SR_RVU | IWDG_SR_PVU)
00126 /**
00127   * @}
00128   */
00129 
00130 /* Private macro -------------------------------------------------------------*/
00131 /* Private variables ---------------------------------------------------------*/
00132 /* Private function prototypes -----------------------------------------------*/
00133 /* Exported functions --------------------------------------------------------*/
00134 
00135 /** @addtogroup IWDG_Exported_Functions
00136   * @{
00137   */
00138 
00139 /** @addtogroup IWDG_Exported_Functions_Group1
00140   *  @brief    Initialization and Start functions.
00141   *
00142 @verbatim
00143  ===============================================================================
00144           ##### Initialization and Start functions #####
00145  ===============================================================================
00146  [..]  This section provides functions allowing to:
00147       (+) Initialize the IWDG according to the specified parameters in the
00148           IWDG_InitTypeDef of associated handle.
00149       (+) Once initialization is performed in HAL_IWDG_Init function, Watchdog
00150           is reloaded in order to exit function with correct time base.
00151 
00152 @endverbatim
00153   * @{
00154   */
00155 
00156 /**
00157   * @brief  Initialize the IWDG according to the specified parameters in the
00158   *         IWDG_InitTypeDef and start watchdog. Before exiting function,
00159   *         watchdog is refreshed in order to have correct time base.
00160   * @param  hiwdg  pointer to a IWDG_HandleTypeDef structure that contains
00161   *                the configuration information for the specified IWDG module.
00162   * @retval HAL status
00163   */
00164 HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
00165 {
00166   uint32_t tickstart;
00167 
00168   /* Check the IWDG handle allocation */
00169   if (hiwdg == NULL)
00170   {
00171     return HAL_ERROR;
00172   }
00173 
00174   /* Check the parameters */
00175   assert_param(IS_IWDG_ALL_INSTANCE(hiwdg->Instance));
00176   assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler));
00177   assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload));
00178 
00179   /* Enable IWDG. LSI is turned on automatically */
00180   __HAL_IWDG_START(hiwdg);
00181 
00182   /* Enable write access to IWDG_PR and IWDG_RLR registers by writing
00183   0x5555 in KR */
00184   IWDG_ENABLE_WRITE_ACCESS(hiwdg);
00185 
00186   /* Write to IWDG registers the Prescaler & Reload values to work with */
00187   hiwdg->Instance->PR = hiwdg->Init.Prescaler;
00188   hiwdg->Instance->RLR = hiwdg->Init.Reload;
00189 
00190   /* Check pending flag, if previous update not done, return timeout */
00191   tickstart = HAL_GetTick();
00192 
00193   /* Wait for register to be updated */
00194   while ((hiwdg->Instance->SR & IWDG_KERNEL_UPDATE_FLAGS) != 0x00u)
00195   {
00196     if ((HAL_GetTick() - tickstart) > HAL_IWDG_DEFAULT_TIMEOUT)
00197     {
00198       if ((hiwdg->Instance->SR & IWDG_KERNEL_UPDATE_FLAGS) != 0x00u)
00199       {
00200         return HAL_TIMEOUT;
00201       }
00202     }
00203   }
00204 
00205   /* Reload IWDG counter with value defined in the reload register */
00206   __HAL_IWDG_RELOAD_COUNTER(hiwdg);
00207 
00208   /* Return function status */
00209   return HAL_OK;
00210 }
00211 
00212 
00213 /**
00214   * @}
00215   */
00216 
00217 
00218 /** @addtogroup IWDG_Exported_Functions_Group2
00219   *  @brief   IO operation functions
00220   *
00221 @verbatim
00222  ===============================================================================
00223                       ##### IO operation functions #####
00224  ===============================================================================
00225  [..]  This section provides functions allowing to:
00226       (+) Refresh the IWDG.
00227 
00228 @endverbatim
00229   * @{
00230   */
00231 
00232 /**
00233   * @brief  Refresh the IWDG.
00234   * @param  hiwdg  pointer to a IWDG_HandleTypeDef structure that contains
00235   *                the configuration information for the specified IWDG module.
00236   * @retval HAL status
00237   */
00238 HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
00239 {
00240   /* Reload IWDG counter with value defined in the reload register */
00241   __HAL_IWDG_RELOAD_COUNTER(hiwdg);
00242 
00243   /* Return function status */
00244   return HAL_OK;
00245 }
00246 
00247 
00248 /**
00249   * @}
00250   */
00251 
00252 /**
00253   * @}
00254   */
00255 
00256 #endif /* HAL_IWDG_MODULE_ENABLED */
00257 /**
00258   * @}
00259   */
00260 
00261 /**
00262   * @}
00263   */
00264 
00265 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/