stm32f4xx_hal_wwdg.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_wwdg.c
  4. * @author MCD Application Team
  5. * @brief WWDG HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Window Watchdog (WWDG) peripheral:
  8. * + Initialization and Configuration functions
  9. * + IO operation functions
  10. @verbatim
  11. ==============================================================================
  12. ##### WWDG Specific features #####
  13. ==============================================================================
  14. [..]
  15. Once enabled the WWDG generates a system reset on expiry of a programmed
  16. time period, unless the program refreshes the counter (T[6;0] downcounter)
  17. before reaching 0x3F value (i.e. a reset is generated when the counter
  18. value rolls down from 0x40 to 0x3F).
  19. (+) An MCU reset is also generated if the counter value is refreshed
  20. before the counter has reached the refresh window value. This
  21. implies that the counter must be refreshed in a limited window.
  22. (+) Once enabled the WWDG cannot be disabled except by a system reset.
  23. (+) If required by application, an Early Wakeup Interrupt can be triggered
  24. in order to be warned before WWDG expiration. The Early Wakeup Interrupt
  25. (EWI) can be used if specific safety operations or data logging must
  26. be performed before the actual reset is generated. When the downcounter
  27. reaches 0x40, interrupt occurs. This mechanism requires WWDG interrupt
  28. line to be enabled in NVIC. Once enabled, EWI interrupt cannot be
  29. disabled except by a system reset.
  30. (+) WWDGRST flag in RCC CSR register can be used to inform when a WWDG
  31. reset occurs.
  32. (+) The WWDG counter input clock is derived from the APB clock divided
  33. by a programmable prescaler.
  34. (+) WWDG clock (Hz) = PCLK1 / (4096 * Prescaler)
  35. (+) WWDG timeout (mS) = 1000 * (T[5;0] + 1) / WWDG clock (Hz)
  36. where T[5;0] are the lowest 6 bits of Counter.
  37. (+) WWDG Counter refresh is allowed between the following limits :
  38. (++) min time (mS) = 1000 * (Counter - Window) / WWDG clock
  39. (++) max time (mS) = 1000 * (Counter - 0x40) / WWDG clock
  40. (+) Typical values:
  41. (++) Counter min (T[5;0] = 0x00) at 42MHz (PCLK1) with zero prescaler:
  42. max timeout before reset: approximately 97.52µs
  43. (++) Counter max (T[5;0] = 0x3F) at 42MHz (PCLK1) with prescaler
  44. dividing by 8:
  45. max timeout before reset: approximately 49.93ms
  46. ##### How to use this driver #####
  47. ==============================================================================
  48. *** Common driver usage ***
  49. ===========================
  50. [..]
  51. (+) Enable WWDG APB1 clock using __HAL_RCC_WWDG_CLK_ENABLE().
  52. (+) Configure the WWDG prescaler, refresh window value, counter value and early
  53. interrupt status using HAL_WWDG_Init() function. This will automatically
  54. enable WWDG and start its downcounter. Time reference can be taken from
  55. function exit. Care must be taken to provide a counter value
  56. greater than 0x40 to prevent generation of immediate reset.
  57. (+) If the Early Wakeup Interrupt (EWI) feature is enabled, an interrupt is
  58. generated when the counter reaches 0x40. When HAL_WWDG_IRQHandler is
  59. triggered by the interrupt service routine, flag will be automatically
  60. cleared and HAL_WWDG_WakeupCallback user callback will be executed. User
  61. can add his own code by customization of callback HAL_WWDG_WakeupCallback.
  62. (+) Then the application program must refresh the WWDG counter at regular
  63. intervals during normal operation to prevent an MCU reset, using
  64. HAL_WWDG_Refresh() function. This operation must occur only when
  65. the counter is lower than the refresh window value already programmed.
  66. *** Callback registration ***
  67. =============================
  68. [..]
  69. The compilation define USE_HAL_WWDG_REGISTER_CALLBACKS when set to 1 allows
  70. the user to configure dynamically the driver callbacks. Use Functions
  71. HAL_WWDG_RegisterCallback() to register a user callback.
  72. (+) Function HAL_WWDG_RegisterCallback() allows to register following
  73. callbacks:
  74. (++) EwiCallback : callback for Early WakeUp Interrupt.
  75. (++) MspInitCallback : WWDG MspInit.
  76. This function takes as parameters the HAL peripheral handle, the Callback ID
  77. and a pointer to the user callback function.
  78. (+) Use function HAL_WWDG_UnRegisterCallback() to reset a callback to
  79. the default weak (surcharged) function. HAL_WWDG_UnRegisterCallback()
  80. takes as parameters the HAL peripheral handle and the Callback ID.
  81. This function allows to reset following callbacks:
  82. (++) EwiCallback : callback for Early WakeUp Interrupt.
  83. (++) MspInitCallback : WWDG MspInit.
  84. [..]
  85. When calling HAL_WWDG_Init function, callbacks are reset to the
  86. corresponding legacy weak (surcharged) functions:
  87. HAL_WWDG_EarlyWakeupCallback() and HAL_WWDG_MspInit() only if they have
  88. not been registered before.
  89. [..]
  90. When compilation define USE_HAL_WWDG_REGISTER_CALLBACKS is set to 0 or
  91. not defined, the callback registering feature is not available
  92. and weak (surcharged) callbacks are used.
  93. *** WWDG HAL driver macros list ***
  94. ===================================
  95. [..]
  96. Below the list of available macros in WWDG HAL driver.
  97. (+) __HAL_WWDG_ENABLE: Enable the WWDG peripheral
  98. (+) __HAL_WWDG_GET_FLAG: Get the selected WWDG's flag status
  99. (+) __HAL_WWDG_CLEAR_FLAG: Clear the WWDG's pending flags
  100. (+) __HAL_WWDG_ENABLE_IT: Enable the WWDG early wakeup interrupt
  101. @endverbatim
  102. ******************************************************************************
  103. * @attention
  104. *
  105. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  106. * All rights reserved.</center></h2>
  107. *
  108. * This software component is licensed by ST under BSD 3-Clause license,
  109. * the "License"; You may not use this file except in compliance with the
  110. * License. You may obtain a copy of the License at:
  111. * opensource.org/licenses/BSD-3-Clause
  112. *
  113. ******************************************************************************
  114. */
  115. /* Includes ------------------------------------------------------------------*/
  116. #include "stm32f4xx_hal.h"
  117. /** @addtogroup STM32F4xx_HAL_Driver
  118. * @{
  119. */
  120. #ifdef HAL_WWDG_MODULE_ENABLED
  121. /** @defgroup WWDG WWDG
  122. * @brief WWDG HAL module driver.
  123. * @{
  124. */
  125. /* Private typedef -----------------------------------------------------------*/
  126. /* Private define ------------------------------------------------------------*/
  127. /* Private macro -------------------------------------------------------------*/
  128. /* Private variables ---------------------------------------------------------*/
  129. /* Private function prototypes -----------------------------------------------*/
  130. /* Exported functions --------------------------------------------------------*/
  131. /** @defgroup WWDG_Exported_Functions WWDG Exported Functions
  132. * @{
  133. */
  134. /** @defgroup WWDG_Exported_Functions_Group1 Initialization and Configuration functions
  135. * @brief Initialization and Configuration functions.
  136. *
  137. @verbatim
  138. ==============================================================================
  139. ##### Initialization and Configuration functions #####
  140. ==============================================================================
  141. [..]
  142. This section provides functions allowing to:
  143. (+) Initialize and start the WWDG according to the specified parameters
  144. in the WWDG_InitTypeDef of associated handle.
  145. (+) Initialize the WWDG MSP.
  146. @endverbatim
  147. * @{
  148. */
  149. /**
  150. * @brief Initialize the WWDG according to the specified.
  151. * parameters in the WWDG_InitTypeDef of associated handle.
  152. * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
  153. * the configuration information for the specified WWDG module.
  154. * @retval HAL status
  155. */
  156. HAL_StatusTypeDef HAL_WWDG_Init(WWDG_HandleTypeDef *hwwdg)
  157. {
  158. /* Check the WWDG handle allocation */
  159. if (hwwdg == NULL)
  160. {
  161. return HAL_ERROR;
  162. }
  163. /* Check the parameters */
  164. assert_param(IS_WWDG_ALL_INSTANCE(hwwdg->Instance));
  165. assert_param(IS_WWDG_PRESCALER(hwwdg->Init.Prescaler));
  166. assert_param(IS_WWDG_WINDOW(hwwdg->Init.Window));
  167. assert_param(IS_WWDG_COUNTER(hwwdg->Init.Counter));
  168. assert_param(IS_WWDG_EWI_MODE(hwwdg->Init.EWIMode));
  169. #if (USE_HAL_WWDG_REGISTER_CALLBACKS == 1)
  170. /* Reset Callback pointers */
  171. if (hwwdg->EwiCallback == NULL)
  172. {
  173. hwwdg->EwiCallback = HAL_WWDG_EarlyWakeupCallback;
  174. }
  175. if (hwwdg->MspInitCallback == NULL)
  176. {
  177. hwwdg->MspInitCallback = HAL_WWDG_MspInit;
  178. }
  179. /* Init the low level hardware */
  180. hwwdg->MspInitCallback(hwwdg);
  181. #else
  182. /* Init the low level hardware */
  183. HAL_WWDG_MspInit(hwwdg);
  184. #endif /* USE_HAL_WWDG_REGISTER_CALLBACKS */
  185. /* Set WWDG Counter */
  186. WRITE_REG(hwwdg->Instance->CR, (WWDG_CR_WDGA | hwwdg->Init.Counter));
  187. /* Set WWDG Prescaler and Window */
  188. WRITE_REG(hwwdg->Instance->CFR, (hwwdg->Init.EWIMode | hwwdg->Init.Prescaler | hwwdg->Init.Window));
  189. /* Return function status */
  190. return HAL_OK;
  191. }
  192. /**
  193. * @brief Initialize the WWDG MSP.
  194. * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
  195. * the configuration information for the specified WWDG module.
  196. * @note When rewriting this function in user file, mechanism may be added
  197. * to avoid multiple initialize when HAL_WWDG_Init function is called
  198. * again to change parameters.
  199. * @retval None
  200. */
  201. __weak void HAL_WWDG_MspInit(WWDG_HandleTypeDef *hwwdg)
  202. {
  203. /* Prevent unused argument(s) compilation warning */
  204. UNUSED(hwwdg);
  205. /* NOTE: This function should not be modified, when the callback is needed,
  206. the HAL_WWDG_MspInit could be implemented in the user file
  207. */
  208. }
  209. #if (USE_HAL_WWDG_REGISTER_CALLBACKS == 1)
  210. /**
  211. * @brief Register a User WWDG Callback
  212. * To be used instead of the weak (surcharged) predefined callback
  213. * @param hwwdg WWDG handle
  214. * @param CallbackID ID of the callback to be registered
  215. * This parameter can be one of the following values:
  216. * @arg @ref HAL_WWDG_EWI_CB_ID Early WakeUp Interrupt Callback ID
  217. * @arg @ref HAL_WWDG_MSPINIT_CB_ID MspInit callback ID
  218. * @param pCallback pointer to the Callback function
  219. * @retval status
  220. */
  221. HAL_StatusTypeDef HAL_WWDG_RegisterCallback(WWDG_HandleTypeDef *hwwdg, HAL_WWDG_CallbackIDTypeDef CallbackID,
  222. pWWDG_CallbackTypeDef pCallback)
  223. {
  224. HAL_StatusTypeDef status = HAL_OK;
  225. if (pCallback == NULL)
  226. {
  227. status = HAL_ERROR;
  228. }
  229. else
  230. {
  231. switch (CallbackID)
  232. {
  233. case HAL_WWDG_EWI_CB_ID:
  234. hwwdg->EwiCallback = pCallback;
  235. break;
  236. case HAL_WWDG_MSPINIT_CB_ID:
  237. hwwdg->MspInitCallback = pCallback;
  238. break;
  239. default:
  240. status = HAL_ERROR;
  241. break;
  242. }
  243. }
  244. return status;
  245. }
  246. /**
  247. * @brief Unregister a WWDG Callback
  248. * WWDG Callback is redirected to the weak (surcharged) predefined callback
  249. * @param hwwdg WWDG handle
  250. * @param CallbackID ID of the callback to be registered
  251. * This parameter can be one of the following values:
  252. * @arg @ref HAL_WWDG_EWI_CB_ID Early WakeUp Interrupt Callback ID
  253. * @arg @ref HAL_WWDG_MSPINIT_CB_ID MspInit callback ID
  254. * @retval status
  255. */
  256. HAL_StatusTypeDef HAL_WWDG_UnRegisterCallback(WWDG_HandleTypeDef *hwwdg, HAL_WWDG_CallbackIDTypeDef CallbackID)
  257. {
  258. HAL_StatusTypeDef status = HAL_OK;
  259. switch (CallbackID)
  260. {
  261. case HAL_WWDG_EWI_CB_ID:
  262. hwwdg->EwiCallback = HAL_WWDG_EarlyWakeupCallback;
  263. break;
  264. case HAL_WWDG_MSPINIT_CB_ID:
  265. hwwdg->MspInitCallback = HAL_WWDG_MspInit;
  266. break;
  267. default:
  268. status = HAL_ERROR;
  269. break;
  270. }
  271. return status;
  272. }
  273. #endif /* USE_HAL_WWDG_REGISTER_CALLBACKS */
  274. /**
  275. * @}
  276. */
  277. /** @defgroup WWDG_Exported_Functions_Group2 IO operation functions
  278. * @brief IO operation functions
  279. *
  280. @verbatim
  281. ==============================================================================
  282. ##### IO operation functions #####
  283. ==============================================================================
  284. [..]
  285. This section provides functions allowing to:
  286. (+) Refresh the WWDG.
  287. (+) Handle WWDG interrupt request and associated function callback.
  288. @endverbatim
  289. * @{
  290. */
  291. /**
  292. * @brief Refresh the WWDG.
  293. * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
  294. * the configuration information for the specified WWDG module.
  295. * @retval HAL status
  296. */
  297. HAL_StatusTypeDef HAL_WWDG_Refresh(WWDG_HandleTypeDef *hwwdg)
  298. {
  299. /* Write to WWDG CR the WWDG Counter value to refresh with */
  300. WRITE_REG(hwwdg->Instance->CR, (hwwdg->Init.Counter));
  301. /* Return function status */
  302. return HAL_OK;
  303. }
  304. /**
  305. * @brief Handle WWDG interrupt request.
  306. * @note The Early Wakeup Interrupt (EWI) can be used if specific safety operations
  307. * or data logging must be performed before the actual reset is generated.
  308. * The EWI interrupt is enabled by calling HAL_WWDG_Init function with
  309. * EWIMode set to WWDG_EWI_ENABLE.
  310. * When the downcounter reaches the value 0x40, and EWI interrupt is
  311. * generated and the corresponding Interrupt Service Routine (ISR) can
  312. * be used to trigger specific actions (such as communications or data
  313. * logging), before resetting the device.
  314. * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
  315. * the configuration information for the specified WWDG module.
  316. * @retval None
  317. */
  318. void HAL_WWDG_IRQHandler(WWDG_HandleTypeDef *hwwdg)
  319. {
  320. /* Check if Early Wakeup Interrupt is enable */
  321. if (__HAL_WWDG_GET_IT_SOURCE(hwwdg, WWDG_IT_EWI) != RESET)
  322. {
  323. /* Check if WWDG Early Wakeup Interrupt occurred */
  324. if (__HAL_WWDG_GET_FLAG(hwwdg, WWDG_FLAG_EWIF) != RESET)
  325. {
  326. /* Clear the WWDG Early Wakeup flag */
  327. __HAL_WWDG_CLEAR_FLAG(hwwdg, WWDG_FLAG_EWIF);
  328. #if (USE_HAL_WWDG_REGISTER_CALLBACKS == 1)
  329. /* Early Wakeup registered callback */
  330. hwwdg->EwiCallback(hwwdg);
  331. #else
  332. /* Early Wakeup callback */
  333. HAL_WWDG_EarlyWakeupCallback(hwwdg);
  334. #endif /* USE_HAL_WWDG_REGISTER_CALLBACKS */
  335. }
  336. }
  337. }
  338. /**
  339. * @brief WWDG Early Wakeup callback.
  340. * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
  341. * the configuration information for the specified WWDG module.
  342. * @retval None
  343. */
  344. __weak void HAL_WWDG_EarlyWakeupCallback(WWDG_HandleTypeDef *hwwdg)
  345. {
  346. /* Prevent unused argument(s) compilation warning */
  347. UNUSED(hwwdg);
  348. /* NOTE: This function should not be modified, when the callback is needed,
  349. the HAL_WWDG_EarlyWakeupCallback could be implemented in the user file
  350. */
  351. }
  352. /**
  353. * @}
  354. */
  355. /**
  356. * @}
  357. */
  358. #endif /* HAL_WWDG_MODULE_ENABLED */
  359. /**
  360. * @}
  361. */
  362. /**
  363. * @}
  364. */
  365. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/