1 | /*--------------------------------------------------------------------------------- |
---|
2 | Copyright (C) 2005 |
---|
3 | Michael Noland (Joat) |
---|
4 | Jason Rogers (Dovoto) |
---|
5 | Dave Murphy (WinterMute) |
---|
6 | |
---|
7 | This software is provided 'as-is', without any express or implied |
---|
8 | warranty. In no event will the authors be held liable for any |
---|
9 | damages arising from the use of this software. |
---|
10 | |
---|
11 | Permission is granted to anyone to use this software for any |
---|
12 | purpose, including commercial applications, and to alter it and |
---|
13 | redistribute it freely, subject to the following restrictions: |
---|
14 | |
---|
15 | 1. The origin of this software must not be misrepresented; you |
---|
16 | must not claim that you wrote the original software. If you use |
---|
17 | this software in a product, an acknowledgment in the product |
---|
18 | documentation would be appreciated but is not required. |
---|
19 | 2. Altered source versions must be plainly marked as such, and |
---|
20 | must not be misrepresented as being the original software. |
---|
21 | 3. This notice may not be removed or altered from any source |
---|
22 | distribution. |
---|
23 | |
---|
24 | ---------------------------------------------------------------------------------*/ |
---|
25 | |
---|
26 | #include "nds/bios.h" |
---|
27 | #include "nds/arm7/clock.h" |
---|
28 | #include "nds/interrupts.h" |
---|
29 | #include "nds/ipc.h" |
---|
30 | |
---|
31 | #include <time.h> |
---|
32 | |
---|
33 | /* |
---|
34 | * forward declaration to avoid warning |
---|
35 | */ |
---|
36 | void syncRTC(void); |
---|
37 | |
---|
38 | // Delay (in swiDelay units) for each bit transfer |
---|
39 | #define RTC_DELAY 48 |
---|
40 | |
---|
41 | // Pin defines on RTC_CR |
---|
42 | #define CS_0 (1<<6) |
---|
43 | #define CS_1 ((1<<6) | (1<<2)) |
---|
44 | #define SCK_0 (1<<5) |
---|
45 | #define SCK_1 ((1<<5) | (1<<1)) |
---|
46 | #define SIO_0 (1<<4) |
---|
47 | #define SIO_1 ((1<<4) | (1<<0)) |
---|
48 | #define SIO_out (1<<4) |
---|
49 | #define SIO_in (1) |
---|
50 | |
---|
51 | //--------------------------------------------------------------------------------- |
---|
52 | void BCDToInteger(uint8 * data, uint32 length) { |
---|
53 | //--------------------------------------------------------------------------------- |
---|
54 | u32 i; |
---|
55 | for (i = 0; i < length; i++) { |
---|
56 | data[i] = (data[i] & 0xF) + ((data[i] & 0xF0)>>4)*10; |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | //--------------------------------------------------------------------------------- |
---|
62 | void integerToBCD(uint8 * data, uint32 length) { |
---|
63 | //--------------------------------------------------------------------------------- |
---|
64 | u32 i; |
---|
65 | for (i = 0; i < length; i++) { |
---|
66 | int high, low; |
---|
67 | swiDivMod(data[i], 10, &high, &low); |
---|
68 | data[i] = (high<<4) | low; |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | //--------------------------------------------------------------------------------- |
---|
73 | void rtcTransaction(uint8 * command, uint32 commandLength, uint8 * result, uint32 resultLength) { |
---|
74 | //--------------------------------------------------------------------------------- |
---|
75 | uint32 bit; |
---|
76 | uint8 data; |
---|
77 | |
---|
78 | // Raise CS |
---|
79 | RTC_CR8 = CS_0 | SCK_1 | SIO_1; |
---|
80 | swiDelay(RTC_DELAY); |
---|
81 | RTC_CR8 = CS_1 | SCK_1 | SIO_1; |
---|
82 | swiDelay(RTC_DELAY); |
---|
83 | |
---|
84 | // Write command byte (high bit first) |
---|
85 | data = *command++; |
---|
86 | |
---|
87 | for (bit = 0; bit < 8; bit++) { |
---|
88 | RTC_CR8 = CS_1 | SCK_0 | SIO_out | (data>>7); |
---|
89 | swiDelay(RTC_DELAY); |
---|
90 | |
---|
91 | RTC_CR8 = CS_1 | SCK_1 | SIO_out | (data>>7); |
---|
92 | swiDelay(RTC_DELAY); |
---|
93 | |
---|
94 | data = data << 1; |
---|
95 | } |
---|
96 | // Write parameter bytes (low bit first) |
---|
97 | for ( ; commandLength > 1; commandLength--) { |
---|
98 | data = *command++; |
---|
99 | |
---|
100 | for (bit = 0; bit < 8; bit++) { |
---|
101 | RTC_CR8 = CS_1 | SCK_0 | SIO_out | (data & 1); |
---|
102 | swiDelay(RTC_DELAY); |
---|
103 | |
---|
104 | RTC_CR8 = CS_1 | SCK_1 | SIO_out | (data & 1); |
---|
105 | swiDelay(RTC_DELAY); |
---|
106 | |
---|
107 | data = data >> 1; |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | // Read result bytes (low bit first) |
---|
112 | for ( ; resultLength > 0; resultLength--) { |
---|
113 | data = 0; |
---|
114 | |
---|
115 | for (bit = 0; bit < 8; bit++) { |
---|
116 | RTC_CR8 = CS_1 | SCK_0; |
---|
117 | swiDelay(RTC_DELAY); |
---|
118 | |
---|
119 | RTC_CR8 = CS_1 | SCK_1; |
---|
120 | swiDelay(RTC_DELAY); |
---|
121 | |
---|
122 | if (RTC_CR8 & SIO_in) data |= (1 << bit); |
---|
123 | } |
---|
124 | *result++ = data; |
---|
125 | } |
---|
126 | |
---|
127 | // Finish up by dropping CS low |
---|
128 | RTC_CR8 = CS_0 | SCK_1; |
---|
129 | swiDelay(RTC_DELAY); |
---|
130 | } |
---|
131 | |
---|
132 | |
---|
133 | //--------------------------------------------------------------------------------- |
---|
134 | void rtcReset(void) { |
---|
135 | //--------------------------------------------------------------------------------- |
---|
136 | uint8 status; |
---|
137 | uint8 command[2]; |
---|
138 | |
---|
139 | // Read the first status register |
---|
140 | command[0] = READ_STATUS_REG1; |
---|
141 | rtcTransaction(command, 1, &status, 1); |
---|
142 | |
---|
143 | // Reset the RTC if needed |
---|
144 | if (status & (STATUS_POC | STATUS_BLD)) { |
---|
145 | command[0] = WRITE_STATUS_REG1; |
---|
146 | command[1] = status | STATUS_RESET; |
---|
147 | rtcTransaction(command, 2, 0, 0); |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | |
---|
152 | //--------------------------------------------------------------------------------- |
---|
153 | void rtcGetTimeAndDate(uint8 * time) { |
---|
154 | //--------------------------------------------------------------------------------- |
---|
155 | uint8 command, status; |
---|
156 | |
---|
157 | command = READ_TIME_AND_DATE; |
---|
158 | rtcTransaction(&command, 1, time, 7); |
---|
159 | |
---|
160 | command = READ_STATUS_REG1; |
---|
161 | rtcTransaction(&command, 1, &status, 1); |
---|
162 | |
---|
163 | if ( status & STATUS_24HRS ) { |
---|
164 | time[4] &= 0x3f; |
---|
165 | } else { |
---|
166 | |
---|
167 | } |
---|
168 | BCDToInteger(time,7); |
---|
169 | } |
---|
170 | |
---|
171 | //--------------------------------------------------------------------------------- |
---|
172 | void rtcSetTimeAndDate(uint8 * time) { |
---|
173 | //--------------------------------------------------------------------------------- |
---|
174 | uint8 command[8]; |
---|
175 | |
---|
176 | int i; |
---|
177 | for ( i=0; i< 8; i++ ) { |
---|
178 | command[i+1] = time[i]; |
---|
179 | } |
---|
180 | command[0] = WRITE_TIME_AND_DATE; |
---|
181 | // fixme: range checking on the data we tell it |
---|
182 | rtcTransaction(command, 8, 0, 0); |
---|
183 | } |
---|
184 | |
---|
185 | //--------------------------------------------------------------------------------- |
---|
186 | void rtcGetTime(uint8 * time) { |
---|
187 | //--------------------------------------------------------------------------------- |
---|
188 | uint8 command, status; |
---|
189 | |
---|
190 | command = READ_TIME; |
---|
191 | rtcTransaction(&command, 1, time, 3); |
---|
192 | |
---|
193 | command = READ_STATUS_REG1; |
---|
194 | rtcTransaction(&command, 1, &status, 1); |
---|
195 | if ( status & STATUS_24HRS ) { |
---|
196 | time[0] &= 0x3f; |
---|
197 | } else { |
---|
198 | |
---|
199 | } |
---|
200 | BCDToInteger(time,3); |
---|
201 | |
---|
202 | } |
---|
203 | |
---|
204 | //--------------------------------------------------------------------------------- |
---|
205 | void rtcSetTime(uint8 * time) { |
---|
206 | //--------------------------------------------------------------------------------- |
---|
207 | uint8 command[4]; |
---|
208 | |
---|
209 | int i; |
---|
210 | for ( i=0; i< 3; i++ ) { |
---|
211 | command[i+1] = time[i]; |
---|
212 | } |
---|
213 | command[0] = WRITE_TIME; |
---|
214 | // fixme: range checking on the data we tell it |
---|
215 | rtcTransaction(command, 4, 0, 0); |
---|
216 | } |
---|
217 | |
---|
218 | //--------------------------------------------------------------------------------- |
---|
219 | void syncRTC(void) { |
---|
220 | //--------------------------------------------------------------------------------- |
---|
221 | if (++IPC->time.rtc.seconds == 60 ) { |
---|
222 | IPC->time.rtc.seconds = 0; |
---|
223 | if (++IPC->time.rtc.minutes == 60) { |
---|
224 | IPC->time.rtc.minutes = 0; |
---|
225 | if (++IPC->time.rtc.hours == 24) { |
---|
226 | rtcGetTimeAndDate((uint8 *)&(IPC->time.rtc.year)); |
---|
227 | } |
---|
228 | } |
---|
229 | } |
---|
230 | |
---|
231 | IPC->unixTime++; |
---|
232 | } |
---|
233 | |
---|
234 | //--------------------------------------------------------------------------------- |
---|
235 | void initClockIRQ() { |
---|
236 | //--------------------------------------------------------------------------------- |
---|
237 | |
---|
238 | REG_RCNT = 0x8100; |
---|
239 | irqSet(IRQ_NETWORK, syncRTC); |
---|
240 | // Reset the clock if needed |
---|
241 | rtcReset(); |
---|
242 | |
---|
243 | uint8 command[4]; |
---|
244 | command[0] = READ_STATUS_REG2; |
---|
245 | rtcTransaction(command, 1, &command[1], 1); |
---|
246 | |
---|
247 | command[0] = WRITE_STATUS_REG2; |
---|
248 | command[1] = 0x41; |
---|
249 | rtcTransaction(command, 2, 0, 0); |
---|
250 | |
---|
251 | command[0] = WRITE_INT_REG1; |
---|
252 | command[1] = 0x01; |
---|
253 | rtcTransaction(command, 2, 0, 0); |
---|
254 | |
---|
255 | command[0] = WRITE_INT_REG2; |
---|
256 | command[1] = 0x00; |
---|
257 | command[2] = 0x21; |
---|
258 | command[3] = 0x35; |
---|
259 | rtcTransaction(command, 4, 0, 0); |
---|
260 | |
---|
261 | // Read all time settings on first start |
---|
262 | rtcGetTimeAndDate((uint8 *)&(IPC->time.rtc.year)); |
---|
263 | |
---|
264 | |
---|
265 | struct tm currentTime; |
---|
266 | |
---|
267 | currentTime.tm_sec = IPC->time.rtc.seconds; |
---|
268 | currentTime.tm_min = IPC->time.rtc.minutes; |
---|
269 | currentTime.tm_hour = IPC->time.rtc.hours; |
---|
270 | |
---|
271 | currentTime.tm_mday = IPC->time.rtc.day; |
---|
272 | currentTime.tm_mon = IPC->time.rtc.month - 1; |
---|
273 | currentTime.tm_year = IPC->time.rtc.year + 100; |
---|
274 | |
---|
275 | currentTime.tm_isdst = -1; |
---|
276 | |
---|
277 | IPC->unixTime = mktime(¤tTime); |
---|
278 | } |
---|
279 | |
---|