stm32h7xx_hal_iwdg.c 11 KB

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