stm32l4xx_hal_iwdg.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_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): ~125us / ~32.7s
  36. The IWDG timeout may vary due to LSI frequency dispersion. STM32L4xx
  37. devices provide the capability to measure the LSI frequency (LSI clock
  38. connected internally to TIM16 CH1 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) 2017 STMicroelectronics</center></h2>
  73. *
  74. * Redistribution and use in source and binary forms, with or without modification,
  75. * are permitted provided that the following conditions are met:
  76. * 1. Redistributions of source code must retain the above copyright notice,
  77. * this list of conditions and the following disclaimer.
  78. * 2. Redistributions in binary form must reproduce the above copyright notice,
  79. * this list of conditions and the following disclaimer in the documentation
  80. * and/or other materials provided with the distribution.
  81. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  82. * may be used to endorse or promote products derived from this software
  83. * without specific prior written permission.
  84. *
  85. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  86. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  87. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  88. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  89. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  90. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  91. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  92. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  93. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  94. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  95. *
  96. ******************************************************************************
  97. */
  98. /* Includes ------------------------------------------------------------------*/
  99. #include "stm32l4xx_hal.h"
  100. /** @addtogroup STM32L4xx_HAL_Driver
  101. * @{
  102. */
  103. #ifdef HAL_IWDG_MODULE_ENABLED
  104. /** @addtogroup IWDG
  105. * @brief IWDG HAL module driver.
  106. * @{
  107. */
  108. /* Private typedef -----------------------------------------------------------*/
  109. /* Private define ------------------------------------------------------------*/
  110. /** @defgroup IWDG_Private_Defines IWDG Private Defines
  111. * @{
  112. */
  113. /* Status register need 5 RC LSI divided by prescaler clock to be updated. With
  114. higher prescaler (256), and according to HSI variation, we need to wait at
  115. least 6 cycles so 48 ms. */
  116. #define HAL_IWDG_DEFAULT_TIMEOUT 48u
  117. /**
  118. * @}
  119. */
  120. /* Private macro -------------------------------------------------------------*/
  121. /* Private variables ---------------------------------------------------------*/
  122. /* Private function prototypes -----------------------------------------------*/
  123. /* Exported functions --------------------------------------------------------*/
  124. /** @addtogroup IWDG_Exported_Functions
  125. * @{
  126. */
  127. /** @addtogroup IWDG_Exported_Functions_Group1
  128. * @brief Initialization and Start functions.
  129. *
  130. @verbatim
  131. ===============================================================================
  132. ##### Initialization and Start functions #####
  133. ===============================================================================
  134. [..] This section provides functions allowing to:
  135. (+) Initialize the IWDG according to the specified parameters in the
  136. IWDG_InitTypeDef of associated handle.
  137. (+) Manage Window option.
  138. (+) Once initialization is performed in HAL_IWDG_Init function, Watchdog
  139. is reloaded in order to exit function with correct time base.
  140. @endverbatim
  141. * @{
  142. */
  143. /**
  144. * @brief Initialize the IWDG according to the specified parameters in the
  145. * IWDG_InitTypeDef and start watchdog. Before exiting function,
  146. * watchdog is refreshed in order to have correct time base.
  147. * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
  148. * the configuration information for the specified IWDG module.
  149. * @retval HAL status
  150. */
  151. HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
  152. {
  153. uint32_t tickstart;
  154. /* Check the IWDG handle allocation */
  155. if(hiwdg == NULL)
  156. {
  157. return HAL_ERROR;
  158. }
  159. /* Check the parameters */
  160. assert_param(IS_IWDG_ALL_INSTANCE(hiwdg->Instance));
  161. assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler));
  162. assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload));
  163. assert_param(IS_IWDG_WINDOW(hiwdg->Init.Window));
  164. /* Enable IWDG. LSI is turned on automaticaly */
  165. __HAL_IWDG_START(hiwdg);
  166. /* Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers by writing
  167. 0x5555 in KR */
  168. IWDG_ENABLE_WRITE_ACCESS(hiwdg);
  169. /* Write to IWDG registers the Prescaler & Reload values to work with */
  170. hiwdg->Instance->PR = hiwdg->Init.Prescaler;
  171. hiwdg->Instance->RLR = hiwdg->Init.Reload;
  172. /* Check pending flag, if previous update not done, return timeout */
  173. tickstart = HAL_GetTick();
  174. /* Wait for register to be updated */
  175. while(hiwdg->Instance->SR != RESET)
  176. {
  177. if((HAL_GetTick() - tickstart ) > HAL_IWDG_DEFAULT_TIMEOUT)
  178. {
  179. return HAL_TIMEOUT;
  180. }
  181. }
  182. /* If window parameter is different than current value, modify window
  183. register */
  184. if(hiwdg->Instance->WINR != hiwdg->Init.Window)
  185. {
  186. /* Write to IWDG WINR the IWDG_Window value to compare with. In any case,
  187. even if window feature is disabled, Watchdog will be reloaded by writing
  188. windows register */
  189. hiwdg->Instance->WINR = hiwdg->Init.Window;
  190. }
  191. else
  192. {
  193. /* Reload IWDG counter with value defined in the reload register */
  194. __HAL_IWDG_RELOAD_COUNTER(hiwdg);
  195. }
  196. /* Return function status */
  197. return HAL_OK;
  198. }
  199. /**
  200. * @}
  201. */
  202. /** @addtogroup IWDG_Exported_Functions_Group2
  203. * @brief IO operation functions
  204. *
  205. @verbatim
  206. ===============================================================================
  207. ##### IO operation functions #####
  208. ===============================================================================
  209. [..] This section provides functions allowing to:
  210. (+) Refresh the IWDG.
  211. @endverbatim
  212. * @{
  213. */
  214. /**
  215. * @brief Refresh the IWDG.
  216. * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
  217. * the configuration information for the specified IWDG module.
  218. * @retval HAL status
  219. */
  220. HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
  221. {
  222. /* Reload IWDG counter with value defined in the reload register */
  223. __HAL_IWDG_RELOAD_COUNTER(hiwdg);
  224. /* Return function status */
  225. return HAL_OK;
  226. }
  227. /**
  228. * @}
  229. */
  230. /**
  231. * @}
  232. */
  233. #endif /* HAL_IWDG_MODULE_ENABLED */
  234. /**
  235. * @}
  236. */
  237. /**
  238. * @}
  239. */
  240. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/