STM32L486xx 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   @verbatim
00011   ==============================================================================
00012                       ##### WWDG Specific features #####
00013   ==============================================================================
00014   [..]
00015     Once enabled the WWDG generates a system reset on expiry of a programmed
00016     time period, unless the program refreshes the counter (T[6;0] downcounter)
00017     before reaching 0x3F value (i.e. a reset is generated when the counter
00018     value rolls down from 0x40 to 0x3F).
00019 
00020     (+) An MCU reset is also generated if the counter value is refreshed
00021         before the counter has reached the refresh window value. This
00022         implies that the counter must be refreshed in a limited window.
00023     (+) Once enabled the WWDG cannot be disabled except by a system reset.
00024     (+) WWDGRST flag in RCC CSR register can be used to inform when a WWDG
00025         reset occurs.
00026     (+) The WWDG counter input clock is derived from the APB clock divided
00027         by a programmable prescaler.
00028     (+) WWDG clock (Hz) = PCLK1 / (4096 * Prescaler)
00029     (+) WWDG timeout (mS) = 1000 * (T[5;0] + 1) / WWDG clock (Hz)
00030         where T[5;0] are the lowest 6 bits of Counter.
00031     (+) WWDG Counter refresh is allowed between the following limits :
00032         (++) min time (mS) = 1000 * (Counter - Window) / WWDG clock
00033         (++) max time (mS) = 1000 * (Counter - 0x40) / WWDG clock
00034     (+) Typical values:
00035         (++) Counter min (T[5;0] = 0x00) @80 MHz(PCLK1) with zero prescaler:
00036              max timeout before reset: ~51.2 µs
00037         (++) Counter max (T[5;0] = 0x3F) @80 MHz(PCLK1) with prescaler dividing by 128:
00038              max timeout before reset: ~26.22 ms
00039 
00040   ==============================================================================
00041                      ##### How to use this driver #####
00042   ==============================================================================
00043   [..]
00044     *** Common driver usage ***
00045     ===========================
00046     (+) Enable WWDG APB1 clock using __HAL_RCC_WWDG_CLK_ENABLE().
00047     (+) Set the WWDG prescaler, refresh window and counter value
00048         using HAL_WWDG_Init() function.
00049     (+) Start the WWDG using HAL_WWDG_Start() function.
00050         When the WWDG is enabled the counter value should be configured to
00051         a value greater than 0x40 to prevent generating an immediate reset.
00052     (+) Optionally you can enable the Early Wakeup Interrupt (EWI) which is
00053         generated when the counter reaches 0x40, and then start the WWDG using
00054         HAL_WWDG_Start_IT(). At EWI HAL_WWDG_WakeupCallback is executed and user can
00055         add his own code by customization of callback HAL_WWDG_WakeupCallback.
00056         Once enabled, EWI interrupt cannot be disabled except by a system reset.
00057     (+) Then the application program must refresh the WWDG counter at regular
00058         intervals during normal operation to prevent an MCU reset, using
00059         HAL_WWDG_Refresh() function. This operation must occur only when
00060         the counter is lower than the refresh window value already programmed.
00061 
00062   [..]
00063     *** Callback registration ***
00064     =============================
00065     The compilation define  USE_HAL_WWDG_REGISTER_CALLBACKS when set to 1 allows
00066     the user to configure dynamically the driver callbacks. Use Functions
00067     @ref HAL_WWDG_RegisterCallback() to register a user callback.
00068 
00069     (+) Function @ref HAL_WWDG_RegisterCallback() allows to register following
00070         callbacks:
00071         (++) EwiCallback : callback for Early WakeUp Interrupt.
00072         (++) MspInitCallback : WWDG MspInit.
00073     This function takes as parameters the HAL peripheral handle, the Callback ID
00074     and a pointer to the user callback function.
00075 
00076     (+) Use function @ref HAL_WWDG_UnRegisterCallback() to reset a callback to
00077     the default weak (surcharged) function. @ref HAL_WWDG_UnRegisterCallback()
00078     takes as parameters the HAL peripheral handle and the Callback ID.
00079     This function allows to reset following callbacks:
00080         (++) EwiCallback : callback for  Early WakeUp Interrupt.
00081         (++) MspInitCallback : WWDG MspInit.
00082 
00083     When calling @ref HAL_WWDG_Init function, callbacks are reset to the
00084     corresponding legacy weak (surcharged) functions: 
00085     @ref HAL_WWDG_EarlyWakeupCallback() and HAL_WWDG_MspInit() only if they have
00086     not been registered before.
00087 
00088     When compilation define USE_HAL_WWDG_REGISTER_CALLBACKS is set to 0 or
00089     not defined, the callback registering feature is not available 
00090     and weak (surcharged) callbacks are used.
00091 
00092     *** WWDG HAL driver macros list ***
00093     ===================================
00094     [..]
00095       Below the list of most used macros in WWDG HAL driver.
00096       (+) __HAL_WWDG_ENABLE: Enable the WWDG peripheral
00097       (+) __HAL_WWDG_GET_FLAG: Get the selected WWDG's flag status
00098       (+) __HAL_WWDG_CLEAR_FLAG: Clear the WWDG's pending flags
00099       (+) __HAL_WWDG_ENABLE_IT: Enable the WWDG early wakeup interrupt
00100 
00101   @endverbatim
00102   ******************************************************************************
00103   * @attention
00104   *
00105   * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
00106   *
00107   * Redistribution and use in source and binary forms, with or without modification,
00108   * are permitted provided that the following conditions are met:
00109   *   1. Redistributions of source code must retain the above copyright notice,
00110   *      this list of conditions and the following disclaimer.
00111   *   2. Redistributions in binary form must reproduce the above copyright notice,
00112   *      this list of conditions and the following disclaimer in the documentation
00113   *      and/or other materials provided with the distribution.
00114   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00115   *      may be used to endorse or promote products derived from this software
00116   *      without specific prior written permission.
00117   *
00118   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00119   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00120   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00121   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00122   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00123   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00124   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00125   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00126   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00127   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00128   *
00129   ******************************************************************************
00130   */
00131 
00132 /* Includes ------------------------------------------------------------------*/
00133 #include "stm32l4xx_hal.h"
00134 
00135 /** @addtogroup STM32L4xx_HAL_Driver
00136   * @{
00137   */
00138 
00139 #ifdef HAL_WWDG_MODULE_ENABLED
00140 /** @defgroup WWDG WWDG
00141   * @brief WWDG HAL module driver.
00142   * @{
00143   */
00144 
00145 /* Private typedef -----------------------------------------------------------*/
00146 /* Private define ------------------------------------------------------------*/
00147 /* Private macro -------------------------------------------------------------*/
00148 /* Private variables ---------------------------------------------------------*/
00149 /* Private function prototypes -----------------------------------------------*/
00150 /* Exported functions --------------------------------------------------------*/
00151 
00152 /** @defgroup WWDG_Exported_Functions WWDG Exported Functions
00153   * @{
00154   */
00155 
00156 /** @defgroup WWDG_Exported_Functions_Group1 Initialization and Configuration functions
00157  *  @brief    Initialization and Configuration functions.
00158  *
00159 @verbatim
00160   ==============================================================================
00161           ##### Initialization and Configuration functions #####
00162   ==============================================================================
00163   [..]
00164     This section provides functions allowing to:
00165       (+) Initialize and start the WWDG according to the specified parameters
00166           in the WWDG_InitTypeDef of associated handle.
00167       (+) Initialize the WWDG MSP.
00168 
00169 @endverbatim
00170   * @{
00171   */
00172 
00173 /**
00174   * @brief  Initialize the WWDG according to the specified.
00175   *         parameters in the WWDG_InitTypeDef of  associated handle.
00176   * @param  hwwdg  pointer to a WWDG_HandleTypeDef structure that contains
00177   *                the configuration information for the specified WWDG module.
00178   * @retval HAL status
00179   */
00180 HAL_StatusTypeDef HAL_WWDG_Init(WWDG_HandleTypeDef *hwwdg)
00181 {
00182   /* Check the WWDG handle allocation */
00183   if (hwwdg == NULL)
00184   {
00185     return HAL_ERROR;
00186   }
00187 
00188   /* Check the parameters */
00189   assert_param(IS_WWDG_ALL_INSTANCE(hwwdg->Instance));
00190   assert_param(IS_WWDG_PRESCALER(hwwdg->Init.Prescaler));
00191   assert_param(IS_WWDG_WINDOW(hwwdg->Init.Window));
00192   assert_param(IS_WWDG_COUNTER(hwwdg->Init.Counter));
00193   assert_param(IS_WWDG_EWI_MODE(hwwdg->Init.EWIMode));
00194 
00195 #if (USE_HAL_WWDG_REGISTER_CALLBACKS == 1)
00196   /* Reset Callback pointers */
00197   if(hwwdg->EwiCallback == NULL)
00198   {
00199     hwwdg->EwiCallback = HAL_WWDG_EarlyWakeupCallback;
00200   }
00201 
00202   if(hwwdg->MspInitCallback == NULL)
00203   {
00204     hwwdg->MspInitCallback = HAL_WWDG_MspInit;
00205   }
00206 
00207   /* Init the low level hardware */
00208   hwwdg->MspInitCallback(hwwdg);
00209 #else
00210   /* Init the low level hardware */
00211   HAL_WWDG_MspInit(hwwdg);
00212 #endif
00213 
00214   /* Set WWDG Counter */
00215   WRITE_REG(hwwdg->Instance->CR, (WWDG_CR_WDGA | hwwdg->Init.Counter));
00216 
00217   /* Set WWDG Prescaler and Window */
00218   WRITE_REG(hwwdg->Instance->CFR, (hwwdg->Init.EWIMode | hwwdg->Init.Prescaler | hwwdg->Init.Window));
00219 
00220   /* Return function status */
00221   return HAL_OK;
00222 }
00223 
00224 
00225 /**
00226   * @brief  Initialize the WWDG MSP.
00227   * @param  hwwdg  pointer to a WWDG_HandleTypeDef structure that contains
00228   *                the configuration information for the specified WWDG module.
00229   * @note   When rewriting this function in user file, mechanism may be added
00230   *         to avoid multiple initialize when HAL_WWDG_Init function is called
00231   *         again to change parameters.
00232   * @retval None
00233   */
00234 __weak void HAL_WWDG_MspInit(WWDG_HandleTypeDef *hwwdg)
00235 {
00236   /* Prevent unused argument(s) compilation warning */
00237   UNUSED(hwwdg);
00238 
00239   /* NOTE: This function should not be modified, when the callback is needed,
00240            the HAL_WWDG_MspInit could be implemented in the user file
00241    */
00242 }
00243 
00244 
00245 #if (USE_HAL_WWDG_REGISTER_CALLBACKS == 1)
00246 /**
00247   * @brief  Register a User WWDG Callback
00248   *         To be used instead of the weak (surcharged) predefined callback
00249   * @param  hwwdg WWDG handle
00250   * @param  CallbackID ID of the callback to be registered
00251   *         This parameter can be one of the following values:
00252   *           @arg @ref HAL_WWDG_EWI_CB_ID Early WakeUp Interrupt Callback ID
00253   *           @arg @ref HAL_WWDG_MSPINIT_CB_ID MspInit callback ID
00254   * @param  pCallback pointer to the Callback function
00255   * @retval status
00256   */
00257 HAL_StatusTypeDef HAL_WWDG_RegisterCallback(WWDG_HandleTypeDef *hwwdg, HAL_WWDG_CallbackIDTypeDef CallbackID, pWWDG_CallbackTypeDef pCallback)
00258 {
00259   HAL_StatusTypeDef status = HAL_OK;
00260 
00261   if(pCallback == NULL)
00262   {
00263     status = HAL_ERROR;
00264   }
00265   else
00266   {
00267     switch(CallbackID)
00268     {
00269       case HAL_WWDG_EWI_CB_ID:
00270         hwwdg->EwiCallback = pCallback;
00271         break;
00272 
00273       case HAL_WWDG_MSPINIT_CB_ID:
00274         hwwdg->MspInitCallback = pCallback;
00275         break;
00276 
00277       default:
00278         status = HAL_ERROR;
00279         break;
00280     }
00281   }
00282 
00283   return status;
00284 }
00285 
00286 
00287 /**
00288   * @brief  Unregister a WWDG Callback
00289   *         WWDG Callback is redirected to the weak (surcharged) predefined callback 
00290   * @param  hwwdg WWDG handle
00291   * @param  CallbackID ID of the callback to be registered
00292   *         This parameter can be one of the following values:
00293   *           @arg @ref HAL_WWDG_EWI_CB_ID Early WakeUp Interrupt Callback ID
00294   *           @arg @ref HAL_WWDG_MSPINIT_CB_ID MspInit callback ID
00295   * @retval status
00296   */
00297 HAL_StatusTypeDef HAL_WWDG_UnRegisterCallback(WWDG_HandleTypeDef *hwwdg, HAL_WWDG_CallbackIDTypeDef CallbackID)
00298 {
00299   HAL_StatusTypeDef status = HAL_OK;
00300 
00301   switch(CallbackID)
00302   {
00303     case HAL_WWDG_EWI_CB_ID:
00304       hwwdg->EwiCallback = HAL_WWDG_EarlyWakeupCallback;
00305       break;
00306 
00307     case HAL_WWDG_MSPINIT_CB_ID:
00308       hwwdg->MspInitCallback = HAL_WWDG_MspInit;
00309       break;
00310 
00311     default:
00312       status = HAL_ERROR;
00313       break;
00314   }
00315 
00316   return status;
00317 }
00318 #endif
00319 
00320 /**
00321   * @}
00322   */
00323 
00324 /** @defgroup WWDG_Exported_Functions_Group2 IO operation functions
00325  *  @brief    IO operation functions
00326  *
00327 @verbatim
00328   ==============================================================================
00329                       ##### IO operation functions #####
00330   ==============================================================================
00331   [..]
00332     This section provides functions allowing to:
00333     (+) Refresh the WWDG.
00334     (+) Handle WWDG interrupt request and associated function callback.
00335 
00336 @endverbatim
00337   * @{
00338   */
00339 
00340 /**
00341   * @brief  Refresh the WWDG.
00342   * @param  hwwdg  pointer to a WWDG_HandleTypeDef structure that contains
00343   *                the configuration information for the specified WWDG module.
00344   * @retval HAL status
00345   */
00346 HAL_StatusTypeDef HAL_WWDG_Refresh(WWDG_HandleTypeDef *hwwdg)
00347 {
00348   /* Write to WWDG CR the WWDG Counter value to refresh with */
00349   WRITE_REG(hwwdg->Instance->CR, (hwwdg->Init.Counter));
00350 
00351   /* Return function status */
00352   return HAL_OK;
00353 }
00354 
00355 /**
00356   * @brief  Handle WWDG interrupt request.
00357   * @note   The Early Wakeup Interrupt (EWI) can be used if specific safety operations
00358   *         or data logging must be performed before the actual reset is generated.
00359   *         The EWI interrupt is enabled by calling HAL_WWDG_Init function with
00360   *         EWIMode set to WWDG_EWI_ENABLE.
00361   *         When the downcounter reaches the value 0x40, and EWI interrupt is
00362   *         generated and the corresponding Interrupt Service Routine (ISR) can
00363   *         be used to trigger specific actions (such as communications or data
00364   *         logging), before resetting the device.
00365   * @param  hwwdg  pointer to a WWDG_HandleTypeDef structure that contains
00366   *                the configuration information for the specified WWDG module.
00367   * @retval None
00368   */
00369 void HAL_WWDG_IRQHandler(WWDG_HandleTypeDef *hwwdg)
00370 {
00371   /* Check if Early Wakeup Interrupt is enable */
00372   if (__HAL_WWDG_GET_IT_SOURCE(hwwdg, WWDG_IT_EWI) != RESET)
00373   {
00374     /* Check if WWDG Early Wakeup Interrupt occurred */
00375     if (__HAL_WWDG_GET_FLAG(hwwdg, WWDG_FLAG_EWIF) != RESET)
00376     {
00377       /* Clear the WWDG Early Wakeup flag */
00378       __HAL_WWDG_CLEAR_FLAG(hwwdg, WWDG_FLAG_EWIF);
00379 
00380 #if (USE_HAL_WWDG_REGISTER_CALLBACKS == 1)
00381       /* Early Wakeup registered callback */
00382       hwwdg->EwiCallback(hwwdg);
00383 #else
00384       /* Early Wakeup callback */
00385       HAL_WWDG_EarlyWakeupCallback(hwwdg);
00386 #endif
00387     }
00388   }
00389 }
00390 
00391 
00392 /**
00393   * @brief  WWDG Early Wakeup callback.
00394   * @param  hwwdg  pointer to a WWDG_HandleTypeDef structure that contains
00395   *                the configuration information for the specified WWDG module.
00396   * @retval None
00397   */
00398 __weak void HAL_WWDG_EarlyWakeupCallback(WWDG_HandleTypeDef *hwwdg)
00399 {
00400   /* Prevent unused argument(s) compilation warning */
00401   UNUSED(hwwdg);
00402 
00403   /* NOTE: This function should not be modified, when the callback is needed,
00404            the HAL_WWDG_EarlyWakeupCallback could be implemented in the user file
00405    */
00406 }
00407 
00408 /**
00409   * @}
00410   */
00411 
00412 /**
00413   * @}
00414   */
00415 
00416 #endif /* HAL_WWDG_MODULE_ENABLED */
00417 /**
00418   * @}
00419   */
00420 
00421 /**
00422   * @}
00423   */
00424 
00425 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/