soft_rtc.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. #ifdef RT_USING_KTIME
  36. static struct timeval init_tv = {0};
  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. #ifdef RT_USING_KTIME
  82. struct timeval *tv;
  83. struct timespec *ts;
  84. struct timeval _tv;
  85. struct timespec _ts;
  86. #endif
  87. struct tm time_temp;
  88. RT_ASSERT(dev != RT_NULL);
  89. rt_memset(&time_temp, 0, sizeof(struct tm));
  90. switch (cmd)
  91. {
  92. case RT_DEVICE_CTRL_RTC_GET_TIME:
  93. t = (time_t *) args;
  94. *t = init_time + (rt_tick_get() - init_tick) / RT_TICK_PER_SECOND;
  95. break;
  96. case RT_DEVICE_CTRL_RTC_SET_TIME:
  97. {
  98. t = (time_t *) args;
  99. set_rtc_time(*t);
  100. _source_device_control(RT_DEVICE_CTRL_RTC_SET_TIME, t);
  101. break;
  102. }
  103. #ifdef RT_USING_ALARM
  104. case RT_DEVICE_CTRL_RTC_GET_ALARM:
  105. *((struct rt_rtc_wkalarm *)args) = wkalarm;
  106. break;
  107. case RT_DEVICE_CTRL_RTC_SET_ALARM:
  108. wkalarm = *((struct rt_rtc_wkalarm *)args);
  109. soft_rtc_alarm_update(&wkalarm);
  110. break;
  111. #endif
  112. #ifdef RT_USING_KTIME
  113. case RT_DEVICE_CTRL_RTC_GET_TIMEVAL:
  114. tv = (struct timeval *)args;
  115. rt_ktime_boottime_get_us(&_tv);
  116. tv->tv_sec = init_time + _tv.tv_sec;
  117. tv->tv_usec = init_tv.tv_usec + _tv.tv_usec;
  118. break;
  119. case RT_DEVICE_CTRL_RTC_SET_TIMEVAL:
  120. tv = (struct timeval *)args;
  121. rt_ktime_boottime_get_us(&_tv);
  122. set_rtc_time(tv->tv_sec);
  123. init_tv.tv_usec = tv->tv_usec - _tv.tv_usec;
  124. _source_device_control(RT_DEVICE_CTRL_RTC_SET_TIME, &(tv->tv_sec));
  125. break;
  126. case RT_DEVICE_CTRL_RTC_GET_TIMESPEC:
  127. ts = (struct timespec *)args;
  128. rt_ktime_boottime_get_ns(&_ts);
  129. ts->tv_sec = init_time + _ts.tv_sec;
  130. ts->tv_nsec = init_ts.tv_nsec + _ts.tv_nsec;
  131. break;
  132. case RT_DEVICE_CTRL_RTC_SET_TIMESPEC:
  133. ts = (struct timespec *)args;
  134. rt_ktime_boottime_get_ns(&_ts);
  135. set_rtc_time(ts->tv_sec);
  136. init_ts.tv_nsec = ts->tv_nsec - _ts.tv_nsec;
  137. _source_device_control(RT_DEVICE_CTRL_RTC_SET_TIME, &(ts->tv_sec));
  138. break;
  139. case RT_DEVICE_CTRL_RTC_GET_TIMERES:
  140. ts = (struct timespec *)args;
  141. ts->tv_sec = 0;
  142. ts->tv_nsec = (rt_ktime_cputimer_getres() / RT_KTIME_RESMUL);
  143. break;
  144. #endif
  145. default:
  146. return -RT_EINVAL;
  147. }
  148. return RT_EOK;
  149. }
  150. #ifdef RT_USING_DEVICE_OPS
  151. const static struct rt_device_ops soft_rtc_ops =
  152. {
  153. RT_NULL,
  154. RT_NULL,
  155. RT_NULL,
  156. RT_NULL,
  157. RT_NULL,
  158. soft_rtc_control
  159. };
  160. #endif
  161. static int rt_soft_rtc_init(void)
  162. {
  163. static rt_bool_t init_ok = RT_FALSE;
  164. struct tm time_new = SOFT_RTC_TIME_DEFAULT;
  165. if (init_ok)
  166. {
  167. return 0;
  168. }
  169. /* make sure only one 'rtc' device */
  170. RT_ASSERT(!rt_device_find("rtc"));
  171. #ifdef RT_USING_ALARM
  172. rt_timer_init(&alarm_time,
  173. "alarm",
  174. alarm_timeout,
  175. &soft_rtc_dev,
  176. 0,
  177. RT_TIMER_FLAG_SOFT_TIMER|RT_TIMER_FLAG_ONE_SHOT);
  178. #endif
  179. init_tick = rt_tick_get();
  180. init_time = timegm(&time_new);
  181. soft_rtc_dev.type = RT_Device_Class_RTC;
  182. /* register rtc device */
  183. #ifdef RT_USING_DEVICE_OPS
  184. soft_rtc_dev.ops = &soft_rtc_ops;
  185. #else
  186. soft_rtc_dev.init = RT_NULL;
  187. soft_rtc_dev.open = RT_NULL;
  188. soft_rtc_dev.close = RT_NULL;
  189. soft_rtc_dev.read = RT_NULL;
  190. soft_rtc_dev.write = RT_NULL;
  191. soft_rtc_dev.control = soft_rtc_control;
  192. #endif
  193. /* no private */
  194. soft_rtc_dev.user_data = RT_NULL;
  195. rt_device_register(&soft_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR);
  196. init_ok = RT_TRUE;
  197. return 0;
  198. }
  199. INIT_DEVICE_EXPORT(rt_soft_rtc_init);
  200. #ifdef RT_USING_SYSTEM_WORKQUEUE
  201. rt_err_t rt_soft_rtc_sync()
  202. {
  203. time_t time = 0;
  204. if (source_device == RT_NULL)
  205. {
  206. rt_kprintf("error: rtc source not found, please set it!!!\n");
  207. return RT_ENOSYS;
  208. }
  209. _source_device_control(RT_DEVICE_CTRL_RTC_GET_TIME, &time);
  210. set_rtc_time(time);
  211. return RT_EOK;
  212. }
  213. static void rtc_sync_work_func(struct rt_work *work, void *work_data)
  214. {
  215. rt_soft_rtc_sync();
  216. rt_work_submit(work, rt_tick_from_millisecond(RTC_AUTO_SYNC_PERIOD * 1000));
  217. }
  218. rt_err_t rt_soft_rtc_set_source(const char *name)
  219. {
  220. RT_ASSERT(name != RT_NULL);
  221. RT_ASSERT(rt_device_find(name)); // make sure source is exist
  222. source_device = rt_device_find(name);
  223. rt_work_init(&rtc_sync_work, rtc_sync_work_func, RT_NULL);
  224. rt_work_submit(&rtc_sync_work, rt_tick_from_millisecond(RTC_AUTO_SYNC_FIRST_DELAY * 1000));
  225. return RT_EOK;
  226. }
  227. #ifdef FINSH_USING_MSH
  228. #include <finsh.h>
  229. static void cmd_rtc_sync(int argc, char **argv)
  230. {
  231. struct timeval tv = {0};
  232. struct timezone tz = {0};
  233. time_t now = (time_t)0;
  234. rt_soft_rtc_sync();
  235. gettimeofday(&tv, &tz);
  236. now = tv.tv_sec;
  237. /* output current time */
  238. rt_kprintf("local time: %.*s", 25, ctime(&now));
  239. rt_kprintf("timestamps: %ld\n", (long)tv.tv_sec);
  240. }
  241. MSH_CMD_EXPORT_ALIAS(cmd_rtc_sync, rtc_sync, Update time by real rtc);
  242. #endif
  243. #endif /* RT_USING_SYSTEM_WORKQUEUE */
  244. #endif /* RT_USING_SOFT_RTC */