rtc.c 4.2 KB

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