rtc.c 6.8 KB

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