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.
 
 
 

283 lines
10 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_hal_iwdg.c
  4. * @author MCD Application Team
  5. * @version V1.2.2
  6. * @date 14-April-2017
  7. * @brief IWDG HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the Independent Watchdog (IWDG) peripheral:
  10. * + Initialization and Start functions
  11. * + IO operation functions
  12. *
  13. @verbatim
  14. ==============================================================================
  15. ##### IWDG Generic features #####
  16. ==============================================================================
  17. [..]
  18. (+) The IWDG can be started by either software or hardware (configurable
  19. through option byte).
  20. (+) The IWDG is clocked by Low-Speed clock (LSI) and thus stays active even
  21. if the main clock fails.
  22. (+) Once the IWDG is started, the LSI is forced ON and both can not be
  23. disabled. The counter starts counting down from the reset value (0xFFF).
  24. When it reaches the end of count value (0x000) a reset signal is
  25. generated (IWDG reset).
  26. (+) Whenever the key value 0x0000 AAAA is written in the IWDG_KR register,
  27. the IWDG_RLR value is reloaded in the counter and the watchdog reset is
  28. prevented.
  29. (+) The IWDG is implemented in the VDD voltage domain that is still functional
  30. in STOP and STANDBY mode (IWDG reset can wake-up from STANDBY).
  31. IWDGRST flag in RCC_CSR register can be used to inform when an IWDG
  32. reset occurs.
  33. (+) Debug mode : When the microcontroller enters debug mode (core halted),
  34. the IWDG counter either continues to work normally or stops, depending
  35. on DBG_IWDG_STOP configuration bit in DBG module, accessible through
  36. __HAL_DBGMCU_FREEZE_IWDG() and __HAL_DBGMCU_UNFREEZE_IWDG() macros
  37. [..] Min-max timeout value @32KHz (LSI): ~125us / ~32.7s
  38. The IWDG timeout may vary due to LSI frequency dispersion. STM32F7xx
  39. devices provide the capability to measure the LSI frequency (LSI clock
  40. connected internally to TIM16 CH1 input capture). The measured value
  41. can be used to have an IWDG timeout with an acceptable accuracy.
  42. ##### How to use this driver #####
  43. ==============================================================================
  44. [..]
  45. (#) Use IWDG using HAL_IWDG_Init() function to :
  46. (++) Enable instance by writing Start keyword in IWDG_KEY register. LSI
  47. clock is forced ON and IWDG counter starts downcounting.
  48. (++) Enable write access to configuration register: IWDG_PR, IWDG_RLR &
  49. IWDG_WINR.
  50. (++) Configure the IWDG prescaler and counter reload value. This reload
  51. value will be loaded in the IWDG counter each time the watchdog is
  52. reloaded, then the IWDG will start counting down from this value.
  53. (++) wait for status flags to be reset
  54. (++) Depending on window parameter:
  55. (+++) If Window Init parameter is same as Window register value,
  56. nothing more is done but reload counter value in order to exit
  57. function withy exact time base.
  58. (+++) Else modify Window register. This will automatically reload
  59. watchdog counter.
  60. (#) Then the application program must refresh the IWDG counter at regular
  61. intervals during normal operation to prevent an MCU reset, using
  62. HAL_IWDG_Refresh() function.
  63. *** IWDG HAL driver macros list ***
  64. ====================================
  65. [..]
  66. Below the list of most used macros in IWDG HAL driver:
  67. (+) __HAL_IWDG_START: Enable the IWDG peripheral
  68. (+) __HAL_IWDG_RELOAD_COUNTER: Reloads IWDG counter with value defined in
  69. the reload register
  70. @endverbatim
  71. ******************************************************************************
  72. * @attention
  73. *
  74. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  75. *
  76. * Redistribution and use in source and binary forms, with or without modification,
  77. * are permitted provided that the following conditions are met:
  78. * 1. Redistributions of source code must retain the above copyright notice,
  79. * this list of conditions and the following disclaimer.
  80. * 2. Redistributions in binary form must reproduce the above copyright notice,
  81. * this list of conditions and the following disclaimer in the documentation
  82. * and/or other materials provided with the distribution.
  83. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  84. * may be used to endorse or promote products derived from this software
  85. * without specific prior written permission.
  86. *
  87. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  88. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  89. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  90. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  91. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  92. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  93. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  94. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  95. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  96. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  97. *
  98. ******************************************************************************
  99. */
  100. /* Includes ------------------------------------------------------------------*/
  101. #include "stm32f7xx_hal.h"
  102. /** @addtogroup STM32F7xx_HAL_Driver
  103. * @{
  104. */
  105. #ifdef HAL_IWDG_MODULE_ENABLED
  106. /** @addtogroup IWDG
  107. * @brief IWDG HAL module driver.
  108. * @{
  109. */
  110. /* Private typedef -----------------------------------------------------------*/
  111. /* Private define ------------------------------------------------------------*/
  112. /** @defgroup IWDG_Private_Defines IWDG Private Defines
  113. * @{
  114. */
  115. /* Status register need 5 RC LSI divided by prescaler clock to be updated. With
  116. higher prescaler (256), and according to LSI variation, we need to wait at
  117. least 6 cycles so 48 ms. */
  118. #define HAL_IWDG_DEFAULT_TIMEOUT 48u
  119. /**
  120. * @}
  121. */
  122. /* Private macro -------------------------------------------------------------*/
  123. /* Private variables ---------------------------------------------------------*/
  124. /* Private function prototypes -----------------------------------------------*/
  125. /* Exported functions --------------------------------------------------------*/
  126. /** @addtogroup IWDG_Exported_Functions
  127. * @{
  128. */
  129. /** @addtogroup IWDG_Exported_Functions_Group1
  130. * @brief Initialization and Start functions.
  131. *
  132. @verbatim
  133. ===============================================================================
  134. ##### Initialization and Start functions #####
  135. ===============================================================================
  136. [..] This section provides functions allowing to:
  137. (+) Initialize the IWDG according to the specified parameters in the
  138. IWDG_InitTypeDef of associated handle.
  139. (+) Manage Window option.
  140. (+) Once initialization is performed in HAL_IWDG_Init function, Watchdog
  141. is reloaded in order to exit function with correct time base.
  142. @endverbatim
  143. * @{
  144. */
  145. /**
  146. * @brief Initialize the IWDG according to the specified parameters in the
  147. * IWDG_InitTypeDef and start watchdog. Before exiting function,
  148. * watchdog is refreshed in order to have correct time base.
  149. * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
  150. * the configuration information for the specified IWDG module.
  151. * @retval HAL status
  152. */
  153. HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
  154. {
  155. uint32_t tickstart;
  156. /* Check the IWDG handle allocation */
  157. if(hiwdg == NULL)
  158. {
  159. return HAL_ERROR;
  160. }
  161. /* Check the parameters */
  162. assert_param(IS_IWDG_ALL_INSTANCE(hiwdg->Instance));
  163. assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler));
  164. assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload));
  165. assert_param(IS_IWDG_WINDOW(hiwdg->Init.Window));
  166. /* Enable IWDG. LSI is turned on automaticaly */
  167. __HAL_IWDG_START(hiwdg);
  168. /* Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers by writing
  169. 0x5555 in KR */
  170. IWDG_ENABLE_WRITE_ACCESS(hiwdg);
  171. /* Write to IWDG registers the Prescaler & Reload values to work with */
  172. hiwdg->Instance->PR = hiwdg->Init.Prescaler;
  173. hiwdg->Instance->RLR = hiwdg->Init.Reload;
  174. /* Check pending flag, if previous update not done, return timeout */
  175. tickstart = HAL_GetTick();
  176. /* Wait for register to be updated */
  177. while(hiwdg->Instance->SR != RESET)
  178. {
  179. if((HAL_GetTick() - tickstart ) > HAL_IWDG_DEFAULT_TIMEOUT)
  180. {
  181. return HAL_TIMEOUT;
  182. }
  183. }
  184. /* If window parameter is different than current value, modify window
  185. register */
  186. if(hiwdg->Instance->WINR != hiwdg->Init.Window)
  187. {
  188. /* Write to IWDG WINR the IWDG_Window value to compare with. In any case,
  189. even if window feature is disabled, Watchdog will be reloaded by writing
  190. windows register */
  191. hiwdg->Instance->WINR = hiwdg->Init.Window;
  192. }
  193. else
  194. {
  195. /* Reload IWDG counter with value defined in the reload register */
  196. __HAL_IWDG_RELOAD_COUNTER(hiwdg);
  197. }
  198. /* Return function status */
  199. return HAL_OK;
  200. }
  201. /**
  202. * @}
  203. */
  204. /** @addtogroup IWDG_Exported_Functions_Group2
  205. * @brief IO operation functions
  206. *
  207. @verbatim
  208. ===============================================================================
  209. ##### IO operation functions #####
  210. ===============================================================================
  211. [..] This section provides functions allowing to:
  212. (+) Refresh the IWDG.
  213. @endverbatim
  214. * @{
  215. */
  216. /**
  217. * @brief Refresh the IWDG.
  218. * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
  219. * the configuration information for the specified IWDG module.
  220. * @retval HAL status
  221. */
  222. HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
  223. {
  224. /* Reload IWDG counter with value defined in the reload register */
  225. __HAL_IWDG_RELOAD_COUNTER(hiwdg);
  226. /* Return function status */
  227. return HAL_OK;
  228. }
  229. /**
  230. * @}
  231. */
  232. /**
  233. * @}
  234. */
  235. #endif /* HAL_IWDG_MODULE_ENABLED */
  236. /**
  237. * @}
  238. */
  239. /**
  240. * @}
  241. */
  242. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/