ft32f0xx_wwdg.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /**
  2. ******************************************************************************
  3. * @file ft32f0xx_wwdg.c
  4. * @author FMD AE
  5. * @brief This file provides firmware functions to manage the following
  6. * functionalities of the Window watchdog (WWDG) peripheral:
  7. * + Prescaler, Refresh window and Counter configuration
  8. * + WWDG activation
  9. * + Interrupts and flags management
  10. * @version V1.0.0
  11. * @data 2021-07-01
  12. ******************************************************************************
  13. */
  14. /* Includes ------------------------------------------------------------------*/
  15. #include "ft32f0xx_wwdg.h"
  16. #include "ft32f0xx_rcc.h"
  17. /* --------------------- WWDG registers bit mask ---------------------------- */
  18. /* CFR register bit mask */
  19. #define CFR_WDGTB_MASK ((uint32_t)0xFFFFFE7F)
  20. #define CFR_W_MASK ((uint32_t)0xFFFFFF80)
  21. #define BIT_MASK ((uint8_t)0x7F)
  22. /**
  23. * @brief Deinitializes the WWDG peripheral registers to their default reset values.
  24. * @param None
  25. * @retval None
  26. */
  27. void WWDG_DeInit(void)
  28. {
  29. RCC_APB1PeriphResetCmd(RCC_APB1Periph_WWDG, ENABLE);
  30. RCC_APB1PeriphResetCmd(RCC_APB1Periph_WWDG, DISABLE);
  31. }
  32. /**
  33. * @brief Sets the WWDG Prescaler.
  34. * @param WWDG_Prescaler: specifies the WWDG Prescaler.
  35. * This parameter can be one of the following values:
  36. * @arg WWDG_Prescaler_1: WWDG counter clock = (PCLK1/4096)/1
  37. * @arg WWDG_Prescaler_2: WWDG counter clock = (PCLK1/4096)/2
  38. * @arg WWDG_Prescaler_4: WWDG counter clock = (PCLK1/4096)/4
  39. * @arg WWDG_Prescaler_8: WWDG counter clock = (PCLK1/4096)/8
  40. * @retval None
  41. */
  42. void WWDG_SetPrescaler(uint32_t WWDG_Prescaler)
  43. {
  44. uint32_t tmpreg = 0;
  45. /* Check the parameters */
  46. assert_param(IS_WWDG_PRESCALER(WWDG_Prescaler));
  47. /* Clear WDGTB[1:0] bits */
  48. tmpreg = WWDG->CFR & CFR_WDGTB_MASK;
  49. /* Set WDGTB[1:0] bits according to WWDG_Prescaler value */
  50. tmpreg |= WWDG_Prescaler;
  51. /* Store the new value */
  52. WWDG->CFR = tmpreg;
  53. }
  54. /**
  55. * @brief Sets the WWDG window value.
  56. * @param WindowValue: specifies the window value to be compared to the downcounter.
  57. * This parameter value must be lower than 0x80.
  58. * @retval None
  59. */
  60. void WWDG_SetWindowValue(uint8_t WindowValue)
  61. {
  62. __IO uint32_t tmpreg = 0;
  63. /* Check the parameters */
  64. assert_param(IS_WWDG_WINDOW_VALUE(WindowValue));
  65. /* Clear W[6:0] bits */
  66. tmpreg = WWDG->CFR & CFR_W_MASK;
  67. /* Set W[6:0] bits according to WindowValue value */
  68. tmpreg |= WindowValue & (uint32_t) BIT_MASK;
  69. /* Store the new value */
  70. WWDG->CFR = tmpreg;
  71. }
  72. /**
  73. * @brief Enables the WWDG Early Wakeup interrupt(EWI).
  74. * @note Once enabled this interrupt cannot be disabled except by a system reset.
  75. * @param None
  76. * @retval None
  77. */
  78. void WWDG_EnableIT(void)
  79. {
  80. WWDG->CFR |= WWDG_CFR_EWI;
  81. }
  82. /**
  83. * @brief Sets the WWDG counter value.
  84. * @param Counter: specifies the watchdog counter value.
  85. * This parameter must be a number between 0x40 and 0x7F (to prevent
  86. * generating an immediate reset).
  87. * @retval None
  88. */
  89. void WWDG_SetCounter(uint8_t Counter)
  90. {
  91. /* Check the parameters */
  92. assert_param(IS_WWDG_COUNTER(Counter));
  93. /* Write to T[6:0] bits to configure the counter value, no need to do
  94. a read-modify-write; writing a 0 to WDGA bit does nothing */
  95. WWDG->CR = Counter & BIT_MASK;
  96. }
  97. /**
  98. * @}
  99. */
  100. /**
  101. * @brief Enables WWDG and load the counter value.
  102. * @param Counter: specifies the watchdog counter value.
  103. * This parameter must be a number between 0x40 and 0x7F (to prevent
  104. * generating an immediate reset).
  105. * @retval None
  106. */
  107. void WWDG_Enable(uint8_t Counter)
  108. {
  109. /* Check the parameters */
  110. assert_param(IS_WWDG_COUNTER(Counter));
  111. WWDG->CR = WWDG_CR_WDGA | Counter;
  112. }
  113. /**
  114. * @}
  115. */
  116. /**
  117. * @brief Checks whether the Early Wakeup interrupt flag is set or not.
  118. * @param None
  119. * @retval The new state of the Early Wakeup interrupt flag (SET or RESET).
  120. */
  121. FlagStatus WWDG_GetFlagStatus(void)
  122. {
  123. FlagStatus bitstatus = RESET;
  124. if ((WWDG->SR) != (uint32_t)RESET)
  125. {
  126. bitstatus = SET;
  127. }
  128. else
  129. {
  130. bitstatus = RESET;
  131. }
  132. return bitstatus;
  133. }
  134. /**
  135. * @brief Clears Early Wakeup interrupt flag.
  136. * @param None
  137. * @retval None
  138. */
  139. void WWDG_ClearFlag(void)
  140. {
  141. WWDG->SR = (uint32_t)RESET;
  142. }
  143. /**
  144. * @}
  145. */
  146. /**
  147. * @}
  148. */
  149. /**
  150. * @}
  151. */
  152. /**
  153. * @}
  154. */
  155. /************************ (C) COPYRIGHT FMD *****END OF FILE****/