rtc.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 <time.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include <rtthread.h>
  17. #ifdef RT_USING_RTC
  18. /**
  19. * Set system date(time not modify, local timezone).
  20. *
  21. * @param rt_uint32_t year e.g: 2012.
  22. * @param rt_uint32_t month e.g: 12 (1~12).
  23. * @param rt_uint32_t day e.g: 31.
  24. *
  25. * @return rt_err_t if set success, return RT_EOK.
  26. *
  27. */
  28. rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day)
  29. {
  30. time_t now;
  31. struct tm *p_tm;
  32. struct tm tm_new;
  33. rt_device_t device;
  34. rt_err_t ret = -RT_ERROR;
  35. /* get current time */
  36. now = time(RT_NULL);
  37. /* lock scheduler. */
  38. rt_enter_critical();
  39. /* converts calendar time into local time. */
  40. p_tm = localtime(&now);
  41. /* copy the statically located variable */
  42. rt_memcpy(&tm_new, p_tm, sizeof(struct tm));
  43. /* unlock scheduler. */
  44. rt_exit_critical();
  45. /* update date. */
  46. tm_new.tm_year = year - 1900;
  47. tm_new.tm_mon = month - 1; /* tm_mon: 0~11 */
  48. tm_new.tm_mday = day;
  49. /* converts the local time into the calendar time. */
  50. now = mktime(&tm_new);
  51. device = rt_device_find("rtc");
  52. if (device == RT_NULL)
  53. {
  54. return -RT_ERROR;
  55. }
  56. /* update to RTC device. */
  57. ret = rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
  58. return ret;
  59. }
  60. /**
  61. * Set system time(date not modify, local timezone).
  62. *
  63. * @param rt_uint32_t hour e.g: 0~23.
  64. * @param rt_uint32_t minute e.g: 0~59.
  65. * @param rt_uint32_t second e.g: 0~59.
  66. *
  67. * @return rt_err_t if set success, return RT_EOK.
  68. *
  69. */
  70. rt_err_t set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second)
  71. {
  72. time_t now;
  73. struct tm *p_tm;
  74. struct tm tm_new;
  75. rt_device_t device;
  76. rt_err_t ret = -RT_ERROR;
  77. /* get current time */
  78. now = time(RT_NULL);
  79. /* lock scheduler. */
  80. rt_enter_critical();
  81. /* converts calendar time into local time. */
  82. p_tm = localtime(&now);
  83. /* copy the statically located variable */
  84. rt_memcpy(&tm_new, p_tm, sizeof(struct tm));
  85. /* unlock scheduler. */
  86. rt_exit_critical();
  87. /* update time. */
  88. tm_new.tm_hour = hour;
  89. tm_new.tm_min = minute;
  90. tm_new.tm_sec = second;
  91. /* converts the local time into the calendar time. */
  92. now = mktime(&tm_new);
  93. device = rt_device_find("rtc");
  94. if (device == RT_NULL)
  95. {
  96. return -RT_ERROR;
  97. }
  98. /* update to RTC device. */
  99. ret = rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
  100. return ret;
  101. }
  102. #ifdef FINSH_USING_MSH
  103. #include <finsh.h>
  104. /**
  105. * get date and time or set (local timezone) [year month day hour min sec]
  106. */
  107. static void date(uint8_t argc, char **argv)
  108. {
  109. if (argc == 1)
  110. {
  111. time_t now;
  112. /* output current time */
  113. now = time(RT_NULL);
  114. rt_kprintf("%.*s", 25, ctime(&now));
  115. }
  116. else if (argc >= 7)
  117. {
  118. /* set time and date */
  119. uint16_t year;
  120. uint8_t month, day, hour, min, sec;
  121. year = atoi(argv[1]);
  122. month = atoi(argv[2]);
  123. day = atoi(argv[3]);
  124. hour = atoi(argv[4]);
  125. min = atoi(argv[5]);
  126. sec = atoi(argv[6]);
  127. if (year > 2099 || year < 2000)
  128. {
  129. rt_kprintf("year is out of range [2000-2099]\n");
  130. return;
  131. }
  132. if (month == 0 || month > 12)
  133. {
  134. rt_kprintf("month is out of range [1-12]\n");
  135. return;
  136. }
  137. if (day == 0 || day > 31)
  138. {
  139. rt_kprintf("day is out of range [1-31]\n");
  140. return;
  141. }
  142. if (hour > 23)
  143. {
  144. rt_kprintf("hour is out of range [0-23]\n");
  145. return;
  146. }
  147. if (min > 59)
  148. {
  149. rt_kprintf("minute is out of range [0-59]\n");
  150. return;
  151. }
  152. if (sec > 59)
  153. {
  154. rt_kprintf("second is out of range [0-59]\n");
  155. return;
  156. }
  157. set_time(hour, min, sec);
  158. set_date(year, month, day);
  159. }
  160. else
  161. {
  162. rt_kprintf("please input: date [year month day hour min sec] or date\n");
  163. rt_kprintf("e.g: date 2018 01 01 23 59 59 or date\n");
  164. }
  165. }
  166. MSH_CMD_EXPORT(date, get date and time or set (local timezone) [year month day hour min sec])
  167. #endif /* FINSH_USING_MSH */
  168. #endif /* RT_USING_RTC */