rtc.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. //rt_kprintf("year %d month %d day %d\n" , year , month , day);
  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. if(hour!=99)
  100. tm_new.tm_hour = hour;
  101. if(minute!=99)
  102. tm_new.tm_min = minute;
  103. if(second!=99)
  104. tm_new.tm_sec = second;
  105. /* converts the local time in time to calendar time. */
  106. now = mktime(&tm_new);
  107. device = rt_device_find("rtc");
  108. if (device == RT_NULL)
  109. {
  110. return -RT_ERROR;
  111. }
  112. /* update to RTC device. */
  113. ret = rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
  114. return ret;
  115. }
  116. #ifdef RTC_SYNC_USING_NTP
  117. static void ntp_sync_thread_enrty(void *param)
  118. {
  119. extern time_t ntp_sync_to_rtc(const char *host_name);
  120. /* first sync delay for network connect */
  121. rt_thread_delay(RTC_NTP_FIRST_SYNC_DELAY * RT_TICK_PER_SECOND);
  122. while (1)
  123. {
  124. ntp_sync_to_rtc(NULL);
  125. rt_thread_delay(RTC_NTP_SYNC_PERIOD * RT_TICK_PER_SECOND);
  126. }
  127. }
  128. int rt_rtc_ntp_sync_init(void)
  129. {
  130. static rt_bool_t init_ok = RT_FALSE;
  131. rt_thread_t thread;
  132. if (init_ok)
  133. {
  134. return 0;
  135. }
  136. thread = rt_thread_create("ntp_sync", ntp_sync_thread_enrty, RT_NULL, 1536, 26, 2);
  137. if (thread)
  138. {
  139. rt_thread_startup(thread);
  140. }
  141. else
  142. {
  143. return -RT_ENOMEM;
  144. }
  145. init_ok = RT_TRUE;
  146. return RT_EOK;
  147. }
  148. INIT_COMPONENT_EXPORT(rt_rtc_ntp_sync_init);
  149. #endif /* RTC_SYNC_USING_NTP */
  150. #ifdef RT_USING_FINSH
  151. #include <finsh.h>
  152. #include <rtdevice.h>
  153. void list_date(void)
  154. {
  155. time_t now;
  156. now = time(RT_NULL);
  157. rt_kprintf("%s\n", ctime(&now));
  158. }
  159. FINSH_FUNCTION_EXPORT(list_date, show date and time.)
  160. FINSH_FUNCTION_EXPORT(set_date, set date. e.g: set_date(2010,2,28))
  161. FINSH_FUNCTION_EXPORT(set_time, set time. e.g: set_time(23,59,59))
  162. #if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH)
  163. static void date(uint8_t argc, char **argv)
  164. {
  165. if (argc == 1)
  166. {
  167. time_t now;
  168. /* output current time */
  169. now = time(RT_NULL);
  170. rt_kprintf("%s", ctime(&now));
  171. }
  172. else if (argc >= 7)
  173. {
  174. /* set time and date */
  175. uint16_t year;
  176. uint8_t month, day, hour, min, sec;
  177. year = atoi(argv[1]);
  178. month = atoi(argv[2]);
  179. day = atoi(argv[3]);
  180. hour = atoi(argv[4]);
  181. min = atoi(argv[5]);
  182. sec = atoi(argv[6]);
  183. if (year > 2099 || year < 2000)
  184. {
  185. rt_kprintf("year is out of range [2000-2099]\n");
  186. return;
  187. }
  188. if (month == 0 || month > 12)
  189. {
  190. rt_kprintf("month is out of range [1-12]\n");
  191. return;
  192. }
  193. if (day == 0 || day > 31)
  194. {
  195. rt_kprintf("day is out of range [1-31]\n");
  196. return;
  197. }
  198. if (hour > 23)
  199. {
  200. rt_kprintf("hour is out of range [0-23]\n");
  201. return;
  202. }
  203. if (min > 59)
  204. {
  205. rt_kprintf("minute is out of range [0-59]\n");
  206. return;
  207. }
  208. if (sec > 59)
  209. {
  210. rt_kprintf("second is out of range [0-59]\n");
  211. return;
  212. }
  213. set_time(hour, min, sec);
  214. set_date(year, month, day);
  215. }
  216. else
  217. {
  218. rt_kprintf("please input: date [year month day hour min sec] or date\n");
  219. rt_kprintf("e.g: date 2018 01 01 23 59 59 or date\n");
  220. }
  221. }
  222. MSH_CMD_EXPORT(date, get date and time or set [year month day hour min sec]);
  223. #endif /* defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) */
  224. #endif /* RT_USING_FINSH */
  225. #endif /* RT_USING_RTC */