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.
 
 
 

279 lines
10 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32wbxx_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. STM32WBxx devices provide the capability to measure the LSI clock
  38. frequency (LSI clock is internally connected to TIM16 CH1 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, IWDG_RLR and IWDG_WINR.
  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. (++) Depending on window parameter:
  62. (+++) If Window Init parameter is same as Window register value,
  63. nothing more is done but reload counter value in order to exit
  64. function with exact time base.
  65. (+++) Else modify Window register. This will automatically reload
  66. watchdog counter.
  67. (#) Then the application program must refresh the IWDG counter at regular
  68. intervals during normal operation to prevent an MCU reset, using
  69. HAL_IWDG_Refresh() function.
  70. *** IWDG HAL driver macros list ***
  71. ====================================
  72. [..]
  73. Below the list of most used macros in IWDG HAL driver:
  74. (+) __HAL_IWDG_START: Enable the IWDG peripheral
  75. (+) __HAL_IWDG_RELOAD_COUNTER: Reloads IWDG counter with value defined in
  76. the reload register
  77. @endverbatim
  78. ******************************************************************************
  79. * @attention
  80. *
  81. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  82. * All rights reserved.</center></h2>
  83. *
  84. * This software component is licensed by ST under BSD 3-Clause license,
  85. * the "License"; You may not use this file except in compliance with the
  86. * License. You may obtain a copy of the License at:
  87. * opensource.org/licenses/BSD-3-Clause
  88. *
  89. ******************************************************************************
  90. */
  91. /* Includes ------------------------------------------------------------------*/
  92. #include "stm32wbxx_hal.h"
  93. /** @addtogroup STM32WBxx_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 also 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. #define HAL_IWDG_DEFAULT_TIMEOUT ((6UL * 256UL * 1000UL) / LSI_VALUE)
  114. /**
  115. * @}
  116. */
  117. /* Private macro -------------------------------------------------------------*/
  118. /* Private variables ---------------------------------------------------------*/
  119. /* Private function prototypes -----------------------------------------------*/
  120. /* Exported functions --------------------------------------------------------*/
  121. /** @addtogroup IWDG_Exported_Functions
  122. * @{
  123. */
  124. /** @addtogroup IWDG_Exported_Functions_Group1
  125. * @brief Initialization and Start functions.
  126. *
  127. @verbatim
  128. ===============================================================================
  129. ##### Initialization and Start functions #####
  130. ===============================================================================
  131. [..] This section provides functions allowing to:
  132. (+) Initialize the IWDG according to the specified parameters in the
  133. IWDG_InitTypeDef of associated handle.
  134. (+) Manage Window option.
  135. (+) Once initialization is performed in HAL_IWDG_Init function, Watchdog
  136. is reloaded in order to exit function with correct time base.
  137. @endverbatim
  138. * @{
  139. */
  140. /**
  141. * @brief Initialize the IWDG according to the specified parameters in the
  142. * IWDG_InitTypeDef and start watchdog. Before exiting function,
  143. * watchdog is refreshed in order to have correct time base.
  144. * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
  145. * the configuration information for the specified IWDG module.
  146. * @retval HAL status
  147. */
  148. HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
  149. {
  150. uint32_t tickstart;
  151. /* Check the IWDG handle allocation */
  152. if (hiwdg == NULL)
  153. {
  154. return HAL_ERROR;
  155. }
  156. /* Check the parameters */
  157. assert_param(IS_IWDG_ALL_INSTANCE(hiwdg->Instance));
  158. assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler));
  159. assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload));
  160. assert_param(IS_IWDG_WINDOW(hiwdg->Init.Window));
  161. /* Enable IWDG. LSI is turned on automatically */
  162. __HAL_IWDG_START(hiwdg);
  163. /* Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers by writing
  164. 0x5555 in KR */
  165. IWDG_ENABLE_WRITE_ACCESS(hiwdg);
  166. /* Write to IWDG registers the Prescaler & Reload values to work with */
  167. hiwdg->Instance->PR = hiwdg->Init.Prescaler;
  168. hiwdg->Instance->RLR = hiwdg->Init.Reload;
  169. /* Check pending flag, if previous update not done, return timeout */
  170. tickstart = HAL_GetTick();
  171. /* Wait for register to be updated */
  172. while (hiwdg->Instance->SR != 0x00u)
  173. {
  174. if ((HAL_GetTick() - tickstart) > HAL_IWDG_DEFAULT_TIMEOUT)
  175. {
  176. return HAL_TIMEOUT;
  177. }
  178. }
  179. /* If window parameter is different than current value, modify window
  180. register */
  181. if (hiwdg->Instance->WINR != hiwdg->Init.Window)
  182. {
  183. /* Write to IWDG WINR the IWDG_Window value to compare with. In any case,
  184. even if window feature is disabled, Watchdog will be reloaded by writing
  185. windows register */
  186. hiwdg->Instance->WINR = hiwdg->Init.Window;
  187. }
  188. else
  189. {
  190. /* Reload IWDG counter with value defined in the reload register */
  191. __HAL_IWDG_RELOAD_COUNTER(hiwdg);
  192. }
  193. /* Return function status */
  194. return HAL_OK;
  195. }
  196. /**
  197. * @}
  198. */
  199. /** @addtogroup IWDG_Exported_Functions_Group2
  200. * @brief IO operation functions
  201. *
  202. @verbatim
  203. ===============================================================================
  204. ##### IO operation functions #####
  205. ===============================================================================
  206. [..] This section provides functions allowing to:
  207. (+) Refresh the IWDG.
  208. @endverbatim
  209. * @{
  210. */
  211. /**
  212. * @brief Refresh the IWDG.
  213. * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
  214. * the configuration information for the specified IWDG module.
  215. * @retval HAL status
  216. */
  217. HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
  218. {
  219. /* Reload IWDG counter with value defined in the reload register */
  220. __HAL_IWDG_RELOAD_COUNTER(hiwdg);
  221. /* Return function status */
  222. return HAL_OK;
  223. }
  224. /**
  225. * @}
  226. */
  227. /**
  228. * @}
  229. */
  230. #endif /* HAL_IWDG_MODULE_ENABLED */
  231. /**
  232. * @}
  233. */
  234. /**
  235. * @}
  236. */
  237. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/