STM32F439xx 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 Low-Speed clock (LSI) and thus stays active even
00020         if the main clock fails.
00021 
00022     (+) Once the IWDG is started, the LSI is forced ON and both can not 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 in the counter and the watchdog reset is
00029         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 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 frequency dispersion. STM32F4xx
00043          devices provide the capability to measure the LSI frequency (LSI clock
00044          connected internally to TIM5 CH4 input capture). The measured value
00045          can be used to have an IWDG timeout with an acceptable accuracy.
00046 
00047                      ##### How to use this driver #####
00048   ==============================================================================
00049   [..]
00050     (#) Use IWDG using HAL_IWDG_Init() function to :
00051       (++) Enable instance by writing Start keyword in IWDG_KEY register. LSI
00052            clock is forced ON and IWDG counter starts downcounting.
00053       (++) Enable write access to configuration register: IWDG_PR & IWDG_RLR.
00054       (++) Configure the IWDG prescaler and counter reload value. This reload
00055            value will be loaded in the IWDG counter each time the watchdog is
00056            reloaded, then the IWDG will start counting down from this value.
00057       (++) wait for status flags to be reset"
00058 
00059     (#) Then the application program must refresh the IWDG counter at regular
00060         intervals during normal operation to prevent an MCU reset, using
00061         HAL_IWDG_Refresh() function.
00062 
00063      *** IWDG HAL driver macros list ***
00064      ====================================
00065      [..]
00066        Below the list of most used macros in IWDG HAL driver:
00067       (+) __HAL_IWDG_START: Enable the IWDG peripheral
00068       (+) __HAL_IWDG_RELOAD_COUNTER: Reloads IWDG counter with value defined in
00069           the reload register
00070 
00071   @endverbatim
00072   ******************************************************************************
00073   * @attention
00074   *
00075   * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
00076   *
00077   * Redistribution and use in source and binary forms, with or without modification,
00078   * are permitted provided that the following conditions are met:
00079   *   1. Redistributions of source code must retain the above copyright notice,
00080   *      this list of conditions and the following disclaimer.
00081   *   2. Redistributions in binary form must reproduce the above copyright notice,
00082   *      this list of conditions and the following disclaimer in the documentation
00083   *      and/or other materials provided with the distribution
00084   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00085   *      may be used to endorse or promote products derived from this software
00086   *      without specific prior written permission.
00087   *
00088   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00089   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00090   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00091   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00092   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00093   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00094   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00095   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00096   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00097   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00098   *
00099   ******************************************************************************
00100   */
00101 
00102 /* Includes ------------------------------------------------------------------*/
00103 #include "stm32f4xx_hal.h"
00104 
00105 /** @addtogroup STM32F4xx_HAL_Driver
00106   * @{
00107   */
00108 
00109 #ifdef HAL_IWDG_MODULE_ENABLED
00110 /** @defgroup IWDG IWDG
00111   * @brief IWDG HAL module driver.
00112   * @{
00113   */
00114 
00115 /* Private typedef -----------------------------------------------------------*/
00116 /* Private define ------------------------------------------------------------*/
00117 /** @defgroup IWDG_Private_Defines IWDG Private Defines
00118   * @{
00119   */
00120 /* Status register need 5 RC LSI divided by prescaler clock to be updated. With 
00121    higher prescaler (256), and according to HSI variation, we need to wait at
00122    least 6 cycles so 48 ms. */
00123 #define HAL_IWDG_DEFAULT_TIMEOUT            48U
00124 /**
00125   * @}
00126   */
00127 
00128 /* Private macro -------------------------------------------------------------*/
00129 /* Private variables ---------------------------------------------------------*/
00130 /* Private function prototypes -----------------------------------------------*/
00131 /* Exported functions --------------------------------------------------------*/
00132 
00133 /** @addtogroup IWDG_Exported_Functions
00134   * @{
00135   */
00136 
00137 /** @addtogroup IWDG_Exported_Functions_Group1
00138   *  @brief    Initialization and Start functions.
00139   *
00140 @verbatim
00141  ===============================================================================
00142           ##### Initialization and Start functions #####
00143  ===============================================================================
00144  [..]  This section provides functions allowing to:
00145       (+) Initialize the IWDG according to the specified parameters in the
00146           IWDG_InitTypeDef of associated handle.
00147       (+) Once initialization is performed in HAL_IWDG_Init function, Watchdog
00148           is reloaded in order to exit function with correct time base.
00149 
00150 @endverbatim
00151   * @{
00152   */
00153 
00154 /**
00155   * @brief  Initialize the IWDG according to the specified parameters in the
00156   *         IWDG_InitTypeDef and start watchdog. Before exiting function,
00157   *         watchdog is refreshed in order to have correct time base.
00158   * @param  hiwdg  pointer to a IWDG_HandleTypeDef structure that contains
00159   *                the configuration information for the specified IWDG module.
00160   * @retval HAL status
00161   */
00162 HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
00163 {
00164   uint32_t tickstart;
00165 
00166   /* Check the IWDG handle allocation */
00167   if(hiwdg == NULL)
00168   {
00169     return HAL_ERROR;
00170   }
00171 
00172   /* Check the parameters */
00173   assert_param(IS_IWDG_ALL_INSTANCE(hiwdg->Instance));
00174   assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler));
00175   assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload));
00176 
00177   /* Enable IWDG. LSI is turned on automaticaly */
00178   __HAL_IWDG_START(hiwdg);
00179 
00180   /* Enable write access to IWDG_PR and IWDG_RLR registers by writing 0x5555 in KR */
00181   IWDG_ENABLE_WRITE_ACCESS(hiwdg);
00182 
00183   /* Write to IWDG registers the Prescaler & Reload values to work with */
00184   hiwdg->Instance->PR = hiwdg->Init.Prescaler;
00185   hiwdg->Instance->RLR = hiwdg->Init.Reload;
00186 
00187   /* Check pending flag, if previous update not done, return timeout */
00188   tickstart = HAL_GetTick();
00189 
00190   /* Wait for register to be updated */
00191   while(hiwdg->Instance->SR != RESET)
00192   {
00193     if((HAL_GetTick() - tickstart ) > HAL_IWDG_DEFAULT_TIMEOUT)
00194     {
00195       return HAL_TIMEOUT;
00196     }
00197   }
00198 
00199   /* Reload IWDG counter with value defined in the reload register */
00200   __HAL_IWDG_RELOAD_COUNTER(hiwdg);
00201 
00202   /* Return function status */
00203   return HAL_OK;
00204 }
00205 
00206 /**
00207   * @}
00208   */
00209 
00210 /** @addtogroup IWDG_Exported_Functions_Group2
00211   *  @brief   IO operation functions
00212   *
00213 @verbatim
00214  ===============================================================================
00215                       ##### IO operation functions #####
00216  ===============================================================================
00217  [..]  This section provides functions allowing to:
00218       (+) Refresh the IWDG.
00219 
00220 @endverbatim
00221   * @{
00222   */
00223 
00224 /**
00225   * @brief  Refresh the IWDG.
00226   * @param  hiwdg  pointer to a IWDG_HandleTypeDef structure that contains
00227   *                the configuration information for the specified IWDG module.
00228   * @retval HAL status
00229   */
00230 HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
00231 {
00232   /* Reload IWDG counter with value defined in the reload register */
00233   __HAL_IWDG_RELOAD_COUNTER(hiwdg);
00234 
00235   /* Return function status */
00236   return HAL_OK;
00237 }
00238 
00239 /**
00240   * @}
00241   */
00242 
00243 /**
00244   * @}
00245   */
00246 
00247 #endif /* HAL_IWDG_MODULE_ENABLED */
00248 /**
00249   * @}
00250   */
00251 
00252 /**
00253   * @}
00254   */
00255 
00256 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/