soft_rtc.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-01-30 armink the first version
  9. */
  10. #include <sys/time.h>
  11. #include <string.h>
  12. #include <rtthread.h>
  13. #include <rtdevice.h>
  14. #include <ktime.h>
  15. #ifdef RT_USING_SOFT_RTC
  16. /* 2018-01-30 14:44:50 = RTC_TIME_INIT(2018, 1, 30, 14, 44, 50) */
  17. #define RTC_TIME_INIT(year, month, day, hour, minute, second) \
  18. {.tm_year = year - 1900, .tm_mon = month - 1, .tm_mday = day, .tm_hour = hour, .tm_min = minute, .tm_sec = second}
  19. #ifndef SOFT_RTC_TIME_DEFAULT
  20. #define SOFT_RTC_TIME_DEFAULT RTC_TIME_INIT(2018, 1, 1, 0, 0 ,0)
  21. #endif
  22. #ifndef RTC_AUTO_SYNC_FIRST_DELAY
  23. #define RTC_AUTO_SYNC_FIRST_DELAY 25
  24. #endif
  25. #ifndef RTC_AUTO_SYNC_PERIOD
  26. #define RTC_AUTO_SYNC_PERIOD 3600
  27. #endif
  28. static struct rt_work rtc_sync_work;
  29. static rt_device_t source_device = RT_NULL;
  30. static struct rt_device soft_rtc_dev;
  31. static rt_tick_t init_tick;
  32. static time_t init_time;
  33. static struct timeval init_tv = {0};
  34. static struct timespec init_ts = {0};
  35. #ifdef RT_USING_ALARM
  36. static struct rt_rtc_wkalarm wkalarm;
  37. static struct rt_timer alarm_time;
  38. static void alarm_timeout(void *param)
  39. {
  40. rt_alarm_update(param, 1);
  41. }
  42. static void soft_rtc_alarm_update(struct rt_rtc_wkalarm *palarm)
  43. {
  44. rt_tick_t next_tick;
  45. if (palarm->enable)
  46. {
  47. next_tick = RT_TICK_PER_SECOND;
  48. rt_timer_control(&alarm_time, RT_TIMER_CTRL_SET_TIME, &next_tick);
  49. rt_timer_start(&alarm_time);
  50. }
  51. else
  52. {
  53. rt_timer_stop(&alarm_time);
  54. }
  55. }
  56. #endif
  57. static void set_rtc_time(time_t t)
  58. {
  59. init_time = t - (rt_tick_get() - init_tick) / RT_TICK_PER_SECOND;
  60. #ifdef RT_USING_ALARM
  61. soft_rtc_alarm_update(&wkalarm);
  62. #endif
  63. }
  64. static void _source_device_control(int cmd, void *args)
  65. {
  66. if (source_device == RT_NULL)
  67. return;
  68. if (rt_device_open(source_device, 0) == RT_EOK)
  69. {
  70. rt_device_control(source_device, cmd, args);
  71. rt_device_close(source_device);
  72. }
  73. }
  74. static rt_err_t soft_rtc_control(rt_device_t dev, int cmd, void *args)
  75. {
  76. time_t *t;
  77. struct timeval *tv;
  78. struct timespec *ts;
  79. struct timeval _tv;
  80. struct timespec _ts;
  81. struct tm time_temp;
  82. RT_ASSERT(dev != RT_NULL);
  83. rt_memset(&time_temp, 0, sizeof(struct tm));
  84. switch (cmd)
  85. {
  86. case RT_DEVICE_CTRL_RTC_GET_TIME:
  87. t = (time_t *) args;
  88. *t = init_time + (rt_tick_get() - init_tick) / RT_TICK_PER_SECOND;
  89. break;
  90. case RT_DEVICE_CTRL_RTC_SET_TIME:
  91. {
  92. t = (time_t *) args;
  93. set_rtc_time(*t);
  94. _source_device_control(RT_DEVICE_CTRL_RTC_SET_TIME, t);
  95. break;
  96. }
  97. #ifdef RT_USING_ALARM
  98. case RT_DEVICE_CTRL_RTC_GET_ALARM:
  99. *((struct rt_rtc_wkalarm *)args) = wkalarm;
  100. break;
  101. case RT_DEVICE_CTRL_RTC_SET_ALARM:
  102. wkalarm = *((struct rt_rtc_wkalarm *)args);
  103. soft_rtc_alarm_update(&wkalarm);
  104. break;
  105. #endif
  106. case RT_DEVICE_CTRL_RTC_GET_TIMEVAL:
  107. tv = (struct timeval *)args;
  108. rt_ktime_boottime_get_us(&_tv);
  109. tv->tv_sec = init_time + (rt_tick_get() - init_tick) / RT_TICK_PER_SECOND;
  110. tv->tv_usec = init_tv.tv_usec + _tv.tv_usec;
  111. break;
  112. case RT_DEVICE_CTRL_RTC_SET_TIMEVAL:
  113. tv = (struct timeval *)args;
  114. rt_ktime_boottime_get_us(&_tv);
  115. set_rtc_time(tv->tv_sec);
  116. init_tv.tv_usec = tv->tv_usec - _tv.tv_usec;
  117. _source_device_control(RT_DEVICE_CTRL_RTC_SET_TIME, &(tv->tv_sec));
  118. break;
  119. case RT_DEVICE_CTRL_RTC_GET_TIMESPEC:
  120. ts = (struct timespec *)args;
  121. rt_ktime_boottime_get_ns(&_ts);
  122. ts->tv_sec = init_time + (rt_tick_get() - init_tick) / RT_TICK_PER_SECOND;
  123. ts->tv_nsec = init_ts.tv_nsec + _ts.tv_nsec;
  124. break;
  125. case RT_DEVICE_CTRL_RTC_SET_TIMESPEC:
  126. ts = (struct timespec *)args;
  127. rt_ktime_boottime_get_ns(&_ts);
  128. set_rtc_time(ts->tv_sec);
  129. init_ts.tv_nsec = ts->tv_nsec - _ts.tv_nsec;
  130. _source_device_control(RT_DEVICE_CTRL_RTC_SET_TIME, &(ts->tv_sec));
  131. break;
  132. case RT_DEVICE_CTRL_RTC_GET_TIMERES:
  133. ts = (struct timespec *)args;
  134. ts->tv_sec = 0;
  135. ts->tv_nsec = (rt_ktime_cputimer_getres() / RT_KTIME_RESMUL);
  136. break;
  137. }
  138. return RT_EOK;
  139. }
  140. #ifdef RT_USING_DEVICE_OPS
  141. const static struct rt_device_ops soft_rtc_ops =
  142. {
  143. RT_NULL,
  144. RT_NULL,
  145. RT_NULL,
  146. RT_NULL,
  147. RT_NULL,
  148. soft_rtc_control
  149. };
  150. #endif
  151. static int rt_soft_rtc_init(void)
  152. {
  153. static rt_bool_t init_ok = RT_FALSE;
  154. struct tm time_new = SOFT_RTC_TIME_DEFAULT;
  155. if (init_ok)
  156. {
  157. return 0;
  158. }
  159. /* make sure only one 'rtc' device */
  160. RT_ASSERT(!rt_device_find("rtc"));
  161. #ifdef RT_USING_ALARM
  162. rt_timer_init(&alarm_time,
  163. "alarm",
  164. alarm_timeout,
  165. &soft_rtc_dev,
  166. 0,
  167. RT_TIMER_FLAG_SOFT_TIMER|RT_TIMER_FLAG_ONE_SHOT);
  168. #endif
  169. init_tick = rt_tick_get();
  170. init_time = timegm(&time_new);
  171. soft_rtc_dev.type = RT_Device_Class_RTC;
  172. /* register rtc device */
  173. #ifdef RT_USING_DEVICE_OPS
  174. soft_rtc_dev.ops = &soft_rtc_ops;
  175. #else
  176. soft_rtc_dev.init = RT_NULL;
  177. soft_rtc_dev.open = RT_NULL;
  178. soft_rtc_dev.close = RT_NULL;
  179. soft_rtc_dev.read = RT_NULL;
  180. soft_rtc_dev.write = RT_NULL;
  181. soft_rtc_dev.control = soft_rtc_control;
  182. #endif
  183. /* no private */
  184. soft_rtc_dev.user_data = RT_NULL;
  185. rt_device_register(&soft_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR);
  186. init_ok = RT_TRUE;
  187. return 0;
  188. }
  189. INIT_DEVICE_EXPORT(rt_soft_rtc_init);
  190. #ifdef RT_USING_SYSTEM_WORKQUEUE
  191. rt_err_t rt_soft_rtc_sync()
  192. {
  193. time_t time = 0;
  194. if (source_device == RT_NULL)
  195. {
  196. rt_kprintf("error: rtc source not found, please set it!!!\n");
  197. return RT_ENOSYS;
  198. }
  199. _source_device_control(RT_DEVICE_CTRL_RTC_GET_TIME, &time);
  200. set_rtc_time(time);
  201. return RT_EOK;
  202. }
  203. static void rtc_sync_work_func(struct rt_work *work, void *work_data)
  204. {
  205. rt_soft_rtc_sync();
  206. rt_work_submit(work, rt_tick_from_millisecond(RTC_AUTO_SYNC_PERIOD * 1000));
  207. }
  208. rt_err_t rt_soft_rtc_set_source(const char *name)
  209. {
  210. RT_ASSERT(name != RT_NULL);
  211. RT_ASSERT(rt_device_find(name)); // make sure source is exist
  212. source_device = rt_device_find(name);
  213. rt_work_init(&rtc_sync_work, rtc_sync_work_func, RT_NULL);
  214. rt_work_submit(&rtc_sync_work, rt_tick_from_millisecond(RTC_AUTO_SYNC_FIRST_DELAY * 1000));
  215. return RT_EOK;
  216. }
  217. #ifdef FINSH_USING_MSH
  218. #include <finsh.h>
  219. static void cmd_rtc_sync(int argc, char **argv)
  220. {
  221. struct timeval tv = {0};
  222. struct timezone tz = {0};
  223. time_t now = (time_t)0;
  224. rt_soft_rtc_sync();
  225. gettimeofday(&tv, &tz);
  226. now = tv.tv_sec;
  227. /* output current time */
  228. rt_kprintf("local time: %.*s", 25, ctime(&now));
  229. rt_kprintf("timestamps: %ld\n", (long)tv.tv_sec);
  230. }
  231. MSH_CMD_EXPORT_ALIAS(cmd_rtc_sync, rtc_sync, Update time by real rtc);
  232. #endif
  233. #endif /* RT_USING_SYSTEM_WORKQUEUE */
  234. #endif /* RT_USING_SOFT_RTC */