rtc.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 <sys/time.h>
  14. #include <string.h>
  15. #include <rtthread.h>
  16. #include <drivers/rtc.h>
  17. #ifdef RT_USING_RTC
  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, local timezone).
  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 into local time. */
  51. p_tm = localtime(&now);
  52. /* copy the statically located variable */
  53. rt_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 into the 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, local timezone).
  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 into local time. */
  93. p_tm = localtime(&now);
  94. /* copy the statically located variable */
  95. rt_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 into the 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. /**
  151. * show date and time (local timezone)
  152. */
  153. void list_date(void)
  154. {
  155. time_t now;
  156. now = time(RT_NULL);
  157. rt_kprintf("%.*s\n", 25, ctime(&now));
  158. }
  159. FINSH_FUNCTION_EXPORT(list_date, show date and time (local timezone))
  160. FINSH_FUNCTION_EXPORT(set_date, set date(local timezone) e.g: set_date(2010,2,28))
  161. FINSH_FUNCTION_EXPORT(set_time, set time(local timezone) e.g: set_time(23,59,59))
  162. #if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH)
  163. /**
  164. * get date and time or set (local timezone) [year month day hour min sec]
  165. */
  166. static void date(uint8_t argc, char **argv)
  167. {
  168. if (argc == 1)
  169. {
  170. time_t now;
  171. /* output current time */
  172. now = time(RT_NULL);
  173. rt_kprintf("%.*s", 25, ctime(&now));
  174. }
  175. else if (argc >= 7)
  176. {
  177. /* set time and date */
  178. uint16_t year;
  179. uint8_t month, day, hour, min, sec;
  180. year = atoi(argv[1]);
  181. month = atoi(argv[2]);
  182. day = atoi(argv[3]);
  183. hour = atoi(argv[4]);
  184. min = atoi(argv[5]);
  185. sec = atoi(argv[6]);
  186. if (year > 2099 || year < 2000)
  187. {
  188. rt_kprintf("year is out of range [2000-2099]\n");
  189. return;
  190. }
  191. if (month == 0 || month > 12)
  192. {
  193. rt_kprintf("month is out of range [1-12]\n");
  194. return;
  195. }
  196. if (day == 0 || day > 31)
  197. {
  198. rt_kprintf("day is out of range [1-31]\n");
  199. return;
  200. }
  201. if (hour > 23)
  202. {
  203. rt_kprintf("hour is out of range [0-23]\n");
  204. return;
  205. }
  206. if (min > 59)
  207. {
  208. rt_kprintf("minute is out of range [0-59]\n");
  209. return;
  210. }
  211. if (sec > 59)
  212. {
  213. rt_kprintf("second is out of range [0-59]\n");
  214. return;
  215. }
  216. set_time(hour, min, sec);
  217. set_date(year, month, day);
  218. }
  219. else
  220. {
  221. rt_kprintf("please input: date [year month day hour min sec] or date\n");
  222. rt_kprintf("e.g: date 2018 01 01 23 59 59 or date\n");
  223. }
  224. }
  225. MSH_CMD_EXPORT(list_date, show date and time (local timezone))
  226. MSH_CMD_EXPORT(date, get date and time or set (local timezone) [year month day hour min sec])
  227. #endif /* defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) */
  228. #endif /* RT_USING_FINSH */
  229. #endif /* RT_USING_RTC */