stm32f4xx_iwdg.c 8.9 KB

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