gd32f30x_rtc.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*!
  2. \file gd32f30x_rtc.c
  3. \brief RTC driver
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-02-10, V1.0.1, firmware for GD32F30x
  8. */
  9. #include "gd32f30x_rtc.h"
  10. /*!
  11. \brief enable RTC interrupt
  12. \param[in] interrupt: specify which interrupt to enbale
  13. \arg RTC_INT_SECOND: second interrupt
  14. \arg RTC_INT_ALARM: alarm interrupt
  15. \arg RTC_INT_OVERFLOW: overflow interrupt
  16. \param[out] none
  17. \retval none
  18. */
  19. void rtc_interrupt_enable(uint32_t interrupt)
  20. {
  21. RTC_INTEN |= interrupt;
  22. }
  23. /*!
  24. \brief disable RTC interrupt
  25. \param[in] interrupt: specify which interrupt to disbale
  26. \arg RTC_INT_SECOND: second interrupt
  27. \arg RTC_INT_ALARM: alarm interrupt
  28. \arg RTC_INT_OVERFLOW: overflow interrupt
  29. \param[out] none
  30. \retval none
  31. */
  32. void rtc_interrupt_disable(uint32_t interrupt)
  33. {
  34. RTC_INTEN &= ~interrupt;
  35. }
  36. /*!
  37. \brief enter RTC configuration mode
  38. \param[in] none
  39. \param[out] none
  40. \retval none
  41. */
  42. void rtc_configuration_mode_enter(void)
  43. {
  44. RTC_CTL |= RTC_CTL_CMF;
  45. }
  46. /*!
  47. \brief exit RTC configuration mode
  48. \param[in] none
  49. \param[out] none
  50. \retval none
  51. */
  52. void rtc_configuration_mode_exit(void)
  53. {
  54. RTC_CTL &= ~RTC_CTL_CMF;
  55. }
  56. /*!
  57. \brief wait RTC last write operation finished flag set
  58. \param[in] none
  59. \param[out] none
  60. \retval none
  61. */
  62. void rtc_lwoff_wait(void)
  63. {
  64. /* loop until LWOFF flag is set */
  65. while (RESET == (RTC_CTL & RTC_CTL_LWOFF)){
  66. }
  67. }
  68. /*!
  69. \brief wait RTC registers synchronized flag set
  70. \param[in] none
  71. \param[out] none
  72. \retval none
  73. */
  74. void rtc_register_sync_wait(void)
  75. {
  76. /* clear RSYNF flag */
  77. RTC_CTL &= ~RTC_CTL_RSYNF;
  78. /* loop until RSYNF flag is set */
  79. while (RESET == (RTC_CTL & RTC_CTL_RSYNF)){
  80. }
  81. }
  82. /*!
  83. \brief get RTC counter value
  84. \param[in] none
  85. \param[out] none
  86. \retval RTC counter value
  87. */
  88. uint32_t rtc_counter_get(void)
  89. {
  90. uint32_t temp = 0x0U;
  91. temp = RTC_CNTL;
  92. temp |= (RTC_CNTH << 16);
  93. return temp;
  94. }
  95. /*!
  96. \brief set RTC counter value
  97. \param[in] cnt: RTC counter value
  98. \param[out] none
  99. \retval none
  100. */
  101. void rtc_counter_set(uint32_t cnt)
  102. {
  103. rtc_configuration_mode_enter();
  104. /* set the RTC counter high bits */
  105. RTC_CNTH = cnt >> 16;
  106. /* set the RTC counter low bits */
  107. RTC_CNTL = (cnt & RTC_LOW_VALUE);
  108. rtc_configuration_mode_exit();
  109. }
  110. /*!
  111. \brief set RTC prescaler value
  112. \param[in] psc: RTC prescaler value
  113. \param[out] none
  114. \retval none
  115. */
  116. void rtc_prescaler_set(uint32_t psc)
  117. {
  118. rtc_configuration_mode_enter();
  119. /* set the RTC prescaler high bits */
  120. RTC_PSCH = (psc & RTC_HIGH_VALUE) >> 16;
  121. /* set the RTC prescaler low bits */
  122. RTC_PSCL = (psc & RTC_LOW_VALUE);
  123. rtc_configuration_mode_exit();
  124. }
  125. /*!
  126. \brief set RTC alarm value
  127. \param[in] alarm: RTC alarm value
  128. \param[out] none
  129. \retval none
  130. */
  131. void rtc_alarm_config(uint32_t alarm)
  132. {
  133. rtc_configuration_mode_enter();
  134. /* set the alarm high bits */
  135. RTC_ALRMH = alarm >> 16;
  136. /* set the alarm low bits */
  137. RTC_ALRML = (alarm & RTC_LOW_VALUE);
  138. rtc_configuration_mode_exit();
  139. }
  140. /*!
  141. \brief get RTC divider value
  142. \param[in] none
  143. \param[out] none
  144. \retval RTC divider value
  145. */
  146. uint32_t rtc_divider_get(void)
  147. {
  148. uint32_t temp = 0x00U;
  149. temp = (RTC_DIVH & RTC_DIVH_DIV) << 16;
  150. temp |= RTC_DIVL;
  151. return temp;
  152. }
  153. /*!
  154. \brief get RTC flag status
  155. \param[in] flag: specify which flag status to get
  156. \arg RTC_FLAG_SECOND: second interrupt flag
  157. \arg RTC_FLAG_ALARM: alarm interrupt flag
  158. \arg RTC_FLAG_OVERFLOW: overflow interrupt flag
  159. \arg RTC_FLAG_RSYN: registers synchronized flag
  160. \arg RTC_FLAG_LWOF: last write operation finished flag
  161. \param[out] none
  162. \retval SET or RESET
  163. */
  164. FlagStatus rtc_flag_get(uint32_t flag)
  165. {
  166. if(RESET != (RTC_CTL & flag)){
  167. return SET;
  168. }else{
  169. return RESET;
  170. }
  171. }
  172. /*!
  173. \brief clear RTC flag status
  174. \param[in] flag: specify which flag status to clear
  175. \arg RTC_FLAG_SECOND: second interrupt flag
  176. \arg RTC_FLAG_ALARM: alarm interrupt flag
  177. \arg RTC_FLAG_OVERFLOW: overflow interrupt flag
  178. \arg RTC_FLAG_RSYN: registers synchronized flag
  179. \param[out] none
  180. \retval none
  181. */
  182. void rtc_flag_clear(uint32_t flag)
  183. {
  184. /* clear RTC flag */
  185. RTC_CTL &= ~flag;
  186. }