blob: 6996b1368d7361383e68a01072f214c94195a4fa (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
#include <revolution/vi.h>
#include <revolution/os.h>
#include "__vi.h"
static volatile u32 __i2c_ident_flag = 1;
static volatile u32 __i2c_ident_first = 0;
#define busRd32(addr) (*(volatile u32 *)(addr))
#define busWrt32(addr, val) (*(volatile u32 *)(addr)) = (val)
void WaitMicroTime(s32 usec) {
OSTime t = __OSGetSystemTime();
while (((__OSGetSystemTime() - t) * 8) / 486 < usec);
}
static void VICheckI2C(void) {
__i2c_ident_flag = 1;
}
static BOOL __VISetSCL(u32 value) {
u32 reg;
value &= 1;
reg = busRd32(0xCD8000C0);
reg &= ~(1 << 14);
reg = (u32)(reg | (value << 14));
busWrt32(0xCD8000C0, reg);
return TRUE;
}
static BOOL __VISetSDA(u32 value) {
u32 reg;
value &= 1;
reg = busRd32(0xCD8000C0);
reg &= ~(1 << 15);
reg = (u32)(reg | (value << 15));
busWrt32(0xCD8000C0, reg);
return TRUE;
}
static u32 __VIGetSDA(void) {
u32 reg = busRd32(0xCD8000C8);
return (u32)((reg >> 15) & 1);
}
static void __VIOpenI2C(u32 dir) {
u32 reg = busRd32(0xCD8000C4);
reg = (u32)(reg & ~(1 << 15));
reg = (u32)(reg | (1 << 14) | (dir << 15));
busWrt32(0xCD8000C4, reg);
}
static int wait4ClkHigh(void) {
WaitMicroTime(2);
return 1;
}
static int sendSlaveAddr(u8 slaveAddr) {
int i;
if (0 == __i2c_ident_flag) {
__VISetSDA(1);
} else {
__VISetSDA(0);
}
WaitMicroTime(2);
__VISetSCL(0);
for (i = 0; i < 8; i++) {
if (slaveAddr & 0x80) {
if (0 == __i2c_ident_flag) {
__VISetSDA(0);
} else {
__VISetSDA(1);
}
} else {
if (0 == __i2c_ident_flag) {
__VISetSDA(1);
} else {
__VISetSDA(0);
}
}
WaitMicroTime(2);
__VISetSCL(1);
if (wait4ClkHigh() == 0) {
return 0;
}
__VISetSCL(0);
slaveAddr <<= 1;
}
__VIOpenI2C(0);
WaitMicroTime(2);
__VISetSCL(1);
if (wait4ClkHigh() == 0) {
return 0;
}
if (1 == __i2c_ident_flag) {
if (__VIGetSDA() != 0) {
return 0;
}
}
if (0 == __i2c_ident_flag) {
__VISetSDA(1);
} else {
__VISetSDA(0);
}
__VIOpenI2C(1);
__VISetSCL(0);
return 1;
}
int __VISendI2CData(u8 slaveAddr, u8* pData, int nBytes) {
s32 i;
u8 data;
BOOL enabled;
if (__i2c_ident_first == 0) {
VICheckI2C();
__i2c_ident_first = 1;
}
enabled = OSDisableInterrupts();
__VIOpenI2C(1);
__VISetSCL(1);
if (0 == __i2c_ident_flag) {
__VISetSDA(0);
} else {
__VISetSDA(1);
}
WaitMicroTime(2);
WaitMicroTime(2);
if (sendSlaveAddr(slaveAddr) == 0) {
OSRestoreInterrupts(enabled);
return 0;
}
__VIOpenI2C(1);
while (nBytes != 0) {
data = *pData++;
for (i = 0; i < 8; i++) {
if (data & 0x80) {
if (0 == __i2c_ident_flag) {
__VISetSDA(0);
} else {
__VISetSDA(1);
}
} else {
if (0 == __i2c_ident_flag) {
__VISetSDA(1);
} else {
__VISetSDA(0);
}
}
WaitMicroTime(2);
__VISetSCL(1);
if (wait4ClkHigh() == 0) {
OSRestoreInterrupts(enabled);
return 0;
}
__VISetSCL(0);
data <<= 1;
}
__VIOpenI2C(0);
WaitMicroTime(2);
__VISetSCL(1);
if (wait4ClkHigh() == 0) {
OSRestoreInterrupts(enabled);
return 0;
}
if (1 == __i2c_ident_flag) {
if (__VIGetSDA() != 0) {
OSRestoreInterrupts(enabled);
return 0;
}
}
if (0 == __i2c_ident_flag) {
__VISetSDA(1);
} else {
__VISetSDA(0);
}
__VIOpenI2C(1);
__VISetSCL(0);
nBytes--;
}
__VIOpenI2C(1);
if (0 == __i2c_ident_flag) {
__VISetSDA(1);
} else {
__VISetSDA(0);
}
WaitMicroTime(2);
__VISetSCL(1);
WaitMicroTime(2);
if (0 == __i2c_ident_flag) {
__VISetSDA(0);
} else {
__VISetSDA(1);
}
OSRestoreInterrupts(enabled);
return 1;
}
|