stm32f4xx_iwdg.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_iwdg.c
  4. * @author MCD Application Team
  5. * @version V1.0.2
  6. * @date 05-March-2012
  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. * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  71. *
  72. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  73. * You may not use this file except in compliance with the License.
  74. * You may obtain a copy of the License at:
  75. *
  76. * http://www.st.com/software_license_agreement_liberty_v2
  77. *
  78. * Unless required by applicable law or agreed to in writing, software
  79. * distributed under the License is distributed on an "AS IS" BASIS,
  80. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  81. * See the License for the specific language governing permissions and
  82. * limitations under the License.
  83. *
  84. ******************************************************************************
  85. */
  86. /* Includes ------------------------------------------------------------------*/
  87. #include "stm32f4xx_iwdg.h"
  88. /** @addtogroup STM32F4xx_StdPeriph_Driver
  89. * @{
  90. */
  91. /** @defgroup IWDG
  92. * @brief IWDG driver modules
  93. * @{
  94. */
  95. /* Private typedef -----------------------------------------------------------*/
  96. /* Private define ------------------------------------------------------------*/
  97. /* KR register bit mask */
  98. #define KR_KEY_RELOAD ((uint16_t)0xAAAA)
  99. #define KR_KEY_ENABLE ((uint16_t)0xCCCC)
  100. /* Private macro -------------------------------------------------------------*/
  101. /* Private variables ---------------------------------------------------------*/
  102. /* Private function prototypes -----------------------------------------------*/
  103. /* Private functions ---------------------------------------------------------*/
  104. /** @defgroup IWDG_Private_Functions
  105. * @{
  106. */
  107. /** @defgroup IWDG_Group1 Prescaler and Counter configuration functions
  108. * @brief Prescaler and Counter configuration functions
  109. *
  110. @verbatim
  111. ===============================================================================
  112. Prescaler and Counter configuration functions
  113. ===============================================================================
  114. @endverbatim
  115. * @{
  116. */
  117. /**
  118. * @brief Enables or disables write access to IWDG_PR and IWDG_RLR registers.
  119. * @param IWDG_WriteAccess: new state of write access to IWDG_PR and IWDG_RLR registers.
  120. * This parameter can be one of the following values:
  121. * @arg IWDG_WriteAccess_Enable: Enable write access to IWDG_PR and IWDG_RLR registers
  122. * @arg IWDG_WriteAccess_Disable: Disable write access to IWDG_PR and IWDG_RLR registers
  123. * @retval None
  124. */
  125. void IWDG_WriteAccessCmd(uint16_t IWDG_WriteAccess)
  126. {
  127. /* Check the parameters */
  128. assert_param(IS_IWDG_WRITE_ACCESS(IWDG_WriteAccess));
  129. IWDG->KR = IWDG_WriteAccess;
  130. }
  131. /**
  132. * @brief Sets IWDG Prescaler value.
  133. * @param IWDG_Prescaler: specifies the IWDG Prescaler value.
  134. * This parameter can be one of the following values:
  135. * @arg IWDG_Prescaler_4: IWDG prescaler set to 4
  136. * @arg IWDG_Prescaler_8: IWDG prescaler set to 8
  137. * @arg IWDG_Prescaler_16: IWDG prescaler set to 16
  138. * @arg IWDG_Prescaler_32: IWDG prescaler set to 32
  139. * @arg IWDG_Prescaler_64: IWDG prescaler set to 64
  140. * @arg IWDG_Prescaler_128: IWDG prescaler set to 128
  141. * @arg IWDG_Prescaler_256: IWDG prescaler set to 256
  142. * @retval None
  143. */
  144. void IWDG_SetPrescaler(uint8_t IWDG_Prescaler)
  145. {
  146. /* Check the parameters */
  147. assert_param(IS_IWDG_PRESCALER(IWDG_Prescaler));
  148. IWDG->PR = IWDG_Prescaler;
  149. }
  150. /**
  151. * @brief Sets IWDG Reload value.
  152. * @param Reload: specifies the IWDG Reload value.
  153. * This parameter must be a number between 0 and 0x0FFF.
  154. * @retval None
  155. */
  156. void IWDG_SetReload(uint16_t Reload)
  157. {
  158. /* Check the parameters */
  159. assert_param(IS_IWDG_RELOAD(Reload));
  160. IWDG->RLR = Reload;
  161. }
  162. /**
  163. * @brief Reloads IWDG counter with value defined in the reload register
  164. * (write access to IWDG_PR and IWDG_RLR registers disabled).
  165. * @param None
  166. * @retval None
  167. */
  168. void IWDG_ReloadCounter(void)
  169. {
  170. IWDG->KR = KR_KEY_RELOAD;
  171. }
  172. /**
  173. * @}
  174. */
  175. /** @defgroup IWDG_Group2 IWDG activation function
  176. * @brief IWDG activation function
  177. *
  178. @verbatim
  179. ===============================================================================
  180. IWDG activation function
  181. ===============================================================================
  182. @endverbatim
  183. * @{
  184. */
  185. /**
  186. * @brief Enables IWDG (write access to IWDG_PR and IWDG_RLR registers disabled).
  187. * @param None
  188. * @retval None
  189. */
  190. void IWDG_Enable(void)
  191. {
  192. IWDG->KR = KR_KEY_ENABLE;
  193. }
  194. /**
  195. * @}
  196. */
  197. /** @defgroup IWDG_Group3 Flag management function
  198. * @brief Flag management function
  199. *
  200. @verbatim
  201. ===============================================================================
  202. Flag management function
  203. ===============================================================================
  204. @endverbatim
  205. * @{
  206. */
  207. /**
  208. * @brief Checks whether the specified IWDG flag is set or not.
  209. * @param IWDG_FLAG: specifies the flag to check.
  210. * This parameter can be one of the following values:
  211. * @arg IWDG_FLAG_PVU: Prescaler Value Update on going
  212. * @arg IWDG_FLAG_RVU: Reload Value Update on going
  213. * @retval The new state of IWDG_FLAG (SET or RESET).
  214. */
  215. FlagStatus IWDG_GetFlagStatus(uint16_t IWDG_FLAG)
  216. {
  217. FlagStatus bitstatus = RESET;
  218. /* Check the parameters */
  219. assert_param(IS_IWDG_FLAG(IWDG_FLAG));
  220. if ((IWDG->SR & IWDG_FLAG) != (uint32_t)RESET)
  221. {
  222. bitstatus = SET;
  223. }
  224. else
  225. {
  226. bitstatus = RESET;
  227. }
  228. /* Return the flag status */
  229. return bitstatus;
  230. }
  231. /**
  232. * @}
  233. */
  234. /**
  235. * @}
  236. */
  237. /**
  238. * @}
  239. */
  240. /**
  241. * @}
  242. */
  243. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/