stm32f1xx_hal_iwdg.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_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) 2016 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_IWDG() and __HAL_DBGMCU_UNFREEZE_IWDG() macros.
  46. [..] Min-max timeout value @32KHz (LSI): ~125us / ~32.7s
  47. The IWDG timeout may vary due to LSI clock frequency dispersion.
  48. STM32F1xx devices provide the capability to measure the LSI clock
  49. frequency (LSI clock is internally connected to TIM5 CH4 input capture).
  50. The measured value can be used to have an IWDG timeout with an
  51. acceptable accuracy.
  52. [..] Default timeout value (necessary for IWDG_SR status register update):
  53. Constant LSI_VALUE is defined based on the nominal LSI clock frequency.
  54. This frequency being subject to variations as mentioned above, the
  55. default timeout value (defined through constant HAL_IWDG_DEFAULT_TIMEOUT
  56. below) may become too short or too long.
  57. In such cases, this default timeout value can be tuned by redefining
  58. the constant LSI_VALUE at user-application level (based, for instance,
  59. on the measured LSI clock frequency as explained above).
  60. ##### How to use this driver #####
  61. ==============================================================================
  62. [..]
  63. (#) Use IWDG using HAL_IWDG_Init() function to :
  64. (++) Enable instance by writing Start keyword in IWDG_KEY register. LSI
  65. clock is forced ON and IWDG counter starts counting down.
  66. (++) Enable write access to configuration registers:
  67. IWDG_PR and IWDG_RLR.
  68. (++) Configure the IWDG prescaler and counter reload value. This reload
  69. value will be loaded in the IWDG counter each time the watchdog is
  70. reloaded, then the IWDG will start counting down from this value.
  71. (++) Wait for status flags to be reset.
  72. (#) Then the application program must refresh the IWDG counter at regular
  73. intervals during normal operation to prevent an MCU reset, using
  74. HAL_IWDG_Refresh() function.
  75. *** IWDG HAL driver macros list ***
  76. ====================================
  77. [..]
  78. Below the list of most used macros in IWDG HAL driver:
  79. (+) __HAL_IWDG_START: Enable the IWDG peripheral
  80. (+) __HAL_IWDG_RELOAD_COUNTER: Reloads IWDG counter with value defined in
  81. the reload register
  82. @endverbatim
  83. */
  84. /* Includes ------------------------------------------------------------------*/
  85. #include "stm32f1xx_hal.h"
  86. /** @addtogroup STM32F1xx_HAL_Driver
  87. * @{
  88. */
  89. #ifdef HAL_IWDG_MODULE_ENABLED
  90. /** @addtogroup IWDG
  91. * @brief IWDG HAL module driver.
  92. * @{
  93. */
  94. /* Private typedef -----------------------------------------------------------*/
  95. /* Private define ------------------------------------------------------------*/
  96. /** @defgroup IWDG_Private_Defines IWDG Private Defines
  97. * @{
  98. */
  99. /* Status register needs up to 5 LSI clock periods divided by the clock
  100. prescaler to be updated. The number of LSI clock periods is upper-rounded to
  101. 6 for the timeout value calculation.
  102. The timeout value is calculated using the highest prescaler (256) and
  103. the LSI_VALUE constant. The value of this constant can be changed by the user
  104. to take into account possible LSI clock period variations.
  105. The timeout value is multiplied by 1000 to be converted in milliseconds.
  106. LSI startup time is also considered here by adding LSI_STARTUP_TIME
  107. converted in milliseconds. */
  108. #define HAL_IWDG_DEFAULT_TIMEOUT (((6UL * 256UL * 1000UL) / LSI_VALUE) + ((LSI_STARTUP_TIME / 1000UL) + 1UL))
  109. #define IWDG_KERNEL_UPDATE_FLAGS (IWDG_SR_RVU | IWDG_SR_PVU)
  110. /**
  111. * @}
  112. */
  113. /* Private macro -------------------------------------------------------------*/
  114. /* Private variables ---------------------------------------------------------*/
  115. /* Private function prototypes -----------------------------------------------*/
  116. /* Exported functions --------------------------------------------------------*/
  117. /** @addtogroup IWDG_Exported_Functions
  118. * @{
  119. */
  120. /** @addtogroup IWDG_Exported_Functions_Group1
  121. * @brief Initialization and Start functions.
  122. *
  123. @verbatim
  124. ===============================================================================
  125. ##### Initialization and Start functions #####
  126. ===============================================================================
  127. [..] This section provides functions allowing to:
  128. (+) Initialize the IWDG according to the specified parameters in the
  129. IWDG_InitTypeDef of associated handle.
  130. (+) Once initialization is performed in HAL_IWDG_Init function, Watchdog
  131. is reloaded in order to exit function with correct time base.
  132. @endverbatim
  133. * @{
  134. */
  135. /**
  136. * @brief Initialize the IWDG according to the specified parameters in the
  137. * IWDG_InitTypeDef and start watchdog. Before exiting function,
  138. * watchdog is refreshed in order to have correct time base.
  139. * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
  140. * the configuration information for the specified IWDG module.
  141. * @retval HAL status
  142. */
  143. HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
  144. {
  145. uint32_t tickstart;
  146. /* Check the IWDG handle allocation */
  147. if (hiwdg == NULL)
  148. {
  149. return HAL_ERROR;
  150. }
  151. /* Check the parameters */
  152. assert_param(IS_IWDG_ALL_INSTANCE(hiwdg->Instance));
  153. assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler));
  154. assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload));
  155. /* Enable IWDG. LSI is turned on automatically */
  156. __HAL_IWDG_START(hiwdg);
  157. /* Enable write access to IWDG_PR and IWDG_RLR registers by writing
  158. 0x5555 in KR */
  159. IWDG_ENABLE_WRITE_ACCESS(hiwdg);
  160. /* Write to IWDG registers the Prescaler & Reload values to work with */
  161. hiwdg->Instance->PR = hiwdg->Init.Prescaler;
  162. hiwdg->Instance->RLR = hiwdg->Init.Reload;
  163. /* Check pending flag, if previous update not done, return timeout */
  164. tickstart = HAL_GetTick();
  165. /* Wait for register to be updated */
  166. while ((hiwdg->Instance->SR & IWDG_KERNEL_UPDATE_FLAGS) != 0x00u)
  167. {
  168. if ((HAL_GetTick() - tickstart) > HAL_IWDG_DEFAULT_TIMEOUT)
  169. {
  170. if ((hiwdg->Instance->SR & IWDG_KERNEL_UPDATE_FLAGS) != 0x00u)
  171. {
  172. return HAL_TIMEOUT;
  173. }
  174. }
  175. }
  176. /* Reload IWDG counter with value defined in the reload register */
  177. __HAL_IWDG_RELOAD_COUNTER(hiwdg);
  178. /* Return function status */
  179. return HAL_OK;
  180. }
  181. /**
  182. * @}
  183. */
  184. /** @addtogroup IWDG_Exported_Functions_Group2
  185. * @brief IO operation functions
  186. *
  187. @verbatim
  188. ===============================================================================
  189. ##### IO operation functions #####
  190. ===============================================================================
  191. [..] This section provides functions allowing to:
  192. (+) Refresh the IWDG.
  193. @endverbatim
  194. * @{
  195. */
  196. /**
  197. * @brief Refresh the IWDG.
  198. * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
  199. * the configuration information for the specified IWDG module.
  200. * @retval HAL status
  201. */
  202. HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
  203. {
  204. /* Reload IWDG counter with value defined in the reload register */
  205. __HAL_IWDG_RELOAD_COUNTER(hiwdg);
  206. /* Return function status */
  207. return HAL_OK;
  208. }
  209. /**
  210. * @}
  211. */
  212. /**
  213. * @}
  214. */
  215. #endif /* HAL_IWDG_MODULE_ENABLED */
  216. /**
  217. * @}
  218. */
  219. /**
  220. * @}
  221. */