wm_rtc.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * @file wm_rtc.c
  3. *
  4. * @brief rtc Driver Module
  5. *
  6. * @author dave
  7. *
  8. * Copyright (c) 2014 Winner Microelectronics Co., Ltd.
  9. */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include "wm_regs.h"
  14. #include "wm_rtc.h"
  15. #include "wm_irq.h"
  16. #include "tls_common.h"
  17. struct rtc_irq_context {
  18. tls_rtc_irq_callback callback;
  19. void *arg;
  20. };
  21. static struct rtc_irq_context rtc_context = {0};
  22. /**
  23. * @brief This function is used to set pmu rtc time
  24. *
  25. * @param[in] tblock time value
  26. *
  27. * @return None
  28. *
  29. * @note None
  30. */
  31. void tls_set_rtc(struct tm *tblock)
  32. {
  33. int ctrl1 = 0;
  34. int ctrl2 = 0;
  35. ctrl2 = tls_reg_read32(HR_PMU_RTC_CTRL2); /* disable */
  36. ctrl2 &= ~(1 << 16);
  37. tls_reg_write32(HR_PMU_RTC_CTRL2, ctrl2);
  38. ctrl1 |= tblock->tm_sec;
  39. ctrl1 |= tblock->tm_min << 8;
  40. ctrl1 |= tblock->tm_hour << 16;
  41. ctrl1 |= tblock->tm_mday << 24;
  42. tls_reg_write32(HR_PMU_RTC_CTRL1, ctrl1);
  43. ctrl2 = 0;
  44. ctrl2 |= tblock->tm_mon;
  45. ctrl2 |= tblock->tm_year << 8;
  46. tls_reg_write32(HR_PMU_RTC_CTRL2, ctrl2);
  47. ctrl2 = tls_reg_read32(HR_PMU_RTC_CTRL2); /* enable */
  48. ctrl2 |= (1 << 16);
  49. tls_reg_write32(HR_PMU_RTC_CTRL2, ctrl2);
  50. }
  51. /**
  52. * @brief This function is used to get pmu rtc time
  53. *
  54. * @param[out] tblock time value
  55. *
  56. * @return None
  57. *
  58. * @note None
  59. */
  60. void tls_get_rtc(struct tm *tblock)
  61. {
  62. int ctrl1 = 0;
  63. int ctrl2 = 0;
  64. ctrl1 = tls_reg_read32(HR_PMU_RTC_CTRL1);
  65. ctrl2 = tls_reg_read32(HR_PMU_RTC_CTRL2);
  66. tblock->tm_year = ((int)((int)ctrl2 & 0x00007f00) >> 8);
  67. tblock->tm_mon = (ctrl2 & 0x0000000f);
  68. tblock->tm_mday = (ctrl1 & 0x1f000000) >> 24;
  69. tblock->tm_hour = (ctrl1 & 0x001f0000) >> 16;
  70. tblock->tm_min = (ctrl1 & 0x00003f00) >> 8;
  71. tblock->tm_sec = ctrl1 & 0x0000003f;
  72. }
  73. void PMU_RTC_IRQHandler(void)
  74. {
  75. tls_reg_write32(HR_PMU_INTERRUPT_SRC, BIT(4)); /* clear rtc interrupt */
  76. if (NULL != rtc_context.callback)
  77. rtc_context.callback(rtc_context.arg);
  78. return;
  79. }
  80. /**
  81. * @brief This function is used to register pmu rtc interrupt
  82. *
  83. * @param[in] callback the rtc interrupt call back function
  84. * @param[in] arg parameter of call back function
  85. *
  86. * @return None
  87. *
  88. * @note
  89. * user not need clear interrupt flag.
  90. * rtc callback function is called in interrupt,
  91. * so can not operate the critical data in the callback fuuction,
  92. * recommendation to send messages to other tasks to operate it.
  93. */
  94. void tls_rtc_isr_register(tls_rtc_irq_callback callback, void *arg)
  95. {
  96. rtc_context.callback = callback;
  97. rtc_context.arg = arg;
  98. tls_irq_enable(PMU_IRQn);
  99. return;
  100. }
  101. /**
  102. * @brief This function is used to start pmu rtc timer
  103. *
  104. * @param[in] tblock timer value
  105. *
  106. * @return None
  107. *
  108. * @note None
  109. */
  110. void tls_rtc_timer_start(struct tm *tblock)
  111. {
  112. int ctrl1 = 0;
  113. int ctrl2 = 0;
  114. tls_irq_enable(PMU_IRQn);
  115. ctrl1 |= tblock->tm_sec;
  116. ctrl1 |= tblock->tm_min << 8;
  117. ctrl1 |= tblock->tm_hour << 16;
  118. ctrl1 |= tblock->tm_mday << 24;
  119. ctrl2 |= tblock->tm_mon;
  120. ctrl2 |= tblock->tm_year << 8;
  121. tls_reg_write32(HR_PMU_RTC_CTRL2, ctrl2 | BIT(16));
  122. tls_reg_write32(HR_PMU_RTC_CTRL1, ctrl1 | BIT(31));/* must set the enable */
  123. return;
  124. }
  125. /**
  126. * @brief This function is used to stop pmu rtc timer
  127. *
  128. * @param None
  129. *
  130. * @return None
  131. *
  132. * @note This function also is used to clear rtc timer interrupt
  133. */
  134. void tls_rtc_timer_stop(void)
  135. {
  136. tls_reg_write32(HR_PMU_RTC_CTRL1, tls_reg_read32(HR_PMU_RTC_CTRL1) & (~BIT(31)));
  137. return;
  138. }