stm32f4xx_iwdg.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_iwdg.c
  4. * @author MCD Application Team
  5. * @version V1.8.1
  6. * @date 27-January-2022
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the Independent watchdog (IWDG) peripheral:
  9. * + Prescaler and Counter configuration
  10. * + IWDG activation
  11. * + Flag management
  12. *
  13. @verbatim
  14. ===============================================================================
  15. ##### IWDG 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 its own dedicated low-speed clock (LSI) and
  21. thus stays active even if the main clock fails.
  22. Once the IWDG is started, the LSI is forced ON and cannot be disabled
  23. (LSI cannot be disabled too), and the counter starts counting down from
  24. the reset value of 0xFFF. When it reaches the end of count value (0x000)
  25. a system reset is generated.
  26. The IWDG counter should be reloaded at regular intervals to prevent
  27. an MCU reset.
  28. The IWDG is implemented in the VDD voltage domain that is still functional
  29. in STOP and STANDBY mode (IWDG reset can wake-up from STANDBY).
  30. IWDGRST flag in RCC_CSR register can be used to inform when a IWDG
  31. reset occurs.
  32. Min-max timeout value @32KHz (LSI): ~125us / ~32.7s
  33. The IWDG timeout may vary due to LSI frequency dispersion. STM32F4xx
  34. devices provide the capability to measure the LSI frequency (LSI clock
  35. connected internally to TIM5 CH4 input capture). The measured value
  36. can be used to have an IWDG timeout with an acceptable accuracy.
  37. For more information, please refer to the STM32F4xx Reference manual
  38. ##### How to use this driver #####
  39. ===============================================================================
  40. [..]
  41. (#) Enable write access to IWDG_PR and IWDG_RLR registers using
  42. IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable) function
  43. (#) Configure the IWDG prescaler using IWDG_SetPrescaler() function
  44. (#) Configure the IWDG counter value using IWDG_SetReload() function.
  45. This value will be loaded in the IWDG counter each time the counter
  46. is reloaded, then the IWDG will start counting down from this value.
  47. (#) Start the IWDG using IWDG_Enable() function, when the IWDG is used
  48. in software mode (no need to enable the LSI, it will be enabled
  49. by hardware)
  50. (#) Then the application program must reload the IWDG counter at regular
  51. intervals during normal operation to prevent an MCU reset, using
  52. IWDG_ReloadCounter() function.
  53. @endverbatim
  54. ******************************************************************************
  55. * @attention
  56. *
  57. * Copyright (c) 2016 STMicroelectronics.
  58. * All rights reserved.
  59. *
  60. * This software is licensed under terms that can be found in the LICENSE file
  61. * in the root directory of this software component.
  62. * If no LICENSE file comes with this software, it is provided AS-IS.
  63. *
  64. ******************************************************************************
  65. */
  66. /* Includes ------------------------------------------------------------------*/
  67. #include "stm32f4xx_iwdg.h"
  68. /** @addtogroup STM32F4xx_StdPeriph_Driver
  69. * @{
  70. */
  71. /** @defgroup IWDG
  72. * @brief IWDG driver modules
  73. * @{
  74. */
  75. /* Private typedef -----------------------------------------------------------*/
  76. /* Private define ------------------------------------------------------------*/
  77. /* KR register bit mask */
  78. #define KR_KEY_RELOAD ((uint16_t)0xAAAA)
  79. #define KR_KEY_ENABLE ((uint16_t)0xCCCC)
  80. /* Private macro -------------------------------------------------------------*/
  81. /* Private variables ---------------------------------------------------------*/
  82. /* Private function prototypes -----------------------------------------------*/
  83. /* Private functions ---------------------------------------------------------*/
  84. /** @defgroup IWDG_Private_Functions
  85. * @{
  86. */
  87. /** @defgroup IWDG_Group1 Prescaler and Counter configuration functions
  88. * @brief Prescaler and Counter configuration functions
  89. *
  90. @verbatim
  91. ===============================================================================
  92. ##### Prescaler and Counter configuration functions #####
  93. ===============================================================================
  94. @endverbatim
  95. * @{
  96. */
  97. /**
  98. * @brief Enables or disables write access to IWDG_PR and IWDG_RLR registers.
  99. * @param IWDG_WriteAccess: new state of write access to IWDG_PR and IWDG_RLR registers.
  100. * This parameter can be one of the following values:
  101. * @arg IWDG_WriteAccess_Enable: Enable write access to IWDG_PR and IWDG_RLR registers
  102. * @arg IWDG_WriteAccess_Disable: Disable write access to IWDG_PR and IWDG_RLR registers
  103. * @retval None
  104. */
  105. void IWDG_WriteAccessCmd(uint16_t IWDG_WriteAccess)
  106. {
  107. /* Check the parameters */
  108. assert_param(IS_IWDG_WRITE_ACCESS(IWDG_WriteAccess));
  109. IWDG->KR = IWDG_WriteAccess;
  110. }
  111. /**
  112. * @brief Sets IWDG Prescaler value.
  113. * @param IWDG_Prescaler: specifies the IWDG Prescaler value.
  114. * This parameter can be one of the following values:
  115. * @arg IWDG_Prescaler_4: IWDG prescaler set to 4
  116. * @arg IWDG_Prescaler_8: IWDG prescaler set to 8
  117. * @arg IWDG_Prescaler_16: IWDG prescaler set to 16
  118. * @arg IWDG_Prescaler_32: IWDG prescaler set to 32
  119. * @arg IWDG_Prescaler_64: IWDG prescaler set to 64
  120. * @arg IWDG_Prescaler_128: IWDG prescaler set to 128
  121. * @arg IWDG_Prescaler_256: IWDG prescaler set to 256
  122. * @retval None
  123. */
  124. void IWDG_SetPrescaler(uint8_t IWDG_Prescaler)
  125. {
  126. /* Check the parameters */
  127. assert_param(IS_IWDG_PRESCALER(IWDG_Prescaler));
  128. IWDG->PR = IWDG_Prescaler;
  129. }
  130. /**
  131. * @brief Sets IWDG Reload value.
  132. * @param Reload: specifies the IWDG Reload value.
  133. * This parameter must be a number between 0 and 0x0FFF.
  134. * @retval None
  135. */
  136. void IWDG_SetReload(uint16_t Reload)
  137. {
  138. /* Check the parameters */
  139. assert_param(IS_IWDG_RELOAD(Reload));
  140. IWDG->RLR = Reload;
  141. }
  142. /**
  143. * @brief Reloads IWDG counter with value defined in the reload register
  144. * (write access to IWDG_PR and IWDG_RLR registers disabled).
  145. * @param None
  146. * @retval None
  147. */
  148. void IWDG_ReloadCounter(void)
  149. {
  150. IWDG->KR = KR_KEY_RELOAD;
  151. }
  152. /**
  153. * @}
  154. */
  155. /** @defgroup IWDG_Group2 IWDG activation function
  156. * @brief IWDG activation function
  157. *
  158. @verbatim
  159. ===============================================================================
  160. ##### IWDG activation function #####
  161. ===============================================================================
  162. @endverbatim
  163. * @{
  164. */
  165. /**
  166. * @brief Enables IWDG (write access to IWDG_PR and IWDG_RLR registers disabled).
  167. * @param None
  168. * @retval None
  169. */
  170. void IWDG_Enable(void)
  171. {
  172. IWDG->KR = KR_KEY_ENABLE;
  173. }
  174. /**
  175. * @}
  176. */
  177. /** @defgroup IWDG_Group3 Flag management function
  178. * @brief Flag management function
  179. *
  180. @verbatim
  181. ===============================================================================
  182. ##### Flag management function #####
  183. ===============================================================================
  184. @endverbatim
  185. * @{
  186. */
  187. /**
  188. * @brief Checks whether the specified IWDG flag is set or not.
  189. * @param IWDG_FLAG: specifies the flag to check.
  190. * This parameter can be one of the following values:
  191. * @arg IWDG_FLAG_PVU: Prescaler Value Update on going
  192. * @arg IWDG_FLAG_RVU: Reload Value Update on going
  193. * @retval The new state of IWDG_FLAG (SET or RESET).
  194. */
  195. FlagStatus IWDG_GetFlagStatus(uint16_t IWDG_FLAG)
  196. {
  197. FlagStatus bitstatus = RESET;
  198. /* Check the parameters */
  199. assert_param(IS_IWDG_FLAG(IWDG_FLAG));
  200. if ((IWDG->SR & IWDG_FLAG) != (uint32_t)RESET)
  201. {
  202. bitstatus = SET;
  203. }
  204. else
  205. {
  206. bitstatus = RESET;
  207. }
  208. /* Return the flag status */
  209. return bitstatus;
  210. }
  211. /**
  212. * @}
  213. */
  214. /**
  215. * @}
  216. */
  217. /**
  218. * @}
  219. */
  220. /**
  221. * @}
  222. */