STM32L443xx HAL User Manual
stm32l4xx_hal_wwdg.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32l4xx_hal_wwdg.c
00004   * @author  MCD Application Team
00005   * @brief   WWDG HAL module driver.
00006   *          This file provides firmware functions to manage the following
00007   *          functionalities of the Window Watchdog (WWDG) peripheral:
00008   *           + Initialization and Configuration functions
00009   *           + IO operation functions
00010   ******************************************************************************
00011   * @attention
00012   *
00013   * Copyright (c) 2017 STMicroelectronics.
00014   * All rights reserved.
00015   *
00016   * This software is licensed under terms that can be found in the LICENSE file
00017   * in the root directory of this software component.
00018   * If no LICENSE file comes with this software, it is provided AS-IS.
00019   *
00020   ******************************************************************************
00021   @verbatim
00022   ==============================================================================
00023                       ##### WWDG Specific features #####
00024   ==============================================================================
00025   [..]
00026     Once enabled the WWDG generates a system reset on expiry of a programmed
00027     time period, unless the program refreshes the counter (T[6;0] downcounter)
00028     before reaching 0x3F value (i.e. a reset is generated when the counter
00029     value rolls down from 0x40 to 0x3F).
00030 
00031     (+) An MCU reset is also generated if the counter value is refreshed
00032         before the counter has reached the refresh window value. This
00033         implies that the counter must be refreshed in a limited window.
00034     (+) Once enabled the WWDG cannot be disabled except by a system reset.
00035     (+) If required by application, an Early Wakeup Interrupt can be triggered
00036         in order to be warned before WWDG expiration. The Early Wakeup Interrupt
00037         (EWI) can be used if specific safety operations or data logging must
00038         be performed before the actual reset is generated. When the downcounter
00039         reaches 0x40, interrupt occurs. This mechanism requires WWDG interrupt
00040         line to be enabled in NVIC. Once enabled, EWI interrupt cannot be
00041         disabled except by a system reset.
00042     (+) WWDGRST flag in RCC CSR register can be used to inform when a WWDG
00043         reset occurs.
00044     (+) The WWDG counter input clock is derived from the APB clock divided
00045         by a programmable prescaler.
00046     (+) WWDG clock (Hz) = PCLK1 / (4096 * Prescaler)
00047     (+) WWDG timeout (mS) = 1000 * (T[5;0] + 1) / WWDG clock (Hz)
00048         where T[5;0] are the lowest 6 bits of Counter.
00049     (+) WWDG Counter refresh is allowed between the following limits :
00050         (++) min time (mS) = 1000 * (Counter - Window) / WWDG clock
00051         (++) max time (mS) = 1000 * (Counter - 0x40) / WWDG clock
00052     (+) Typical values:
00053         (++) Counter min (T[5;0] = 0x00) at 56MHz (PCLK1) with zero prescaler:
00054              max timeout before reset: approximately 73.14us
00055         (++) Counter max (T[5;0] = 0x3F) at 56MHz (PCLK1) with prescaler
00056              dividing by 8:
00057              max timeout before reset: approximately 599.18ms
00058 
00059                      ##### How to use this driver #####
00060   ==============================================================================
00061 
00062     *** Common driver usage ***
00063     ===========================
00064 
00065   [..]
00066     (+) Enable WWDG APB1 clock using __HAL_RCC_WWDG_CLK_ENABLE().
00067     (+) Configure the WWDG prescaler, refresh window value, counter value and early
00068         interrupt status using HAL_WWDG_Init() function. This will automatically
00069         enable WWDG and start its downcounter. Time reference can be taken from
00070         function exit. Care must be taken to provide a counter value
00071         greater than 0x40 to prevent generation of immediate reset.
00072     (+) If the Early Wakeup Interrupt (EWI) feature is enabled, an interrupt is
00073         generated when the counter reaches 0x40. When HAL_WWDG_IRQHandler is
00074         triggered by the interrupt service routine, flag will be automatically
00075         cleared and HAL_WWDG_WakeupCallback user callback will be executed. User
00076         can add his own code by customization of callback HAL_WWDG_WakeupCallback.
00077     (+) Then the application program must refresh the WWDG counter at regular
00078         intervals during normal operation to prevent an MCU reset, using
00079         HAL_WWDG_Refresh() function. This operation must occur only when
00080         the counter is lower than the refresh window value already programmed.
00081 
00082     *** Callback registration ***
00083     =============================
00084 
00085   [..]
00086     The compilation define USE_HAL_WWDG_REGISTER_CALLBACKS when set to 1 allows
00087     the user to configure dynamically the driver callbacks. Use Functions
00088     HAL_WWDG_RegisterCallback() to register a user callback.
00089 
00090     (+) Function HAL_WWDG_RegisterCallback() allows to register following
00091         callbacks:
00092         (++) EwiCallback : callback for Early WakeUp Interrupt.
00093         (++) MspInitCallback : WWDG MspInit.
00094     This function takes as parameters the HAL peripheral handle, the Callback ID
00095     and a pointer to the user callback function.
00096 
00097     (+) Use function HAL_WWDG_UnRegisterCallback() to reset a callback to
00098     the default weak (surcharged) function. HAL_WWDG_UnRegisterCallback()
00099     takes as parameters the HAL peripheral handle and the Callback ID.
00100     This function allows to reset following callbacks:
00101         (++) EwiCallback : callback for  Early WakeUp Interrupt.
00102         (++) MspInitCallback : WWDG MspInit.
00103 
00104     [..]
00105     When calling HAL_WWDG_Init function, callbacks are reset to the
00106     corresponding legacy weak (surcharged) functions:
00107     HAL_WWDG_EarlyWakeupCallback() and HAL_WWDG_MspInit() only if they have
00108     not been registered before.
00109 
00110     [..]
00111     When compilation define USE_HAL_WWDG_REGISTER_CALLBACKS is set to 0 or
00112     not defined, the callback registering feature is not available
00113     and weak (surcharged) callbacks are used.
00114 
00115     *** WWDG HAL driver macros list ***
00116     ===================================
00117     [..]
00118       Below the list of available macros in WWDG HAL driver.
00119       (+) __HAL_WWDG_ENABLE: Enable the WWDG peripheral
00120       (+) __HAL_WWDG_GET_FLAG: Get the selected WWDG's flag status
00121       (+) __HAL_WWDG_CLEAR_FLAG: Clear the WWDG's pending flags
00122       (+) __HAL_WWDG_ENABLE_IT: Enable the WWDG early wakeup interrupt
00123 
00124   @endverbatim
00125   ******************************************************************************
00126   */
00127 
00128 /* Includes ------------------------------------------------------------------*/
00129 #include "stm32l4xx_hal.h"
00130 
00131 /** @addtogroup STM32L4xx_HAL_Driver
00132   * @{
00133   */
00134 
00135 #ifdef HAL_WWDG_MODULE_ENABLED
00136 /** @defgroup WWDG WWDG
00137   * @brief WWDG HAL module driver.
00138   * @{
00139   */
00140 
00141 /* Private typedef -----------------------------------------------------------*/
00142 /* Private define ------------------------------------------------------------*/
00143 /* Private macro -------------------------------------------------------------*/
00144 /* Private variables ---------------------------------------------------------*/
00145 /* Private function prototypes -----------------------------------------------*/
00146 /* Exported functions --------------------------------------------------------*/
00147 
00148 /** @defgroup WWDG_Exported_Functions WWDG Exported Functions
00149   * @{
00150   */
00151 
00152 /** @defgroup WWDG_Exported_Functions_Group1 Initialization and Configuration functions
00153   *  @brief    Initialization and Configuration functions.
00154   *
00155 @verbatim
00156   ==============================================================================
00157           ##### Initialization and Configuration functions #####
00158   ==============================================================================
00159   [..]
00160     This section provides functions allowing to:
00161       (+) Initialize and start the WWDG according to the specified parameters
00162           in the WWDG_InitTypeDef of associated handle.
00163       (+) Initialize the WWDG MSP.
00164 
00165 @endverbatim
00166   * @{
00167   */
00168 
00169 /**
00170   * @brief  Initialize the WWDG according to the specified.
00171   *         parameters in the WWDG_InitTypeDef of  associated handle.
00172   * @param  hwwdg  pointer to a WWDG_HandleTypeDef structure that contains
00173   *                the configuration information for the specified WWDG module.
00174   * @retval HAL status
00175   */
00176 HAL_StatusTypeDef HAL_WWDG_Init(WWDG_HandleTypeDef *hwwdg)
00177 {
00178   /* Check the WWDG handle allocation */
00179   if (hwwdg == NULL)
00180   {
00181     return HAL_ERROR;
00182   }
00183 
00184   /* Check the parameters */
00185   assert_param(IS_WWDG_ALL_INSTANCE(hwwdg->Instance));
00186   assert_param(IS_WWDG_PRESCALER(hwwdg->Init.Prescaler));
00187   assert_param(IS_WWDG_WINDOW(hwwdg->Init.Window));
00188   assert_param(IS_WWDG_COUNTER(hwwdg->Init.Counter));
00189   assert_param(IS_WWDG_EWI_MODE(hwwdg->Init.EWIMode));
00190 
00191 #if (USE_HAL_WWDG_REGISTER_CALLBACKS == 1)
00192   /* Reset Callback pointers */
00193   if (hwwdg->EwiCallback == NULL)
00194   {
00195     hwwdg->EwiCallback = HAL_WWDG_EarlyWakeupCallback;
00196   }
00197 
00198   if (hwwdg->MspInitCallback == NULL)
00199   {
00200     hwwdg->MspInitCallback = HAL_WWDG_MspInit;
00201   }
00202 
00203   /* Init the low level hardware */
00204   hwwdg->MspInitCallback(hwwdg);
00205 #else
00206   /* Init the low level hardware */
00207   HAL_WWDG_MspInit(hwwdg);
00208 #endif /* USE_HAL_WWDG_REGISTER_CALLBACKS */
00209 
00210   /* Set WWDG Counter */
00211   WRITE_REG(hwwdg->Instance->CR, (WWDG_CR_WDGA | hwwdg->Init.Counter));
00212 
00213   /* Set WWDG Prescaler and Window */
00214   WRITE_REG(hwwdg->Instance->CFR, (hwwdg->Init.EWIMode | hwwdg->Init.Prescaler | hwwdg->Init.Window));
00215 
00216   /* Return function status */
00217   return HAL_OK;
00218 }
00219 
00220 
00221 /**
00222   * @brief  Initialize the WWDG MSP.
00223   * @param  hwwdg  pointer to a WWDG_HandleTypeDef structure that contains
00224   *                the configuration information for the specified WWDG module.
00225   * @note   When rewriting this function in user file, mechanism may be added
00226   *         to avoid multiple initialize when HAL_WWDG_Init function is called
00227   *         again to change parameters.
00228   * @retval None
00229   */
00230 __weak void HAL_WWDG_MspInit(WWDG_HandleTypeDef *hwwdg)
00231 {
00232   /* Prevent unused argument(s) compilation warning */
00233   UNUSED(hwwdg);
00234 
00235   /* NOTE: This function should not be modified, when the callback is needed,
00236            the HAL_WWDG_MspInit could be implemented in the user file
00237    */
00238 }
00239 
00240 
00241 #if (USE_HAL_WWDG_REGISTER_CALLBACKS == 1)
00242 /**
00243   * @brief  Register a User WWDG Callback
00244   *         To be used instead of the weak (surcharged) predefined callback
00245   * @param  hwwdg WWDG handle
00246   * @param  CallbackID ID of the callback to be registered
00247   *         This parameter can be one of the following values:
00248   *           @arg @ref HAL_WWDG_EWI_CB_ID Early WakeUp Interrupt Callback ID
00249   *           @arg @ref HAL_WWDG_MSPINIT_CB_ID MspInit callback ID
00250   * @param  pCallback pointer to the Callback function
00251   * @retval status
00252   */
00253 HAL_StatusTypeDef HAL_WWDG_RegisterCallback(WWDG_HandleTypeDef *hwwdg, HAL_WWDG_CallbackIDTypeDef CallbackID,
00254                                             pWWDG_CallbackTypeDef pCallback)
00255 {
00256   HAL_StatusTypeDef status = HAL_OK;
00257 
00258   if (pCallback == NULL)
00259   {
00260     status = HAL_ERROR;
00261   }
00262   else
00263   {
00264     switch (CallbackID)
00265     {
00266       case HAL_WWDG_EWI_CB_ID:
00267         hwwdg->EwiCallback = pCallback;
00268         break;
00269 
00270       case HAL_WWDG_MSPINIT_CB_ID:
00271         hwwdg->MspInitCallback = pCallback;
00272         break;
00273 
00274       default:
00275         status = HAL_ERROR;
00276         break;
00277     }
00278   }
00279 
00280   return status;
00281 }
00282 
00283 
00284 /**
00285   * @brief  Unregister a WWDG Callback
00286   *         WWDG Callback is redirected to the weak (surcharged) predefined callback
00287   * @param  hwwdg WWDG handle
00288   * @param  CallbackID ID of the callback to be registered
00289   *         This parameter can be one of the following values:
00290   *           @arg @ref HAL_WWDG_EWI_CB_ID Early WakeUp Interrupt Callback ID
00291   *           @arg @ref HAL_WWDG_MSPINIT_CB_ID MspInit callback ID
00292   * @retval status
00293   */
00294 HAL_StatusTypeDef HAL_WWDG_UnRegisterCallback(WWDG_HandleTypeDef *hwwdg, HAL_WWDG_CallbackIDTypeDef CallbackID)
00295 {
00296   HAL_StatusTypeDef status = HAL_OK;
00297 
00298   switch (CallbackID)
00299   {
00300     case HAL_WWDG_EWI_CB_ID:
00301       hwwdg->EwiCallback = HAL_WWDG_EarlyWakeupCallback;
00302       break;
00303 
00304     case HAL_WWDG_MSPINIT_CB_ID:
00305       hwwdg->MspInitCallback = HAL_WWDG_MspInit;
00306       break;
00307 
00308     default:
00309       status = HAL_ERROR;
00310       break;
00311   }
00312 
00313   return status;
00314 }
00315 #endif /* USE_HAL_WWDG_REGISTER_CALLBACKS */
00316 
00317 /**
00318   * @}
00319   */
00320 
00321 /** @defgroup WWDG_Exported_Functions_Group2 IO operation functions
00322   *  @brief    IO operation functions
00323   *
00324 @verbatim
00325   ==============================================================================
00326                       ##### IO operation functions #####
00327   ==============================================================================
00328   [..]
00329     This section provides functions allowing to:
00330     (+) Refresh the WWDG.
00331     (+) Handle WWDG interrupt request and associated function callback.
00332 
00333 @endverbatim
00334   * @{
00335   */
00336 
00337 /**
00338   * @brief  Refresh the WWDG.
00339   * @param  hwwdg  pointer to a WWDG_HandleTypeDef structure that contains
00340   *                the configuration information for the specified WWDG module.
00341   * @retval HAL status
00342   */
00343 HAL_StatusTypeDef HAL_WWDG_Refresh(WWDG_HandleTypeDef *hwwdg)
00344 {
00345   /* Write to WWDG CR the WWDG Counter value to refresh with */
00346   WRITE_REG(hwwdg->Instance->CR, (hwwdg->Init.Counter));
00347 
00348   /* Return function status */
00349   return HAL_OK;
00350 }
00351 
00352 /**
00353   * @brief  Handle WWDG interrupt request.
00354   * @note   The Early Wakeup Interrupt (EWI) can be used if specific safety operations
00355   *         or data logging must be performed before the actual reset is generated.
00356   *         The EWI interrupt is enabled by calling HAL_WWDG_Init function with
00357   *         EWIMode set to WWDG_EWI_ENABLE.
00358   *         When the downcounter reaches the value 0x40, and EWI interrupt is
00359   *         generated and the corresponding Interrupt Service Routine (ISR) can
00360   *         be used to trigger specific actions (such as communications or data
00361   *         logging), before resetting the device.
00362   * @param  hwwdg  pointer to a WWDG_HandleTypeDef structure that contains
00363   *                the configuration information for the specified WWDG module.
00364   * @retval None
00365   */
00366 void HAL_WWDG_IRQHandler(WWDG_HandleTypeDef *hwwdg)
00367 {
00368   /* Check if Early Wakeup Interrupt is enable */
00369   if (__HAL_WWDG_GET_IT_SOURCE(hwwdg, WWDG_IT_EWI) != RESET)
00370   {
00371     /* Check if WWDG Early Wakeup Interrupt occurred */
00372     if (__HAL_WWDG_GET_FLAG(hwwdg, WWDG_FLAG_EWIF) != RESET)
00373     {
00374       /* Clear the WWDG Early Wakeup flag */
00375       __HAL_WWDG_CLEAR_FLAG(hwwdg, WWDG_FLAG_EWIF);
00376 
00377 #if (USE_HAL_WWDG_REGISTER_CALLBACKS == 1)
00378       /* Early Wakeup registered callback */
00379       hwwdg->EwiCallback(hwwdg);
00380 #else
00381       /* Early Wakeup callback */
00382       HAL_WWDG_EarlyWakeupCallback(hwwdg);
00383 #endif /* USE_HAL_WWDG_REGISTER_CALLBACKS */
00384     }
00385   }
00386 }
00387 
00388 
00389 /**
00390   * @brief  WWDG Early Wakeup callback.
00391   * @param  hwwdg  pointer to a WWDG_HandleTypeDef structure that contains
00392   *                the configuration information for the specified WWDG module.
00393   * @retval None
00394   */
00395 __weak void HAL_WWDG_EarlyWakeupCallback(WWDG_HandleTypeDef *hwwdg)
00396 {
00397   /* Prevent unused argument(s) compilation warning */
00398   UNUSED(hwwdg);
00399 
00400   /* NOTE: This function should not be modified, when the callback is needed,
00401            the HAL_WWDG_EarlyWakeupCallback could be implemented in the user file
00402    */
00403 }
00404 
00405 /**
00406   * @}
00407   */
00408 
00409 /**
00410   * @}
00411   */
00412 
00413 #endif /* HAL_WWDG_MODULE_ENABLED */
00414 /**
00415   * @}
00416   */
00417 
00418 /**
00419   * @}
00420   */