You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

414 lines
14 KiB

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