stm32f1xx_hal_wwdg.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 de-initialization functions
  9. * + IO operation functions
  10. * + Peripheral State functions
  11. @verbatim
  12. ==============================================================================
  13. ##### WWDG specific features #####
  14. ==============================================================================
  15. [..]
  16. Once enabled the WWDG generates a system reset on expiry of a programmed
  17. time period, unless the program refreshes the counter (downcounter)
  18. before reaching 0x3F value (i.e. a reset is generated when the counter
  19. value rolls over from 0x40 to 0x3F).
  20. (+) An MCU reset is also generated if the counter value is refreshed
  21. before the counter has reached the refresh window value. This
  22. implies that the counter must be refreshed in a limited window.
  23. (+) Once enabled the WWDG cannot be disabled except by a system reset.
  24. (+) WWDGRST flag in RCC_CSR register can be used to inform when a WWDG
  25. reset occurs.
  26. (+) The WWDG counter input clock is derived from the APB clock divided
  27. by a programmable prescaler.
  28. (+) WWDG clock (Hz) = PCLK1 / (4096 * Prescaler)
  29. (+) WWDG timeout (mS) = 1000 * Counter / WWDG clock
  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. (+) Min-max timeout value at 36 MHz(PCLK1): 910 us / 58.25 ms
  34. (+) The Early Wakeup Interrupt (EWI) can be used if specific safety
  35. operations or data logging must be performed before the actual reset is
  36. generated. When the downcounter reaches the value 0x40, an EWI interrupt
  37. is generated and the corresponding interrupt service routine (ISR) can
  38. be used to trigger specific actions (such as communications or data
  39. logging), before resetting the device.
  40. In some applications, the EWI interrupt can be used to manage a software
  41. system check and/or system recovery/graceful degradation, without
  42. generating a WWDG reset. In this case, the corresponding interrupt
  43. service routine (ISR) should reload the WWDG counter to avoid the WWDG
  44. reset, then trigger the required actions.
  45. Note:When the EWI interrupt cannot be served, e.g. due to a system lock
  46. in a higher priority task, the WWDG reset will eventually be generated.
  47. (+) Debug mode : When the microcontroller enters debug mode (core halted),
  48. the WWDG counter either continues to work normally or stops, depending
  49. on DBG_WWDG_STOP configuration bit in DBG module, accessible through
  50. __HAL_DBGMCU_FREEZE_WWDG() and __HAL_DBGMCU_UNFREEZE_WWDG() macros
  51. ##### How to use this driver #####
  52. ==============================================================================
  53. [..]
  54. (+) Enable WWDG APB1 clock using __HAL_RCC_WWDG_CLK_ENABLE().
  55. (+) Set the WWDG prescaler, refresh window, counter value and Early Wakeup
  56. Interrupt mode using using HAL_WWDG_Init() function.
  57. This enables WWDG peripheral and the downcounter starts downcounting
  58. from given counter value.
  59. Init function can be called again to modify all watchdog parameters,
  60. however if EWI mode has been set once, it can't be clear until next
  61. reset.
  62. (+) 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 window value already programmed.
  66. (+) if Early Wakeup Interrupt mode is enable an interrupt is generated when
  67. the counter reaches 0x40. User can add his own code in weak function
  68. HAL_WWDG_EarlyWakeupCallback().
  69. *** WWDG HAL driver macros list ***
  70. ==================================
  71. [..]
  72. Below the list of most used macros in WWDG HAL driver.
  73. (+) __HAL_WWDG_GET_IT_SOURCE: Check the selected WWDG's interrupt source.
  74. (+) __HAL_WWDG_GET_FLAG: Get the selected WWDG's flag status.
  75. (+) __HAL_WWDG_CLEAR_FLAG: Clear the WWDG's pending flags.
  76. @endverbatim
  77. ******************************************************************************
  78. * @attention
  79. *
  80. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  81. * All rights reserved.</center></h2>
  82. *
  83. * This software component is licensed by ST under BSD 3-Clause license,
  84. * the "License"; You may not use this file except in compliance with the
  85. * License. You may obtain a copy of the License at:
  86. * opensource.org/licenses/BSD-3-Clause
  87. *
  88. ******************************************************************************
  89. */
  90. /* Includes ------------------------------------------------------------------*/
  91. #include "stm32f1xx_hal.h"
  92. /** @addtogroup STM32F1xx_HAL_Driver
  93. * @{
  94. */
  95. #ifdef HAL_WWDG_MODULE_ENABLED
  96. /** @defgroup WWDG WWDG
  97. * @brief WWDG HAL module driver.
  98. * @{
  99. */
  100. /* Private typedef -----------------------------------------------------------*/
  101. /* Private define ------------------------------------------------------------*/
  102. /* Private macro -------------------------------------------------------------*/
  103. /* Private variables ---------------------------------------------------------*/
  104. /* Private function prototypes -----------------------------------------------*/
  105. /* Exported functions --------------------------------------------------------*/
  106. /** @defgroup WWDG_Exported_Functions WWDG Exported Functions
  107. * @{
  108. */
  109. /** @defgroup WWDG_Exported_Functions_Group1 Initialization and Configuration functions
  110. * @brief Initialization and Configuration functions.
  111. *
  112. @verbatim
  113. ==============================================================================
  114. ##### Initialization and Configuration functions #####
  115. ==============================================================================
  116. [..]
  117. This section provides functions allowing to:
  118. (+) Initialize and start the WWDG according to the specified parameters
  119. in the WWDG_InitTypeDef of associated handle.
  120. (+) Initialize the WWDG MSP.
  121. @endverbatim
  122. * @{
  123. */
  124. /**
  125. * @brief Initialize the WWDG according to the specified.
  126. * parameters in the WWDG_InitTypeDef of associated handle.
  127. * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
  128. * the configuration information for the specified WWDG module.
  129. * @retval HAL status
  130. */
  131. HAL_StatusTypeDef HAL_WWDG_Init(WWDG_HandleTypeDef *hwwdg)
  132. {
  133. /* Check the WWDG handle allocation */
  134. if (hwwdg == NULL)
  135. {
  136. return HAL_ERROR;
  137. }
  138. /* Check the parameters */
  139. assert_param(IS_WWDG_ALL_INSTANCE(hwwdg->Instance));
  140. assert_param(IS_WWDG_PRESCALER(hwwdg->Init.Prescaler));
  141. assert_param(IS_WWDG_WINDOW(hwwdg->Init.Window));
  142. assert_param(IS_WWDG_COUNTER(hwwdg->Init.Counter));
  143. assert_param(IS_WWDG_EWI_MODE(hwwdg->Init.EWIMode));
  144. #if (USE_HAL_WWDG_REGISTER_CALLBACKS == 1)
  145. /* Reset Callback pointers */
  146. if(hwwdg->EwiCallback == NULL)
  147. {
  148. hwwdg->EwiCallback = HAL_WWDG_EarlyWakeupCallback;
  149. }
  150. if(hwwdg->MspInitCallback == NULL)
  151. {
  152. hwwdg->MspInitCallback = HAL_WWDG_MspInit;
  153. }
  154. /* Init the low level hardware */
  155. hwwdg->MspInitCallback(hwwdg);
  156. #else
  157. /* Init the low level hardware */
  158. HAL_WWDG_MspInit(hwwdg);
  159. #endif
  160. /* Set WWDG Counter */
  161. WRITE_REG(hwwdg->Instance->CR, (WWDG_CR_WDGA | hwwdg->Init.Counter));
  162. /* Set WWDG Prescaler and Window */
  163. WRITE_REG(hwwdg->Instance->CFR, (hwwdg->Init.EWIMode | hwwdg->Init.Prescaler | hwwdg->Init.Window));
  164. /* Return function status */
  165. return HAL_OK;
  166. }
  167. /**
  168. * @brief Initialize the WWDG MSP.
  169. * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
  170. * the configuration information for the specified WWDG module.
  171. * @note When rewriting this function in user file, mechanism may be added
  172. * to avoid multiple initialize when HAL_WWDG_Init function is called
  173. * again to change parameters.
  174. * @retval None
  175. */
  176. __weak void HAL_WWDG_MspInit(WWDG_HandleTypeDef *hwwdg)
  177. {
  178. /* Prevent unused argument(s) compilation warning */
  179. UNUSED(hwwdg);
  180. /* NOTE: This function should not be modified, when the callback is needed,
  181. the HAL_WWDG_MspInit could be implemented in the user file
  182. */
  183. }
  184. #if (USE_HAL_WWDG_REGISTER_CALLBACKS == 1)
  185. /**
  186. * @brief Register a User WWDG Callback
  187. * To be used instead of the weak (surcharged) predefined callback
  188. * @param hwwdg WWDG handle
  189. * @param CallbackID ID of the callback to be registered
  190. * This parameter can be one of the following values:
  191. * @arg @ref HAL_WWDG_EWI_CB_ID Early WakeUp Interrupt Callback ID
  192. * @arg @ref HAL_WWDG_MSPINIT_CB_ID MspInit callback ID
  193. * @param pCallback pointer to the Callback function
  194. * @retval status
  195. */
  196. HAL_StatusTypeDef HAL_WWDG_RegisterCallback(WWDG_HandleTypeDef *hwwdg, HAL_WWDG_CallbackIDTypeDef CallbackID, pWWDG_CallbackTypeDef pCallback)
  197. {
  198. HAL_StatusTypeDef status = HAL_OK;
  199. if(pCallback == NULL)
  200. {
  201. status = HAL_ERROR;
  202. }
  203. else
  204. {
  205. switch(CallbackID)
  206. {
  207. case HAL_WWDG_EWI_CB_ID:
  208. hwwdg->EwiCallback = pCallback;
  209. break;
  210. case HAL_WWDG_MSPINIT_CB_ID:
  211. hwwdg->MspInitCallback = pCallback;
  212. break;
  213. default:
  214. status = HAL_ERROR;
  215. break;
  216. }
  217. }
  218. return status;
  219. }
  220. /**
  221. * @brief Unregister a WWDG Callback
  222. * WWDG Callback is redirected to the weak (surcharged) predefined callback
  223. * @param hwwdg WWDG handle
  224. * @param CallbackID ID of the callback to be registered
  225. * This parameter can be one of the following values:
  226. * @arg @ref HAL_WWDG_EWI_CB_ID Early WakeUp Interrupt Callback ID
  227. * @arg @ref HAL_WWDG_MSPINIT_CB_ID MspInit callback ID
  228. * @retval status
  229. */
  230. HAL_StatusTypeDef HAL_WWDG_UnRegisterCallback(WWDG_HandleTypeDef *hwwdg, HAL_WWDG_CallbackIDTypeDef CallbackID)
  231. {
  232. HAL_StatusTypeDef status = HAL_OK;
  233. switch(CallbackID)
  234. {
  235. case HAL_WWDG_EWI_CB_ID:
  236. hwwdg->EwiCallback = HAL_WWDG_EarlyWakeupCallback;
  237. break;
  238. case HAL_WWDG_MSPINIT_CB_ID:
  239. hwwdg->MspInitCallback = HAL_WWDG_MspInit;
  240. break;
  241. default:
  242. status = HAL_ERROR;
  243. break;
  244. }
  245. return status;
  246. }
  247. #endif
  248. /**
  249. * @}
  250. */
  251. /** @defgroup WWDG_Exported_Functions_Group2 IO operation functions
  252. * @brief IO operation functions
  253. *
  254. @verbatim
  255. ==============================================================================
  256. ##### IO operation functions #####
  257. ==============================================================================
  258. [..]
  259. This section provides functions allowing to:
  260. (+) Refresh the WWDG.
  261. (+) Handle WWDG interrupt request and associated function callback.
  262. @endverbatim
  263. * @{
  264. */
  265. /**
  266. * @brief Refresh the WWDG.
  267. * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
  268. * the configuration information for the specified WWDG module.
  269. * @retval HAL status
  270. */
  271. HAL_StatusTypeDef HAL_WWDG_Refresh(WWDG_HandleTypeDef *hwwdg)
  272. {
  273. /* Write to WWDG CR the WWDG Counter value to refresh with */
  274. WRITE_REG(hwwdg->Instance->CR, (hwwdg->Init.Counter));
  275. /* Return function status */
  276. return HAL_OK;
  277. }
  278. /**
  279. * @brief Handle WWDG interrupt request.
  280. * @note The Early Wakeup Interrupt (EWI) can be used if specific safety operations
  281. * or data logging must be performed before the actual reset is generated.
  282. * The EWI interrupt is enabled by calling HAL_WWDG_Init function with
  283. * EWIMode set to WWDG_EWI_ENABLE.
  284. * When the downcounter reaches the value 0x40, and EWI interrupt is
  285. * generated and the corresponding Interrupt Service Routine (ISR) can
  286. * be used to trigger specific actions (such as communications or data
  287. * logging), before resetting the device.
  288. * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
  289. * the configuration information for the specified WWDG module.
  290. * @retval None
  291. */
  292. void HAL_WWDG_IRQHandler(WWDG_HandleTypeDef *hwwdg)
  293. {
  294. /* Check if Early Wakeup Interrupt is enable */
  295. if (__HAL_WWDG_GET_IT_SOURCE(hwwdg, WWDG_IT_EWI) != RESET)
  296. {
  297. /* Check if WWDG Early Wakeup Interrupt occurred */
  298. if (__HAL_WWDG_GET_FLAG(hwwdg, WWDG_FLAG_EWIF) != RESET)
  299. {
  300. /* Clear the WWDG Early Wakeup flag */
  301. __HAL_WWDG_CLEAR_FLAG(hwwdg, WWDG_FLAG_EWIF);
  302. #if (USE_HAL_WWDG_REGISTER_CALLBACKS == 1)
  303. /* Early Wakeup registered callback */
  304. hwwdg->EwiCallback(hwwdg);
  305. #else
  306. /* Early Wakeup callback */
  307. HAL_WWDG_EarlyWakeupCallback(hwwdg);
  308. #endif
  309. }
  310. }
  311. }
  312. /**
  313. * @brief WWDG Early Wakeup callback.
  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. __weak void HAL_WWDG_EarlyWakeupCallback(WWDG_HandleTypeDef *hwwdg)
  319. {
  320. /* Prevent unused argument(s) compilation warning */
  321. UNUSED(hwwdg);
  322. /* NOTE: This function should not be modified, when the callback is needed,
  323. the HAL_WWDG_EarlyWakeupCallback could be implemented in the user file
  324. */
  325. }
  326. /**
  327. * @}
  328. */
  329. /**
  330. * @}
  331. */
  332. #endif /* HAL_WWDG_MODULE_ENABLED */
  333. /**
  334. * @}
  335. */
  336. /**
  337. * @}
  338. */
  339. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/