stm32f1xx_hal_iwdg.c 9.6 KB

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