stm32l4xx_hal_wwdg.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_wwdg.c
  4. * @author MCD Application Team
  5. * @version V1.7.2
  6. * @date 16-June-2017
  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 Configuration function
  11. * + IO operation functions
  12. @verbatim
  13. ==============================================================================
  14. ##### WWDG specific features #####
  15. ==============================================================================
  16. [..]
  17. Once enabled the WWDG generates a system reset on expiry of a programmed
  18. time period, unless the program refreshes the counter (T[6;0] downcounter)
  19. before reaching 0x3F value (i.e. a reset is generated when the counter
  20. value rolls over from 0x40 to 0x3F).
  21. (+) An MCU reset is also generated if the counter value is refreshed
  22. before the counter has reached the refresh window value. This
  23. implies that the counter must be refreshed in a limited window.
  24. (+) Once enabled the WWDG cannot be disabled except by a system reset.
  25. (+) WWDGRST flag in RCC_CSR register informs when a WWDG reset has
  26. occurred (check available with __HAL_RCC_GET_FLAG(RCC_FLAG_WWDGRST)).
  27. (+) The WWDG downcounter input clock is derived from the APB clock divided
  28. by a programmable prescaler.
  29. (+) WWDG downcounter clock (Hz) = PCLK1 / (4096 * Prescaler)
  30. (+) WWDG timeout (ms) = (1000 * (T[5;0] + 1)) / (WWDG downcounter clock)
  31. where T[5;0] are the lowest 6 bits of downcounter.
  32. (+) WWDG Counter refresh is allowed between the following limits :
  33. (++) min time (ms) = (1000 * (T[5;0] - Window)) / (WWDG downcounter clock)
  34. (++) max time (ms) = (1000 * (T[5;0] - 0x40)) / (WWDG downcounter clock)
  35. (+) Min-max timeout value @80 MHz(PCLK1): ~51.2 us / ~26.22 ms
  36. (+) The Early Wakeup Interrupt (EWI) can be used if specific safety
  37. operations or data logging must be performed before the actual reset is
  38. generated. When the downcounter reaches the value 0x40, an EWI interrupt
  39. is generated and the corresponding interrupt service routine (ISR) can
  40. be used to trigger specific actions (such as communications or data
  41. logging), before resetting the device.
  42. In some applications, the EWI interrupt can be used to manage a software
  43. system check and/or system recovery/graceful degradation, without
  44. generating a WWDG reset. In this case, the corresponding interrupt
  45. service routine (ISR) should reload the WWDG counter to avoid the WWDG
  46. reset, then trigger the required actions.
  47. Note:When the EWI interrupt cannot be served, e.g. due to a system lock
  48. in a higher priority task, the WWDG reset will eventually be generated.
  49. (+) Debug mode : When the microcontroller enters debug mode (core halted),
  50. the WWDG counter either continues to work normally or stops, depending
  51. on DBG_WWDG_STOP configuration bit in DBG module, accessible through
  52. __HAL_DBGMCU_FREEZE_WWDG() and __HAL_DBGMCU_UNFREEZE_WWDG() macros
  53. ##### How to use this driver #####
  54. ==============================================================================
  55. [..]
  56. (+) Enable WWDG APB1 clock using __HAL_RCC_WWDG_CLK_ENABLE().
  57. (+) Set the WWDG prescaler, refresh window, counter value and Early Wakeup
  58. Interrupt mode using using HAL_WWDG_Init() function.
  59. This enables WWDG peripheral and the downcounter starts downcounting
  60. from given counter value.
  61. Init function can be called again to modify all watchdog parameters,
  62. however if EWI mode has been set once, it can't be clear until next
  63. reset.
  64. (+) The application program must refresh the WWDG counter at regular
  65. intervals during normal operation to prevent an MCU reset using
  66. HAL_WWDG_Refresh() function. This operation must occur only when
  67. the counter is lower than the window value already programmed.
  68. (+) if Early Wakeup Interrupt mode is enable an interrupt is generated when
  69. the counter reaches 0x40. User can add his own code in weak function
  70. HAL_WWDG_EarlyWakeupCallback().
  71. *** WWDG HAL driver macros list ***
  72. ==================================
  73. [..]
  74. Below the list of most used macros in WWDG HAL driver.
  75. (+) __HAL_WWDG_GET_IT_SOURCE: Check the selected WWDG's interrupt source.
  76. (+) __HAL_WWDG_GET_FLAG: Get the selected WWDG's flag status.
  77. (+) __HAL_WWDG_CLEAR_FLAG: Clear the WWDG's pending flags.
  78. @endverbatim
  79. ******************************************************************************
  80. * @attention
  81. *
  82. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  83. *
  84. * Redistribution and use in source and binary forms, with or without modification,
  85. * are permitted provided that the following conditions are met:
  86. * 1. Redistributions of source code must retain the above copyright notice,
  87. * this list of conditions and the following disclaimer.
  88. * 2. Redistributions in binary form must reproduce the above copyright notice,
  89. * this list of conditions and the following disclaimer in the documentation
  90. * and/or other materials provided with the distribution.
  91. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  92. * may be used to endorse or promote products derived from this software
  93. * without specific prior written permission.
  94. *
  95. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  96. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  97. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  98. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  99. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  100. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  101. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  102. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  103. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  104. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  105. *
  106. ******************************************************************************
  107. */
  108. /* Includes ------------------------------------------------------------------*/
  109. #include "stm32l4xx_hal.h"
  110. /** @addtogroup STM32L4xx_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. /* Init the low level hardware */
  163. HAL_WWDG_MspInit(hwwdg);
  164. /* Set WWDG Counter */
  165. WRITE_REG(hwwdg->Instance->CR, (WWDG_CR_WDGA | hwwdg->Init.Counter));
  166. /* Set WWDG Prescaler and Window */
  167. WRITE_REG(hwwdg->Instance->CFR, (hwwdg->Init.EWIMode | hwwdg->Init.Prescaler | hwwdg->Init.Window));
  168. /* Return function status */
  169. return HAL_OK;
  170. }
  171. /**
  172. * @brief Initialize the WWDG MSP.
  173. * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
  174. * the configuration information for the specified WWDG module.
  175. * @note When rewriting this function in user file, mechanism may be added
  176. * to avoid multiple initialize when HAL_WWDG_Init function is called
  177. * again to change parameters.
  178. * @retval None
  179. */
  180. __weak void HAL_WWDG_MspInit(WWDG_HandleTypeDef *hwwdg)
  181. {
  182. /* Prevent unused argument(s) compilation warning */
  183. UNUSED(hwwdg);
  184. /* NOTE: This function should not be modified, when the callback is needed,
  185. the HAL_WWDG_MspInit could be implemented in the user file
  186. */
  187. }
  188. /**
  189. * @}
  190. */
  191. /** @defgroup WWDG_Exported_Functions_Group2 IO operation functions
  192. * @brief IO operation functions
  193. *
  194. @verbatim
  195. ==============================================================================
  196. ##### IO operation functions #####
  197. ==============================================================================
  198. [..]
  199. This section provides functions allowing to:
  200. (+) Refresh the WWDG.
  201. (+) Handle WWDG interrupt request and associated function callback.
  202. @endverbatim
  203. * @{
  204. */
  205. /**
  206. * @brief Refresh the WWDG.
  207. * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
  208. * the configuration information for the specified WWDG module.
  209. * @retval HAL status
  210. */
  211. HAL_StatusTypeDef HAL_WWDG_Refresh(WWDG_HandleTypeDef *hwwdg)
  212. {
  213. /* Write to WWDG CR the WWDG Counter value to refresh with */
  214. WRITE_REG(hwwdg->Instance->CR, (hwwdg->Init.Counter));
  215. /* Return function status */
  216. return HAL_OK;
  217. }
  218. /**
  219. * @brief Handle WWDG interrupt request.
  220. * @note The Early Wakeup Interrupt (EWI) can be used if specific safety operations
  221. * or data logging must be performed before the actual reset is generated.
  222. * The EWI interrupt is enabled by calling HAL_WWDG_Init function with
  223. * EWIMode set to WWDG_EWI_ENABLE.
  224. * When the downcounter reaches the value 0x40, and EWI interrupt is
  225. * generated and the corresponding Interrupt Service Routine (ISR) can
  226. * be used to trigger specific actions (such as communications or data
  227. * logging), before resetting the device.
  228. * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
  229. * the configuration information for the specified WWDG module.
  230. * @retval None
  231. */
  232. void HAL_WWDG_IRQHandler(WWDG_HandleTypeDef *hwwdg)
  233. {
  234. /* Check if Early Wakeup Interrupt is enable */
  235. if(__HAL_WWDG_GET_IT_SOURCE(hwwdg, WWDG_IT_EWI) != RESET)
  236. {
  237. /* Check if WWDG Early Wakeup Interrupt occurred */
  238. if(__HAL_WWDG_GET_FLAG(hwwdg, WWDG_FLAG_EWIF) != RESET)
  239. {
  240. /* Clear the WWDG Early Wakeup flag */
  241. __HAL_WWDG_CLEAR_FLAG(hwwdg, WWDG_FLAG_EWIF);
  242. /* Early Wakeup callback */
  243. HAL_WWDG_EarlyWakeupCallback(hwwdg);
  244. }
  245. }
  246. }
  247. /**
  248. * @brief WWDG Early Wakeup callback.
  249. * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
  250. * the configuration information for the specified WWDG module.
  251. * @retval None
  252. */
  253. __weak void HAL_WWDG_EarlyWakeupCallback(WWDG_HandleTypeDef* hwwdg)
  254. {
  255. /* Prevent unused argument(s) compilation warning */
  256. UNUSED(hwwdg);
  257. /* NOTE: This function should not be modified, when the callback is needed,
  258. the HAL_WWDG_EarlyWakeupCallback could be implemented in the user file
  259. */
  260. }
  261. /**
  262. * @}
  263. */
  264. /**
  265. * @}
  266. */
  267. #endif /* HAL_WWDG_MODULE_ENABLED */
  268. /**
  269. * @}
  270. */
  271. /**
  272. * @}
  273. */
  274. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/