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.
 
 
 

265 lines
9.2 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l0xx_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 @32KHz (LSI): ~0.512ms / ~32.0s
  36. The IWDG timeout may vary due to LSI frequency dispersion. STM32L0xx
  37. devices provide the capability to measure the LSI frequency (LSI clock
  38. connected internally to TIM5 CH4 input capture). The measured value
  39. can be used to have an IWDG timeout with an acceptable accuracy.
  40. ##### How to use this driver #####
  41. ==============================================================================
  42. [..]
  43. (#) Use IWDG using HAL_IWDG_Init() function to :
  44. (++) Enable instance by writing Start keyword in IWDG_KEY register. LSI
  45. clock is forced ON and IWDG counter starts downcounting.
  46. (++) Enable write access to configuration register: IWDG_PR, IWDG_RLR &
  47. IWDG_WINR.
  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. (++) Depending on window parameter:
  53. (+++) If Window Init parameter is same as Window register value,
  54. nothing more is done but reload counter value in order to exit
  55. function withy exact time base.
  56. (+++) Else modify Window register. This will automatically reload
  57. watchdog counter.
  58. (#) Then the application program must refresh the IWDG counter at regular
  59. intervals during normal operation to prevent an MCU reset, using
  60. HAL_IWDG_Refresh() function.
  61. *** IWDG HAL driver macros list ***
  62. ====================================
  63. [..]
  64. Below the list of most used macros in IWDG HAL driver:
  65. (+) __HAL_IWDG_START: Enable the IWDG peripheral
  66. (+) __HAL_IWDG_RELOAD_COUNTER: Reloads IWDG counter with value defined in
  67. the reload register
  68. @endverbatim
  69. ******************************************************************************
  70. * @attention
  71. *
  72. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  73. * All rights reserved.</center></h2>
  74. *
  75. * This software component is licensed by ST under BSD 3-Clause license,
  76. * the "License"; You may not use this file except in compliance with the
  77. * License. You may obtain a copy of the License at:
  78. * opensource.org/licenses/BSD-3-Clause
  79. *
  80. ******************************************************************************
  81. */
  82. /* Includes ------------------------------------------------------------------*/
  83. #include "stm32l0xx_hal.h"
  84. /** @addtogroup STM32L0xx_HAL_Driver
  85. * @{
  86. */
  87. #ifdef HAL_IWDG_MODULE_ENABLED
  88. /** @addtogroup IWDG
  89. * @brief IWDG HAL module driver.
  90. * @{
  91. */
  92. /* Private typedef -----------------------------------------------------------*/
  93. /* Private define ------------------------------------------------------------*/
  94. /** @defgroup IWDG_Private_Defines IWDG Private Defines
  95. * @{
  96. */
  97. /* Status register need 5 RC LSI divided by prescaler clock to be updated. With
  98. higher prescaler (256), and according to LSI variation, we need to wait at
  99. least 6 cycles so 48 ms. */
  100. #define HAL_IWDG_DEFAULT_TIMEOUT 48U
  101. /**
  102. * @}
  103. */
  104. /* Private macro -------------------------------------------------------------*/
  105. /* Private variables ---------------------------------------------------------*/
  106. /* Private function prototypes -----------------------------------------------*/
  107. /* Exported functions --------------------------------------------------------*/
  108. /** @addtogroup IWDG_Exported_Functions
  109. * @{
  110. */
  111. /** @addtogroup IWDG_Exported_Functions_Group1
  112. * @brief Initialization and Start functions.
  113. *
  114. @verbatim
  115. ===============================================================================
  116. ##### Initialization and Start functions #####
  117. ===============================================================================
  118. [..] This section provides functions allowing to:
  119. (+) Initialize the IWDG according to the specified parameters in the
  120. IWDG_InitTypeDef of associated handle.
  121. (+) Manage Window option.
  122. (+) Once initialization is performed in HAL_IWDG_Init function, Watchdog
  123. is reloaded in order to exit function with correct time base.
  124. @endverbatim
  125. * @{
  126. */
  127. /**
  128. * @brief Initialize the IWDG according to the specified parameters in the
  129. * IWDG_InitTypeDef and start watchdog. Before exiting function,
  130. * watchdog is refreshed in order to have correct time base.
  131. * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
  132. * the configuration information for the specified IWDG module.
  133. * @retval HAL status
  134. */
  135. HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
  136. {
  137. uint32_t tickstart;
  138. /* Check the IWDG handle allocation */
  139. if(hiwdg == NULL)
  140. {
  141. return HAL_ERROR;
  142. }
  143. /* Check the parameters */
  144. assert_param(IS_IWDG_ALL_INSTANCE(hiwdg->Instance));
  145. assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler));
  146. assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload));
  147. assert_param(IS_IWDG_WINDOW(hiwdg->Init.Window));
  148. /* Enable IWDG. LSI is turned on automaticaly */
  149. __HAL_IWDG_START(hiwdg);
  150. /* Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers by writing
  151. 0x5555 in KR */
  152. IWDG_ENABLE_WRITE_ACCESS(hiwdg);
  153. /* Write to IWDG registers the Prescaler & Reload values to work with */
  154. hiwdg->Instance->PR = hiwdg->Init.Prescaler;
  155. hiwdg->Instance->RLR = hiwdg->Init.Reload;
  156. /* Check pending flag, if previous update not done, return timeout */
  157. tickstart = HAL_GetTick();
  158. /* Wait for register to be updated */
  159. while(hiwdg->Instance->SR != RESET)
  160. {
  161. if((HAL_GetTick() - tickstart ) > HAL_IWDG_DEFAULT_TIMEOUT)
  162. {
  163. return HAL_TIMEOUT;
  164. }
  165. }
  166. /* If window parameter is different than current value, modify window
  167. register */
  168. if(hiwdg->Instance->WINR != hiwdg->Init.Window)
  169. {
  170. /* Write to IWDG WINR the IWDG_Window value to compare with. In any case,
  171. even if window feature is disabled, Watchdog will be reloaded by writing
  172. windows register */
  173. hiwdg->Instance->WINR = hiwdg->Init.Window;
  174. }
  175. else
  176. {
  177. /* Reload IWDG counter with value defined in the reload register */
  178. __HAL_IWDG_RELOAD_COUNTER(hiwdg);
  179. }
  180. /* Return function status */
  181. return HAL_OK;
  182. }
  183. /**
  184. * @}
  185. */
  186. /** @addtogroup IWDG_Exported_Functions_Group2
  187. * @brief IO operation functions
  188. *
  189. @verbatim
  190. ===============================================================================
  191. ##### IO operation functions #####
  192. ===============================================================================
  193. [..] This section provides functions allowing to:
  194. (+) Refresh the IWDG.
  195. @endverbatim
  196. * @{
  197. */
  198. /**
  199. * @brief Refresh the IWDG.
  200. * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
  201. * the configuration information for the specified IWDG module.
  202. * @retval HAL status
  203. */
  204. HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
  205. {
  206. /* Reload IWDG counter with value defined in the reload register */
  207. __HAL_IWDG_RELOAD_COUNTER(hiwdg);
  208. /* Return function status */
  209. return HAL_OK;
  210. }
  211. /**
  212. * @}
  213. */
  214. /**
  215. * @}
  216. */
  217. #endif /* HAL_IWDG_MODULE_ENABLED */
  218. /**
  219. * @}
  220. */
  221. /**
  222. * @}
  223. */
  224. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/