at32f413_rtc.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /**
  2. **************************************************************************
  3. * @file at32f413_rtc.c
  4. * @brief contains all the functions for the rtc firmware library
  5. **************************************************************************
  6. * Copyright notice & Disclaimer
  7. *
  8. * The software Board Support Package (BSP) that is made available to
  9. * download from Artery official website is the copyrighted work of Artery.
  10. * Artery authorizes customers to use, copy, and distribute the BSP
  11. * software and its related documentation for the purpose of design and
  12. * development in conjunction with Artery microcontrollers. Use of the
  13. * software is governed by this copyright notice and the following disclaimer.
  14. *
  15. * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  16. * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  17. * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  18. * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  19. * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  21. *
  22. **************************************************************************
  23. */
  24. #include "at32f413_conf.h"
  25. /** @addtogroup AT32F413_periph_driver
  26. * @{
  27. */
  28. /** @defgroup RTC
  29. * @brief RTC driver modules
  30. * @{
  31. */
  32. #ifdef RTC_MODULE_ENABLED
  33. /** @defgroup RTC_private_functions
  34. * @{
  35. */
  36. /**
  37. * @brief rtc counter set
  38. * @param counter_value (0x0000_0000 ~ 0xFFFF_FFFF)
  39. * @retval none
  40. */
  41. void rtc_counter_set(uint32_t counter_value)
  42. {
  43. /* enter rtc config mode */
  44. RTC->ctrll = 0x003F;
  45. /* set rtc counter */
  46. RTC->cnth_bit.cnt = (uint16_t)(counter_value >> 16);
  47. RTC->cntl_bit.cnt = (uint16_t)(counter_value & 0x0000FFFF);
  48. /* exit rtc config mode */
  49. RTC->ctrll = 0x000F;
  50. }
  51. /**
  52. * @brief rtc counter get
  53. * @param none
  54. * @retval rtc counter
  55. */
  56. uint32_t rtc_counter_get(void)
  57. {
  58. uint32_t cnt = 0;
  59. cnt = RTC->cnth;
  60. cnt = (cnt << 16) | RTC->cntl;
  61. return cnt;
  62. }
  63. /**
  64. * @brief rtc divider set
  65. * @param div_value (0x0000_0000 ~ 0x000F_FFFF)
  66. * @retval none
  67. */
  68. void rtc_divider_set(uint32_t div_value)
  69. {
  70. /* enter rtc config mode */
  71. RTC->ctrll = 0x003F;
  72. /* set rtc divider */
  73. RTC->divh_bit.div = (uint16_t)(div_value >> 16);
  74. RTC->divl_bit.div = (uint16_t)(div_value & 0x0000FFFF);
  75. /* exit rtc config mode */
  76. RTC->ctrll = 0x000F;
  77. }
  78. /**
  79. * @brief rtc divider get
  80. * @param none
  81. * @retval rtc counter
  82. */
  83. uint32_t rtc_divider_get(void)
  84. {
  85. uint32_t div = 0;
  86. div = RTC->divcnth;
  87. div = (div << 16) | RTC->divcntl;
  88. return div;
  89. }
  90. /**
  91. * @brief rtc alarm value set
  92. * @param alarm_value (0x0000_0000 ~ 0xFFFF_FFFF)
  93. * @retval none
  94. */
  95. void rtc_alarm_set(uint32_t alarm_value)
  96. {
  97. /* enter rtc config mode */
  98. RTC->ctrll = 0x003F;
  99. /* set rtc alarm value */
  100. RTC->tah_bit.ta = (uint16_t)(alarm_value >> 16);
  101. RTC->tal_bit.ta = (uint16_t)(alarm_value & 0x0000FFFF);
  102. /* exit rtc config mode */
  103. RTC->ctrll = 0x000F;
  104. }
  105. /**
  106. * @brief rtc interrupt enable
  107. * @param source
  108. * this parameter can be any combination of the following values:
  109. * - RTC_TS_INT: time second interrupt.
  110. * - RTC_TA_INT: time alarm interrupt.
  111. * - RTC_OVF_INT: overflow interrupt.
  112. * @param new_state (TRUE or FALSE)
  113. * @retval none
  114. */
  115. void rtc_interrupt_enable(uint16_t source, confirm_state new_state)
  116. {
  117. if(new_state == FALSE)
  118. {
  119. RTC->ctrlh &= ~source;
  120. }
  121. else
  122. {
  123. RTC->ctrlh |= source;
  124. }
  125. }
  126. /**
  127. * @brief rtc flag get
  128. * @param flag
  129. * this parameter can be one of the following values:
  130. * - RTC_TS_FLAG: time second flag.
  131. * - RTC_TA_FLAG: time alarm flag.
  132. * - RTC_OVF_FLAG: overflow flag.
  133. * - RTC_UPDF_FLAG: rtc update finish flag.
  134. * - RTC_CFGF_FLAG: rtc configuration finish flag.
  135. * @retval state of rtc flag
  136. */
  137. flag_status rtc_flag_get(uint16_t flag)
  138. {
  139. flag_status status = RESET;
  140. if ((RTC->ctrll & flag) != (uint16_t)RESET)
  141. {
  142. status = SET;
  143. }
  144. else
  145. {
  146. status = RESET;
  147. }
  148. return status;
  149. }
  150. /**
  151. * @brief rtc flag clear
  152. * @param interrupt_flag
  153. * this parameter can be any combination of the following values:
  154. * - RTC_TS_FLAG: time second flag.
  155. * - RTC_TA_FLAG: time alarm flag.
  156. * - RTC_OVF_FLAG: overflow flag.
  157. * - RTC_UPDF_FLAG: rtc update finish flag.
  158. * @retval none
  159. */
  160. void rtc_flag_clear(uint16_t flag)
  161. {
  162. RTC->ctrll = ~(flag | 0x10) | (RTC->ctrll_bit.cfgen << 4);
  163. }
  164. /**
  165. * @brief rtc wait configuration finish
  166. * @param none
  167. * @retval none
  168. */
  169. void rtc_wait_config_finish(void)
  170. {
  171. while (RTC->ctrll_bit.cfgf == 0);
  172. }
  173. /**
  174. * @brief rtc wait update finish
  175. * @param none
  176. * @retval none
  177. */
  178. void rtc_wait_update_finish(void)
  179. {
  180. while (RTC->ctrll_bit.updf == 0);
  181. }
  182. /**
  183. * @}
  184. */
  185. #endif
  186. /**
  187. * @}
  188. */
  189. /**
  190. * @}
  191. */