-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathI2C.cpp
executable file
·257 lines (176 loc) · 4.76 KB
/
I2C.cpp
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include "I2C.h"
#include "DriverData.h"
static int I2C_bit_sendbytes(struct CardData *card, struct I2C *device, unsigned char *bytes, int count);
static int I2C_bit_readbytes(struct CardData *card, struct I2C *device, unsigned char *bytes, int count);
static int I2C_bit_probeaddr(struct CardData *card, unsigned short addr);
struct I2C *AllocI2C(unsigned char addr)
{
struct I2C *device;
device = (struct I2C *) new I2C;
if (device == NULL)
return NULL;
device->addr = addr;
device->flags = 0;
return device;
}
void FreeI2C(struct I2C *device)
{
delete device;
}
int WriteBytesI2C(struct CardData *card, struct I2C *device, unsigned char *bytes, int count)
{
return I2C_bit_sendbytes(card, device, bytes, count);
}
int ReadBytesI2C(struct CardData *card, struct I2C *device, unsigned char *bytes, int count)
{
return I2C_bit_readbytes(card, device, bytes, count);
}
int ProbeAddressI2C(struct CardData *card, unsigned short addr)
{
return I2C_bit_probeaddr(card, addr);
}
// Let the specific chip set up GPIO dir and mask for example
// before we start feeding bits and bytes...
static inline void I2C_bit_hw_start(struct CardData *card)
{
if (card->bit_ops->Start)
card->bit_ops->Start(card);
}
static inline void I2C_bit_hw_stop(struct CardData *card)
{
if (card->bit_ops->Stop)
card->bit_ops->Stop(card);
}
static void I2C_bit_direction(struct CardData *card, int clock, int data)
{
if (card->bit_ops->SetDir_CLK_SDA)
card->bit_ops->SetDir_CLK_SDA(card, clock, data);
}
static void I2C_bit_set(struct CardData *card, int clock, int data)
{
card->bit_ops->Write_CLK_SDA(card, clock, data);
}
/*static int I2C_bit_clock(struct CardData *card)
{
if (card->bit_ops->GetClock)
return card->bit_ops->GetClock(card);
return 0;
}*/
static int I2C_bit_data(struct CardData *card, int ack)
{
return card->bit_ops->GetData(card, ack);
}
// See page 17 and 18 of the I2C spec, version 2.1 January 2000,
// especially, figure 18 (START byte procedure)
static void I2C_bit_start(struct CardData *card)
{
// chip-specific init
I2C_bit_hw_start(card);
// we're gonna write to both
I2C_bit_direction(card, 1, 1);
I2C_bit_set(card, 1, 1);
I2C_bit_set(card, 1, 0);
I2C_bit_set(card, 0, 0);
}
// see figure 10 and 19
static void I2C_bit_stop(struct CardData *card)
{
I2C_bit_set(card, 0, 0);
I2C_bit_set(card, 1, 0);
I2C_bit_set(card, 1, 1);
// chip-specific stop
I2C_bit_hw_stop(card);
}
// data gets send on high clock
static void I2C_bit_send(struct CardData *card, int data)
{
I2C_bit_set(card, 0, data);
I2C_bit_set(card, 1, data);
I2C_bit_set(card, 0, data);
}
static int I2C_bit_ack(struct CardData *card)
{
int ack;
// figure 7
I2C_bit_set(card, 0, 1);
I2C_bit_set(card, 1, 1);
// then read the ack bit
I2C_bit_direction(card, 1, 0);
ack = I2C_bit_data(card, 1);
I2C_bit_direction(card, 1, 1);
I2C_bit_set(card, 0, 1);
return ack;
}
static int I2C_bit_sendbyte(struct CardData *card, unsigned char data)
{
int i, err;
// send the byte bit for bit, MSB first
for (i = 7; i >= 0; i--)
I2C_bit_send(card, (data & (1 << i)));
if ((err = I2C_bit_ack(card)) < 0)
return err;
return 0;
}
static int I2C_bit_readbyte(struct CardData *card, int last)
{
int i;
unsigned char data = 0;
I2C_bit_set(card, 0, 1);
I2C_bit_direction(card, 1, 0);
for (i = 7; i >= 0; i--) {
I2C_bit_set(card, 1, 1);
if (I2C_bit_data(card, 0))
data |= (1 << i);
I2C_bit_set(card, 0, 1);
}
I2C_bit_direction(card, 1, 1);
I2C_bit_send(card, last);
return data;
}
static int I2C_bit_sendbytes(struct CardData *card, struct I2C *device, unsigned char *bytes, int count)
{
int err, res = 0;
I2C_bit_start(card);
// first send address of the I2C chip we want to reach
if ((err = I2C_bit_sendbyte(card, device->addr << 1)) < 0) {
I2C_bit_hw_stop(card);
return err;
}
// then send all consecutive bytes with an 'ack' in between
while (count-- > 0) {
if ((err = I2C_bit_sendbyte(card, *bytes++)) < 0) {
I2C_bit_hw_stop(card);
return err;
}
res++;
}
I2C_bit_stop(card);
return res;
}
static int I2C_bit_readbytes(struct CardData *card, struct I2C *device, unsigned char *bytes, int count)
{
int err, res = 0;
I2C_bit_start(card);
if ((err = I2C_bit_sendbyte(card, (device->addr << 1) | 1)) < 0) {
I2C_bit_hw_stop(card);
return err;
}
while (count-- > 0) {
if ((err = I2C_bit_readbyte(card, count == 0)) < 0) {
I2C_bit_hw_stop(card);
return err;
}
*bytes++ = (unsigned char)err;
res++;
}
I2C_bit_stop(card);
return res;
}
static int I2C_bit_probeaddr(struct CardData *card, unsigned short addr)
{
int err;
I2C_bit_start(card);
err = I2C_bit_sendbyte(card, addr << 1);
I2C_bit_stop(card);
return err;
}