1
0

rtc.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. /* Using NTP auto sync RTC time */
  31. #ifdef RTC_SYNC_USING_NTP
  32. /* NTP first sync delay time for network connect, unit: second */
  33. #ifndef RTC_NTP_FIRST_SYNC_DELAY
  34. #define RTC_NTP_FIRST_SYNC_DELAY (30)
  35. #endif
  36. /* NTP sync period, unit: second */
  37. #ifndef RTC_NTP_SYNC_PERIOD
  38. #define RTC_NTP_SYNC_PERIOD (1L*60L*60L)
  39. #endif
  40. #endif /* RTC_SYNC_USING_NTP */
  41. /**
  42. * Returns the current time.
  43. *
  44. * @param time_t * t the timestamp pointer, if not used, keep NULL.
  45. *
  46. * @return time_t return timestamp current.
  47. *
  48. */
  49. /* for IAR 6.2 later Compiler */
  50. #if defined (__IAR_SYSTEMS_ICC__) && (__VER__) >= 6020000
  51. #pragma module_name = "?time"
  52. time_t (__time32)(time_t *t) /* Only supports 32-bit timestamp */
  53. #else
  54. time_t time(time_t *t)
  55. #endif
  56. {
  57. static rt_device_t device = RT_NULL;
  58. time_t time_now = 0;
  59. /* optimization: find rtc device only first. */
  60. if (device == RT_NULL)
  61. {
  62. device = rt_device_find("rtc");
  63. }
  64. /* read timestamp from RTC device. */
  65. if (device != RT_NULL)
  66. {
  67. if (rt_device_open(device, 0) == RT_EOK)
  68. {
  69. rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time_now);
  70. rt_device_close(device);
  71. }
  72. }
  73. /* if t is not NULL, write timestamp to *t */
  74. if (t != RT_NULL)
  75. {
  76. *t = time_now;
  77. }
  78. return time_now;
  79. }
  80. /**
  81. * Set system date(time not modify).
  82. *
  83. * @param rt_uint32_t year e.g: 2012.
  84. * @param rt_uint32_t month e.g: 12 (1~12).
  85. * @param rt_uint32_t day e.g: 31.
  86. *
  87. * @return rt_err_t if set success, return RT_EOK.
  88. *
  89. */
  90. rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day)
  91. {
  92. time_t now;
  93. struct tm *p_tm;
  94. struct tm tm_new;
  95. rt_device_t device;
  96. rt_err_t ret = -RT_ERROR;
  97. /* get current time */
  98. now = time(RT_NULL);
  99. /* lock scheduler. */
  100. rt_enter_critical();
  101. /* converts calendar time time into local time. */
  102. p_tm = localtime(&now);
  103. /* copy the statically located variable */
  104. memcpy(&tm_new, p_tm, sizeof(struct tm));
  105. /* unlock scheduler. */
  106. rt_exit_critical();
  107. /* update date. */
  108. tm_new.tm_year = year - 1900;
  109. tm_new.tm_mon = month - 1; /* tm_mon: 0~11 */
  110. tm_new.tm_mday = day;
  111. /* converts the local time in time to calendar time. */
  112. now = mktime(&tm_new);
  113. device = rt_device_find("rtc");
  114. if (device == RT_NULL)
  115. {
  116. return -RT_ERROR;
  117. }
  118. /* update to RTC device. */
  119. ret = rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
  120. return ret;
  121. }
  122. /**
  123. * Set system time(date not modify).
  124. *
  125. * @param rt_uint32_t hour e.g: 0~23.
  126. * @param rt_uint32_t minute e.g: 0~59.
  127. * @param rt_uint32_t second e.g: 0~59.
  128. *
  129. * @return rt_err_t if set success, return RT_EOK.
  130. *
  131. */
  132. rt_err_t set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second)
  133. {
  134. time_t now;
  135. struct tm *p_tm;
  136. struct tm tm_new;
  137. rt_device_t device;
  138. rt_err_t ret = -RT_ERROR;
  139. /* get current time */
  140. now = time(RT_NULL);
  141. /* lock scheduler. */
  142. rt_enter_critical();
  143. /* converts calendar time time into local time. */
  144. p_tm = localtime(&now);
  145. /* copy the statically located variable */
  146. memcpy(&tm_new, p_tm, sizeof(struct tm));
  147. /* unlock scheduler. */
  148. rt_exit_critical();
  149. /* update time. */
  150. tm_new.tm_hour = hour;
  151. tm_new.tm_min = minute;
  152. tm_new.tm_sec = second;
  153. /* converts the local time in time to calendar time. */
  154. now = mktime(&tm_new);
  155. device = rt_device_find("rtc");
  156. if (device == RT_NULL)
  157. {
  158. return -RT_ERROR;
  159. }
  160. /* update to RTC device. */
  161. ret = rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
  162. return ret;
  163. }
  164. #ifdef RTC_SYNC_USING_NTP
  165. static void ntp_sync_thread_enrty(void *param)
  166. {
  167. extern time_t ntp_sync_to_rtc(void);
  168. /* first sync delay for network connect */
  169. rt_thread_delay(RTC_NTP_FIRST_SYNC_DELAY * RT_TICK_PER_SECOND);
  170. while (1)
  171. {
  172. ntp_sync_to_rtc();
  173. rt_thread_delay(RTC_NTP_SYNC_PERIOD * RT_TICK_PER_SECOND);
  174. }
  175. }
  176. int rt_rtc_ntp_sync_init(void)
  177. {
  178. static rt_bool_t init_ok = RT_FALSE;
  179. rt_thread_t thread;
  180. if (init_ok)
  181. {
  182. return 0;
  183. }
  184. thread = rt_thread_create("ntp_sync", ntp_sync_thread_enrty, RT_NULL, 1536, 26, 2);
  185. if (thread)
  186. {
  187. rt_thread_startup(thread);
  188. }
  189. else
  190. {
  191. return -RT_ENOMEM;
  192. }
  193. init_ok = RT_TRUE;
  194. }
  195. INIT_COMPONENT_EXPORT(rt_rtc_ntp_sync_init);
  196. #endif /* RTC_SYNC_USING_NTP */
  197. #ifdef RT_USING_FINSH
  198. #include <finsh.h>
  199. #include <rtdevice.h>
  200. void list_date(void)
  201. {
  202. time_t now;
  203. now = time(RT_NULL);
  204. rt_kprintf("%s\n", ctime(&now));
  205. }
  206. FINSH_FUNCTION_EXPORT(list_date, show date and time.)
  207. FINSH_FUNCTION_EXPORT(set_date, set date. e.g: set_date(2010,2,28))
  208. FINSH_FUNCTION_EXPORT(set_time, set time. e.g: set_time(23,59,59))
  209. #endif /* RT_USING_FINSH */