rtc.c 6.0 KB

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