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.
 
 
 

266 lines
9.2 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_hal_iwdg.c
  4. * @author MCD Application Team
  5. * @brief IWDG HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Independent Watchdog (IWDG) peripheral:
  8. * + Initialization and Start functions
  9. * + IO operation functions
  10. *
  11. @verbatim
  12. ==============================================================================
  13. ##### IWDG Generic features #####
  14. ==============================================================================
  15. [..]
  16. (+) The IWDG can be started by either software or hardware (configurable
  17. through option byte).
  18. (+) The IWDG is clocked by Low-Speed clock (LSI) and thus stays active even
  19. if the main clock fails.
  20. (+) Once the IWDG is started, the LSI is forced ON and both can not be
  21. disabled. The counter starts counting down from the reset value (0xFFF).
  22. When it reaches the end of count value (0x000) a reset signal is
  23. generated (IWDG reset).
  24. (+) Whenever the key value 0x0000 AAAA is written in the IWDG_KR register,
  25. the IWDG_RLR value is reloaded in the counter and the watchdog reset is
  26. prevented.
  27. (+) The IWDG is implemented in the VDD voltage domain that is still functional
  28. in STOP and STANDBY mode (IWDG reset can wake-up from STANDBY).
  29. IWDGRST flag in RCC_CSR register can be used to inform when an IWDG
  30. reset occurs.
  31. (+) Debug mode : When the microcontroller enters debug mode (core halted),
  32. the IWDG counter either continues to work normally or stops, depending
  33. on DBG_IWDG_STOP configuration bit in DBG module, accessible through
  34. __HAL_DBGMCU_FREEZE_IWDG1() or __HAL_DBGMCU_FREEZE2_IWDG2() and
  35. __HAL_DBGMCU_UnFreeze_IWDG1 or __HAL_DBGMCU_UnFreeze2_IWDG2() macros.
  36. [..] Min-max timeout value @32KHz (LSI): ~125us / ~32.7s
  37. The IWDG timeout may vary due to LSI frequency dispersion. STM32H7xx
  38. devices provide the capability to measure the LSI frequency (LSI clock
  39. connected internally to TIM16 CH1 input capture). The measured value
  40. can be used to have an IWDG timeout with an acceptable accuracy.
  41. ##### How to use this driver #####
  42. ==============================================================================
  43. [..]
  44. (#) Use IWDG using HAL_IWDG_Init() function to :
  45. (++) Enable instance by writing Start keyword in IWDG_KEY register. LSI
  46. clock is forced ON and IWDG counter starts counting down.
  47. (++) Enable write access to configuration registers:
  48. IWDG_PR, IWDG_RLR and IWDG_WINR.
  49. (++) Configure the IWDG prescaler and counter reload value. This reload
  50. value will be loaded in the IWDG counter each time the watchdog is
  51. reloaded, then the IWDG will start counting down from this value.
  52. (++) Wait for status flags to be reset.
  53. (++) Depending on window parameter:
  54. (+++) If Window Init parameter is same as Window register value,
  55. nothing more is done but reload counter value in order to exit
  56. function with exact time base.
  57. (+++) Else modify Window register. This will automatically reload
  58. watchdog counter.
  59. (#) Then the application program must refresh the IWDG counter at regular
  60. intervals during normal operation to prevent an MCU reset, using
  61. HAL_IWDG_Refresh() function.
  62. *** IWDG HAL driver macros list ***
  63. ====================================
  64. [..]
  65. Below the list of most used macros in IWDG HAL driver:
  66. (+) __HAL_IWDG_START: Enable the IWDG peripheral
  67. (+) __HAL_IWDG_RELOAD_COUNTER: Reloads IWDG counter with value defined in
  68. the reload register
  69. @endverbatim
  70. ******************************************************************************
  71. * @attention
  72. *
  73. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  74. * All rights reserved.</center></h2>
  75. *
  76. * This software component is licensed by ST under BSD 3-Clause license,
  77. * the "License"; You may not use this file except in compliance with the
  78. * License. You may obtain a copy of the License at:
  79. * opensource.org/licenses/BSD-3-Clause
  80. *
  81. ******************************************************************************
  82. */
  83. /* Includes ------------------------------------------------------------------*/
  84. #include "stm32h7xx_hal.h"
  85. /** @addtogroup STM32H7xx_HAL_Driver
  86. * @{
  87. */
  88. #ifdef HAL_IWDG_MODULE_ENABLED
  89. /** @addtogroup IWDG
  90. * @brief IWDG HAL module driver.
  91. * @{
  92. */
  93. /* Private typedef -----------------------------------------------------------*/
  94. /* Private define ------------------------------------------------------------*/
  95. /** @defgroup IWDG_Private_Defines IWDG Private Defines
  96. * @{
  97. */
  98. /* Status register need 5 RC LSI divided by prescaler clock to be updated. With
  99. higher prescaler (256), and according to LSI variation, we need to wait at
  100. least 6 cycles so 48 ms. */
  101. #define HAL_IWDG_DEFAULT_TIMEOUT 48u
  102. /**
  103. * @}
  104. */
  105. /* Private macro -------------------------------------------------------------*/
  106. /* Private variables ---------------------------------------------------------*/
  107. /* Private function prototypes -----------------------------------------------*/
  108. /* Exported functions --------------------------------------------------------*/
  109. /** @addtogroup IWDG_Exported_Functions
  110. * @{
  111. */
  112. /** @addtogroup IWDG_Exported_Functions_Group1
  113. * @brief Initialization and Start functions.
  114. *
  115. @verbatim
  116. ===============================================================================
  117. ##### Initialization and Start functions #####
  118. ===============================================================================
  119. [..] This section provides functions allowing to:
  120. (+) Initialize the IWDG according to the specified parameters in the
  121. IWDG_InitTypeDef of associated handle.
  122. (+) Manage Window option.
  123. (+) Once initialization is performed in HAL_IWDG_Init function, Watchdog
  124. is reloaded in order to exit function with correct time base.
  125. @endverbatim
  126. * @{
  127. */
  128. /**
  129. * @brief Initialize the IWDG according to the specified parameters in the
  130. * IWDG_InitTypeDef and start watchdog. Before exiting function,
  131. * watchdog is refreshed in order to have correct time base.
  132. * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
  133. * the configuration information for the specified IWDG module.
  134. * @retval HAL status
  135. */
  136. HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
  137. {
  138. uint32_t tickstart;
  139. /* Check the IWDG handle allocation */
  140. if (hiwdg == NULL)
  141. {
  142. return HAL_ERROR;
  143. }
  144. /* Check the parameters */
  145. assert_param(IS_IWDG_ALL_INSTANCE(hiwdg->Instance));
  146. assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler));
  147. assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload));
  148. assert_param(IS_IWDG_WINDOW(hiwdg->Init.Window));
  149. /* Enable IWDG. LSI is turned on automatically */
  150. __HAL_IWDG_START(hiwdg);
  151. /* Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers by writing
  152. 0x5555 in KR */
  153. IWDG_ENABLE_WRITE_ACCESS(hiwdg);
  154. /* Write to IWDG registers the Prescaler & Reload values to work with */
  155. hiwdg->Instance->PR = hiwdg->Init.Prescaler;
  156. hiwdg->Instance->RLR = hiwdg->Init.Reload;
  157. /* Check pending flag, if previous update not done, return timeout */
  158. tickstart = HAL_GetTick();
  159. /* Wait for register to be updated */
  160. while (hiwdg->Instance->SR != 0x00u)
  161. {
  162. if ((HAL_GetTick() - tickstart) > HAL_IWDG_DEFAULT_TIMEOUT)
  163. {
  164. return HAL_TIMEOUT;
  165. }
  166. }
  167. /* If window parameter is different than current value, modify window
  168. register */
  169. if (hiwdg->Instance->WINR != hiwdg->Init.Window)
  170. {
  171. /* Write to IWDG WINR the IWDG_Window value to compare with. In any case,
  172. even if window feature is disabled, Watchdog will be reloaded by writing
  173. windows register */
  174. hiwdg->Instance->WINR = hiwdg->Init.Window;
  175. }
  176. else
  177. {
  178. /* Reload IWDG counter with value defined in the reload register */
  179. __HAL_IWDG_RELOAD_COUNTER(hiwdg);
  180. }
  181. /* Return function status */
  182. return HAL_OK;
  183. }
  184. /**
  185. * @}
  186. */
  187. /** @addtogroup IWDG_Exported_Functions_Group2
  188. * @brief IO operation functions
  189. *
  190. @verbatim
  191. ===============================================================================
  192. ##### IO operation functions #####
  193. ===============================================================================
  194. [..] This section provides functions allowing to:
  195. (+) Refresh the IWDG.
  196. @endverbatim
  197. * @{
  198. */
  199. /**
  200. * @brief Refresh the IWDG.
  201. * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
  202. * the configuration information for the specified IWDG module.
  203. * @retval HAL status
  204. */
  205. HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
  206. {
  207. /* Reload IWDG counter with value defined in the reload register */
  208. __HAL_IWDG_RELOAD_COUNTER(hiwdg);
  209. /* Return function status */
  210. return HAL_OK;
  211. }
  212. /**
  213. * @}
  214. */
  215. /**
  216. * @}
  217. */
  218. #endif /* HAL_IWDG_MODULE_ENABLED */
  219. /**
  220. * @}
  221. */
  222. /**
  223. * @}
  224. */
  225. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/