TUKS MCU Introductory course / TUKS-COURSE-THERMOMETER

Fork of TUKS-COURSE-TIMER by TUKS MCU Introductory course

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stm32l4xx_hal_iwdg.c Source File

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   * @version V1.5.1
00006   * @date    31-May-2016
00007   * @brief   IWDG HAL module driver.
00008   *          This file provides firmware functions to manage the following 
00009   *          functionalities of the Independent Watchdog (IWDG) peripheral:
00010   *           + Initialization and Start functions
00011   *           + IO operation functions
00012   *
00013   @verbatim
00014   ==============================================================================
00015                     ##### IWDG Generic features #####
00016   ==============================================================================
00017   [..]
00018     (+) The IWDG can be started by either software or hardware (configurable
00019         through option byte).
00020 
00021     (+) The IWDG is clocked by Low-Speed clock (LSI) and thus stays active even
00022         if the main clock fails.
00023 
00024     (+) Once the IWDG is started, the LSI is forced ON and both can not be 
00025         disabled. The counter starts counting down from the reset value (0xFFF).
00026         When it reaches the end of count value (0x000) a reset signal is 
00027         generated (IWDG reset).
00028 
00029     (+) Whenever the key value 0x0000 AAAA is written in the IWDG_KR register, 
00030         the IWDG_RLR value is reloaded in the counter and the watchdog reset is
00031         prevented.
00032 
00033     (+) The IWDG is implemented in the VDD voltage domain that is still functional
00034         in STOP and STANDBY mode (IWDG reset can wake-up from STANDBY).
00035         IWDGRST flag in RCC_CSR register can be used to inform when an IWDG
00036         reset occurs.
00037 
00038     (+) Debug mode : When the microcontroller enters debug mode (core halted),
00039         the IWDG counter either continues to work normally or stops, depending 
00040         on DBG_IWDG_STOP configuration bit in DBG module, accessible through
00041         __HAL_DBGMCU_FREEZE_IWDG() and __HAL_DBGMCU_UNFREEZE_IWDG() macros
00042 
00043     [..] Min-max timeout value @32KHz (LSI): ~125us / ~32.7s
00044          The IWDG timeout may vary due to LSI frequency dispersion. STM32L4xx
00045          devices provide the capability to measure the LSI frequency (LSI clock
00046          connected internally to TIM16 CH1 input capture). The measured value
00047          can be used to have an IWDG timeout with an acceptable accuracy.
00048 
00049                      ##### How to use this driver #####
00050   ==============================================================================
00051   [..]
00052     (#) Use IWDG using HAL_IWDG_Init() function to :
00053       (+) Enable instance by writing Start keyword in IWDG_KEY register. LSI 
00054            clock is forced ON and IWDG counter starts downcounting.
00055       (+) Enable write access to configuration register: IWDG_PR, IWDG_RLR & 
00056            IWDG_WINR.
00057       (+) Configure the IWDG prescaler and counter reload value. This reload 
00058            value will be loaded in the IWDG counter each time the watchdog is 
00059            reloaded, then the IWDG will start counting down from this value.
00060       (+) wait for status flags to be reset"
00061       (+) Depending on window parameter:
00062         (++) If Window Init parameter is same as Window register value, 
00063              nothing more is done but reload counter value in order to exit 
00064              function withy exact time base.
00065         (++) Else modify Window register. This will automatically reload
00066              watchdog counter.
00067 
00068     (#) Then the application program must refresh the IWDG counter at regular
00069         intervals during normal operation to prevent an MCU reset, using
00070         HAL_IWDG_Refresh() function.
00071 
00072      *** IWDG HAL driver macros list ***
00073      ====================================
00074      [..]
00075        Below the list of most used macros in IWDG HAL driver:
00076       (+) __HAL_IWDG_START: Enable the IWDG peripheral
00077       (+) __HAL_IWDG_RELOAD_COUNTER: Reloads IWDG counter with value defined in
00078           the reload register
00079 
00080   @endverbatim
00081   ******************************************************************************
00082   * @attention
00083   *
00084   * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
00085   *
00086   * Redistribution and use in source and binary forms, with or without modification,
00087   * are permitted provided that the following conditions are met:
00088   *   1. Redistributions of source code must retain the above copyright notice,
00089   *      this list of conditions and the following disclaimer.
00090   *   2. Redistributions in binary form must reproduce the above copyright notice,
00091   *      this list of conditions and the following disclaimer in the documentation
00092   *      and/or other materials provided with the distribution.
00093   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00094   *      may be used to endorse or promote products derived from this software
00095   *      without specific prior written permission.
00096   *
00097   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00098   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00099   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00100   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00101   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00102   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00103   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00104   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00105   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00106   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00107   *
00108   ******************************************************************************
00109   */
00110 
00111 /* Includes ------------------------------------------------------------------*/
00112 #include "stm32l4xx_hal.h"
00113 
00114 /** @addtogroup STM32L4xx_HAL_Driver
00115   * @{
00116   */
00117 
00118 #ifdef HAL_IWDG_MODULE_ENABLED
00119 /** @addtogroup IWDG
00120   * @brief IWDG HAL module driver.
00121   * @{
00122   */
00123 
00124 /* Private typedef -----------------------------------------------------------*/
00125 /* Private define ------------------------------------------------------------*/
00126 /** @defgroup IWDG_Private_Defines IWDG Private Defines
00127   * @{
00128   */
00129 /* Status register need 5 RC LSI divided by prescaler clock to be updated. With 
00130    higher prescaler (256), and according to HSI variation, we need to wait at 
00131    least 6 cycles so 48 ms. */
00132 #define HAL_IWDG_DEFAULT_TIMEOUT            48u
00133 /**
00134   * @}
00135   */
00136 
00137 /* Private macro -------------------------------------------------------------*/
00138 /* Private variables ---------------------------------------------------------*/
00139 /* Private function prototypes -----------------------------------------------*/
00140 /* Exported functions --------------------------------------------------------*/
00141 
00142 /** @addtogroup IWDG_Exported_Functions
00143   * @{
00144   */
00145 
00146 /** @addtogroup IWDG_Exported_Functions_Group1
00147  *  @brief    Initialization and Start functions.
00148  *
00149 @verbatim
00150  ===============================================================================
00151           ##### Initialization and Start functions #####
00152  ===============================================================================
00153  [..]  This section provides functions allowing to:
00154       (+) Initialize the IWDG according to the specified parameters in the 
00155           IWDG_InitTypeDef of associated handle.
00156       (+) Manage Window option.
00157       (+) Once initialization is performed in HAL_IWDG_Init function, Watchdog 
00158           is reloaded in order to exit function with correct time base.
00159 
00160 @endverbatim
00161   * @{
00162   */
00163 
00164 /**
00165   * @brief  Initialize the IWDG according to the specified parameters in the 
00166   *         IWDG_InitTypeDef and start watchdog. Before exiting function, 
00167   *         watchdog is refreshed in order to have correct time base.
00168   * @param  hiwdg  pointer to a IWDG_HandleTypeDef structure that contains
00169   *                the configuration information for the specified IWDG module.
00170   * @retval HAL status
00171   */
00172 HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
00173 {
00174   uint32_t tickstart;
00175 
00176   /* Check the IWDG handle allocation */
00177   if(hiwdg == NULL)
00178   {
00179     return HAL_ERROR;
00180   }
00181 
00182   /* Check the parameters */
00183   assert_param(IS_IWDG_ALL_INSTANCE(hiwdg->Instance));
00184   assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler));
00185   assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload));
00186   assert_param(IS_IWDG_WINDOW(hiwdg->Init.Window));
00187 
00188   /* Enable IWDG. LSI is turned on automaticaly */
00189   __HAL_IWDG_START(hiwdg);
00190 
00191   /* Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers by writing
00192   0x5555 in KR */
00193   IWDG_ENABLE_WRITE_ACCESS(hiwdg);
00194 
00195   /* Write to IWDG registers the Prescaler & Reload values to work with */
00196   hiwdg->Instance->PR = hiwdg->Init.Prescaler;
00197   hiwdg->Instance->RLR = hiwdg->Init.Reload;
00198 
00199   /* Check pending flag, if previous update not done, return timeout */
00200   tickstart = HAL_GetTick();
00201 
00202    /* Wait for register to be updated */
00203   while(hiwdg->Instance->SR != RESET)
00204   {
00205     if((HAL_GetTick() - tickstart ) > HAL_IWDG_DEFAULT_TIMEOUT)
00206     {
00207       return HAL_TIMEOUT;
00208     }
00209   }
00210 
00211   /* If window parameter is different than current value, modify window 
00212   register */
00213   if(hiwdg->Instance->WINR != hiwdg->Init.Window)
00214   {
00215     /* Write to IWDG WINR the IWDG_Window value to compare with. In any case,
00216     even if window feature is disabled, Watchdog will be reloaded by writing 
00217     windows register */
00218     hiwdg->Instance->WINR = hiwdg->Init.Window;
00219   }
00220   else
00221   {
00222     /* Reload IWDG counter with value defined in the reload register */
00223     __HAL_IWDG_RELOAD_COUNTER(hiwdg);
00224   }
00225 
00226   /* Return function status */
00227   return HAL_OK;
00228 }
00229 
00230 /**
00231   * @}
00232   */
00233 
00234 
00235 /** @addtogroup IWDG_Exported_Functions_Group2
00236  *  @brief   IO operation functions
00237  *
00238 @verbatim
00239  ===============================================================================
00240                       ##### IO operation functions #####
00241  ===============================================================================
00242  [..]  This section provides functions allowing to:
00243       (+) Refresh the IWDG.
00244 
00245 @endverbatim
00246   * @{
00247   */
00248 
00249 
00250 /**
00251   * @brief  Refresh the IWDG.
00252   * @param  hiwdg  pointer to a IWDG_HandleTypeDef structure that contains
00253   *                the configuration information for the specified IWDG module.
00254   * @retval HAL status
00255   */
00256 HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
00257 {
00258   /* Reload IWDG counter with value defined in the reload register */
00259   __HAL_IWDG_RELOAD_COUNTER(hiwdg);
00260 
00261   /* Return function status */
00262   return HAL_OK;
00263 }
00264 
00265 /**
00266   * @}
00267   */
00268 
00269 /**
00270   * @}
00271   */
00272 
00273 #endif /* HAL_IWDG_MODULE_ENABLED */
00274 /**
00275   * @}
00276   */
00277 
00278 /**
00279   * @}
00280   */
00281 
00282 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/