rtc.c 6.1 KB

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