stm32f4xx_hal_wwdg.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_wwdg.c
  4. * @author MCD Application Team
  5. * @version V1.4.3
  6. * @date 11-December-2015
  7. * @brief WWDG HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the Window Watchdog (WWDG) peripheral:
  10. * + Initialization and de-initialization functions
  11. * + IO operation functions
  12. * + Peripheral State functions
  13. @verbatim
  14. ==============================================================================
  15. ##### WWDG specific features #####
  16. ==============================================================================
  17. [..]
  18. Once enabled the WWDG generates a system reset on expiry of a programmed
  19. time period, unless the program refreshes the counter (downcounter)
  20. before reaching 0x3F value (i.e. a reset is generated when the counter
  21. value rolls over from 0x40 to 0x3F).
  22. (+) An MCU reset is also generated if the counter value is refreshed
  23. before the counter has reached the refresh window value. This
  24. implies that the counter must be refreshed in a limited window.
  25. (+) Once enabled the WWDG cannot be disabled except by a system reset.
  26. (+) WWDGRST flag in RCC_CSR register can be used to inform when a WWDG
  27. reset occurs.
  28. (+) The WWDG counter input clock is derived from the APB clock divided
  29. by a programmable prescaler.
  30. (+) WWDG clock (Hz) = PCLK1 / (4096 * Prescaler)
  31. (+) WWDG timeout (mS) = 1000 * Counter / WWDG clock
  32. (+) WWDG Counter refresh is allowed between the following limits :
  33. (++) min time (mS) = 1000 * (Counter _ Window) / WWDG clock
  34. (++) max time (mS) = 1000 * (Counter _ 0x40) / WWDG clock
  35. (+) Min-max timeout value at 50 MHz(PCLK1): 81.9 us / 41.9 ms
  36. ##### How to use this driver #####
  37. ==============================================================================
  38. [..]
  39. (+) Enable WWDG APB1 clock using __HAL_RCC_WWDG_CLK_ENABLE().
  40. (+) Set the WWDG prescaler, refresh window and counter value
  41. using HAL_WWDG_Init() function.
  42. (+) Start the WWDG using HAL_WWDG_Start() function.
  43. When the WWDG is enabled the counter value should be configured to
  44. a value greater than 0x40 to prevent generating an immediate reset.
  45. (+) Optionally you can enable the Early Wakeup Interrupt (EWI) which is
  46. generated when the counter reaches 0x40, and then start the WWDG using
  47. HAL_WWDG_Start_IT(). At EWI HAL_WWDG_WakeupCallback is executed and user can
  48. add his own code by customization of function pointer HAL_WWDG_WakeupCallback
  49. Once enabled, EWI interrupt cannot be disabled except by a system reset.
  50. (+) Then the application program must refresh the WWDG counter at regular
  51. intervals during normal operation to prevent an MCU reset, using
  52. HAL_WWDG_Refresh() function. This operation must occur only when
  53. the counter is lower than the refresh window value already programmed.
  54. *** WWDG HAL driver macros list ***
  55. ==================================
  56. [..]
  57. Below the list of most used macros in WWDG HAL driver.
  58. (+) __HAL_WWDG_ENABLE: Enable the WWDG peripheral
  59. (+) __HAL_WWDG_GET_FLAG: Get the selected WWDG's flag status
  60. (+) __HAL_WWDG_CLEAR_FLAG: Clear the WWDG's pending flags
  61. (+) __HAL_WWDG_ENABLE_IT: Enables the WWDG early wake-up interrupt
  62. @endverbatim
  63. ******************************************************************************
  64. * @attention
  65. *
  66. * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  67. *
  68. * Redistribution and use in source and binary forms, with or without modification,
  69. * are permitted provided that the following conditions are met:
  70. * 1. Redistributions of source code must retain the above copyright notice,
  71. * this list of conditions and the following disclaimer.
  72. * 2. Redistributions in binary form must reproduce the above copyright notice,
  73. * this list of conditions and the following disclaimer in the documentation
  74. * and/or other materials provided with the distribution.
  75. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  76. * may be used to endorse or promote products derived from this software
  77. * without specific prior written permission.
  78. *
  79. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  80. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  81. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  82. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  83. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  84. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  85. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  86. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  87. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  88. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  89. *
  90. ******************************************************************************
  91. */
  92. /* Includes ------------------------------------------------------------------*/
  93. #include "stm32f4xx_hal.h"
  94. /** @addtogroup STM32F4xx_HAL_Driver
  95. * @{
  96. */
  97. /** @defgroup WWDG WWDG
  98. * @brief WWDG HAL module driver.
  99. * @{
  100. */
  101. #ifdef HAL_WWDG_MODULE_ENABLED
  102. /* Private typedef -----------------------------------------------------------*/
  103. /* Private define ------------------------------------------------------------*/
  104. /* Private macro -------------------------------------------------------------*/
  105. /* Private variables ---------------------------------------------------------*/
  106. /* Private function prototypes -----------------------------------------------*/
  107. /* Exported functions --------------------------------------------------------*/
  108. /** @defgroup WWDG_Exported_Functions WWDG Exported Functions
  109. * @{
  110. */
  111. /** @defgroup WWDG_Exported_Functions_Group1 Initialization and de-initialization functions
  112. * @brief Initialization and Configuration functions.
  113. *
  114. @verbatim
  115. ==============================================================================
  116. ##### Initialization and de-initialization functions #####
  117. ==============================================================================
  118. [..]
  119. This section provides functions allowing to:
  120. (+) Initialize the WWDG according to the specified parameters
  121. in the WWDG_InitTypeDef and create the associated handle
  122. (+) DeInitialize the WWDG peripheral
  123. (+) Initialize the WWDG MSP
  124. (+) DeInitialize the WWDG MSP
  125. @endverbatim
  126. * @{
  127. */
  128. /**
  129. * @brief Initializes the WWDG according to the specified
  130. * parameters in the WWDG_InitTypeDef and creates the associated handle.
  131. * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
  132. * the configuration information for the specified WWDG module.
  133. * @retval HAL status
  134. */
  135. HAL_StatusTypeDef HAL_WWDG_Init(WWDG_HandleTypeDef *hwwdg)
  136. {
  137. /* Check the WWDG handle allocation */
  138. if(hwwdg == NULL)
  139. {
  140. return HAL_ERROR;
  141. }
  142. /* Check the parameters */
  143. assert_param(IS_WWDG_ALL_INSTANCE(hwwdg->Instance));
  144. assert_param(IS_WWDG_PRESCALER(hwwdg->Init.Prescaler));
  145. assert_param(IS_WWDG_WINDOW(hwwdg->Init.Window));
  146. assert_param(IS_WWDG_COUNTER(hwwdg->Init.Counter));
  147. if(hwwdg->State == HAL_WWDG_STATE_RESET)
  148. {
  149. /* Allocate lock resource and initialize it */
  150. hwwdg->Lock = HAL_UNLOCKED;
  151. /* Init the low level hardware */
  152. HAL_WWDG_MspInit(hwwdg);
  153. }
  154. /* Change WWDG peripheral state */
  155. hwwdg->State = HAL_WWDG_STATE_BUSY;
  156. /* Set WWDG Prescaler and Window */
  157. MODIFY_REG(hwwdg->Instance->CFR, (WWDG_CFR_WDGTB | WWDG_CFR_W), (hwwdg->Init.Prescaler | hwwdg->Init.Window));
  158. /* Set WWDG Counter */
  159. MODIFY_REG(hwwdg->Instance->CR, WWDG_CR_T, hwwdg->Init.Counter);
  160. /* Change WWDG peripheral state */
  161. hwwdg->State = HAL_WWDG_STATE_READY;
  162. /* Return function status */
  163. return HAL_OK;
  164. }
  165. /**
  166. * @brief DeInitializes the WWDG peripheral.
  167. * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
  168. * the configuration information for the specified WWDG module.
  169. * @retval HAL status
  170. */
  171. HAL_StatusTypeDef HAL_WWDG_DeInit(WWDG_HandleTypeDef *hwwdg)
  172. {
  173. /* Check the WWDG handle allocation */
  174. if(hwwdg == NULL)
  175. {
  176. return HAL_ERROR;
  177. }
  178. /* Check the parameters */
  179. assert_param(IS_WWDG_ALL_INSTANCE(hwwdg->Instance));
  180. /* Change WWDG peripheral state */
  181. hwwdg->State = HAL_WWDG_STATE_BUSY;
  182. /* DeInit the low level hardware */
  183. HAL_WWDG_MspDeInit(hwwdg);
  184. /* Reset WWDG Control register */
  185. hwwdg->Instance->CR = (uint32_t)0x0000007F;
  186. /* Reset WWDG Configuration register */
  187. hwwdg->Instance->CFR = (uint32_t)0x0000007F;
  188. /* Reset WWDG Status register */
  189. hwwdg->Instance->SR = 0;
  190. /* Change WWDG peripheral state */
  191. hwwdg->State = HAL_WWDG_STATE_RESET;
  192. /* Release Lock */
  193. __HAL_UNLOCK(hwwdg);
  194. /* Return function status */
  195. return HAL_OK;
  196. }
  197. /**
  198. * @brief Initializes the WWDG MSP.
  199. * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
  200. * the configuration information for the specified WWDG module.
  201. * @retval None
  202. */
  203. __weak void HAL_WWDG_MspInit(WWDG_HandleTypeDef *hwwdg)
  204. {
  205. /* Prevent unused argument(s) compilation warning */
  206. UNUSED(hwwdg);
  207. /* NOTE: This function Should not be modified, when the callback is needed,
  208. the HAL_WWDG_MspInit could be implemented in the user file
  209. */
  210. }
  211. /**
  212. * @brief DeInitializes the WWDG MSP.
  213. * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
  214. * the configuration information for the specified WWDG module.
  215. * @retval None
  216. */
  217. __weak void HAL_WWDG_MspDeInit(WWDG_HandleTypeDef *hwwdg)
  218. {
  219. /* Prevent unused argument(s) compilation warning */
  220. UNUSED(hwwdg);
  221. /* NOTE: This function Should not be modified, when the callback is needed,
  222. the HAL_WWDG_MspDeInit could be implemented in the user file
  223. */
  224. }
  225. /**
  226. * @}
  227. */
  228. /** @defgroup WWDG_Exported_Functions_Group2 IO operation functions
  229. * @brief IO operation functions
  230. *
  231. @verbatim
  232. ==============================================================================
  233. ##### IO operation functions #####
  234. ==============================================================================
  235. [..]
  236. This section provides functions allowing to:
  237. (+) Start the WWDG.
  238. (+) Refresh the WWDG.
  239. (+) Handle WWDG interrupt request.
  240. @endverbatim
  241. * @{
  242. */
  243. /**
  244. * @brief Starts the WWDG.
  245. * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
  246. * the configuration information for the specified WWDG module.
  247. * @retval HAL status
  248. */
  249. HAL_StatusTypeDef HAL_WWDG_Start(WWDG_HandleTypeDef *hwwdg)
  250. {
  251. /* Process Locked */
  252. __HAL_LOCK(hwwdg);
  253. /* Change WWDG peripheral state */
  254. hwwdg->State = HAL_WWDG_STATE_BUSY;
  255. /* Enable the peripheral */
  256. __HAL_WWDG_ENABLE(hwwdg);
  257. /* Change WWDG peripheral state */
  258. hwwdg->State = HAL_WWDG_STATE_READY;
  259. /* Process Unlocked */
  260. __HAL_UNLOCK(hwwdg);
  261. /* Return function status */
  262. return HAL_OK;
  263. }
  264. /**
  265. * @brief Starts the WWDG with interrupt enabled.
  266. * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
  267. * the configuration information for the specified WWDG module.
  268. * @retval HAL status
  269. */
  270. HAL_StatusTypeDef HAL_WWDG_Start_IT(WWDG_HandleTypeDef *hwwdg)
  271. {
  272. /* Process Locked */
  273. __HAL_LOCK(hwwdg);
  274. /* Change WWDG peripheral state */
  275. hwwdg->State = HAL_WWDG_STATE_BUSY;
  276. /* Enable the Early Wakeup Interrupt */
  277. __HAL_WWDG_ENABLE_IT(hwwdg, WWDG_IT_EWI);
  278. /* Enable the peripheral */
  279. __HAL_WWDG_ENABLE(hwwdg);
  280. /* Return function status */
  281. return HAL_OK;
  282. }
  283. /**
  284. * @brief Refreshes the WWDG.
  285. * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
  286. * the configuration information for the specified WWDG module.
  287. * @param Counter: value of counter to put in WWDG counter
  288. * @retval HAL status
  289. */
  290. HAL_StatusTypeDef HAL_WWDG_Refresh(WWDG_HandleTypeDef *hwwdg, uint32_t Counter)
  291. {
  292. /* Process Locked */
  293. __HAL_LOCK(hwwdg);
  294. /* Change WWDG peripheral state */
  295. hwwdg->State = HAL_WWDG_STATE_BUSY;
  296. /* Check the parameters */
  297. assert_param(IS_WWDG_COUNTER(Counter));
  298. /* Write to WWDG CR the WWDG Counter value to refresh with */
  299. MODIFY_REG(hwwdg->Instance->CR, (uint32_t)WWDG_CR_T, Counter);
  300. /* Change WWDG peripheral state */
  301. hwwdg->State = HAL_WWDG_STATE_READY;
  302. /* Process Unlocked */
  303. __HAL_UNLOCK(hwwdg);
  304. /* Return function status */
  305. return HAL_OK;
  306. }
  307. /**
  308. * @brief Handles WWDG interrupt request.
  309. * @note The Early Wakeup Interrupt (EWI) can be used if specific safety operations
  310. * or data logging must be performed before the actual reset is generated.
  311. * The EWI interrupt is enabled using __HAL_WWDG_ENABLE_IT() macro.
  312. * When the downcounter reaches the value 0x40, and EWI interrupt is
  313. * generated and the corresponding Interrupt Service Routine (ISR) can
  314. * be used to trigger specific actions (such as communications or data
  315. * logging), before resetting the device.
  316. * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
  317. * the configuration information for the specified WWDG module.
  318. * @retval None
  319. */
  320. void HAL_WWDG_IRQHandler(WWDG_HandleTypeDef *hwwdg)
  321. {
  322. /* Check if Early Wakeup Interrupt is enable */
  323. if(__HAL_WWDG_GET_IT_SOURCE(hwwdg, WWDG_IT_EWI) != RESET)
  324. {
  325. /* Check if WWDG Early Wakeup Interrupt occurred */
  326. if(__HAL_WWDG_GET_FLAG(hwwdg, WWDG_FLAG_EWIF) != RESET)
  327. {
  328. /* Early Wakeup callback */
  329. HAL_WWDG_WakeupCallback(hwwdg);
  330. /* Change WWDG peripheral state */
  331. hwwdg->State = HAL_WWDG_STATE_READY;
  332. /* Clear the WWDG Early Wakeup flag */
  333. __HAL_WWDG_CLEAR_FLAG(hwwdg, WWDG_FLAG_EWIF);
  334. /* Process Unlocked */
  335. __HAL_UNLOCK(hwwdg);
  336. }
  337. }
  338. }
  339. /**
  340. * @brief Early Wakeup WWDG callback.
  341. * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
  342. * the configuration information for the specified WWDG module.
  343. * @retval None
  344. */
  345. __weak void HAL_WWDG_WakeupCallback(WWDG_HandleTypeDef* hwwdg)
  346. {
  347. /* Prevent unused argument(s) compilation warning */
  348. UNUSED(hwwdg);
  349. /* NOTE: This function Should not be modified, when the callback is needed,
  350. the HAL_WWDG_WakeupCallback could be implemented in the user file
  351. */
  352. }
  353. /**
  354. * @}
  355. */
  356. /** @defgroup WWDG_Exported_Functions_Group3 Peripheral State functions
  357. * @brief Peripheral State functions.
  358. *
  359. @verbatim
  360. ==============================================================================
  361. ##### Peripheral State functions #####
  362. ==============================================================================
  363. [..]
  364. This subsection permits to get in run-time the status of the peripheral
  365. and the data flow.
  366. @endverbatim
  367. * @{
  368. */
  369. /**
  370. * @brief Returns the WWDG state.
  371. * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
  372. * the configuration information for the specified WWDG module.
  373. * @retval HAL state
  374. */
  375. HAL_WWDG_StateTypeDef HAL_WWDG_GetState(WWDG_HandleTypeDef *hwwdg)
  376. {
  377. return hwwdg->State;
  378. }
  379. /**
  380. * @}
  381. */
  382. /**
  383. * @}
  384. */
  385. #endif /* HAL_WWDG_MODULE_ENABLED */
  386. /**
  387. * @}
  388. */
  389. /**
  390. * @}
  391. */
  392. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/