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.

stm32l1xx_hal_wwdg.c 14KB

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