diff options
Diffstat (limited to 'firmware/common')
-rw-r--r-- | firmware/common/timefuncs.c | 77 |
1 files changed, 12 insertions, 65 deletions
diff --git a/firmware/common/timefuncs.c b/firmware/common/timefuncs.c index 405bce4f5f..eef804d302 100644 --- a/firmware/common/timefuncs.c +++ b/firmware/common/timefuncs.c | |||
@@ -27,9 +27,7 @@ | |||
27 | #include "timefuncs.h" | 27 | #include "timefuncs.h" |
28 | #include "debug.h" | 28 | #include "debug.h" |
29 | 29 | ||
30 | #if !defined SIMULATOR || !CONFIG_RTC | ||
31 | static struct tm tm; | 30 | static struct tm tm; |
32 | #endif /* !defined SIMULATOR || !CONFIG_RTC */ | ||
33 | 31 | ||
34 | #if !CONFIG_RTC | 32 | #if !CONFIG_RTC |
35 | static void fill_default_tm(struct tm *tm) | 33 | static void fill_default_tm(struct tm *tm) |
@@ -62,7 +60,6 @@ bool valid_time(const struct tm *tm) | |||
62 | 60 | ||
63 | struct tm *get_time(void) | 61 | struct tm *get_time(void) |
64 | { | 62 | { |
65 | #ifndef SIMULATOR | ||
66 | #if CONFIG_RTC | 63 | #if CONFIG_RTC |
67 | static long timeout = 0; | 64 | static long timeout = 0; |
68 | 65 | ||
@@ -71,76 +68,25 @@ struct tm *get_time(void) | |||
71 | { | 68 | { |
72 | /* Once per second, 1/10th of a second off */ | 69 | /* Once per second, 1/10th of a second off */ |
73 | timeout = HZ * (current_tick / HZ + 1) + HZ / 5; | 70 | timeout = HZ * (current_tick / HZ + 1) + HZ / 5; |
74 | #if CONFIG_RTC != RTC_JZ47XX | 71 | rtc_read_datetime(&tm); |
75 | char rtcbuf[7]; | ||
76 | rtc_read_datetime(rtcbuf); | ||
77 | |||
78 | tm.tm_sec = ((rtcbuf[0] & 0x70) >> 4) * 10 + (rtcbuf[0] & 0x0f); | ||
79 | tm.tm_min = ((rtcbuf[1] & 0x70) >> 4) * 10 + (rtcbuf[1] & 0x0f); | ||
80 | tm.tm_hour = ((rtcbuf[2] & 0x30) >> 4) * 10 + (rtcbuf[2] & 0x0f); | ||
81 | tm.tm_wday = rtcbuf[3] & 0x07; | ||
82 | tm.tm_mday = ((rtcbuf[4] & 0x30) >> 4) * 10 + (rtcbuf[4] & 0x0f); | ||
83 | tm.tm_mon = ((rtcbuf[5] & 0x10) >> 4) * 10 + (rtcbuf[5] & 0x0f) - 1; | ||
84 | #ifdef IRIVER_H300_SERIES | ||
85 | /* Special kludge to coexist with the iriver firmware. The iriver firmware | ||
86 | stores the date as 1965+nn, and allows a range of 1980..2064. We use | ||
87 | 1964+nn here to make leap years work correctly, so the date will be one | ||
88 | year off in the iriver firmware but at least won't be reset anymore. */ | ||
89 | tm.tm_year = ((rtcbuf[6] & 0xf0) >> 4) * 10 + (rtcbuf[6] & 0x0f) + 64; | ||
90 | #else /* Not IRIVER_H300_SERIES */ | ||
91 | tm.tm_year = ((rtcbuf[6] & 0xf0) >> 4) * 10 + (rtcbuf[6] & 0x0f) + 100; | ||
92 | #endif /* IRIVER_H300_SERIES */ | ||
93 | 72 | ||
94 | tm.tm_yday = 0; /* Not implemented for now */ | 73 | tm.tm_yday = 0; /* Not implemented for now */ |
95 | tm.tm_isdst = -1; /* Not implemented for now */ | 74 | tm.tm_isdst = -1; /* Not implemented for now */ |
96 | #else /* CONFIG_RTC == RTC_JZ47XX */ | ||
97 | rtc_read_datetime((unsigned char*)&tm); | ||
98 | #endif /* CONFIG_RTC */ | ||
99 | } | 75 | } |
100 | #else /* No RTC */ | 76 | #else /* No RTC */ |
101 | fill_default_tm(&tm); | 77 | fill_default_tm(&tm); |
102 | #endif /* RTC */ | 78 | #endif /* RTC */ |
103 | return &tm; | 79 | return &tm; |
104 | |||
105 | #else /* SIMULATOR */ | ||
106 | #if CONFIG_RTC | ||
107 | time_t now = time(NULL); | ||
108 | return localtime(&now); | ||
109 | #else /* Simulator, no RTC */ | ||
110 | fill_default_tm(&tm); | ||
111 | return &tm; | ||
112 | #endif | ||
113 | #endif /* SIMULATOR */ | ||
114 | } | 80 | } |
115 | 81 | ||
116 | int set_time(const struct tm *tm) | 82 | int set_time(const struct tm *tm) |
117 | { | 83 | { |
118 | #if CONFIG_RTC | 84 | #if CONFIG_RTC |
119 | int rc; | 85 | int rc; |
120 | #if CONFIG_RTC != RTC_JZ47XX | 86 | |
121 | char rtcbuf[7]; | ||
122 | #endif | ||
123 | |||
124 | if (valid_time(tm)) | 87 | if (valid_time(tm)) |
125 | { | 88 | { |
126 | #if CONFIG_RTC != RTC_JZ47XX | 89 | rc = rtc_write_datetime(tm); |
127 | rtcbuf[0]=((tm->tm_sec/10) << 4) | (tm->tm_sec%10); | ||
128 | rtcbuf[1]=((tm->tm_min/10) << 4) | (tm->tm_min%10); | ||
129 | rtcbuf[2]=((tm->tm_hour/10) << 4) | (tm->tm_hour%10); | ||
130 | rtcbuf[3]=tm->tm_wday; | ||
131 | rtcbuf[4]=((tm->tm_mday/10) << 4) | (tm->tm_mday%10); | ||
132 | rtcbuf[5]=(((tm->tm_mon+1)/10) << 4) | ((tm->tm_mon+1)%10); | ||
133 | #ifdef IRIVER_H300_SERIES | ||
134 | /* Iriver firmware compatibility kludge, see get_time(). */ | ||
135 | rtcbuf[6]=(((tm->tm_year-64)/10) << 4) | ((tm->tm_year-64)%10); | ||
136 | #else | ||
137 | rtcbuf[6]=(((tm->tm_year-100)/10) << 4) | ((tm->tm_year-100)%10); | ||
138 | #endif | ||
139 | |||
140 | rc = rtc_write_datetime(rtcbuf); | ||
141 | #else | ||
142 | rc = rtc_write_datetime((unsigned char*)tm); | ||
143 | #endif | ||
144 | 90 | ||
145 | if (rc < 0) | 91 | if (rc < 0) |
146 | return -1; | 92 | return -1; |
@@ -151,10 +97,10 @@ int set_time(const struct tm *tm) | |||
151 | { | 97 | { |
152 | return -2; | 98 | return -2; |
153 | } | 99 | } |
154 | #else | 100 | #else /* No RTC */ |
155 | (void)tm; | 101 | (void)tm; |
156 | return 0; | 102 | return 0; |
157 | #endif | 103 | #endif /* RTC */ |
158 | } | 104 | } |
159 | 105 | ||
160 | #if CONFIG_RTC | 106 | #if CONFIG_RTC |
@@ -196,11 +142,12 @@ time_t mktime(struct tm *t) | |||
196 | 142 | ||
197 | void set_day_of_week(struct tm *tm) | 143 | void set_day_of_week(struct tm *tm) |
198 | { | 144 | { |
199 | int y=tm->tm_year+1900; | 145 | int y=tm->tm_year+1900; |
200 | int d=tm->tm_mday; | 146 | int d=tm->tm_mday; |
201 | int m=tm->tm_mon; | 147 | int m=tm->tm_mon; |
202 | static const char mo[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 }; | 148 | static const char mo[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 }; |
203 | 149 | ||
204 | if(m == 0 || m == 1) y--; | 150 | if(m == 0 || m == 1) y--; |
205 | tm->tm_wday = (d + mo[m] + y + y/4 - y/100 + y/400) % 7; | 151 | tm->tm_wday = (d + mo[m] + y + y/4 - y/100 + y/400) % 7; |
206 | } | 152 | } |
153 | |||