drv_rtc.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright (c) 2020-2021, Bluetrum Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-01-28 greedyhao first version
  9. * 2021-03-19 iysheng modify just set time first power up
  10. */
  11. #include "board.h"
  12. #include <time.h>
  13. #include <sys/time.h>
  14. #ifdef BSP_USING_ONCHIP_RTC
  15. //#define DRV_DEBUG
  16. #define LOG_TAG "drv.rtc"
  17. #include <drv_log.h>
  18. static struct rt_device rtc;
  19. /************** HAL Start *******************/
  20. #define IRTC_ENTER_CRITICAL() uint32_t cpu_ie = PICCON & BIT(0); PICCONCLR = BIT(0);
  21. #define IRTC_EXIT_CRITICAL() PICCON |= cpu_ie
  22. uint8_t get_weekday(struct tm *const _tm)
  23. {
  24. uint8_t weekday;
  25. time_t secs = timegm(_tm);
  26. weekday = (secs / 86400 + 4) % 7;
  27. return weekday;
  28. }
  29. void irtc_write(uint32_t cmd)
  30. {
  31. RTCDAT = cmd;
  32. while (RTCCON & RTC_CON_TRANS_DONE);
  33. }
  34. uint8_t irtc_read(void)
  35. {
  36. RTCDAT = 0x00;
  37. while (RTCCON & RTC_CON_TRANS_DONE);
  38. return (uint8_t)RTCDAT;
  39. }
  40. void irtc_time_write(uint32_t cmd, uint32_t dat)
  41. {
  42. IRTC_ENTER_CRITICAL();
  43. RTCCON |= RTC_CON_CHIP_SELECT;
  44. irtc_write(cmd | RTC_WR);
  45. irtc_write((uint8_t)(dat >> 24));
  46. irtc_write((uint8_t)(dat >> 16));
  47. irtc_write((uint8_t)(dat >> 8));
  48. irtc_write((uint8_t)(dat >> 0));
  49. RTCCON &= ~RTC_CON_CHIP_SELECT;
  50. IRTC_EXIT_CRITICAL();
  51. }
  52. uint32_t irtc_time_read(uint32_t cmd)
  53. {
  54. uint32_t rd_val;
  55. IRTC_ENTER_CRITICAL();
  56. RTCCON |= RTC_CON_CHIP_SELECT;
  57. irtc_write(cmd | RTC_RD);
  58. *((uint8_t *)&rd_val + 3) = irtc_read();
  59. *((uint8_t *)&rd_val + 2) = irtc_read();
  60. *((uint8_t *)&rd_val + 1) = irtc_read();
  61. *((uint8_t *)&rd_val + 0) = irtc_read();
  62. RTCCON &= ~RTC_CON_CHIP_SELECT;
  63. IRTC_EXIT_CRITICAL();
  64. return rd_val;
  65. }
  66. void irtc_sfr_write(uint32_t cmd, uint8_t dat)
  67. {
  68. IRTC_ENTER_CRITICAL();
  69. RTCCON |= RTC_CON_CHIP_SELECT;
  70. irtc_write(cmd | RTC_WR);
  71. irtc_write(dat);
  72. RTCCON &= ~RTC_CON_CHIP_SELECT;
  73. IRTC_EXIT_CRITICAL();
  74. }
  75. uint8_t irtc_sfr_read(uint32_t cmd)
  76. {
  77. uint8_t rd_val;
  78. IRTC_ENTER_CRITICAL();
  79. RTCCON |= RTC_CON_CHIP_SELECT;
  80. irtc_write(cmd | RTC_RD);
  81. rd_val = irtc_read();
  82. RTCCON &= ~RTC_CON_CHIP_SELECT;
  83. IRTC_EXIT_CRITICAL();
  84. }
  85. static void _init_rtc_clock(void)
  86. {
  87. uint8_t rtccon0;
  88. uint8_t rtccon2;
  89. rtccon0 = irtc_sfr_read(RTCCON0_CMD);
  90. rtccon2 = irtc_sfr_read(RTCCON2_CMD);
  91. #ifdef RTC_USING_INTERNAL_CLK
  92. rtccon0 &= ~RTC_CON0_XOSC32K_ENABLE;
  93. rtccon0 |= RTC_CON0_INTERNAL_32K;
  94. rtccon2 | RTC_CON2_32K_SELECT;
  95. #else
  96. rtccon0 |= RTC_CON0_XOSC32K_ENABLE;
  97. rtccon0 &= ~RTC_CON0_INTERNAL_32K;
  98. rtccon2 & ~RTC_CON2_32K_SELECT;
  99. #endif
  100. irtc_sfr_write(RTCCON0_CMD, rtccon0);
  101. irtc_sfr_write(RTCCON2_CMD, rtccon2);
  102. }
  103. void hal_rtc_init(void)
  104. {
  105. time_t sec = 0;
  106. struct tm tm_new = {0};
  107. uint8_t temp;
  108. _init_rtc_clock();
  109. temp = irtc_sfr_read(RTCCON0_CMD);
  110. if (temp & RTC_CON0_PWRUP_FIRST) {
  111. temp &= ~RTC_CON0_PWRUP_FIRST;
  112. irtc_sfr_write(RTCCON0_CMD, temp); /* First power on */
  113. tm_new.tm_mday = 29;
  114. tm_new.tm_mon = 1 - 1;
  115. tm_new.tm_year = 2021 - 1900;
  116. sec = timegm(&tm_new);
  117. irtc_time_write(RTCCNT_CMD, sec);
  118. }
  119. }
  120. /************** HAL End *******************/
  121. static time_t get_rtc_timestamp(void)
  122. {
  123. time_t sec = 0;
  124. sec = irtc_time_read(RTCCNT_CMD);
  125. LOG_D("get rtc time.");
  126. return sec;
  127. }
  128. static rt_err_t set_rtc_time_stamp(time_t time_stamp)
  129. {
  130. irtc_time_write(RTCCNT_CMD, time_stamp);
  131. return RT_EOK;
  132. }
  133. static void rt_rtc_init(void)
  134. {
  135. hal_rtc_init();
  136. }
  137. static rt_err_t rt_rtc_control(rt_device_t dev, int cmd, void *args)
  138. {
  139. rt_err_t result = RT_EOK;
  140. RT_ASSERT(dev != RT_NULL);
  141. switch (cmd)
  142. {
  143. case RT_DEVICE_CTRL_RTC_GET_TIME:
  144. *(rt_uint32_t *)args = get_rtc_timestamp();
  145. LOG_D("RTC: get rtc_time %x\n", *(rt_uint32_t *)args);
  146. break;
  147. case RT_DEVICE_CTRL_RTC_SET_TIME:
  148. if (set_rtc_time_stamp(*(rt_uint32_t *)args))
  149. {
  150. result = -RT_ERROR;
  151. }
  152. LOG_D("RTC: set rtc_time %x\n", *(rt_uint32_t *)args);
  153. break;
  154. }
  155. return result;
  156. }
  157. #ifdef RT_USING_DEVICE_OPS
  158. const static struct rt_device_ops rtc_ops =
  159. {
  160. RT_NULL,
  161. RT_NULL,
  162. RT_NULL,
  163. RT_NULL,
  164. RT_NULL,
  165. rt_rtc_control
  166. };
  167. #endif
  168. static rt_err_t rt_hw_rtc_register(rt_device_t device, const char *name, rt_uint32_t flag)
  169. {
  170. RT_ASSERT(device != RT_NULL);
  171. rt_rtc_init();
  172. #ifdef RT_USING_DEVICE_OPS
  173. device->ops = &rtc_ops;
  174. #else
  175. device->init = RT_NULL;
  176. device->open = RT_NULL;
  177. device->close = RT_NULL;
  178. device->read = RT_NULL;
  179. device->write = RT_NULL;
  180. device->control = rt_rtc_control;
  181. #endif
  182. device->type = RT_Device_Class_RTC;
  183. device->rx_indicate = RT_NULL;
  184. device->tx_complete = RT_NULL;
  185. device->user_data = RT_NULL;
  186. /* register a character device */
  187. return rt_device_register(device, name, flag);
  188. }
  189. int rt_hw_rtc_init(void)
  190. {
  191. rt_err_t result;
  192. result = rt_hw_rtc_register(&rtc, "rtc", RT_DEVICE_FLAG_RDWR);
  193. if (result != RT_EOK)
  194. {
  195. LOG_E("rtc register err code: %d", result);
  196. return result;
  197. }
  198. LOG_D("rtc init success");
  199. return RT_EOK;
  200. }
  201. INIT_DEVICE_EXPORT(rt_hw_rtc_init);
  202. #endif /* BSP_USING_ONCHIP_RTC */