rtc.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #include <stdio.h>
  2. #include "FreeRTOS.h"
  3. #include "chip.h"
  4. #include "errno.h"
  5. #include "access_module.h"
  6. /* 2020-01-01 Wednesday */
  7. static struct rtc_time default_tm = {
  8. .tm_sec = 0,
  9. .tm_min = 0,
  10. .tm_hour = 0,
  11. .tm_mday = 1,
  12. .tm_mon = 0,
  13. .tm_year = 120,
  14. .tm_wday = 3,
  15. .tm_yday = 1,
  16. };
  17. static const unsigned char rtc_days_in_month[] = {
  18. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  19. };
  20. static const unsigned short rtc_ydays[2][13] = {
  21. /* Normal years */
  22. { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
  23. /* Leap years */
  24. { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
  25. };
  26. #define LEAPS_THRU_END_OF(y) ((y)/4 - (y)/100 + (y)/400)
  27. /*
  28. * The number of days in the month.
  29. */
  30. int rtc_month_days(unsigned int month, unsigned int year)
  31. {
  32. return rtc_days_in_month[month] + (is_leap_year(year) && month == 1);
  33. }
  34. /*
  35. * The number of days since January 1. (0 to 365)
  36. */
  37. int rtc_year_days(unsigned int day, unsigned int month, unsigned int year)
  38. {
  39. return rtc_ydays[is_leap_year(year)][month] + day-1;
  40. }
  41. /*
  42. * rtc_time_to_tm - Converts time to rtc_time.
  43. * Convert seconds since 01-01-1970 00:00:00 to Gregorian date.
  44. */
  45. void rtc_time_to_tm(uint32_t time, struct rtc_time *tm)
  46. {
  47. unsigned int month, year;
  48. unsigned long secs;
  49. int days;
  50. /* time must be positive */
  51. days = time / 86400;
  52. secs = time - (unsigned int) days * 86400;
  53. /* day of the week, 1970-01-01 was a Thursday */
  54. tm->tm_wday = (days + 4) % 7;
  55. year = 1970 + days / 365;
  56. days -= (year - 1970) * 365
  57. + LEAPS_THRU_END_OF(year - 1)
  58. - LEAPS_THRU_END_OF(1970 - 1);
  59. if (days < 0) {
  60. year -= 1;
  61. days += 365 + is_leap_year(year);
  62. }
  63. tm->tm_year = year - 1900;
  64. tm->tm_yday = days + 1;
  65. for (month = 0; month < 11; month++) {
  66. int newdays;
  67. newdays = days - rtc_month_days(month, year);
  68. if (newdays < 0)
  69. break;
  70. days = newdays;
  71. }
  72. tm->tm_mon = month;
  73. tm->tm_mday = days + 1;
  74. tm->tm_hour = secs / 3600;
  75. secs -= tm->tm_hour * 3600;
  76. tm->tm_min = secs / 60;
  77. tm->tm_sec = secs - tm->tm_min * 60;
  78. }
  79. /*
  80. * Does the rtc_time represent a valid date/time?
  81. */
  82. int rtc_valid_tm(struct rtc_time *tm)
  83. {
  84. if (tm->tm_year < 70
  85. || ((unsigned)tm->tm_mon) >= 12
  86. || tm->tm_mday < 1
  87. || tm->tm_mday > rtc_month_days(tm->tm_mon, tm->tm_year + 1900)
  88. || ((unsigned)tm->tm_hour) >= 24
  89. || ((unsigned)tm->tm_min) >= 60
  90. || ((unsigned)tm->tm_sec) >= 60)
  91. return -EINVAL;
  92. return 0;
  93. }
  94. /*
  95. * mktime - Converts date to seconds.
  96. * Converts Gregorian date to seconds since 1970-01-01 00:00:00.
  97. * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
  98. * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
  99. *
  100. * [For the Julian calendar (which was used in Russia before 1917,
  101. * Britain & colonies before 1752, anywhere else before 1582,
  102. * and is still in use by some communities) leave out the
  103. * -year/100+year/400 terms, and add 10.]
  104. *
  105. * This algorithm was first published by Gauss (I think).
  106. *
  107. * A leap second can be indicated by calling this function with sec as
  108. * 60 (allowable under ISO 8601). The leap second is treated the same
  109. * as the following second since they don't exist in UNIX time.
  110. *
  111. * An encoding of midnight at the end of the day as 24:00:00 - ie. midnight
  112. * tomorrow - (allowable under ISO 8601) is supported.
  113. */
  114. uint32_t mktime(const unsigned int year0, const unsigned int mon0,
  115. const unsigned int day, const unsigned int hour,
  116. const unsigned int min, const unsigned int sec)
  117. {
  118. unsigned int mon = mon0, year = year0;
  119. /* 1..12 -> 11,12,1..10 */
  120. if (0 >= (int) (mon -= 2)) {
  121. mon += 12; /* Puts Feb last since it has leap day */
  122. year -= 1;
  123. }
  124. return ((((uint32_t)
  125. (year/4 - year/100 + year/400 + 367*mon/12 + day) +
  126. year*365 - 719499
  127. )*24 + hour /* now have hours - midnight tomorrow handled here */
  128. )*60 + min /* now have minutes */
  129. )*60 + sec; /* finally seconds */
  130. }
  131. /*
  132. * rtc_tm_to_time - Converts rtc_time to time.
  133. * Convert Gregorian date to seconds since 01-01-1970 00:00:00.
  134. */
  135. uint32_t rtc_tm_to_time(struct rtc_time *tm)
  136. {
  137. return mktime(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  138. tm->tm_hour, tm->tm_min, tm->tm_sec);
  139. }
  140. /*
  141. * rtc_read_time - set the time
  142. * @tm: holds date and time
  143. *
  144. * This function read time and date. On success it will return 0
  145. * otherwise -ve error is returned.
  146. */
  147. static int rtc_read_time(struct rtc_time *tm)
  148. {
  149. unsigned int time;
  150. #ifdef ACCESS_RTC_SUPPORT
  151. AccessRTC(ART_GET_TIME, &time);
  152. #else
  153. //
  154. #endif
  155. rtc_time_to_tm(time, tm);
  156. return 0;
  157. }
  158. /*
  159. * rtc_set_time - set the time
  160. * @tm: holds date and time
  161. *
  162. * This function set time and date. On success it will return 0
  163. * otherwise -ve error is returned.
  164. */
  165. static int rtc_set_time(struct rtc_time *tm)
  166. {
  167. unsigned int time;
  168. if (rtc_valid_tm(tm) < 0)
  169. return -EINVAL;
  170. /* convert tm to seconds. */
  171. time = rtc_tm_to_time(tm);
  172. #ifdef ACCESS_RTC_SUPPORT
  173. return AccessRTC(ART_SET_TIME, &time);
  174. #else
  175. //
  176. #endif
  177. return 0;
  178. }
  179. int iGetLocalTime(SystemTime_t *tm)
  180. {
  181. if (tm == NULL)
  182. return -1;
  183. rtc_read_time(tm);
  184. tm->tm_year += 1900;
  185. tm->tm_mon += 1;
  186. return 0;
  187. }
  188. int iSetLocalTime(SystemTime_t *tm)
  189. {
  190. if (tm == NULL)
  191. return -1;
  192. tm->tm_year -= 1900;
  193. tm->tm_mon -= 1;
  194. rtc_set_time(tm);
  195. return 0;
  196. }