stm32f1xx_hal_wwdg.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_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. ******************************************************************************
  11. * @attention
  12. *
  13. * Copyright (c) 2016 STMicroelectronics.
  14. * All rights reserved.
  15. *
  16. * This software is licensed under terms that can be found in the LICENSE file
  17. * in the root directory of this software component.
  18. * If no LICENSE file comes with this software, it is provided AS-IS.
  19. *
  20. ******************************************************************************
  21. @verbatim
  22. ==============================================================================
  23. ##### WWDG Specific features #####
  24. ==============================================================================
  25. [..]
  26. Once enabled the WWDG generates a system reset on expiry of a programmed
  27. time period, unless the program refreshes the counter (T[6;0] downcounter)
  28. before reaching 0x3F value (i.e. a reset is generated when the counter
  29. value rolls down from 0x40 to 0x3F).
  30. (+) An MCU reset is also generated if the counter value is refreshed
  31. before the counter has reached the refresh window value. This
  32. implies that the counter must be refreshed in a limited window.
  33. (+) Once enabled the WWDG cannot be disabled except by a system reset.
  34. (+) If required by application, an Early Wakeup Interrupt can be triggered
  35. in order to be warned before WWDG expiration. The Early Wakeup Interrupt
  36. (EWI) can be used if specific safety operations or data logging must
  37. be performed before the actual reset is generated. When the downcounter
  38. reaches 0x40, interrupt occurs. This mechanism requires WWDG interrupt
  39. line to be enabled in NVIC. Once enabled, EWI interrupt cannot be
  40. disabled except by a system reset.
  41. (+) WWDGRST flag in RCC CSR register can be used to inform when a WWDG
  42. reset occurs.
  43. (+) The WWDG counter input clock is derived from the APB clock divided
  44. by a programmable prescaler.
  45. (+) WWDG clock (Hz) = PCLK1 / (4096 * Prescaler)
  46. (+) WWDG timeout (mS) = 1000 * (T[5;0] + 1) / WWDG clock (Hz)
  47. where T[5;0] are the lowest 6 bits of Counter.
  48. (+) WWDG Counter refresh is allowed between the following limits :
  49. (++) min time (mS) = 1000 * (Counter - Window) / WWDG clock
  50. (++) max time (mS) = 1000 * (Counter - 0x40) / WWDG clock
  51. (+) Typical values:
  52. (++) Counter min (T[5;0] = 0x00) at 36MHz (PCLK1) with zero prescaler:
  53. max timeout before reset: approximately 910us
  54. (++) Counter max (T[5;0] = 0x3F) at 36MHz (PCLK1) with prescaler
  55. dividing by 8:
  56. max timeout before reset: approximately 58.25ms
  57. ##### How to use this driver #####
  58. ==============================================================================
  59. *** Common driver usage ***
  60. ===========================
  61. [..]
  62. (+) Enable WWDG APB1 clock using __HAL_RCC_WWDG_CLK_ENABLE().
  63. (+) Configure the WWDG prescaler, refresh window value, counter value and early
  64. interrupt status using HAL_WWDG_Init() function. This will automatically
  65. enable WWDG and start its downcounter. Time reference can be taken from
  66. function exit. Care must be taken to provide a counter value
  67. greater than 0x40 to prevent generation of immediate reset.
  68. (+) If the Early Wakeup Interrupt (EWI) feature is enabled, an interrupt is
  69. generated when the counter reaches 0x40. When HAL_WWDG_IRQHandler is
  70. triggered by the interrupt service routine, flag will be automatically
  71. cleared and HAL_WWDG_WakeupCallback user callback will be executed. User
  72. can add his own code by customization of callback HAL_WWDG_WakeupCallback.
  73. (+) Then the application program must refresh the WWDG counter at regular
  74. intervals during normal operation to prevent an MCU reset, using
  75. HAL_WWDG_Refresh() function. This operation must occur only when
  76. the counter is lower than the refresh window value already programmed.
  77. *** Callback registration ***
  78. =============================
  79. [..]
  80. The compilation define USE_HAL_WWDG_REGISTER_CALLBACKS when set to 1 allows
  81. the user to configure dynamically the driver callbacks. Use Functions
  82. HAL_WWDG_RegisterCallback() to register a user callback.
  83. (+) Function HAL_WWDG_RegisterCallback() allows to register following
  84. callbacks:
  85. (++) EwiCallback : callback for Early WakeUp Interrupt.
  86. (++) MspInitCallback : WWDG MspInit.
  87. This function takes as parameters the HAL peripheral handle, the Callback ID
  88. and a pointer to the user callback function.
  89. (+) Use function HAL_WWDG_UnRegisterCallback() to reset a callback to
  90. the default weak (surcharged) function. HAL_WWDG_UnRegisterCallback()
  91. takes as parameters the HAL peripheral handle and the Callback ID.
  92. This function allows to reset following callbacks:
  93. (++) EwiCallback : callback for Early WakeUp Interrupt.
  94. (++) MspInitCallback : WWDG MspInit.
  95. [..]
  96. When calling HAL_WWDG_Init function, callbacks are reset to the
  97. corresponding legacy weak (surcharged) functions:
  98. HAL_WWDG_EarlyWakeupCallback() and HAL_WWDG_MspInit() only if they have
  99. not been registered before.
  100. [..]
  101. When compilation define USE_HAL_WWDG_REGISTER_CALLBACKS is set to 0 or
  102. not defined, the callback registering feature is not available
  103. and weak (surcharged) callbacks are used.
  104. *** WWDG HAL driver macros list ***
  105. ===================================
  106. [..]
  107. Below the list of available macros in WWDG HAL driver.
  108. (+) __HAL_WWDG_ENABLE: Enable the WWDG peripheral
  109. (+) __HAL_WWDG_GET_FLAG: Get the selected WWDG's flag status
  110. (+) __HAL_WWDG_CLEAR_FLAG: Clear the WWDG's pending flags
  111. (+) __HAL_WWDG_ENABLE_IT: Enable the WWDG early wakeup interrupt
  112. @endverbatim
  113. ******************************************************************************
  114. */
  115. /* Includes ------------------------------------------------------------------*/
  116. #include "stm32f1xx_hal.h"
  117. /** @addtogroup STM32F1xx_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. */