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