stm32f4xx_hal_iwdg.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_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. STM32F4xx
  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. (++) Configure the IWDG prescaler and counter reload value. This reload
  48. value will be loaded in the IWDG counter each time the watchdog is
  49. reloaded, then the IWDG will start counting down from this value.
  50. (++) wait for status flags to be reset"
  51. (#) Then the application program must refresh the IWDG counter at regular
  52. intervals during normal operation to prevent an MCU reset, using
  53. HAL_IWDG_Refresh() function.
  54. *** IWDG HAL driver macros list ***
  55. ====================================
  56. [..]
  57. Below the list of most used macros in IWDG HAL driver:
  58. (+) __HAL_IWDG_START: Enable the IWDG peripheral
  59. (+) __HAL_IWDG_RELOAD_COUNTER: Reloads IWDG counter with value defined in
  60. the reload register
  61. @endverbatim
  62. ******************************************************************************
  63. * @attention
  64. *
  65. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  66. *
  67. * Redistribution and use in source and binary forms, with or without modification,
  68. * are permitted provided that the following conditions are met:
  69. * 1. Redistributions of source code must retain the above copyright notice,
  70. * this list of conditions and the following disclaimer.
  71. * 2. Redistributions in binary form must reproduce the above copyright notice,
  72. * this list of conditions and the following disclaimer in the documentation
  73. * and/or other materials provided with the distribution
  74. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  75. * may be used to endorse or promote products derived from this software
  76. * without specific prior written permission.
  77. *
  78. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  79. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  80. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  81. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  82. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  83. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  84. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  85. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  86. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  87. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  88. *
  89. ******************************************************************************
  90. */
  91. /* Includes ------------------------------------------------------------------*/
  92. #include "stm32f4xx_hal.h"
  93. /** @addtogroup STM32F4xx_HAL_Driver
  94. * @{
  95. */
  96. #ifdef HAL_IWDG_MODULE_ENABLED
  97. /** @defgroup IWDG 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 need 5 RC LSI divided by prescaler clock to be updated. With
  107. higher prescaler (256), and according to HSI variation, we need to wait at
  108. least 6 cycles so 48 ms. */
  109. #define HAL_IWDG_DEFAULT_TIMEOUT 48U
  110. /**
  111. * @}
  112. */
  113. /* Private macro -------------------------------------------------------------*/
  114. /* Private variables ---------------------------------------------------------*/
  115. /* Private function prototypes -----------------------------------------------*/
  116. /* Exported functions --------------------------------------------------------*/
  117. /** @addtogroup IWDG_Exported_Functions
  118. * @{
  119. */
  120. /** @addtogroup IWDG_Exported_Functions_Group1
  121. * @brief Initialization and Start functions.
  122. *
  123. @verbatim
  124. ===============================================================================
  125. ##### Initialization and Start functions #####
  126. ===============================================================================
  127. [..] This section provides functions allowing to:
  128. (+) Initialize the IWDG according to the specified parameters in the
  129. IWDG_InitTypeDef of associated handle.
  130. (+) Once initialization is performed in HAL_IWDG_Init function, Watchdog
  131. is reloaded in order to exit function with correct time base.
  132. @endverbatim
  133. * @{
  134. */
  135. /**
  136. * @brief Initialize the IWDG according to the specified parameters in the
  137. * IWDG_InitTypeDef and start watchdog. Before exiting function,
  138. * watchdog is refreshed in order to have correct time base.
  139. * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
  140. * the configuration information for the specified IWDG module.
  141. * @retval HAL status
  142. */
  143. HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
  144. {
  145. uint32_t tickstart;
  146. /* Check the IWDG handle allocation */
  147. if(hiwdg == NULL)
  148. {
  149. return HAL_ERROR;
  150. }
  151. /* Check the parameters */
  152. assert_param(IS_IWDG_ALL_INSTANCE(hiwdg->Instance));
  153. assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler));
  154. assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload));
  155. /* Enable IWDG. LSI is turned on automaticaly */
  156. __HAL_IWDG_START(hiwdg);
  157. /* Enable write access to IWDG_PR and IWDG_RLR registers by writing 0x5555 in KR */
  158. IWDG_ENABLE_WRITE_ACCESS(hiwdg);
  159. /* Write to IWDG registers the Prescaler & Reload values to work with */
  160. hiwdg->Instance->PR = hiwdg->Init.Prescaler;
  161. hiwdg->Instance->RLR = hiwdg->Init.Reload;
  162. /* Check pending flag, if previous update not done, return timeout */
  163. tickstart = HAL_GetTick();
  164. /* Wait for register to be updated */
  165. while(hiwdg->Instance->SR != RESET)
  166. {
  167. if((HAL_GetTick() - tickstart ) > HAL_IWDG_DEFAULT_TIMEOUT)
  168. {
  169. return HAL_TIMEOUT;
  170. }
  171. }
  172. /* Reload IWDG counter with value defined in the reload register */
  173. __HAL_IWDG_RELOAD_COUNTER(hiwdg);
  174. /* Return function status */
  175. return HAL_OK;
  176. }
  177. /**
  178. * @}
  179. */
  180. /** @addtogroup IWDG_Exported_Functions_Group2
  181. * @brief IO operation functions
  182. *
  183. @verbatim
  184. ===============================================================================
  185. ##### IO operation functions #####
  186. ===============================================================================
  187. [..] This section provides functions allowing to:
  188. (+) Refresh the IWDG.
  189. @endverbatim
  190. * @{
  191. */
  192. /**
  193. * @brief Refresh the IWDG.
  194. * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
  195. * the configuration information for the specified IWDG module.
  196. * @retval HAL status
  197. */
  198. HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
  199. {
  200. /* Reload IWDG counter with value defined in the reload register */
  201. __HAL_IWDG_RELOAD_COUNTER(hiwdg);
  202. /* Return function status */
  203. return HAL_OK;
  204. }
  205. /**
  206. * @}
  207. */
  208. /**
  209. * @}
  210. */
  211. #endif /* HAL_IWDG_MODULE_ENABLED */
  212. /**
  213. * @}
  214. */
  215. /**
  216. * @}
  217. */
  218. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/