gd32vf103_rtc.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*!
  2. \file gd32vf103_rtc.c
  3. \brief RTC driver
  4. \version 2019-6-5, V1.0.0, firmware for GD32VF103
  5. */
  6. /*
  7. Copyright (c) 2019, 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 "gd32vf103_rtc.h"
  30. /* RTC register high / low bits mask */
  31. #define RTC_HIGH_BITS_MASK ((uint32_t)0x000F0000U) /* RTC high bits mask */
  32. #define RTC_LOW_BITS_MASK ((uint32_t)0x0000FFFFU) /* RTC low bits mask */
  33. /* RTC register high bits offset */
  34. #define RTC_HIGH_BITS_OFFSET ((uint32_t)16U)
  35. /*!
  36. \brief enter RTC configuration mode
  37. \param[in] none
  38. \param[out] none
  39. \retval none
  40. */
  41. void rtc_configuration_mode_enter(void)
  42. {
  43. RTC_CTL |= RTC_CTL_CMF;
  44. }
  45. /*!
  46. \brief exit RTC configuration mode
  47. \param[in] none
  48. \param[out] none
  49. \retval none
  50. */
  51. void rtc_configuration_mode_exit(void)
  52. {
  53. RTC_CTL &= ~RTC_CTL_CMF;
  54. }
  55. /*!
  56. \brief set RTC counter value
  57. \param[in] cnt: RTC counter value
  58. \param[out] none
  59. \retval none
  60. */
  61. void rtc_counter_set(uint32_t cnt)
  62. {
  63. rtc_configuration_mode_enter();
  64. /* set the RTC counter high bits */
  65. RTC_CNTH = (cnt >> RTC_HIGH_BITS_OFFSET);
  66. /* set the RTC counter low bits */
  67. RTC_CNTL = (cnt & RTC_LOW_BITS_MASK);
  68. rtc_configuration_mode_exit();
  69. }
  70. /*!
  71. \brief set RTC prescaler value
  72. \param[in] psc: RTC prescaler value
  73. \param[out] none
  74. \retval none
  75. */
  76. void rtc_prescaler_set(uint32_t psc)
  77. {
  78. rtc_configuration_mode_enter();
  79. /* set the RTC prescaler high bits */
  80. RTC_PSCH = ((psc & RTC_HIGH_BITS_MASK) >> RTC_HIGH_BITS_OFFSET);
  81. /* set the RTC prescaler low bits */
  82. RTC_PSCL = (psc & RTC_LOW_BITS_MASK);
  83. rtc_configuration_mode_exit();
  84. }
  85. /*!
  86. \brief wait RTC last write operation finished flag set
  87. \param[in] none
  88. \param[out] none
  89. \retval none
  90. */
  91. void rtc_lwoff_wait(void)
  92. {
  93. /* loop until LWOFF flag is set */
  94. while(RESET == (RTC_CTL & RTC_CTL_LWOFF)){
  95. }
  96. }
  97. /*!
  98. \brief wait RTC registers synchronized flag set
  99. \param[in] none
  100. \param[out] none
  101. \retval none
  102. */
  103. void rtc_register_sync_wait(void)
  104. {
  105. /* clear RSYNF flag */
  106. RTC_CTL &= ~RTC_CTL_RSYNF;
  107. /* loop until RSYNF flag is set */
  108. while(RESET == (RTC_CTL & RTC_CTL_RSYNF)){
  109. }
  110. }
  111. /*!
  112. \brief set RTC alarm value
  113. \param[in] alarm: RTC alarm value
  114. \param[out] none
  115. \retval none
  116. */
  117. void rtc_alarm_config(uint32_t alarm)
  118. {
  119. rtc_configuration_mode_enter();
  120. /* set the alarm high bits */
  121. RTC_ALRMH = (alarm >> RTC_HIGH_BITS_OFFSET);
  122. /* set the alarm low bits */
  123. RTC_ALRML = (alarm & RTC_LOW_BITS_MASK);
  124. rtc_configuration_mode_exit();
  125. }
  126. /*!
  127. \brief get RTC counter value
  128. \param[in] none
  129. \param[out] none
  130. \retval RTC counter value
  131. */
  132. uint32_t rtc_counter_get(void)
  133. {
  134. uint32_t temp = 0x0U;
  135. temp = RTC_CNTL;
  136. temp |= (RTC_CNTH << RTC_HIGH_BITS_OFFSET);
  137. return temp;
  138. }
  139. /*!
  140. \brief get RTC divider value
  141. \param[in] none
  142. \param[out] none
  143. \retval RTC divider value
  144. */
  145. uint32_t rtc_divider_get(void)
  146. {
  147. uint32_t temp = 0x00U;
  148. temp = ((RTC_DIVH & RTC_DIVH_DIV) << RTC_HIGH_BITS_OFFSET);
  149. temp |= RTC_DIVL;
  150. return temp;
  151. }
  152. /*!
  153. \brief get RTC flag status
  154. \param[in] flag: specify which flag status to get
  155. only one parameter can be selected which is shown as below:
  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. one or more parameters can be selected which are shown as below:
  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. \param[out] none
  181. \retval none
  182. */
  183. void rtc_flag_clear(uint32_t flag)
  184. {
  185. /* clear RTC flag */
  186. RTC_CTL &= ~flag;
  187. }
  188. /*!
  189. \brief get RTC interrupt flag status
  190. \param[in] flag: specify which flag status to get
  191. only one parameter can be selected which is shown as below:
  192. \arg RTC_INT_FLAG_SECOND: second interrupt flag
  193. \arg RTC_INT_FLAG_ALARM: alarm interrupt flag
  194. \arg RTC_INT_FLAG_OVERFLOW: overflow interrupt flag
  195. \param[out] none
  196. \retval SET or RESET
  197. */
  198. FlagStatus rtc_interrupt_flag_get(uint32_t flag)
  199. {
  200. if(RESET != (RTC_CTL & flag)){
  201. return SET;
  202. }else{
  203. return RESET;
  204. }
  205. }
  206. /*!
  207. \brief clear RTC interrupt flag status
  208. \param[in] flag: specify which flag status to clear
  209. one or more parameters can be selected which are shown as below:
  210. \arg RTC_INT_FLAG_SECOND: second interrupt flag
  211. \arg RTC_INT_FLAG_ALARM: alarm interrupt flag
  212. \arg RTC_INT_FLAG_OVERFLOW: overflow interrupt flag
  213. \param[out] none
  214. \retval none
  215. */
  216. void rtc_interrupt_flag_clear(uint32_t flag)
  217. {
  218. /* clear RTC interrupt flag */
  219. RTC_CTL &= ~flag;
  220. }
  221. /*!
  222. \brief enable RTC interrupt
  223. \param[in] interrupt: specify which interrupt to enbale
  224. one or more parameters can be selected which are shown as below:
  225. \arg RTC_INT_SECOND: second interrupt
  226. \arg RTC_INT_ALARM: alarm interrupt
  227. \arg RTC_INT_OVERFLOW: overflow interrupt
  228. \param[out] none
  229. \retval none
  230. */
  231. void rtc_interrupt_enable(uint32_t interrupt)
  232. {
  233. RTC_INTEN |= interrupt;
  234. }
  235. /*!
  236. \brief disable RTC interrupt
  237. \param[in] interrupt: specify which interrupt to disbale
  238. one or more parameters can be selected which are shown as below:
  239. \arg RTC_INT_SECOND: second interrupt
  240. \arg RTC_INT_ALARM: alarm interrupt
  241. \arg RTC_INT_OVERFLOW: overflow interrupt
  242. \param[out] none
  243. \retval none
  244. */
  245. void rtc_interrupt_disable(uint32_t interrupt)
  246. {
  247. RTC_INTEN &= ~interrupt;
  248. }