stm32l4xx_hal_wwdg.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_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 function
  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 over 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 informs when a WWDG reset has
  24. occurred (check available with __HAL_RCC_GET_FLAG(RCC_FLAG_WWDGRST)).
  25. (+) The WWDG downcounter input clock is derived from the APB clock divided
  26. by a programmable prescaler.
  27. (+) WWDG downcounter clock (Hz) = PCLK1 / (4096 * Prescaler)
  28. (+) WWDG timeout (ms) = (1000 * (T[5;0] + 1)) / (WWDG downcounter clock)
  29. where T[5;0] are the lowest 6 bits of downcounter.
  30. (+) WWDG Counter refresh is allowed between the following limits :
  31. (++) min time (ms) = (1000 * (T[5;0] - Window)) / (WWDG downcounter clock)
  32. (++) max time (ms) = (1000 * (T[5;0] - 0x40)) / (WWDG downcounter clock)
  33. (+) Min-max timeout value @80 MHz(PCLK1): ~51.2 us / ~26.22 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) 2017 STMicroelectronics</center></h2>
  81. *
  82. * Redistribution and use in source and binary forms, with or without modification,
  83. * are permitted provided that the following conditions are met:
  84. * 1. Redistributions of source code must retain the above copyright notice,
  85. * this list of conditions and the following disclaimer.
  86. * 2. Redistributions in binary form must reproduce the above copyright notice,
  87. * this list of conditions and the following disclaimer in the documentation
  88. * and/or other materials provided with the distribution.
  89. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  90. * may be used to endorse or promote products derived from this software
  91. * without specific prior written permission.
  92. *
  93. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  94. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  95. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  96. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  97. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  98. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  99. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  100. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  101. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  102. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  103. *
  104. ******************************************************************************
  105. */
  106. /* Includes ------------------------------------------------------------------*/
  107. #include "stm32l4xx_hal.h"
  108. /** @addtogroup STM32L4xx_HAL_Driver
  109. * @{
  110. */
  111. #ifdef HAL_WWDG_MODULE_ENABLED
  112. /** @defgroup WWDG WWDG
  113. * @brief WWDG HAL module driver.
  114. * @{
  115. */
  116. /* Private typedef -----------------------------------------------------------*/
  117. /* Private define ------------------------------------------------------------*/
  118. /* Private macro -------------------------------------------------------------*/
  119. /* Private variables ---------------------------------------------------------*/
  120. /* Private function prototypes -----------------------------------------------*/
  121. /* Exported functions --------------------------------------------------------*/
  122. /** @defgroup WWDG_Exported_Functions WWDG Exported Functions
  123. * @{
  124. */
  125. /** @defgroup WWDG_Exported_Functions_Group1 Initialization and Configuration functions
  126. * @brief Initialization and Configuration functions.
  127. *
  128. @verbatim
  129. ==============================================================================
  130. ##### Initialization and Configuration functions #####
  131. ==============================================================================
  132. [..]
  133. This section provides functions allowing to:
  134. (+) Initialize and start the WWDG according to the specified parameters
  135. in the WWDG_InitTypeDef of associated handle.
  136. (+) Initialize the WWDG MSP.
  137. @endverbatim
  138. * @{
  139. */
  140. /**
  141. * @brief Initialize the WWDG according to the specified.
  142. * parameters in the WWDG_InitTypeDef of associated handle.
  143. * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
  144. * the configuration information for the specified WWDG module.
  145. * @retval HAL status
  146. */
  147. HAL_StatusTypeDef HAL_WWDG_Init(WWDG_HandleTypeDef *hwwdg)
  148. {
  149. /* Check the WWDG handle allocation */
  150. if(hwwdg == NULL)
  151. {
  152. return HAL_ERROR;
  153. }
  154. /* Check the parameters */
  155. assert_param(IS_WWDG_ALL_INSTANCE(hwwdg->Instance));
  156. assert_param(IS_WWDG_PRESCALER(hwwdg->Init.Prescaler));
  157. assert_param(IS_WWDG_WINDOW(hwwdg->Init.Window));
  158. assert_param(IS_WWDG_COUNTER(hwwdg->Init.Counter));
  159. assert_param(IS_WWDG_EWI_MODE(hwwdg->Init.EWIMode));
  160. /* Init the low level hardware */
  161. HAL_WWDG_MspInit(hwwdg);
  162. /* Set WWDG Counter */
  163. WRITE_REG(hwwdg->Instance->CR, (WWDG_CR_WDGA | hwwdg->Init.Counter));
  164. /* Set WWDG Prescaler and Window */
  165. WRITE_REG(hwwdg->Instance->CFR, (hwwdg->Init.EWIMode | hwwdg->Init.Prescaler | hwwdg->Init.Window));
  166. /* Return function status */
  167. return HAL_OK;
  168. }
  169. /**
  170. * @brief Initialize the WWDG MSP.
  171. * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
  172. * the configuration information for the specified WWDG module.
  173. * @note When rewriting this function in user file, mechanism may be added
  174. * to avoid multiple initialize when HAL_WWDG_Init function is called
  175. * again to change parameters.
  176. * @retval None
  177. */
  178. __weak void HAL_WWDG_MspInit(WWDG_HandleTypeDef *hwwdg)
  179. {
  180. /* Prevent unused argument(s) compilation warning */
  181. UNUSED(hwwdg);
  182. /* NOTE: This function should not be modified, when the callback is needed,
  183. the HAL_WWDG_MspInit could be implemented in the user file
  184. */
  185. }
  186. /**
  187. * @}
  188. */
  189. /** @defgroup WWDG_Exported_Functions_Group2 IO operation functions
  190. * @brief IO operation functions
  191. *
  192. @verbatim
  193. ==============================================================================
  194. ##### IO operation functions #####
  195. ==============================================================================
  196. [..]
  197. This section provides functions allowing to:
  198. (+) Refresh the WWDG.
  199. (+) Handle WWDG interrupt request and associated function callback.
  200. @endverbatim
  201. * @{
  202. */
  203. /**
  204. * @brief Refresh the WWDG.
  205. * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
  206. * the configuration information for the specified WWDG module.
  207. * @retval HAL status
  208. */
  209. HAL_StatusTypeDef HAL_WWDG_Refresh(WWDG_HandleTypeDef *hwwdg)
  210. {
  211. /* Write to WWDG CR the WWDG Counter value to refresh with */
  212. WRITE_REG(hwwdg->Instance->CR, (hwwdg->Init.Counter));
  213. /* Return function status */
  214. return HAL_OK;
  215. }
  216. /**
  217. * @brief Handle WWDG interrupt request.
  218. * @note The Early Wakeup Interrupt (EWI) can be used if specific safety operations
  219. * or data logging must be performed before the actual reset is generated.
  220. * The EWI interrupt is enabled by calling HAL_WWDG_Init function with
  221. * EWIMode set to WWDG_EWI_ENABLE.
  222. * When the downcounter reaches the value 0x40, and EWI interrupt is
  223. * generated and the corresponding Interrupt Service Routine (ISR) can
  224. * be used to trigger specific actions (such as communications or data
  225. * logging), before resetting the device.
  226. * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
  227. * the configuration information for the specified WWDG module.
  228. * @retval None
  229. */
  230. void HAL_WWDG_IRQHandler(WWDG_HandleTypeDef *hwwdg)
  231. {
  232. /* Check if Early Wakeup Interrupt is enable */
  233. if(__HAL_WWDG_GET_IT_SOURCE(hwwdg, WWDG_IT_EWI) != RESET)
  234. {
  235. /* Check if WWDG Early Wakeup Interrupt occurred */
  236. if(__HAL_WWDG_GET_FLAG(hwwdg, WWDG_FLAG_EWIF) != RESET)
  237. {
  238. /* Clear the WWDG Early Wakeup flag */
  239. __HAL_WWDG_CLEAR_FLAG(hwwdg, WWDG_FLAG_EWIF);
  240. /* Early Wakeup callback */
  241. HAL_WWDG_EarlyWakeupCallback(hwwdg);
  242. }
  243. }
  244. }
  245. /**
  246. * @brief WWDG Early Wakeup callback.
  247. * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
  248. * the configuration information for the specified WWDG module.
  249. * @retval None
  250. */
  251. __weak void HAL_WWDG_EarlyWakeupCallback(WWDG_HandleTypeDef* hwwdg)
  252. {
  253. /* Prevent unused argument(s) compilation warning */
  254. UNUSED(hwwdg);
  255. /* NOTE: This function should not be modified, when the callback is needed,
  256. the HAL_WWDG_EarlyWakeupCallback could be implemented in the user file
  257. */
  258. }
  259. /**
  260. * @}
  261. */
  262. /**
  263. * @}
  264. */
  265. #endif /* HAL_WWDG_MODULE_ENABLED */
  266. /**
  267. * @}
  268. */
  269. /**
  270. * @}
  271. */
  272. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/