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