rtc.c 4.7 KB

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