stm32f0xx_iwdg.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_iwdg.c
  4. * @author MCD Application Team
  5. * @version V1.5.0
  6. * @date 05-December-2014
  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. [..] 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 @40KHz (LSI): ~0.1ms / ~28.3s
  33. The IWDG timeout may vary due to LSI frequency dispersion. STM32F0xx
  34. devices provide the capability to measure the LSI frequency (LSI clock
  35. should be seleted as RTC clock which is internally connected to TIM10 CH1
  36. input capture). The measured value can be used to have an IWDG timeout with
  37. an acceptable accuracy.
  38. For more information, please refer to the STM32F0xx Reference manual.
  39. ##### How to use this driver #####
  40. ==============================================================================
  41. [..] This driver allows to use IWDG peripheral with either window option enabled
  42. or disabled. To do so follow one of the two procedures below.
  43. (#) Window option is enabled:
  44. (++) Start the IWDG using IWDG_Enable() function, when the IWDG is used
  45. in software mode (no need to enable the LSI, it will be enabled
  46. by hardware).
  47. (++) Enable write access to IWDG_PR and IWDG_RLR registers using
  48. IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable) function.
  49. (++) Configure the IWDG prescaler using IWDG_SetPrescaler() function.
  50. (++) Configure the IWDG counter value using IWDG_SetReload() function.
  51. This value will be loaded in the IWDG counter each time the counter
  52. is reloaded, then the IWDG will start counting down from this value.
  53. (++) Wait for the IWDG registers to be updated using IWDG_GetFlagStatus() function.
  54. (++) Configure the IWDG refresh window using IWDG_SetWindowValue() function.
  55. (#) Window option is disabled:
  56. (++) Enable write access to IWDG_PR and IWDG_RLR registers using
  57. IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable) function.
  58. (++) Configure the IWDG prescaler using IWDG_SetPrescaler() function.
  59. (++) Configure the IWDG counter value using IWDG_SetReload() function.
  60. This value will be loaded in the IWDG counter each time the counter
  61. is reloaded, then the IWDG will start counting down from this value.
  62. (++) Wait for the IWDG registers to be updated using IWDG_GetFlagStatus() function.
  63. (++) reload the IWDG counter at regular intervals during normal operation
  64. to prevent an MCU reset, using IWDG_ReloadCounter() function.
  65. (++) Start the IWDG using IWDG_Enable() function, when the IWDG is used
  66. in software mode (no need to enable the LSI, it will be enabled
  67. by hardware).
  68. @endverbatim
  69. *
  70. ******************************************************************************
  71. * @attention
  72. *
  73. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  74. *
  75. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  76. * You may not use this file except in compliance with the License.
  77. * You may obtain a copy of the License at:
  78. *
  79. * http://www.st.com/software_license_agreement_liberty_v2
  80. *
  81. * Unless required by applicable law or agreed to in writing, software
  82. * distributed under the License is distributed on an "AS IS" BASIS,
  83. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  84. * See the License for the specific language governing permissions and
  85. * limitations under the License.
  86. *
  87. ******************************************************************************
  88. */
  89. /* Includes ------------------------------------------------------------------*/
  90. #include "stm32f0xx_iwdg.h"
  91. /** @addtogroup STM32F0xx_StdPeriph_Driver
  92. * @{
  93. */
  94. /** @defgroup IWDG
  95. * @brief IWDG driver modules
  96. * @{
  97. */
  98. /* Private typedef -----------------------------------------------------------*/
  99. /* Private define ------------------------------------------------------------*/
  100. /* ---------------------- IWDG registers bit mask ----------------------------*/
  101. /* KR register bit mask */
  102. #define KR_KEY_RELOAD ((uint16_t)0xAAAA)
  103. #define KR_KEY_ENABLE ((uint16_t)0xCCCC)
  104. /* Private macro -------------------------------------------------------------*/
  105. /* Private variables ---------------------------------------------------------*/
  106. /* Private function prototypes -----------------------------------------------*/
  107. /* Private functions ---------------------------------------------------------*/
  108. /** @defgroup IWDG_Private_Functions
  109. * @{
  110. */
  111. /** @defgroup IWDG_Group1 Prescaler and Counter configuration functions
  112. * @brief Prescaler and Counter configuration functions
  113. *
  114. @verbatim
  115. ==============================================================================
  116. ##### Prescaler and Counter configuration functions #####
  117. ==============================================================================
  118. @endverbatim
  119. * @{
  120. */
  121. /**
  122. * @brief Enables or disables write access to IWDG_PR and IWDG_RLR registers.
  123. * @param IWDG_WriteAccess: new state of write access to IWDG_PR and IWDG_RLR registers.
  124. * This parameter can be one of the following values:
  125. * @arg IWDG_WriteAccess_Enable: Enable write access to IWDG_PR and IWDG_RLR registers
  126. * @arg IWDG_WriteAccess_Disable: Disable write access to IWDG_PR and IWDG_RLR registers
  127. * @retval None
  128. */
  129. void IWDG_WriteAccessCmd(uint16_t IWDG_WriteAccess)
  130. {
  131. /* Check the parameters */
  132. assert_param(IS_IWDG_WRITE_ACCESS(IWDG_WriteAccess));
  133. IWDG->KR = IWDG_WriteAccess;
  134. }
  135. /**
  136. * @brief Sets IWDG Prescaler value.
  137. * @param IWDG_Prescaler: specifies the IWDG Prescaler value.
  138. * This parameter can be one of the following values:
  139. * @arg IWDG_Prescaler_4: IWDG prescaler set to 4
  140. * @arg IWDG_Prescaler_8: IWDG prescaler set to 8
  141. * @arg IWDG_Prescaler_16: IWDG prescaler set to 16
  142. * @arg IWDG_Prescaler_32: IWDG prescaler set to 32
  143. * @arg IWDG_Prescaler_64: IWDG prescaler set to 64
  144. * @arg IWDG_Prescaler_128: IWDG prescaler set to 128
  145. * @arg IWDG_Prescaler_256: IWDG prescaler set to 256
  146. * @retval None
  147. */
  148. void IWDG_SetPrescaler(uint8_t IWDG_Prescaler)
  149. {
  150. /* Check the parameters */
  151. assert_param(IS_IWDG_PRESCALER(IWDG_Prescaler));
  152. IWDG->PR = IWDG_Prescaler;
  153. }
  154. /**
  155. * @brief Sets IWDG Reload value.
  156. * @param Reload: specifies the IWDG Reload value.
  157. * This parameter must be a number between 0 and 0x0FFF.
  158. * @retval None
  159. */
  160. void IWDG_SetReload(uint16_t Reload)
  161. {
  162. /* Check the parameters */
  163. assert_param(IS_IWDG_RELOAD(Reload));
  164. IWDG->RLR = Reload;
  165. }
  166. /**
  167. * @brief Reloads IWDG counter with value defined in the reload register
  168. * (write access to IWDG_PR and IWDG_RLR registers disabled).
  169. * @param None
  170. * @retval None
  171. */
  172. void IWDG_ReloadCounter(void)
  173. {
  174. IWDG->KR = KR_KEY_RELOAD;
  175. }
  176. /**
  177. * @brief Sets the IWDG window value.
  178. * @param WindowValue: specifies the window value to be compared to the downcounter.
  179. * @retval None
  180. */
  181. void IWDG_SetWindowValue(uint16_t WindowValue)
  182. {
  183. /* Check the parameters */
  184. assert_param(IS_IWDG_WINDOW_VALUE(WindowValue));
  185. IWDG->WINR = WindowValue;
  186. }
  187. /**
  188. * @}
  189. */
  190. /** @defgroup IWDG_Group2 IWDG activation function
  191. * @brief IWDG activation function
  192. *
  193. @verbatim
  194. ==============================================================================
  195. ##### IWDG activation function #####
  196. ==============================================================================
  197. @endverbatim
  198. * @{
  199. */
  200. /**
  201. * @brief Enables IWDG (write access to IWDG_PR and IWDG_RLR registers disabled).
  202. * @param None
  203. * @retval None
  204. */
  205. void IWDG_Enable(void)
  206. {
  207. IWDG->KR = KR_KEY_ENABLE;
  208. }
  209. /**
  210. * @}
  211. */
  212. /** @defgroup IWDG_Group3 Flag management function
  213. * @brief Flag management function
  214. *
  215. @verbatim
  216. ===============================================================================
  217. ##### Flag management function #####
  218. ===============================================================================
  219. @endverbatim
  220. * @{
  221. */
  222. /**
  223. * @brief Checks whether the specified IWDG flag is set or not.
  224. * @param IWDG_FLAG: specifies the flag to check.
  225. * This parameter can be one of the following values:
  226. * @arg IWDG_FLAG_PVU: Prescaler Value Update on going
  227. * @arg IWDG_FLAG_RVU: Reload Value Update on going
  228. * @arg IWDG_FLAG_WVU: Counter Window Value Update on going
  229. * @retval The new state of IWDG_FLAG (SET or RESET).
  230. */
  231. FlagStatus IWDG_GetFlagStatus(uint16_t IWDG_FLAG)
  232. {
  233. FlagStatus bitstatus = RESET;
  234. /* Check the parameters */
  235. assert_param(IS_IWDG_FLAG(IWDG_FLAG));
  236. if ((IWDG->SR & IWDG_FLAG) != (uint32_t)RESET)
  237. {
  238. bitstatus = SET;
  239. }
  240. else
  241. {
  242. bitstatus = RESET;
  243. }
  244. /* Return the flag status */
  245. return bitstatus;
  246. }
  247. /**
  248. * @}
  249. */
  250. /**
  251. * @}
  252. */
  253. /**
  254. * @}
  255. */
  256. /**
  257. * @}
  258. */
  259. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/