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.

stm32l1xx_hal_iwdg.c 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_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_IWDG() and __HAL_DBGMCU_UNFREEZE_IWDG() macros
  35. (+) Min-max timeout value @37KHz (LSI): ~108us / ~28.3s
  36. The IWDG timeout may vary due to LSI frequency dispersion. STM32L1xx
  37. devices provide the capability to measure the LSI frequency (LSI clock
  38. connected internally to TIM10 CH1 input capture). The measured value
  39. can be used to have an IWDG timeout with an acceptable accuracy.
  40. For more information, please refer to the STM32L1xx Reference manual.
  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 downcounting.
  47. (++) Enable write access to configuration register: IWDG_PR, IWDG_RLR.
  48. (++) Configure the IWDG prescaler and counter reload value. This reload
  49. value will be loaded in the IWDG counter each time the watchdog is
  50. reloaded, then the IWDG will start counting down from this value.
  51. (++) wait for status flags to be reset"
  52. (#) Then the application program must refresh the IWDG counter at regular
  53. intervals during normal operation to prevent an MCU reset, using
  54. HAL_IWDG_Refresh() function.
  55. *** IWDG HAL driver macros list ***
  56. ====================================
  57. [..]
  58. Below the list of most used macros in IWDG HAL driver:
  59. (+) __HAL_IWDG_START: Enable the IWDG peripheral
  60. (+) __HAL_IWDG_RELOAD_COUNTER: Reloads IWDG counter with value defined in
  61. the reload register
  62. @endverbatim
  63. ******************************************************************************
  64. * @attention
  65. *
  66. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  67. * All rights reserved.</center></h2>
  68. *
  69. * This software component is licensed by ST under BSD 3-Clause license,
  70. * the "License"; You may not use this file except in compliance with the
  71. * License. You may obtain a copy of the License at:
  72. * opensource.org/licenses/BSD-3-Clause
  73. *
  74. ******************************************************************************
  75. */
  76. /* Includes ------------------------------------------------------------------*/
  77. #include "stm32l1xx_hal.h"
  78. /** @addtogroup STM32L1xx_HAL_Driver
  79. * @{
  80. */
  81. #ifdef HAL_IWDG_MODULE_ENABLED
  82. /** @addtogroup IWDG
  83. * @brief IWDG HAL module driver.
  84. * @{
  85. */
  86. /* Private typedef -----------------------------------------------------------*/
  87. /* Private define ------------------------------------------------------------*/
  88. /** @defgroup IWDG_Private_Defines IWDG Private Defines
  89. * @{
  90. */
  91. /* Status register need 5 RC LSI divided by prescaler clock to be updated. With
  92. higher prescaler (256), and according to HSI variation, we need to wait at
  93. least 6 cycles so 48 ms. */
  94. #define HAL_IWDG_DEFAULT_TIMEOUT 48u
  95. /**
  96. * @}
  97. */
  98. /* Private macro -------------------------------------------------------------*/
  99. /* Private variables ---------------------------------------------------------*/
  100. /* Private function prototypes -----------------------------------------------*/
  101. /* Exported functions --------------------------------------------------------*/
  102. /** @addtogroup IWDG_Exported_Functions
  103. * @{
  104. */
  105. /** @addtogroup IWDG_Exported_Functions_Group1
  106. * @brief Initialization and Start functions.
  107. *
  108. @verbatim
  109. ===============================================================================
  110. ##### Initialization and Start functions #####
  111. ===============================================================================
  112. [..] This section provides functions allowing to:
  113. (+) Initialize the IWDG according to the specified parameters in the
  114. IWDG_InitTypeDef of associated handle.
  115. (+) Once initialization is performed in HAL_IWDG_Init function, Watchdog
  116. is reloaded in order to exit function with correct time base.
  117. @endverbatim
  118. * @{
  119. */
  120. /**
  121. * @brief Initialize the IWDG according to the specified parameters in the
  122. * IWDG_InitTypeDef and start watchdog. Before exiting function,
  123. * watchdog is refreshed in order to have correct time base.
  124. * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
  125. * the configuration information for the specified IWDG module.
  126. * @retval HAL status
  127. */
  128. HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
  129. {
  130. uint32_t tickstart;
  131. /* Check the IWDG handle allocation */
  132. if(hiwdg == NULL)
  133. {
  134. return HAL_ERROR;
  135. }
  136. /* Check the parameters */
  137. assert_param(IS_IWDG_ALL_INSTANCE(hiwdg->Instance));
  138. assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler));
  139. assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload));
  140. /* Enable IWDG. LSI is turned on automaticaly */
  141. __HAL_IWDG_START(hiwdg);
  142. /* Enable write access to IWDG_PR, IWDG_RLR registers by writing
  143. 0x5555 in KR */
  144. IWDG_ENABLE_WRITE_ACCESS(hiwdg);
  145. /* Write to IWDG registers the Prescaler & Reload values to work with */
  146. hiwdg->Instance->PR = hiwdg->Init.Prescaler;
  147. hiwdg->Instance->RLR = hiwdg->Init.Reload;
  148. /* Check pending flag, if previous update not done, return timeout */
  149. tickstart = HAL_GetTick();
  150. /* Wait for register to be updated */
  151. while(hiwdg->Instance->SR != RESET)
  152. {
  153. if((HAL_GetTick() - tickstart ) > HAL_IWDG_DEFAULT_TIMEOUT)
  154. {
  155. return HAL_TIMEOUT;
  156. }
  157. }
  158. /* Reload IWDG counter with value defined in the reload register */
  159. __HAL_IWDG_RELOAD_COUNTER(hiwdg);
  160. /* Return function status */
  161. return HAL_OK;
  162. }
  163. /**
  164. * @}
  165. */
  166. /** @addtogroup IWDG_Exported_Functions_Group2
  167. * @brief IO operation functions
  168. *
  169. @verbatim
  170. ===============================================================================
  171. ##### IO operation functions #####
  172. ===============================================================================
  173. [..] This section provides functions allowing to:
  174. (+) Refresh the IWDG.
  175. @endverbatim
  176. * @{
  177. */
  178. /**
  179. * @brief Refresh the IWDG.
  180. * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
  181. * the configuration information for the specified IWDG module.
  182. * @retval HAL status
  183. */
  184. HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
  185. {
  186. /* Reload IWDG counter with value defined in the reload register */
  187. __HAL_IWDG_RELOAD_COUNTER(hiwdg);
  188. /* Return function status */
  189. return HAL_OK;
  190. }
  191. /**
  192. * @}
  193. */
  194. /**
  195. * @}
  196. */
  197. #endif /* HAL_IWDG_MODULE_ENABLED */
  198. /**
  199. * @}
  200. */
  201. /**
  202. * @}
  203. */
  204. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/