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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
/*-------------------------------------------------------------------------
i2c390.c
Copyright (C) 2001, Johan Knol <johan.knol AT iduna.nl>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this library; see the file COPYING. If not, write to the
Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
As a special exception, if you link this library with other files,
some of which are compiled with SDCC, to produce an executable,
this library does not by itself cause the resulting executable to
be covered by the GNU General Public License. This exception does
not however invalidate any other reasons why the executable file
might be covered by the GNU General Public License.
-------------------------------------------------------------------------*/
/* This implemenation is based on an example I once grabbed from
the Philips bbs.
Don't know who wrote it, but is has been hacked so heavily, he/she wouldn't
recogize it anyway */
//#define DEBUG_I2C ==> DON'T DO THIS, THIS IS A LIBRARY <==
#ifdef DEBUG_I2C
#include <stdio.h>
#else
#include <tinibios.h>
#endif
// we are (ab)using the CAN CTX and CRX for serial data and serial clock
#define SCL_HIGH (P5 |= 1)
#define SCL_LOW (P5 &= ~1)
#define SDA_HIGH (P5 |= 2)
#define SDA_LOW (P5 &= ~2)
#define SDA_OUT(b) (b ? SDA_HIGH : SDA_LOW)
#define SDA_IN ((P5>>1)&1)
#define SCL_IN (P5&1)
/*
* I2C error values
*/
#define I2CERR_OK 0 /* No error */
#define I2CERR_NAK 1 /* No ACK from slave */
#define I2CERR_LOST 2 /* Arbitration lost */
#define I2CERR_BUS 3 /* Bus is stuck (not used yet) */
#define I2CERR_TIMEOUT 4 /* Timeout on bus */
char i2cTransmitBuffer[I2C_BUFSIZE]; /* Global transfer buffers */
char i2cReceiveBuffer[I2C_BUFSIZE];
static char i2cError = 0; /* Last error */
#define I2CDELAY 1
void I2CDelay(volatile long delay) {
while (delay--)
;
}
void I2CDumpError(char error);
/*
* Makes sure that the bus is in a known condition. Returns 1 on success,
* 0 if some other device is pulling on the bus.
*/
char I2CReset(void)
{
SDA_LOW;
SCL_LOW;
SCL_HIGH;
SDA_HIGH;
i2cError = 0;
return (SCL_IN && SDA_IN);
}
/*
* Generates a start condition on the bus. Returns 0 on success, 1 if some
* other device is holding the bus.
*/
char I2CStart(void)
{
SDA_HIGH;
SCL_HIGH;
I2CDelay(I2CDELAY);
SDA_LOW; /* Pull SDA down... */
I2CDelay(I2CDELAY);
SCL_LOW; /* ...and then SCL -> start condition. */
I2CDelay(I2CDELAY);
return 0;
}
/*
* Generates a stop condition on the bus. Returns 0 on success, 1 if some
* other device is holding the bus.
*/
char I2CStop(void)
{
SDA_LOW;
SCL_HIGH; /* Let SCL go up */
I2CDelay(I2CDELAY);
SDA_HIGH; /* ...and then SDA up -> stop condition. */
I2CDelay(I2CDELAY);
return (SCL_IN && SDA_IN); /* Both will be up, if everything is fine */
}
/*
* Clock out one bit.
* Returns 0 on success, 1 if we lose arbitration.
*/
char BitOutI2C(unsigned char bout)
{
SDA_OUT(bout); /* Put data out on SDA */
I2CDelay(I2CDELAY);
SCL_HIGH; /* Let SCL go up */
while(!SCL_IN) /* Wait until all other devices are ready */
{
// should do a timeout here
}
if (SDA_IN != bout) /* Arbitration lost, release bus and return */
{
SDA_HIGH; /* Should be up anyway, but make sure */
i2cError = I2CERR_LOST;
I2CDumpError(i2cError);
return 1;
}
I2CDelay(I2CDELAY);
SCL_LOW; /* Pull SCL back down */
I2CDelay(I2CDELAY);
return 0; /* OK */
}
/*
* Clock in one bit.
*/
char BitInI2C(void)
{
char bin;
// SDA is opencollector, so:
SDA_HIGH;
SCL_HIGH; /* Let SCL go up */
while(!SCL_IN) /* Wait for other devices */
{
// should do a timeout here
}
bin = SDA_IN; /* Read in data */
I2CDelay(I2CDELAY);
SCL_LOW; /* Pull SCL back up */
I2CDelay(I2CDELAY);
return bin; /* Return the sampled bit */
}
/*
* Send one byte on the bus. No start or stop conditions are generated here,
* but i2cError will be set according to the result.
* Returns 0 on success, 1 if we lose arbitration or if the slave doesn't
* acknowledge the byte. Check i2cError for the actual result on error.
*/
char ByteOutI2C(char dat)
{
char bit_count;
bit_count = 8;
while(bit_count) {
if (dat & 0x80) {
if (BitOutI2C(1)) {
I2CDumpError(i2cError);
return 1;
}
} else {
if (BitOutI2C(0)) {
I2CDumpError(i2cError);
return 1;
}
}
dat <<= 1;
bit_count--;
}
if (BitInI2C()) {
i2cError = I2CERR_NAK;
I2CDumpError(i2cError);
return 1;
}
return 0;
}
/*
* Reads one byte in from the slave. Ack must be 1 if this is the last byte
* to be read during this transfer, 0 otherwise (as per I2C bus specification,
* the receiving master must acknowledge all but the last byte during a
* transfer).
*/
char I2CByteIn(char ack)
{
char bit_count, byte_in;
bit_count = 8;
byte_in = 0;
while(bit_count)
{
byte_in <<= 1;
if (BitInI2C()) byte_in |= 0x01;
bit_count--;
}
BitOutI2C(ack);
SDA_HIGH; /* Added 18-Jul-95 - thanks to Ray Bellis */
return byte_in;
}
/*
* Send 'count' bytes to slave 'addr'.
* Returns 0 on success. Stop condition is sent only when send_stop is true.
*/
char I2CSendStop(char addr, char count, char send_stop)
{
char byteptr, byte_out;
if (I2CStart()) return 1;
i2cError = 0;
byte_out = addr & 0xfe; /* Ensure that it's a write address */
count++; /* Include slave address to byte count */
byteptr = 0;
while(count)
{
if (ByteOutI2C(byte_out))
{
if (i2cError == I2CERR_NAK && send_stop) I2CStop();
return i2cError;
}
byte_out = i2cTransmitBuffer[byteptr];
byteptr++;
count--;
}
if (send_stop) I2CStop();
return 0;
}
/*
* Read in 'count' bytes from slave 'addr'.
* Returns 0 on success.
*/
char i2c_recv(char addr, char count)
{
char byteptr, byte_in;
if (I2CStart()) return 1;
i2cError = 0;
byteptr = 0;
byte_in = addr | 0x01;
if (ByteOutI2C(byte_in))
{
if (i2cError == I2CERR_NAK) I2CStop();
return i2cError;
}
while(count)
{
count-=1;
if (count) {
byte_in = I2CByteIn(0);
} else {
byte_in = I2CByteIn(1); /* No ACK during last byte */
}
i2cReceiveBuffer[byteptr] = byte_in;
byteptr++;
}
I2CStop();
return (i2cError ? 1 : 0);
}
/*
* Write 'tx_count' bytes to slave 'addr', then use a repeated start condition
* to read 'rx_count' bytes from the same slave during the same transfer.
* Returns 0 on success, 1 otherwise. On error, check i2cError for the actual
* error value.
*/
char I2CSendReceive(char addr, char tx_count, char rx_count)
{
if (I2CSendStop(addr, tx_count, 0))
{
/* If send fails, abort but don't send a stop condition if we lost
arbitration */
if (i2cError != I2CERR_LOST) I2CStop();
return 1;
}
SDA_HIGH; /* One of these may be low now, in which case the next */
SCL_HIGH; /* start condition wouldn't be detected so make */
I2CDelay(I2CDELAY); /* sure that they're up and wait for one delay slot */
if (i2c_recv((char)(addr|0x01), rx_count)) return 1;
return (i2cError ? 1 : 0);
}
/*
* Dump an error message.
*/
void I2CDumpError(char error)
{
#ifdef DEBUG_I2C
switch(error)
{
case 0:
puts("I2C: OK.");
break;
case I2CERR_NAK:
puts("I2C: Slave didn't acknowledge");
break;
case I2CERR_LOST:
puts("I2C: Lost arbitration with another master");
break;
case I2CERR_TIMEOUT:
puts("I2C: Timeout on bus");
break;
case I2CERR_BUS:
puts("I2C: The bus is stuck");
break;
default:
puts("I2C: Unknown error");
break;
}
#else
error; // hush the compiler
#endif
}
|