gd32f30x_rtc.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*!
  2. \file gd32f30x_rtc.c
  3. \brief RTC driver
  4. \version 2023-12-30, V2.2.0, firmware for GD32F30x
  5. */
  6. /*
  7. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  8. Redistribution and use in source and binary forms, with or without modification,
  9. are permitted provided that the following conditions are met:
  10. 1. Redistributions of source code must retain the above copyright notice, this
  11. list of conditions and the following disclaimer.
  12. 2. Redistributions in binary form must reproduce the above copyright notice,
  13. this list of conditions and the following disclaimer in the documentation
  14. and/or other materials provided with the distribution.
  15. 3. Neither the name of the copyright holder nor the names of its contributors
  16. may be used to endorse or promote products derived from this software without
  17. specific prior written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  22. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  27. OF SUCH DAMAGE.
  28. */
  29. #include "gd32f30x_rtc.h"
  30. /*!
  31. \brief enable RTC interrupt
  32. \param[in] interrupt: specify which interrupt to enbale
  33. \arg RTC_INT_SECOND: second interrupt
  34. \arg RTC_INT_ALARM: alarm interrupt
  35. \arg RTC_INT_OVERFLOW: overflow interrupt
  36. \param[out] none
  37. \retval none
  38. */
  39. void rtc_interrupt_enable(uint32_t interrupt)
  40. {
  41. RTC_INTEN |= interrupt;
  42. }
  43. /*!
  44. \brief disable RTC interrupt
  45. \param[in] interrupt: specify which interrupt to disbale
  46. \arg RTC_INT_SECOND: second interrupt
  47. \arg RTC_INT_ALARM: alarm interrupt
  48. \arg RTC_INT_OVERFLOW: overflow interrupt
  49. \param[out] none
  50. \retval none
  51. */
  52. void rtc_interrupt_disable(uint32_t interrupt)
  53. {
  54. RTC_INTEN &= ~interrupt;
  55. }
  56. /*!
  57. \brief enter RTC configuration mode
  58. \param[in] none
  59. \param[out] none
  60. \retval none
  61. */
  62. void rtc_configuration_mode_enter(void)
  63. {
  64. RTC_CTL |= RTC_CTL_CMF;
  65. }
  66. /*!
  67. \brief exit RTC configuration mode
  68. \param[in] none
  69. \param[out] none
  70. \retval none
  71. */
  72. void rtc_configuration_mode_exit(void)
  73. {
  74. RTC_CTL &= ~RTC_CTL_CMF;
  75. }
  76. /*!
  77. \brief wait RTC last write operation finished flag set
  78. \param[in] none
  79. \param[out] none
  80. \retval none
  81. */
  82. void rtc_lwoff_wait(void)
  83. {
  84. /* loop until LWOFF flag is set */
  85. while (RESET == (RTC_CTL & RTC_CTL_LWOFF)){
  86. }
  87. }
  88. /*!
  89. \brief wait RTC registers synchronized flag set
  90. \param[in] none
  91. \param[out] none
  92. \retval none
  93. */
  94. void rtc_register_sync_wait(void)
  95. {
  96. /* clear RSYNF flag */
  97. RTC_CTL &= ~RTC_CTL_RSYNF;
  98. /* loop until RSYNF flag is set */
  99. while (RESET == (RTC_CTL & RTC_CTL_RSYNF)){
  100. }
  101. }
  102. /*!
  103. \brief get RTC counter value
  104. \param[in] none
  105. \param[out] none
  106. \retval RTC counter value
  107. */
  108. uint32_t rtc_counter_get(void)
  109. {
  110. uint32_t temp = 0x0U;
  111. temp = RTC_CNTL;
  112. temp |= (RTC_CNTH << 16);
  113. return temp;
  114. }
  115. /*!
  116. \brief set RTC counter value
  117. \param[in] cnt: RTC counter value
  118. \param[out] none
  119. \retval none
  120. */
  121. void rtc_counter_set(uint32_t cnt)
  122. {
  123. rtc_configuration_mode_enter();
  124. /* set the RTC counter high bits */
  125. RTC_CNTH = cnt >> 16;
  126. /* set the RTC counter low bits */
  127. RTC_CNTL = (cnt & RTC_LOW_VALUE);
  128. rtc_configuration_mode_exit();
  129. }
  130. /*!
  131. \brief set RTC prescaler value
  132. \param[in] psc: RTC prescaler value
  133. \param[out] none
  134. \retval none
  135. */
  136. void rtc_prescaler_set(uint32_t psc)
  137. {
  138. rtc_configuration_mode_enter();
  139. /* set the RTC prescaler high bits */
  140. RTC_PSCH = (psc & RTC_HIGH_VALUE) >> 16;
  141. /* set the RTC prescaler low bits */
  142. RTC_PSCL = (psc & RTC_LOW_VALUE);
  143. rtc_configuration_mode_exit();
  144. }
  145. /*!
  146. \brief set RTC alarm value
  147. \param[in] alarm: RTC alarm value
  148. \param[out] none
  149. \retval none
  150. */
  151. void rtc_alarm_config(uint32_t alarm)
  152. {
  153. rtc_configuration_mode_enter();
  154. /* set the alarm high bits */
  155. RTC_ALRMH = alarm >> 16;
  156. /* set the alarm low bits */
  157. RTC_ALRML = (alarm & RTC_LOW_VALUE);
  158. rtc_configuration_mode_exit();
  159. }
  160. /*!
  161. \brief get RTC divider value
  162. \param[in] none
  163. \param[out] none
  164. \retval RTC divider value
  165. */
  166. uint32_t rtc_divider_get(void)
  167. {
  168. uint32_t temp = 0x00U;
  169. temp = (RTC_DIVH & RTC_DIVH_DIV) << 16;
  170. temp |= RTC_DIVL;
  171. return temp;
  172. }
  173. /*!
  174. \brief get RTC flag status
  175. \param[in] flag: specify which flag status to get
  176. \arg RTC_FLAG_SECOND: second interrupt flag
  177. \arg RTC_FLAG_ALARM: alarm interrupt flag
  178. \arg RTC_FLAG_OVERFLOW: overflow interrupt flag
  179. \arg RTC_FLAG_RSYN: registers synchronized flag
  180. \arg RTC_FLAG_LWOF: last write operation finished flag
  181. \param[out] none
  182. \retval SET or RESET
  183. */
  184. FlagStatus rtc_flag_get(uint32_t flag)
  185. {
  186. if(RESET != (RTC_CTL & flag)){
  187. return SET;
  188. }else{
  189. return RESET;
  190. }
  191. }
  192. /*!
  193. \brief clear RTC flag status
  194. \param[in] flag: specify which flag status to clear
  195. \arg RTC_FLAG_SECOND: second interrupt flag
  196. \arg RTC_FLAG_ALARM: alarm interrupt flag
  197. \arg RTC_FLAG_OVERFLOW: overflow interrupt flag
  198. \arg RTC_FLAG_RSYN: registers synchronized flag
  199. \param[out] none
  200. \retval none
  201. */
  202. void rtc_flag_clear(uint32_t flag)
  203. {
  204. /* clear RTC flag */
  205. RTC_CTL &= ~flag;
  206. }