stm32l0xx_hal_wwdg.c 14 KB

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