rtc.c 4.6 KB

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