gd32f30x_rtc.c 6.2 KB

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