gd32f10x_rtc.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /**
  2. ******************************************************************************
  3. * @brief RTC functions of the firmware library.
  4. ******************************************************************************
  5. */
  6. /* Includes ------------------------------------------------------------------*/
  7. #include "gd32f10x_rtc.h"
  8. /** @addtogroup GD32F10x_Firmware
  9. * @{
  10. */
  11. /** @defgroup RTC
  12. * @brief RTC driver modules
  13. * @{
  14. */
  15. /** @defgroup RTC_Private_Defines
  16. * @{
  17. */
  18. /* RTC LSB Mask */
  19. #define RTC_LSB_MASK ((uint32_t)0x0000FFFF)
  20. /* RTC Prescaler MSB Mask */
  21. #define PRL1_MSB_MASK ((uint32_t)0x000F0000)
  22. /**
  23. * @}
  24. */
  25. /** @defgroup RTC_Private_Functions
  26. * @{
  27. */
  28. /**
  29. * @brief Enable or disable the specified RTC interrupts.
  30. * @param RTC_INT: specify the RTC interrupt sources
  31. * This parameter can be any combination of the following value:
  32. * @arg RTC_INT_OVI: Overflow event interrupt
  33. * @arg RTC_INT_AI: Alarm event interrupt
  34. * @arg RTC_INT_SI: Tamper event interrupt
  35. * @param NewValue: RTC interrupt state to configure
  36. * This parameter can be: ENABLE or DISABLE.
  37. * @retval None
  38. */
  39. void RTC_INT_Enable(uint16_t RTC_INT, TypeState NewValue)
  40. {
  41. if (NewValue == ENABLE) {
  42. /* Enable the interrupts in RTC_CTLR register */
  43. RTC->CTLR1 |= RTC_INT;
  44. } else {
  45. /* Disable the interrupts in RTC_CTLR register */
  46. RTC->CTLR1 &= ~((uint16_t)RTC_INT);
  47. }
  48. }
  49. /**
  50. * @brief Enter the RTC configuration mode.
  51. * @param None
  52. * @retval None
  53. */
  54. void RTC_EnterConfigMode(void)
  55. {
  56. /* Enter in the Configuration Mode by set the CMF flag */
  57. RTC->CTLR2 |= RTC_CTLR2_CMF;
  58. }
  59. /**
  60. * @brief Exit from the RTC configuration mode.
  61. * @param None
  62. * @retval None
  63. */
  64. void RTC_ExitConfigMode(void)
  65. {
  66. /* Exit from the Configuration Mode by reset the CMF flag */
  67. RTC->CTLR2 &= ~((uint16_t)RTC_CTLR2_CMF);
  68. }
  69. /**
  70. * @brief Get the RTC counter value.
  71. * @param None
  72. * @retval The value of RTC counter.
  73. */
  74. uint32_t RTC_GetCounter(void)
  75. {
  76. uint16_t temp = 0;
  77. temp = RTC->CNT2;
  78. return (((uint32_t)RTC->CNT1 << 16) | temp) ;
  79. }
  80. /**
  81. * @brief Set the RTC counter value.
  82. * @param CounterValue: New value of the RTC counter.
  83. * @retval None
  84. */
  85. void RTC_SetCounter(uint32_t CounterValue)
  86. {
  87. RTC_EnterConfigMode();
  88. /* Set the RTC counter MSB word */
  89. RTC->CNT1 = CounterValue >> 16;
  90. /* Set the RTC counter LSB word */
  91. RTC->CNT2 = (CounterValue & RTC_LSB_MASK);
  92. RTC_ExitConfigMode();
  93. }
  94. /**
  95. * @brief Set the RTC prescaler value.
  96. * @param PrescalerValue: New value of the RTC prescaler.
  97. * @retval None
  98. */
  99. void RTC_SetPrescaler(uint32_t PrescalerValue)
  100. {
  101. RTC_EnterConfigMode();
  102. /* Set the RTC prescaler MSB word */
  103. RTC->PLR1 = (PrescalerValue & PRL1_MSB_MASK) >> 16;
  104. /* Set the RTC prescaler LSB word */
  105. RTC->PLR2 = (PrescalerValue & RTC_LSB_MASK);
  106. RTC_ExitConfigMode();
  107. }
  108. /**
  109. * @brief Set the RTC alarm value.
  110. * @param AlarmValue: New value of the RTC alarm.
  111. * @retval None
  112. */
  113. void RTC_SetAlarm(uint32_t AlarmValue)
  114. {
  115. RTC_EnterConfigMode();
  116. /* Set the alarm MSB word */
  117. RTC->ALRMR1 = AlarmValue >> 16;
  118. /* Set the alarm LSB word */
  119. RTC->ALRMR2 = (AlarmValue & RTC_LSB_MASK);
  120. RTC_ExitConfigMode();
  121. }
  122. /**
  123. * @brief Get the RTC divider value.
  124. * @param None
  125. * @retval The value of RTC Divider.
  126. */
  127. uint32_t RTC_GetDivider(void)
  128. {
  129. uint32_t temp = 0x00;
  130. temp = ((uint32_t)RTC->PREDIV1 & (uint32_t)0x000F) << 16;
  131. temp |= RTC->PREDIV2;
  132. return temp;
  133. }
  134. /**
  135. * @brief Wait until last write operation on RTC registers has finished.
  136. * @note This function must be called before any write to RTC registers.
  137. * @param None
  138. * @retval None
  139. */
  140. void RTC_WaitLWOFF(void)
  141. {
  142. /* Loop until RTOFF flag is set */
  143. while ((RTC->CTLR2 & RTC_FLAG_LWOFF) == (uint16_t)RESET) {
  144. }
  145. }
  146. /**
  147. * @brief Wait until the RTC registers (RTC_CNT, RTC_ALR and RTC_PRL)
  148. * are synchronized with RTC APB clock.
  149. * @note This function must be called before any read operation after an APB reset
  150. * or an APB clock stop.
  151. * @param None
  152. * @retval None
  153. */
  154. void RTC_WaitRSF(void)
  155. {
  156. /* Clear RSF flag */
  157. RTC->CTLR2 &= ~((uint16_t)RTC_FLAG_RSF);
  158. /* Loop until RSF flag is set */
  159. while ((RTC->CTLR2 & RTC_FLAG_RSF) == (uint16_t)RESET) {
  160. }
  161. }
  162. /**
  163. * @brief Check whether the specified RTC flag is set or not.
  164. * @param RTC_FLAG: specifie the flag to check.
  165. * This parameter can be one the following values:
  166. * @arg RTC_FLAG_LWOFF: RTC Operation Off flag
  167. * @arg RTC_FLAG_RSF: Registers Synchronized flag
  168. * @arg RTC_FLAG_OVF: Overflow flag
  169. * @arg RTC_FLAG_AF: Alarm flag
  170. * @arg RTC_FLAG_SF: Second flag
  171. * @retval The new bitstate of RTC_FLAG (SET or RESET).
  172. */
  173. TypeState RTC_GetBitState(uint16_t RTC_FLAG)
  174. {
  175. if ((RTC->CTLR2 & RTC_FLAG) != (uint16_t)RESET) {
  176. return SET;
  177. } else {
  178. return RESET;
  179. }
  180. }
  181. /**
  182. * @brief Clear the RTC's pending flags.
  183. * @param RTC_FLAG: specifie the flag to clear.
  184. * This parameter can be any combination of the following values:
  185. * @arg RTC_FLAG_RSF: Registers Synchronized flag. This flag is cleared only after
  186. * an APB reset or an APB Clock stop.
  187. * @arg RTC_FLAG_OVF: Overflow flag
  188. * @arg RTC_FLAG_AF: Alarm flag
  189. * @arg RTC_FLAG_SF: Second flag
  190. * @retval None
  191. */
  192. void RTC_ClearBitState(uint16_t RTC_flag)
  193. {
  194. /* Clear the corresponding RTC flag */
  195. RTC->CTLR2 &= ~((uint16_t)RTC_flag);
  196. }
  197. /**
  198. * @brief Check whether the specified RTC interrupt has occurred or not.
  199. * @param RTC_INT: specifie the RTC interrupts sources to check.
  200. * This parameter can be one of the following values:
  201. * @arg RTC_INT_OVI: Overflow event interrupt
  202. * @arg RTC_INT_AI: Alarm event interrupt
  203. * @arg RTC_INT_SI: Tamper event interrupt
  204. * @retval The new state of the RTC_IT (SET or RESET).
  205. */
  206. TypeState RTC_GetIntBitState(uint16_t RTC_INT)
  207. {
  208. if (((RTC->CTLR1 & RTC_INT) != (uint16_t)RESET) && ((RTC->CTLR2 & RTC_INT) != (uint16_t)RESET)) {
  209. return SET;
  210. } else {
  211. return RESET;
  212. }
  213. }
  214. /**
  215. * @brief Clear the RTC's interrupt bit.
  216. * @param RTC_INT: specifie the interrupt bit to clear.
  217. * This parameter can be any combination of the following values:
  218. * @arg RTC_INT_OVI: Overflow event interrupt
  219. * @arg RTC_INT_AI: Alarm event interrupt
  220. * @arg RTC_INT_SI: Tamper event interrupt
  221. * @retval None
  222. */
  223. void RTC_ClearIntBitState(uint16_t RTC_INT)
  224. {
  225. /* Clear the RTC's interrupt bitstate */
  226. RTC->CTLR2 &= ~((uint16_t)RTC_INT);
  227. }
  228. /**
  229. * @}
  230. */
  231. /**
  232. * @}
  233. */
  234. /**
  235. * @}
  236. */