STM32F479xx HAL User Manual
|
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 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 (+) If required by application, an Early Wakeup Interrupt can be triggered 00025 in order to be warned before WWDG expiration. The Early Wakeup Interrupt 00026 (EWI) can be used if specific safety operations or data logging must 00027 be performed before the actual reset is generated. When the downcounter 00028 reaches 0x40, interrupt occurs. This mechanism requires WWDG interrupt 00029 line to be enabled in NVIC. Once enabled, EWI interrupt cannot be 00030 disabled except by a system reset. 00031 (+) WWDGRST flag in RCC CSR register can be used to inform when a WWDG 00032 reset occurs. 00033 (+) The WWDG counter input clock is derived from the APB clock divided 00034 by a programmable prescaler. 00035 (+) WWDG clock (Hz) = PCLK1 / (4096 * Prescaler) 00036 (+) WWDG timeout (mS) = 1000 * (T[5;0] + 1) / WWDG clock (Hz) 00037 where T[5;0] are the lowest 6 bits of Counter. 00038 (+) WWDG Counter refresh is allowed between the following limits : 00039 (++) min time (mS) = 1000 * (Counter - Window) / WWDG clock 00040 (++) max time (mS) = 1000 * (Counter - 0x40) / WWDG clock 00041 (+) Typical values: 00042 (++) Counter min (T[5;0] = 0x00) at 42MHz (PCLK1) with zero prescaler: 00043 max timeout before reset: approximately 97.52µs 00044 (++) Counter max (T[5;0] = 0x3F) at 42MHz (PCLK1) with prescaler 00045 dividing by 8: 00046 max timeout before reset: approximately 49.93ms 00047 00048 ##### How to use this driver ##### 00049 ============================================================================== 00050 00051 *** Common driver usage *** 00052 =========================== 00053 00054 [..] 00055 (+) Enable WWDG APB1 clock using __HAL_RCC_WWDG_CLK_ENABLE(). 00056 (+) Configure the WWDG prescaler, refresh window value, counter value and early 00057 interrupt status using HAL_WWDG_Init() function. This will automatically 00058 enable WWDG and start its downcounter. Time reference can be taken from 00059 function exit. Care must be taken to provide a counter value 00060 greater than 0x40 to prevent generation of immediate reset. 00061 (+) If the Early Wakeup Interrupt (EWI) feature is enabled, an interrupt is 00062 generated when the counter reaches 0x40. When HAL_WWDG_IRQHandler is 00063 triggered by the interrupt service routine, flag will be automatically 00064 cleared and HAL_WWDG_WakeupCallback user callback will be executed. User 00065 can add his own code by customization of callback HAL_WWDG_WakeupCallback. 00066 (+) Then the application program must refresh the WWDG counter at regular 00067 intervals during normal operation to prevent an MCU reset, using 00068 HAL_WWDG_Refresh() function. This operation must occur only when 00069 the counter is lower than the refresh window value already programmed. 00070 00071 *** Callback registration *** 00072 ============================= 00073 00074 [..] 00075 The compilation define USE_HAL_WWDG_REGISTER_CALLBACKS when set to 1 allows 00076 the user to configure dynamically the driver callbacks. Use Functions 00077 HAL_WWDG_RegisterCallback() to register a user callback. 00078 00079 (+) Function HAL_WWDG_RegisterCallback() allows to register following 00080 callbacks: 00081 (++) EwiCallback : callback for Early WakeUp Interrupt. 00082 (++) MspInitCallback : WWDG MspInit. 00083 This function takes as parameters the HAL peripheral handle, the Callback ID 00084 and a pointer to the user callback function. 00085 00086 (+) Use function HAL_WWDG_UnRegisterCallback() to reset a callback to 00087 the default weak (surcharged) function. HAL_WWDG_UnRegisterCallback() 00088 takes as parameters the HAL peripheral handle and the Callback ID. 00089 This function allows to reset following callbacks: 00090 (++) EwiCallback : callback for Early WakeUp Interrupt. 00091 (++) MspInitCallback : WWDG MspInit. 00092 00093 [..] 00094 When calling HAL_WWDG_Init function, callbacks are reset to the 00095 corresponding legacy weak (surcharged) functions: 00096 HAL_WWDG_EarlyWakeupCallback() and HAL_WWDG_MspInit() only if they have 00097 not been registered before. 00098 00099 [..] 00100 When compilation define USE_HAL_WWDG_REGISTER_CALLBACKS is set to 0 or 00101 not defined, the callback registering feature is not available 00102 and weak (surcharged) callbacks are used. 00103 00104 *** WWDG HAL driver macros list *** 00105 =================================== 00106 [..] 00107 Below the list of available macros in WWDG HAL driver. 00108 (+) __HAL_WWDG_ENABLE: Enable the WWDG peripheral 00109 (+) __HAL_WWDG_GET_FLAG: Get the selected WWDG's flag status 00110 (+) __HAL_WWDG_CLEAR_FLAG: Clear the WWDG's pending flags 00111 (+) __HAL_WWDG_ENABLE_IT: Enable the WWDG early wakeup interrupt 00112 00113 @endverbatim 00114 ****************************************************************************** 00115 * @attention 00116 * 00117 * <h2><center>© Copyright (c) 2016 STMicroelectronics. 00118 * All rights reserved.</center></h2> 00119 * 00120 * This software component is licensed by ST under BSD 3-Clause license, 00121 * the "License"; You may not use this file except in compliance with the 00122 * License. You may obtain a copy of the License at: 00123 * opensource.org/licenses/BSD-3-Clause 00124 * 00125 ****************************************************************************** 00126 */ 00127 00128 /* Includes ------------------------------------------------------------------*/ 00129 #include "stm32f4xx_hal.h" 00130 00131 /** @addtogroup STM32F4xx_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 */ 00421 00422 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/