STM32F439xx HAL User Manual
stm32f4xx_hal_wwdg.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f4xx_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 de-initialization functions
00009   *           + IO operation functions
00010   *           + Peripheral State functions
00011   @verbatim
00012   ==============================================================================
00013                       ##### WWDG specific features #####
00014   ==============================================================================
00015   [..] 
00016     Once enabled the WWDG generates a system reset on expiry of a programmed
00017     time period, unless the program refreshes the counter (downcounter) 
00018     before reaching 0x3F value (i.e. a reset is generated when the counter
00019     value rolls over from 0x40 to 0x3F). 
00020        
00021     (+) An MCU reset is also generated if the counter value is refreshed
00022         before the counter has reached the refresh window value. This 
00023         implies that the counter must be refreshed in a limited window.
00024     (+) Once enabled the WWDG cannot be disabled except by a system reset.
00025     (+) WWDGRST flag in RCC_CSR register can be used to inform when a WWDG
00026         reset occurs.               
00027     (+) The WWDG counter input clock is derived from the APB clock divided 
00028         by a programmable prescaler.
00029     (+) WWDG clock (Hz) = PCLK1 / (4096 * Prescaler)
00030     (+) WWDG timeout (mS) = 1000 * Counter / WWDG clock
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     
00035     (+) Min-max timeout value at 50 MHz(PCLK1): 81.9 us / 41.9 ms 
00036 
00037     (+) The Early Wakeup Interrupt (EWI) can be used if specific safety 
00038         operations or data logging must be performed before the actual reset is
00039         generated. When the downcounter reaches the value 0x40, an EWI interrupt
00040         is generated and the corresponding interrupt service routine (ISR) can 
00041         be used to trigger specific actions (such as communications or data 
00042         logging), before resetting the device.
00043         In some applications, the EWI interrupt can be used to manage a software
00044         system check and/or system recovery/graceful degradation, without 
00045         generating a WWDG reset. In this case, the corresponding interrupt 
00046         service routine (ISR) should reload the WWDG counter to avoid the WWDG 
00047         reset, then trigger the required actions.
00048         Note:When the EWI interrupt cannot be served, e.g. due to a system lock 
00049         in a higher priority task, the WWDG reset will eventually be generated.
00050 
00051     (+) Debug mode : When the microcontroller enters debug mode (core halted),
00052         the WWDG counter either continues to work normally or stops, depending 
00053         on DBG_WWDG_STOP configuration bit in DBG module, accessible through
00054         __HAL_DBGMCU_FREEZE_WWDG() and __HAL_DBGMCU_UNFREEZE_WWDG() macros
00055 
00056                      ##### How to use this driver #####
00057   ==============================================================================
00058   [..]
00059     (+) Enable WWDG APB1 clock using __HAL_RCC_WWDG_CLK_ENABLE().
00060 
00061     (+) Set the WWDG prescaler, refresh window, counter value and Early Wakeup 
00062         Interrupt mode using using HAL_WWDG_Init() function.
00063         This enables WWDG peripheral and the downcounter starts downcounting 
00064         from given counter value.
00065         Init function can be called again to modify all watchdog parameters, 
00066         however if EWI mode has been set once, it can't be clear until next 
00067         reset.
00068 
00069     (+) The application program must refresh the WWDG counter at regular
00070         intervals during normal operation to prevent an MCU reset using
00071         HAL_WWDG_Refresh() function. This operation must occur only when
00072         the counter is lower than the window value already programmed.
00073 
00074     (+) if Early Wakeup Interrupt mode is enable an interrupt is generated when 
00075         the counter reaches 0x40. User can add his own code in weak function 
00076         HAL_WWDG_EarlyWakeupCallback().
00077 
00078      *** WWDG HAL driver macros list ***
00079      ==================================
00080      [..]
00081        Below the list of most used macros in WWDG HAL driver.
00082 
00083       (+) __HAL_WWDG_GET_IT_SOURCE: Check the selected WWDG's interrupt source.
00084       (+) __HAL_WWDG_GET_FLAG: Get the selected WWDG's flag status.
00085       (+) __HAL_WWDG_CLEAR_FLAG: Clear the WWDG's pending flags.
00086 
00087   @endverbatim
00088   ******************************************************************************
00089   * @attention
00090   *
00091   * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
00092   *
00093   * Redistribution and use in source and binary forms, with or without modification,
00094   * are permitted provided that the following conditions are met:
00095   *   1. Redistributions of source code must retain the above copyright notice,
00096   *      this list of conditions and the following disclaimer.
00097   *   2. Redistributions in binary form must reproduce the above copyright notice,
00098   *      this list of conditions and the following disclaimer in the documentation
00099   *      and/or other materials provided with the distribution.
00100   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00101   *      may be used to endorse or promote products derived from this software
00102   *      without specific prior written permission.
00103   *
00104   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00105   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00106   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00107   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00108   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00109   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00110   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00111   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00112   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00113   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00114   *
00115   ******************************************************************************
00116   */
00117 
00118 /* Includes ------------------------------------------------------------------*/
00119 #include "stm32f4xx_hal.h"
00120 
00121 /** @addtogroup STM32F4xx_HAL_Driver
00122   * @{
00123   */
00124 
00125 #ifdef HAL_WWDG_MODULE_ENABLED
00126 /** @defgroup WWDG WWDG
00127   * @brief WWDG HAL module driver.
00128   * @{
00129   */
00130 
00131 /* Private typedef -----------------------------------------------------------*/
00132 /* Private define ------------------------------------------------------------*/
00133 /* Private macro -------------------------------------------------------------*/
00134 /* Private variables ---------------------------------------------------------*/
00135 /* Private function prototypes -----------------------------------------------*/
00136 /* Exported functions --------------------------------------------------------*/
00137 
00138 /** @defgroup WWDG_Exported_Functions WWDG Exported Functions
00139   * @{
00140   */
00141 
00142 /** @defgroup WWDG_Exported_Functions_Group1 Initialization and Configuration functions
00143   *  @brief    Initialization and Configuration functions.
00144   *
00145 @verbatim
00146   ==============================================================================
00147           ##### Initialization and Configuration functions #####
00148   ==============================================================================
00149   [..]  
00150     This section provides functions allowing to:
00151       (+) Initialize and start the WWDG according to the specified parameters
00152           in the WWDG_InitTypeDef of associated handle.
00153       (+) Initialize the WWDG MSP.
00154 
00155 @endverbatim
00156   * @{
00157   */
00158 
00159 /**
00160   * @brief  Initialize the WWDG according to the specified.
00161   *         parameters in the WWDG_InitTypeDef of  associated handle.
00162   * @param  hwwdg  pointer to a WWDG_HandleTypeDef structure that contains
00163   *                the configuration information for the specified WWDG module.
00164   * @retval HAL status
00165   */
00166 HAL_StatusTypeDef HAL_WWDG_Init(WWDG_HandleTypeDef *hwwdg)
00167 {
00168   /* Check the WWDG handle allocation */
00169   if(hwwdg == NULL)
00170   {
00171     return HAL_ERROR;
00172   }
00173 
00174   /* Check the parameters */
00175   assert_param(IS_WWDG_ALL_INSTANCE(hwwdg->Instance));
00176   assert_param(IS_WWDG_PRESCALER(hwwdg->Init.Prescaler));
00177   assert_param(IS_WWDG_WINDOW(hwwdg->Init.Window));
00178   assert_param(IS_WWDG_COUNTER(hwwdg->Init.Counter));
00179   assert_param(IS_WWDG_EWI_MODE(hwwdg->Init.EWIMode));
00180 
00181   /* Init the low level hardware */
00182   HAL_WWDG_MspInit(hwwdg);
00183 
00184   /* Set WWDG Counter */
00185   WRITE_REG(hwwdg->Instance->CR, (WWDG_CR_WDGA | hwwdg->Init.Counter));
00186 
00187   /* Set WWDG Prescaler and Window */
00188   WRITE_REG(hwwdg->Instance->CFR, (hwwdg->Init.EWIMode | hwwdg->Init.Prescaler | hwwdg->Init.Window));
00189 
00190   /* Return function status */
00191   return HAL_OK;
00192 }
00193 
00194 /**
00195   * @brief  Initialize the WWDG MSP.
00196   * @param  hwwdg  pointer to a WWDG_HandleTypeDef structure that contains
00197   *                the configuration information for the specified WWDG module.
00198   * @note   When rewriting this function in user file, mechanism may be added
00199   *         to avoid multiple initialize when HAL_WWDG_Init function is called
00200   *         again to change parameters.
00201   * @retval None
00202   */
00203 __weak void HAL_WWDG_MspInit(WWDG_HandleTypeDef *hwwdg)
00204 {
00205   /* Prevent unused argument(s) compilation warning */
00206   UNUSED(hwwdg);
00207 
00208   /* NOTE: This function should not be modified, when the callback is needed,
00209            the HAL_WWDG_MspInit could be implemented in the user file
00210    */
00211 }
00212 
00213 /**
00214   * @}
00215   */
00216 
00217 /** @defgroup WWDG_Exported_Functions_Group2 IO operation functions
00218  *  @brief    IO operation functions 
00219  *
00220 @verbatim
00221   ==============================================================================
00222                       ##### IO operation functions #####
00223   ==============================================================================  
00224   [..]
00225     This section provides functions allowing to:
00226     (+) Refresh the WWDG.
00227     (+) Handle WWDG interrupt request and associated function callback.
00228 
00229 @endverbatim
00230   * @{
00231   */
00232 
00233 /**
00234   * @brief  Refresh the WWDG.
00235   * @param  hwwdg  pointer to a WWDG_HandleTypeDef structure that contains
00236   *                the configuration information for the specified WWDG module.
00237   * @retval HAL status
00238   */
00239 HAL_StatusTypeDef HAL_WWDG_Refresh(WWDG_HandleTypeDef *hwwdg)
00240 {
00241   /* Write to WWDG CR the WWDG Counter value to refresh with */
00242   WRITE_REG(hwwdg->Instance->CR, (hwwdg->Init.Counter));
00243 
00244   /* Return function status */
00245   return HAL_OK;
00246 }
00247 
00248 /**
00249   * @brief  Handle WWDG interrupt request.
00250   * @note   The Early Wakeup Interrupt (EWI) can be used if specific safety operations
00251   *         or data logging must be performed before the actual reset is generated.
00252   *         The EWI interrupt is enabled by calling HAL_WWDG_Init function with 
00253   *         EWIMode set to WWDG_EWI_ENABLE.
00254   *         When the downcounter reaches the value 0x40, and EWI interrupt is
00255   *         generated and the corresponding Interrupt Service Routine (ISR) can
00256   *         be used to trigger specific actions (such as communications or data
00257   *         logging), before resetting the device.
00258   * @param  hwwdg  pointer to a WWDG_HandleTypeDef structure that contains
00259   *                the configuration information for the specified WWDG module.
00260   * @retval None
00261   */
00262 void HAL_WWDG_IRQHandler(WWDG_HandleTypeDef *hwwdg)
00263 {
00264   /* Check if Early Wakeup Interrupt is enable */
00265   if(__HAL_WWDG_GET_IT_SOURCE(hwwdg, WWDG_IT_EWI) != RESET)
00266   {
00267     /* Check if WWDG Early Wakeup Interrupt occurred */
00268     if(__HAL_WWDG_GET_FLAG(hwwdg, WWDG_FLAG_EWIF) != RESET)
00269     {
00270       /* Clear the WWDG Early Wakeup flag */
00271       __HAL_WWDG_CLEAR_FLAG(hwwdg, WWDG_FLAG_EWIF);
00272 
00273       /* Early Wakeup callback */ 
00274       HAL_WWDG_EarlyWakeupCallback(hwwdg);
00275     }
00276   }
00277 }
00278 
00279 /**
00280   * @brief  WWDG Early Wakeup callback.
00281   * @param  hwwdg  pointer to a WWDG_HandleTypeDef structure that contains
00282   *                the configuration information for the specified WWDG module.
00283   * @retval None
00284   */
00285 __weak void HAL_WWDG_EarlyWakeupCallback(WWDG_HandleTypeDef* hwwdg)
00286 {
00287   /* Prevent unused argument(s) compilation warning */
00288   UNUSED(hwwdg);
00289 
00290   /* NOTE: This function should not be modified, when the callback is needed,
00291            the HAL_WWDG_EarlyWakeupCallback could be implemented in the user file
00292    */
00293 }
00294 
00295 /**
00296   * @}
00297   */
00298 
00299 /**
00300   * @}
00301   */
00302 
00303 #endif /* HAL_WWDG_MODULE_ENABLED */
00304 /**
00305   * @}
00306   */
00307 
00308 /**
00309   * @}
00310   */
00311 
00312 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/