1
0

dev_soft_rtc.c 8.2 KB

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