stm32f1xx_hal_iwdg.c 9.6 KB

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