1 /*-------------------------------------------------------------------------
4 Copyright (C) 2001, Johan Knol <johan.knol AT iduna.nl>
6 This library is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this library; see the file COPYING. If not, write to the
18 Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
21 As a special exception, if you link this library with other files,
22 some of which are compiled with SDCC, to produce an executable,
23 this library does not by itself cause the resulting executable to
24 be covered by the GNU General Public License. This exception does
25 not however invalidate any other reasons why the executable file
26 might be covered by the GNU General Public License.
27 -------------------------------------------------------------------------*/
29 /* This implementation is based on an example I once grabbed from
31 Don't know who wrote it, but is has been hacked so heavily, he/she wouldn't
32 recognize it anyway */
34 //#define DEBUG_I2C ==> DON'T DO THIS, THIS IS A LIBRARY <==
42 // we are (ab)using the CAN CTX and CRX for serial data and serial clock
43 #define SCL_HIGH (P5 |= 1)
44 #define SCL_LOW (P5 &= ~1)
45 #define SDA_HIGH (P5 |= 2)
46 #define SDA_LOW (P5 &= ~2)
48 #define SDA_OUT(b) (b ? SDA_HIGH : SDA_LOW)
49 #define SDA_IN ((P5>>1)&1)
56 #define I2CERR_OK 0 /* No error */
57 #define I2CERR_NAK 1 /* No ACK from slave */
58 #define I2CERR_LOST 2 /* Arbitration lost */
59 #define I2CERR_BUS 3 /* Bus is stuck (not used yet) */
60 #define I2CERR_TIMEOUT 4 /* Timeout on bus */
62 char i2cTransmitBuffer
[I2C_BUFSIZE
]; /* Global transfer buffers */
63 char i2cReceiveBuffer
[I2C_BUFSIZE
];
65 static char i2cError
= 0; /* Last error */
69 void I2CDelay(volatile long delay
) {
74 void I2CDumpError(char error
);
77 * Makes sure that the bus is in a known condition. Returns 1 on success,
78 * 0 if some other device is pulling on the bus.
88 return (SCL_IN
&& SDA_IN
);
93 * Generates a start condition on the bus. Returns 0 on success, 1 if some
94 * other device is holding the bus.
102 SDA_LOW
; /* Pull SDA down... */
104 SCL_LOW
; /* ...and then SCL -> start condition. */
111 * Generates a stop condition on the bus. Returns 0 on success, 1 if some
112 * other device is holding the bus.
118 SCL_HIGH
; /* Let SCL go up */
120 SDA_HIGH
; /* ...and then SDA up -> stop condition. */
123 return (SCL_IN
&& SDA_IN
); /* Both will be up, if everything is fine */
129 * Returns 0 on success, 1 if we lose arbitration.
132 char BitOutI2C(unsigned char bout
)
134 SDA_OUT(bout
); /* Put data out on SDA */
136 SCL_HIGH
; /* Let SCL go up */
137 while(!SCL_IN
) /* Wait until all other devices are ready */
139 // should do a timeout here
142 if (SDA_IN
!= bout
) /* Arbitration lost, release bus and return */
144 SDA_HIGH
; /* Should be up anyway, but make sure */
145 i2cError
= I2CERR_LOST
;
146 I2CDumpError(i2cError
);
150 SCL_LOW
; /* Pull SCL back down */
164 // SDA is opencollector, so:
167 SCL_HIGH
; /* Let SCL go up */
168 while(!SCL_IN
) /* Wait for other devices */
170 // should do a timeout here
172 bin
= SDA_IN
; /* Read in data */
174 SCL_LOW
; /* Pull SCL back up */
176 return bin
; /* Return the sampled bit */
181 * Send one byte on the bus. No start or stop conditions are generated here,
182 * but i2cError will be set according to the result.
183 * Returns 0 on success, 1 if we lose arbitration or if the slave doesn't
184 * acknowledge the byte. Check i2cError for the actual result on error.
187 char ByteOutI2C(char dat
)
195 I2CDumpError(i2cError
);
200 I2CDumpError(i2cError
);
209 i2cError
= I2CERR_NAK
;
210 I2CDumpError(i2cError
);
218 * Reads one byte in from the slave. Ack must be 1 if this is the last byte
219 * to be read during this transfer, 0 otherwise (as per I2C bus specification,
220 * the receiving master must acknowledge all but the last byte during a
224 char I2CByteIn(char ack
)
226 char bit_count
, byte_in
;
234 if (BitInI2C()) byte_in
|= 0x01;
239 SDA_HIGH
; /* Added 18-Jul-95 - thanks to Ray Bellis */
245 * Send 'count' bytes to slave 'addr'.
246 * Returns 0 on success. Stop condition is sent only when send_stop is true.
249 char I2CSendStop(char addr
, char count
, char send_stop
)
251 char byteptr
, byte_out
;
253 if (I2CStart()) return 1;
256 byte_out
= addr
& 0xfe; /* Ensure that it's a write address */
257 count
++; /* Include slave address to byte count */
261 if (ByteOutI2C(byte_out
))
263 if (i2cError
== I2CERR_NAK
&& send_stop
) I2CStop();
266 byte_out
= i2cTransmitBuffer
[byteptr
];
271 if (send_stop
) I2CStop();
277 * Read in 'count' bytes from slave 'addr'.
278 * Returns 0 on success.
281 char i2c_recv(char addr
, char count
)
283 char byteptr
, byte_in
;
285 if (I2CStart()) return 1;
289 byte_in
= addr
| 0x01;
291 if (ByteOutI2C(byte_in
))
293 if (i2cError
== I2CERR_NAK
) I2CStop();
301 byte_in
= I2CByteIn(0);
303 byte_in
= I2CByteIn(1); /* No ACK during last byte */
305 i2cReceiveBuffer
[byteptr
] = byte_in
;
311 return (i2cError
? 1 : 0);
316 * Write 'tx_count' bytes to slave 'addr', then use a repeated start condition
317 * to read 'rx_count' bytes from the same slave during the same transfer.
318 * Returns 0 on success, 1 otherwise. On error, check i2cError for the actual
322 char I2CSendReceive(char addr
, char tx_count
, char rx_count
)
324 if (I2CSendStop(addr
, tx_count
, 0))
326 /* If send fails, abort but don't send a stop condition if we lost
329 if (i2cError
!= I2CERR_LOST
) I2CStop();
333 SDA_HIGH
; /* One of these may be low now, in which case the next */
334 SCL_HIGH
; /* start condition wouldn't be detected so make */
335 I2CDelay(I2CDELAY
); /* sure that they're up and wait for one delay slot */
337 if (i2c_recv((char)(addr
|0x01), rx_count
)) return 1;
338 return (i2cError
? 1 : 0);
342 * Dump an error message.
345 void I2CDumpError(char error
)
354 puts("I2C: Slave didn't acknowledge");
357 puts("I2C: Lost arbitration with another master");
360 puts("I2C: Timeout on bus");
363 puts("I2C: The bus is stuck");
366 puts("I2C: Unknown error");
370 error
; // hush the compiler