dev_soft_rtc.c 7.6 KB

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