stm32f4xx_iwdg.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_iwdg.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 30-September-2011
  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. * ===================================================================
  16. * IWDG features
  17. * ===================================================================
  18. *
  19. * The IWDG can be started by either software or hardware (configurable
  20. * through option byte).
  21. *
  22. * The IWDG is clocked by its own dedicated low-speed clock (LSI) and
  23. * thus stays active even if the main clock fails.
  24. * Once the IWDG is started, the LSI is forced ON and cannot be disabled
  25. * (LSI cannot be disabled too), and the counter starts counting down from
  26. * the reset value of 0xFFF. When it reaches the end of count value (0x000)
  27. * a system reset is generated.
  28. * The IWDG counter should be reloaded at regular intervals to prevent
  29. * an MCU reset.
  30. *
  31. * The IWDG is implemented in the VDD voltage domain that is still functional
  32. * in STOP and STANDBY mode (IWDG reset can wake-up from STANDBY).
  33. *
  34. * IWDGRST flag in RCC_CSR register can be used to inform when a IWDG
  35. * reset occurs.
  36. *
  37. * Min-max timeout value @32KHz (LSI): ~125us / ~32.7s
  38. * The IWDG timeout may vary due to LSI frequency dispersion. STM32F4xx
  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. * For more information, please refer to the STM32F4xx Reference manual
  43. *
  44. *
  45. * ===================================================================
  46. * How to use this driver
  47. * ===================================================================
  48. * 1. Enable write access to IWDG_PR and IWDG_RLR registers using
  49. * IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable) function
  50. *
  51. * 2. Configure the IWDG prescaler using IWDG_SetPrescaler() function
  52. *
  53. * 3. Configure the IWDG counter value using IWDG_SetReload() function.
  54. * This value will be loaded in the IWDG counter each time the counter
  55. * is reloaded, then the IWDG will start counting down from this value.
  56. *
  57. * 4. Start the IWDG using IWDG_Enable() function, when the IWDG is used
  58. * in software mode (no need to enable the LSI, it will be enabled
  59. * by hardware)
  60. *
  61. * 5. Then the application program must reload the IWDG counter at regular
  62. * intervals during normal operation to prevent an MCU reset, using
  63. * IWDG_ReloadCounter() function.
  64. *
  65. * @endverbatim
  66. *
  67. ******************************************************************************
  68. * @attention
  69. *
  70. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  71. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  72. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  73. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  74. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  75. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  76. *
  77. * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  78. ******************************************************************************
  79. */
  80. /* Includes ------------------------------------------------------------------*/
  81. #include "stm32f4xx_iwdg.h"
  82. /** @addtogroup STM32F4xx_StdPeriph_Driver
  83. * @{
  84. */
  85. /** @defgroup IWDG
  86. * @brief IWDG driver modules
  87. * @{
  88. */
  89. /* Private typedef -----------------------------------------------------------*/
  90. /* Private define ------------------------------------------------------------*/
  91. /* KR register bit mask */
  92. #define KR_KEY_RELOAD ((uint16_t)0xAAAA)
  93. #define KR_KEY_ENABLE ((uint16_t)0xCCCC)
  94. /* Private macro -------------------------------------------------------------*/
  95. /* Private variables ---------------------------------------------------------*/
  96. /* Private function prototypes -----------------------------------------------*/
  97. /* Private functions ---------------------------------------------------------*/
  98. /** @defgroup IWDG_Private_Functions
  99. * @{
  100. */
  101. /** @defgroup IWDG_Group1 Prescaler and Counter configuration functions
  102. * @brief Prescaler and Counter configuration functions
  103. *
  104. @verbatim
  105. ===============================================================================
  106. Prescaler and Counter configuration functions
  107. ===============================================================================
  108. @endverbatim
  109. * @{
  110. */
  111. /**
  112. * @brief Enables or disables write access to IWDG_PR and IWDG_RLR registers.
  113. * @param IWDG_WriteAccess: new state of write access to IWDG_PR and IWDG_RLR registers.
  114. * This parameter can be one of the following values:
  115. * @arg IWDG_WriteAccess_Enable: Enable write access to IWDG_PR and IWDG_RLR registers
  116. * @arg IWDG_WriteAccess_Disable: Disable write access to IWDG_PR and IWDG_RLR registers
  117. * @retval None
  118. */
  119. void IWDG_WriteAccessCmd(uint16_t IWDG_WriteAccess)
  120. {
  121. /* Check the parameters */
  122. assert_param(IS_IWDG_WRITE_ACCESS(IWDG_WriteAccess));
  123. IWDG->KR = IWDG_WriteAccess;
  124. }
  125. /**
  126. * @brief Sets IWDG Prescaler value.
  127. * @param IWDG_Prescaler: specifies the IWDG Prescaler value.
  128. * This parameter can be one of the following values:
  129. * @arg IWDG_Prescaler_4: IWDG prescaler set to 4
  130. * @arg IWDG_Prescaler_8: IWDG prescaler set to 8
  131. * @arg IWDG_Prescaler_16: IWDG prescaler set to 16
  132. * @arg IWDG_Prescaler_32: IWDG prescaler set to 32
  133. * @arg IWDG_Prescaler_64: IWDG prescaler set to 64
  134. * @arg IWDG_Prescaler_128: IWDG prescaler set to 128
  135. * @arg IWDG_Prescaler_256: IWDG prescaler set to 256
  136. * @retval None
  137. */
  138. void IWDG_SetPrescaler(uint8_t IWDG_Prescaler)
  139. {
  140. /* Check the parameters */
  141. assert_param(IS_IWDG_PRESCALER(IWDG_Prescaler));
  142. IWDG->PR = IWDG_Prescaler;
  143. }
  144. /**
  145. * @brief Sets IWDG Reload value.
  146. * @param Reload: specifies the IWDG Reload value.
  147. * This parameter must be a number between 0 and 0x0FFF.
  148. * @retval None
  149. */
  150. void IWDG_SetReload(uint16_t Reload)
  151. {
  152. /* Check the parameters */
  153. assert_param(IS_IWDG_RELOAD(Reload));
  154. IWDG->RLR = Reload;
  155. }
  156. /**
  157. * @brief Reloads IWDG counter with value defined in the reload register
  158. * (write access to IWDG_PR and IWDG_RLR registers disabled).
  159. * @param None
  160. * @retval None
  161. */
  162. void IWDG_ReloadCounter(void)
  163. {
  164. IWDG->KR = KR_KEY_RELOAD;
  165. }
  166. /**
  167. * @}
  168. */
  169. /** @defgroup IWDG_Group2 IWDG activation function
  170. * @brief IWDG activation function
  171. *
  172. @verbatim
  173. ===============================================================================
  174. IWDG activation function
  175. ===============================================================================
  176. @endverbatim
  177. * @{
  178. */
  179. /**
  180. * @brief Enables IWDG (write access to IWDG_PR and IWDG_RLR registers disabled).
  181. * @param None
  182. * @retval None
  183. */
  184. void IWDG_Enable(void)
  185. {
  186. IWDG->KR = KR_KEY_ENABLE;
  187. }
  188. /**
  189. * @}
  190. */
  191. /** @defgroup IWDG_Group3 Flag management function
  192. * @brief Flag management function
  193. *
  194. @verbatim
  195. ===============================================================================
  196. Flag management function
  197. ===============================================================================
  198. @endverbatim
  199. * @{
  200. */
  201. /**
  202. * @brief Checks whether the specified IWDG flag is set or not.
  203. * @param IWDG_FLAG: specifies the flag to check.
  204. * This parameter can be one of the following values:
  205. * @arg IWDG_FLAG_PVU: Prescaler Value Update on going
  206. * @arg IWDG_FLAG_RVU: Reload Value Update on going
  207. * @retval The new state of IWDG_FLAG (SET or RESET).
  208. */
  209. FlagStatus IWDG_GetFlagStatus(uint16_t IWDG_FLAG)
  210. {
  211. FlagStatus bitstatus = RESET;
  212. /* Check the parameters */
  213. assert_param(IS_IWDG_FLAG(IWDG_FLAG));
  214. if ((IWDG->SR & IWDG_FLAG) != (uint32_t)RESET)
  215. {
  216. bitstatus = SET;
  217. }
  218. else
  219. {
  220. bitstatus = RESET;
  221. }
  222. /* Return the flag status */
  223. return bitstatus;
  224. }
  225. /**
  226. * @}
  227. */
  228. /**
  229. * @}
  230. */
  231. /**
  232. * @}
  233. */
  234. /**
  235. * @}
  236. */
  237. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/