gd32f10x_wwdg.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. ******************************************************************************
  3. * @brief WWDG functions of the firmware library.
  4. ******************************************************************************
  5. */
  6. /* Includes ------------------------------------------------------------------*/
  7. #include "gd32f10x_wwdg.h"
  8. #include "gd32f10x_rcc.h"
  9. /** @addtogroup GD32F10x_Firmware
  10. * @{
  11. */
  12. /** @defgroup WWDG
  13. * @brief WWDG driver modules
  14. * @{
  15. */
  16. /** @defgroup WWDG_Private_Variables
  17. * @{
  18. */
  19. /* CFR register bit mask */
  20. #define CFR_PS_MASK ((uint32_t)0xFFFFFE7F)
  21. #define CFR_WIN_MASK ((uint32_t)0xFFFFFF80)
  22. #define BIT_MASK ((uint8_t)0x7F)
  23. /**
  24. * @}
  25. */
  26. /** @defgroup WWDG_Private_Functions
  27. * @{
  28. */
  29. /**
  30. * @brief Reset the WWDG configuration.
  31. * @param None
  32. * @retval None
  33. */
  34. void WWDG_DeInit(void)
  35. {
  36. RCC_APB1PeriphReset_Enable(RCC_APB1PERIPH_WWDG, ENABLE);
  37. RCC_APB1PeriphReset_Enable(RCC_APB1PERIPH_WWDG, DISABLE);
  38. }
  39. /**
  40. * @brief Set WWDG prescaler value.
  41. * @param PrescalerValue: WWDG Prescaler value.
  42. * This parameter can be one of the following values:
  43. * @arg WWDG_PRESCALER_1: the time base of watchdog counter = (PCLK1/4096)/1
  44. * @arg WWDG_PRESCALER_2: the time base of watchdog counter = (PCLK1/4096)/2
  45. * @arg WWDG_PRESCALER_4: the time base of watchdog counter = (PCLK1/4096)/4
  46. * @arg WWDG_PRESCALER_8: the time base of watchdog counter = (PCLK1/4096)/8
  47. * @retval None
  48. */
  49. void WWDG_SetPrescalerValue(uint32_t PrescalerValue)
  50. {
  51. uint32_t temp = 0;
  52. /* Clear PS[1:0] bits */
  53. temp = WWDG->CFR & CFR_PS_MASK;
  54. /* Set PS[1:0] bits */
  55. temp |= PrescalerValue;
  56. WWDG->CFR = temp;
  57. }
  58. /**
  59. * @brief Set watchdog window value.
  60. * @param WindowValue: the window value to be compared to the downcounter.
  61. * @retval None
  62. */
  63. void WWDG_SetWindowValue(uint8_t WindowValue)
  64. {
  65. __IO uint32_t temp = 0;
  66. /* Clear WIN[6:0] bits */
  67. temp = WWDG->CFR & CFR_WIN_MASK;
  68. /* Set WIN[6:0] bits */
  69. temp |= WindowValue & (uint32_t) BIT_MASK;
  70. WWDG->CFR = temp;
  71. }
  72. /**
  73. * @brief Enable the WWDG Early wakeup interrupt(EWI).
  74. * @note An interrupt occurs when the counter reaches 0x40 if the bit is set.
  75. * It's cleared by a hardware reset. A write of 0 has no effect.
  76. * @param None
  77. * @retval None
  78. */
  79. void WWDG_EnableInt(void)
  80. {
  81. WWDG->CFR |= WWDG_CFR_EWI;
  82. }
  83. /**
  84. * @brief Set the value of the watchdog counter.
  85. * @param CounterValue: the watchdog counter value.
  86. * @retval None
  87. */
  88. void WWDG_SetCounterValue(uint8_t CounterValue)
  89. {
  90. /* Write to CNT[6:0] bits */
  91. WWDG->CTLR = CounterValue & BIT_MASK;
  92. }
  93. /**
  94. * @brief Start the window watchdog counter.
  95. * @param CounterValue: The value of the watchdog counter.
  96. * @retval None
  97. */
  98. void WWDG_Enable(uint8_t CounterValue)
  99. {
  100. WWDG->CTLR = WWDG_CTLR_WDGEN | CounterValue;
  101. }
  102. /**
  103. * @brief Check the Early Wakeup interrupt bit state.
  104. * @param None
  105. * @retval The new state of the Early Wakeup interrupt (SET or RESET).
  106. */
  107. TypeState WWDG_GetBitState(void)
  108. {
  109. if ((WWDG->STR) != (uint32_t)RESET) {
  110. return SET;
  111. } else {
  112. return RESET;
  113. }
  114. }
  115. /**
  116. * @brief Clear Early Wakeup interrupt flag.
  117. * @param None
  118. * @retval None
  119. */
  120. void WWDG_ClearBitState(void)
  121. {
  122. WWDG->STR = (uint32_t)RESET;
  123. }
  124. /**
  125. * @}
  126. */
  127. /**
  128. * @}
  129. */
  130. /**
  131. * @}
  132. */