rtc.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2012-01-29 aozima first version.
  9. * 2012-04-12 aozima optimization: find rtc device only first.
  10. * 2012-04-16 aozima add scheduler lock for set_date and set_time.
  11. * 2018-02-16 armink add auto sync time by NTP
  12. */
  13. #include <rtdevice.h>
  14. #ifdef RT_USING_RTC
  15. #include <time.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18. /* Using NTP auto sync RTC time */
  19. #ifdef RTC_SYNC_USING_NTP
  20. /* NTP first sync delay time for network connect, unit: second */
  21. #ifndef RTC_NTP_FIRST_SYNC_DELAY
  22. #define RTC_NTP_FIRST_SYNC_DELAY (30)
  23. #endif
  24. /* NTP sync period, unit: second */
  25. #ifndef RTC_NTP_SYNC_PERIOD
  26. #define RTC_NTP_SYNC_PERIOD (1L*60L*60L)
  27. #endif
  28. #endif /* RTC_SYNC_USING_NTP */
  29. /**
  30. * Set system date(time not modify).
  31. *
  32. * @param rt_uint32_t year e.g: 2012.
  33. * @param rt_uint32_t month e.g: 12 (1~12).
  34. * @param rt_uint32_t day e.g: 31.
  35. *
  36. * @return rt_err_t if set success, return RT_EOK.
  37. *
  38. */
  39. rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day)
  40. {
  41. time_t now;
  42. struct tm *p_tm;
  43. struct tm tm_new;
  44. rt_device_t device;
  45. rt_err_t ret = -RT_ERROR;
  46. /* get current time */
  47. now = time(RT_NULL);
  48. /* lock scheduler. */
  49. rt_enter_critical();
  50. /* converts calendar time time into local time. */
  51. p_tm = localtime(&now);
  52. /* copy the statically located variable */
  53. memcpy(&tm_new, p_tm, sizeof(struct tm));
  54. /* unlock scheduler. */
  55. rt_exit_critical();
  56. /* update date. */
  57. tm_new.tm_year = year - 1900;
  58. tm_new.tm_mon = month - 1; /* tm_mon: 0~11 */
  59. tm_new.tm_mday = day;
  60. /* converts the local time in time to calendar time. */
  61. now = mktime(&tm_new);
  62. device = rt_device_find("rtc");
  63. if (device == RT_NULL)
  64. {
  65. return -RT_ERROR;
  66. }
  67. /* update to RTC device. */
  68. ret = rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
  69. return ret;
  70. }
  71. /**
  72. * Set system time(date not modify).
  73. *
  74. * @param rt_uint32_t hour e.g: 0~23.
  75. * @param rt_uint32_t minute e.g: 0~59.
  76. * @param rt_uint32_t second e.g: 0~59.
  77. *
  78. * @return rt_err_t if set success, return RT_EOK.
  79. *
  80. */
  81. rt_err_t set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second)
  82. {
  83. time_t now;
  84. struct tm *p_tm;
  85. struct tm tm_new;
  86. rt_device_t device;
  87. rt_err_t ret = -RT_ERROR;
  88. /* get current time */
  89. now = time(RT_NULL);
  90. /* lock scheduler. */
  91. rt_enter_critical();
  92. /* converts calendar time time into local time. */
  93. p_tm = localtime(&now);
  94. /* copy the statically located variable */
  95. memcpy(&tm_new, p_tm, sizeof(struct tm));
  96. /* unlock scheduler. */
  97. rt_exit_critical();
  98. /* update time. */
  99. tm_new.tm_hour = hour;
  100. tm_new.tm_min = minute;
  101. tm_new.tm_sec = second;
  102. /* converts the local time in time to calendar time. */
  103. now = mktime(&tm_new);
  104. device = rt_device_find("rtc");
  105. if (device == RT_NULL)
  106. {
  107. return -RT_ERROR;
  108. }
  109. /* update to RTC device. */
  110. ret = rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
  111. return ret;
  112. }
  113. #ifdef RTC_SYNC_USING_NTP
  114. static void ntp_sync_thread_enrty(void *param)
  115. {
  116. extern time_t ntp_sync_to_rtc(const char *host_name);
  117. /* first sync delay for network connect */
  118. rt_thread_delay(RTC_NTP_FIRST_SYNC_DELAY * RT_TICK_PER_SECOND);
  119. while (1)
  120. {
  121. ntp_sync_to_rtc(NULL);
  122. rt_thread_delay(RTC_NTP_SYNC_PERIOD * RT_TICK_PER_SECOND);
  123. }
  124. }
  125. int rt_rtc_ntp_sync_init(void)
  126. {
  127. static rt_bool_t init_ok = RT_FALSE;
  128. rt_thread_t thread;
  129. if (init_ok)
  130. {
  131. return 0;
  132. }
  133. thread = rt_thread_create("ntp_sync", ntp_sync_thread_enrty, RT_NULL, 1536, 26, 2);
  134. if (thread)
  135. {
  136. rt_thread_startup(thread);
  137. }
  138. else
  139. {
  140. return -RT_ENOMEM;
  141. }
  142. init_ok = RT_TRUE;
  143. return RT_EOK;
  144. }
  145. INIT_COMPONENT_EXPORT(rt_rtc_ntp_sync_init);
  146. #endif /* RTC_SYNC_USING_NTP */
  147. #ifdef RT_USING_FINSH
  148. #include <finsh.h>
  149. #include <rtdevice.h>
  150. void list_date(void)
  151. {
  152. time_t now;
  153. now = time(RT_NULL);
  154. rt_kprintf("%.*s\n", 25, ctime(&now));
  155. }
  156. FINSH_FUNCTION_EXPORT(list_date, show date and time.)
  157. FINSH_FUNCTION_EXPORT(set_date, set date. e.g: set_date(2010,2,28))
  158. FINSH_FUNCTION_EXPORT(set_time, set time. e.g: set_time(23,59,59))
  159. #if defined(RT_USING_FINSH)
  160. static void date(uint8_t argc, char **argv)
  161. {
  162. if (argc == 1)
  163. {
  164. time_t now;
  165. /* output current time */
  166. now = time(RT_NULL);
  167. rt_kprintf("%.*s", 25, ctime(&now));
  168. }
  169. else if (argc >= 7)
  170. {
  171. /* set time and date */
  172. uint16_t year;
  173. uint8_t month, day, hour, min, sec;
  174. year = atoi(argv[1]);
  175. month = atoi(argv[2]);
  176. day = atoi(argv[3]);
  177. hour = atoi(argv[4]);
  178. min = atoi(argv[5]);
  179. sec = atoi(argv[6]);
  180. if (year > 2099 || year < 2000)
  181. {
  182. rt_kprintf("year is out of range [2000-2099]\n");
  183. return;
  184. }
  185. if (month == 0 || month > 12)
  186. {
  187. rt_kprintf("month is out of range [1-12]\n");
  188. return;
  189. }
  190. if (day == 0 || day > 31)
  191. {
  192. rt_kprintf("day is out of range [1-31]\n");
  193. return;
  194. }
  195. if (hour > 23)
  196. {
  197. rt_kprintf("hour is out of range [0-23]\n");
  198. return;
  199. }
  200. if (min > 59)
  201. {
  202. rt_kprintf("minute is out of range [0-59]\n");
  203. return;
  204. }
  205. if (sec > 59)
  206. {
  207. rt_kprintf("second is out of range [0-59]\n");
  208. return;
  209. }
  210. set_time(hour, min, sec);
  211. set_date(year, month, day);
  212. }
  213. else
  214. {
  215. rt_kprintf("please input: date [year month day hour min sec] or date\n");
  216. rt_kprintf("e.g: date 2018 01 01 23 59 59 or date\n");
  217. }
  218. }
  219. MSH_CMD_EXPORT(date, get date and time or set [year month day hour min sec]);
  220. #endif /* defined(RT_USING_FINSH) */
  221. #endif /* RT_USING_FINSH */
  222. #endif /* RT_USING_RTC */