driver_rtc.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * @File name : driver_rtc.c
  3. * @Author : Bluetrum IOT Team
  4. * @Date : 2023-02-15
  5. * @Description : This file provides functions to manage the most functionalities
  6. * of the RTC Base peripheral.
  7. *
  8. * Copyright (c) by Bluetrum, All Rights Reserved.
  9. */
  10. #include "driver_rtc.h"
  11. /**
  12. * @brief Set the RTC counter register value.
  13. * @param cnt: specifies the counter register new value.
  14. * @retval None
  15. */
  16. AT(.com_periph.rtc.cnt_set)
  17. void rtc_set_counter(uint32_t cnt)
  18. {
  19. RTCCNT = (uint32_t)cnt;
  20. }
  21. /**
  22. * @brief Get the RTC counter register value.
  23. * @param None
  24. * @retval specifies the counter register value.
  25. */
  26. AT(.com_periph.rtc.cnt_get)
  27. uint32_t rtc_get_counter(void)
  28. {
  29. return (uint32_t)RTCCNT;
  30. }
  31. /**
  32. * @brief Set the RTC alarm register value.
  33. * @param alm: specifies the alarm register new value. RTCALM = (real time) - 1.
  34. * @retval None
  35. */
  36. AT(.com_periph.rtc.alarm_set)
  37. void rtc_set_alarm(uint32_t alarm)
  38. {
  39. RTCALM = (uint32_t)alarm;
  40. }
  41. /**
  42. * @brief Get the RTC alarm register value.
  43. * @param None
  44. * @retval specifies the alarm register value. (real time) = RTCALM + 1.
  45. */
  46. AT(.com_periph.rtc.alarm_get)
  47. uint32_t rtc_get_alarm(void)
  48. {
  49. return (uint32_t)RTCALM;
  50. }
  51. /**
  52. * @brief Config predivision factor for 1Hz counter.
  53. * @param psc: Predivision factor configuration function of 1hz second counter.
  54. * @retval None
  55. */
  56. void rtc_prescale_set(uint32_t psc)
  57. {
  58. u32 reg_1s_period_mask = 0x1ffff;
  59. u32 rtccon2 = RTCCON2;
  60. rtccon2 &= ~reg_1s_period_mask;
  61. rtccon2 |= psc & reg_1s_period_mask;;
  62. RTCCON2 = rtccon2;
  63. }
  64. /**
  65. * @brief Get the RTC counter 1Hz prescale value.
  66. * @retval the prescale value.
  67. */
  68. uint32_t rtc_prescale_get(void)
  69. {
  70. return RTCCON2 & 0x1ffff;
  71. }
  72. /**
  73. * @brief Enable or disable the RTC interrupt.
  74. * @param isr: Function to be executed for service interruption.
  75. * @param pr: Priority of service interruption.
  76. * @param interrupt_type: specifies the RTC interrupt sources to be enable or disable.
  77. * this parameter can be one of the following values:
  78. * @arg RTC_IT_ALM: RTC alarm interrupt enable bit.
  79. * @arg RTC_IT_1S: RTC 1S interrupt enable bit.
  80. * @param state: the state of the RTC peripheral.
  81. * this parameter can be: ENABLE or DISABLE.
  82. * @retval None
  83. */
  84. void rtc_pic_config(isr_t isr, int pr, RTC_IT_TYPEDEF interrupt_type, FUNCTIONAL_STATE state)
  85. {
  86. uint32_t interrupt_bit = 0;
  87. if (interrupt_type == 0) {
  88. return;
  89. }
  90. if (interrupt_type & RTC_IT_ALM) {
  91. interrupt_bit |= RTCCON_ALMIE;
  92. }
  93. if (interrupt_type & RTC_IT_1S) {
  94. interrupt_bit |= RTCCON_RTC1SIE;
  95. }
  96. if (state != DISABLE) {
  97. RTCCON |= interrupt_bit;
  98. sys_irq_init(IRQn_RTC_SECOND_ALARM_LVD_WDT, pr, isr);
  99. if (interrupt_type & RTC_IT_1S){
  100. RTCCON11 |= RTCCON11_RTCWKSLPEN;
  101. }
  102. } else {
  103. RTCCON &= ~interrupt_bit;
  104. if (interrupt_type & RTC_IT_1S){
  105. RTCCON11 &= ~RTCCON11_RTCWKSLPEN;
  106. }
  107. }
  108. }
  109. /**
  110. * @brief Get the RTC interrupt pending.
  111. * @param interrupt_type: specifies the flag to get.
  112. * this parameter can be one of the following values:
  113. * @arg RTC_IT_1S: RTC 1S interrupt pending bit.
  114. * @arg RTC_IT_ALM: RTC alarm interrupt pending bit.
  115. * @retval The state of rtc_flag (SET or RESET).
  116. */
  117. AT(.com_periph.rct.get)
  118. FLAG_STATE rtc_get_flag(RTC_IT_TYPEDEF interrupt_type)
  119. {
  120. uint32_t interrupt_bit = 0;
  121. if (interrupt_type & RTC_IT_ALM) {
  122. interrupt_bit |= RTCCON_ALMPND;
  123. }
  124. if (interrupt_type & RTC_IT_1S) {
  125. interrupt_bit |= RTCCON_RTCWKSLPPND;
  126. }
  127. if ((RTCCON & interrupt_bit) != RESET) {
  128. return SET;
  129. } else {
  130. return RESET;
  131. }
  132. }
  133. /**
  134. * @brief Clear the RTC interrupt pending.
  135. * @param rtc_flag: specifies the flag to clear.
  136. * this parameter can be one of the following values:
  137. * @arg RTC_FLAG_1S: RTC 1S interrupt pending bit.
  138. * @arg RTC_FLAG_ALM: RTC alarm interrupt pending bit.
  139. * @retval None
  140. */
  141. AT(.com_periph.rct.clear)
  142. void rtc_clear_flag(RTC_IT_TYPEDEF interrupt_type)
  143. {
  144. uint32_t interrupt_bit = 0;
  145. if (interrupt_type & RTC_IT_ALM) {
  146. interrupt_bit |= RTCCON_ALMPND;
  147. }
  148. if (interrupt_type & RTC_IT_1S) {
  149. interrupt_bit |= RTCCON_RTCWKSLPPND;
  150. }
  151. RTCCPND |= (uint32_t)interrupt_bit;
  152. }
  153. /**
  154. * @brief Get the RTC count overflow flag.
  155. * @param interrupt_type: specifies the flag to get.
  156. * this parameter can be one of the following values:
  157. * @arg RTC_IT_ALM: RTC alarm interrupt pending bit.
  158. * @retval The state of rtc_flag (SET or RESET).
  159. */
  160. AT(.com_periph.rct.get)
  161. FLAG_STATE rtc_get_overflow_flag(RTC_IT_TYPEDEF interrupt_type)
  162. {
  163. if (interrupt_type & RTC_IT_ALM) {
  164. if (RTCCNT >= RTCALM) {
  165. return SET;
  166. }
  167. }
  168. return RESET;
  169. }