rtc.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * File : rtc.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2012-01-29 aozima first version.
  23. * 2012-04-12 aozima optimization: find rtc device only first.
  24. * 2012-04-16 aozima add scheduler lock for set_date and set_time.
  25. * 2018-02-16 armink add auto sync time by NTP
  26. */
  27. #include <time.h>
  28. #include <string.h>
  29. #include <rtthread.h>
  30. /* Using NTP auto sync RTC time */
  31. #ifdef RTC_SYNC_USING_NTP
  32. /* NTP first sync delay time for network connect, unit: second */
  33. #ifndef RTC_NTP_FIRST_SYNC_DELAY
  34. #define RTC_NTP_FIRST_SYNC_DELAY (30)
  35. #endif
  36. /* NTP sync period, unit: second */
  37. #ifndef RTC_NTP_SYNC_PERIOD
  38. #define RTC_NTP_SYNC_PERIOD (1L*60L*60L)
  39. #endif
  40. #endif /* RTC_SYNC_USING_NTP */
  41. /**
  42. * Returns the current time.
  43. *
  44. * @param time_t * t the timestamp pointer, if not used, keep NULL.
  45. *
  46. * @return time_t return timestamp current.
  47. *
  48. */
  49. /* for IAR 6.2 later Compiler */
  50. #if defined (__IAR_SYSTEMS_ICC__) && (__VER__) >= 6020000
  51. #pragma module_name = "?time"
  52. time_t (__time32)(time_t *t) /* Only supports 32-bit timestamp */
  53. #else
  54. time_t time(time_t *t)
  55. #endif
  56. {
  57. static rt_device_t device = RT_NULL;
  58. time_t time_now = 0;
  59. /* optimization: find rtc device only first. */
  60. if (device == RT_NULL)
  61. {
  62. device = rt_device_find("rtc");
  63. }
  64. /* read timestamp from RTC device. */
  65. if (device != RT_NULL)
  66. {
  67. if (rt_device_open(device, 0) == RT_EOK)
  68. {
  69. rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time_now);
  70. rt_device_close(device);
  71. }
  72. }
  73. /* if t is not NULL, write timestamp to *t */
  74. if (t != RT_NULL)
  75. {
  76. *t = time_now;
  77. }
  78. return time_now;
  79. }
  80. RT_WEAK clock_t clock(void)
  81. {
  82. return rt_tick_get();
  83. }
  84. /**
  85. * Set system date(time not modify).
  86. *
  87. * @param rt_uint32_t year e.g: 2012.
  88. * @param rt_uint32_t month e.g: 12 (1~12).
  89. * @param rt_uint32_t day e.g: 31.
  90. *
  91. * @return rt_err_t if set success, return RT_EOK.
  92. *
  93. */
  94. rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day)
  95. {
  96. time_t now;
  97. struct tm *p_tm;
  98. struct tm tm_new;
  99. rt_device_t device;
  100. rt_err_t ret = -RT_ERROR;
  101. /* get current time */
  102. now = time(RT_NULL);
  103. /* lock scheduler. */
  104. rt_enter_critical();
  105. /* converts calendar time time into local time. */
  106. p_tm = localtime(&now);
  107. /* copy the statically located variable */
  108. memcpy(&tm_new, p_tm, sizeof(struct tm));
  109. /* unlock scheduler. */
  110. rt_exit_critical();
  111. /* update date. */
  112. tm_new.tm_year = year - 1900;
  113. tm_new.tm_mon = month - 1; /* tm_mon: 0~11 */
  114. tm_new.tm_mday = day;
  115. /* converts the local time in time to calendar time. */
  116. now = mktime(&tm_new);
  117. device = rt_device_find("rtc");
  118. if (device == RT_NULL)
  119. {
  120. return -RT_ERROR;
  121. }
  122. /* update to RTC device. */
  123. ret = rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
  124. return ret;
  125. }
  126. /**
  127. * Set system time(date not modify).
  128. *
  129. * @param rt_uint32_t hour e.g: 0~23.
  130. * @param rt_uint32_t minute e.g: 0~59.
  131. * @param rt_uint32_t second e.g: 0~59.
  132. *
  133. * @return rt_err_t if set success, return RT_EOK.
  134. *
  135. */
  136. rt_err_t set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second)
  137. {
  138. time_t now;
  139. struct tm *p_tm;
  140. struct tm tm_new;
  141. rt_device_t device;
  142. rt_err_t ret = -RT_ERROR;
  143. /* get current time */
  144. now = time(RT_NULL);
  145. /* lock scheduler. */
  146. rt_enter_critical();
  147. /* converts calendar time time into local time. */
  148. p_tm = localtime(&now);
  149. /* copy the statically located variable */
  150. memcpy(&tm_new, p_tm, sizeof(struct tm));
  151. /* unlock scheduler. */
  152. rt_exit_critical();
  153. /* update time. */
  154. tm_new.tm_hour = hour;
  155. tm_new.tm_min = minute;
  156. tm_new.tm_sec = second;
  157. /* converts the local time in time to calendar time. */
  158. now = mktime(&tm_new);
  159. device = rt_device_find("rtc");
  160. if (device == RT_NULL)
  161. {
  162. return -RT_ERROR;
  163. }
  164. /* update to RTC device. */
  165. ret = rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
  166. return ret;
  167. }
  168. #ifdef RTC_SYNC_USING_NTP
  169. static void ntp_sync_thread_enrty(void *param)
  170. {
  171. extern time_t ntp_sync_to_rtc(void);
  172. /* first sync delay for network connect */
  173. rt_thread_delay(RTC_NTP_FIRST_SYNC_DELAY * RT_TICK_PER_SECOND);
  174. while (1)
  175. {
  176. ntp_sync_to_rtc();
  177. rt_thread_delay(RTC_NTP_SYNC_PERIOD * RT_TICK_PER_SECOND);
  178. }
  179. }
  180. int rt_rtc_ntp_sync_init(void)
  181. {
  182. static rt_bool_t init_ok = RT_FALSE;
  183. rt_thread_t thread;
  184. if (init_ok)
  185. {
  186. return 0;
  187. }
  188. thread = rt_thread_create("ntp_sync", ntp_sync_thread_enrty, RT_NULL, 1536, 26, 2);
  189. if (thread)
  190. {
  191. rt_thread_startup(thread);
  192. }
  193. else
  194. {
  195. return -RT_ENOMEM;
  196. }
  197. init_ok = RT_TRUE;
  198. return RT_EOK;
  199. }
  200. INIT_COMPONENT_EXPORT(rt_rtc_ntp_sync_init);
  201. #endif /* RTC_SYNC_USING_NTP */
  202. #ifdef RT_USING_FINSH
  203. #include <finsh.h>
  204. #include <rtdevice.h>
  205. void list_date(void)
  206. {
  207. time_t now;
  208. now = time(RT_NULL);
  209. rt_kprintf("%s\n", ctime(&now));
  210. }
  211. FINSH_FUNCTION_EXPORT(list_date, show date and time.)
  212. FINSH_FUNCTION_EXPORT(set_date, set date. e.g: set_date(2010,2,28))
  213. FINSH_FUNCTION_EXPORT(set_time, set time. e.g: set_time(23,59,59))
  214. #if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH)
  215. static void date(uint8_t argc, char **argv)
  216. {
  217. if (argc == 1)
  218. {
  219. time_t now;
  220. /* output current time */
  221. now = time(RT_NULL);
  222. rt_kprintf("%s", ctime(&now));
  223. }
  224. else if (argc >= 7)
  225. {
  226. /* set time and date */
  227. uint16_t year;
  228. uint8_t month, day, hour, min, sec;
  229. year = atoi(argv[1]);
  230. month = atoi(argv[2]);
  231. day = atoi(argv[3]);
  232. hour = atoi(argv[4]);
  233. min = atoi(argv[5]);
  234. sec = atoi(argv[6]);
  235. if (year > 2099 || year < 2000)
  236. {
  237. rt_kprintf("year is out of range [2000-2099]\n");
  238. return;
  239. }
  240. if (month == 0 || month > 12)
  241. {
  242. rt_kprintf("month is out of range [1-12]\n");
  243. return;
  244. }
  245. if (day == 0 || day > 31)
  246. {
  247. rt_kprintf("day is out of range [1-31]\n");
  248. return;
  249. }
  250. if (hour > 23)
  251. {
  252. rt_kprintf("hour is out of range [0-23]\n");
  253. return;
  254. }
  255. if (min > 59)
  256. {
  257. rt_kprintf("minute is out of range [0-59]\n");
  258. return;
  259. }
  260. if (sec > 59)
  261. {
  262. rt_kprintf("second is out of range [0-59]\n");
  263. return;
  264. }
  265. set_time(hour, min, sec);
  266. set_date(year, month, day);
  267. }
  268. else
  269. {
  270. rt_kprintf("please input: date [year month day hour min sec] or date\n");
  271. rt_kprintf("e.g: date 2018 01 01 23 59 59 or date\n");
  272. }
  273. }
  274. MSH_CMD_EXPORT(date, get date and time or set [year month day hour min sec]);
  275. #endif /* defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) */
  276. #endif /* RT_USING_FINSH */