2 * Copyright 2007, Axel Dörfler, axeld@pinc-software.de. All Rights Reserved.
3 * Copyright 2003, Thomas Kurschel. All Rights Reserved.
4 * Distributed under the terms of the MIT License.
13 #include <KernelExport.h>
23 void _sPrintf(const char *format
, ...);
24 # define TRACE(x...) _sPrintf("I2C: " x)
26 # define TRACE(x...) ;
31 I2c timings, rounded up (Philips 1995 i2c bus specification, p20)
34 //! Timing for standard mode i2c (100kHz max)
35 const static i2c_timing kTiming100k
= {
47 // these are unspecified, use half a clock cycle as a safe guess
51 .ack_start_timeout
= 5,
55 //! Timing for fast mode i2c (400kHz max)
56 const static i2c_timing kTiming400k
= {
68 // these are unspecified, use half a clock cycle as a safe guess
72 .ack_start_timeout
= 2,
78 There's no spin in user space, but we need it to wait a couple
80 (in this case, snooze has much too much overhead)
85 bigtime_t startTime
= system_time();
87 while (system_time() - startTime
< delay
)
92 //! Wait until slave releases clock signal ("clock stretching")
94 wait_for_clk(const i2c_bus
*bus
, bigtime_t timeout
)
98 // wait for clock signal to raise
101 startTime
= system_time();
106 bus
->get_signals(bus
->cookie
, &clk
, &data
);
110 if (system_time() - startTime
> timeout
) {
111 TRACE("%s: Timeout waiting on clock (r)\n");
120 //! Send start or repeated start condition
122 send_start_condition(const i2c_bus
*bus
)
126 bus
->set_signals(bus
->cookie
, 1, 1);
128 status
= wait_for_clk(bus
, bus
->timing
.start_timeout
);
129 if (status
!= B_OK
) {
130 TRACE("%s: Timeout sending start condition\n", __func__
);
134 spin(bus
->timing
.su_sta
);
135 bus
->set_signals(bus
->cookie
, 1, 0);
136 spin(bus
->timing
.hd_sta
);
137 bus
->set_signals(bus
->cookie
, 0, 0);
144 //! Send stop condition
146 send_stop_condition(const i2c_bus
*bus
)
150 bus
->set_signals(bus
->cookie
, 0, 0);
152 bus
->set_signals(bus
->cookie
, 1, 0);
154 // a slave may wait for us, so let elapse the acknowledge timeout
155 // to make the slave release bus control
156 status
= wait_for_clk(bus
, bus
->timing
.ack_timeout
);
157 if (status
!= B_OK
) {
158 TRACE("%s: Timeout sending stop condition\n", __func__
);
162 spin(bus
->timing
.su_sto
);
163 bus
->set_signals(bus
->cookie
, 1, 1);
164 spin(bus
->timing
.buf
);
172 send_bit(const i2c_bus
*bus
, uint8 bit
, int timeout
)
176 //TRACE("send_bit(bit = %d)\n", bit & 1);
178 bus
->set_signals(bus
->cookie
, 0, bit
& 1);
179 spin(bus
->timing
.su_dat
);
180 bus
->set_signals(bus
->cookie
, 1, bit
& 1);
182 status
= wait_for_clk(bus
, timeout
);
183 if (status
!= B_OK
) {
184 TRACE("%s: Timeout when sending next bit\n", __func__
);
188 spin(bus
->timing
.high
);
189 bus
->set_signals(bus
->cookie
, 0, bit
& 1);
190 spin(bus
->timing
.f
+ bus
->timing
.low
);
196 //! Send acknowledge and wait for reply
198 send_acknowledge(const i2c_bus
*bus
)
203 // release data so slave can modify it
204 bus
->set_signals(bus
->cookie
, 0, 1);
205 spin(bus
->timing
.su_dat
);
206 bus
->set_signals(bus
->cookie
, 1, 1);
208 status
= wait_for_clk(bus
, bus
->timing
.ack_start_timeout
);
209 if (status
!= B_OK
) {
210 TRACE("%s: Timeout when sending acknowledge\n", __func__
);
214 // data and clock is high, now wait for slave to pull data low
215 // (according to spec, this can happen any time once clock is high)
216 startTime
= system_time();
221 bus
->get_signals(bus
->cookie
, &clk
, &data
);
226 if (system_time() - startTime
> bus
->timing
.ack_timeout
) {
227 TRACE("%s: slave didn't acknowledge byte within ack_timeout: %ld\n",
228 __func__
, bus
->timing
.ack_timeout
);
235 TRACE("send_acknowledge(): Success!\n");
237 // make sure we've waited at least t_high
238 spin(bus
->timing
.high
);
240 bus
->set_signals(bus
->cookie
, 0, 1);
241 spin(bus
->timing
.f
+ bus
->timing
.low
);
247 //! Send byte and wait for acknowledge if <ackowledge> is true
249 send_byte(const i2c_bus
*bus
, uint8 byte
, bool acknowledge
)
253 //TRACE("%s: (byte = %x)\n", __func__, byte);
255 for (i
= 7; i
>= 0; --i
) {
256 status_t status
= send_bit(bus
, byte
>> i
,
257 i
== 7 ? bus
->timing
.byte_timeout
: bus
->timing
.bit_timeout
);
263 return send_acknowledge(bus
);
269 //! Send slave address, obeying 10-bit addresses and general call addresses
271 send_slave_address(const i2c_bus
*bus
, int slaveAddress
, bool isWrite
)
275 TRACE("%s: 0x%X\n", __func__
, slaveAddress
);
276 status
= send_byte(bus
, (slaveAddress
& 0xfe) | !isWrite
, true);
280 // there are the following special cases if the first byte looks like:
281 // - 0000 0000 - general call address (second byte with address follows)
282 // - 0000 0001 - start byte
283 // - 0000 001x - CBus address
284 // - 0000 010x - address reserved for different bus format
286 // - 0000 1xxx |-> reserved
288 // - 1111 0xxx - 10 bit address (second byte contains remaining 8 bits)
290 // the lsb is 0 for write and 1 for read (except for general call address)
291 if ((slaveAddress
& 0xff) != 0 && (slaveAddress
& 0xf8) != 0xf0)
294 return send_byte(bus
, slaveAddress
>> 8, true);
295 // send second byte if required
301 receive_bit(const i2c_bus
*bus
, bool *bit
, int timeout
)
306 bus
->set_signals(bus
->cookie
, 1, 1);
309 // wait for slave to raise clock
310 status
= wait_for_clk(bus
, timeout
);
311 if (status
!= B_OK
) {
312 TRACE("%s: Timeout waiting for bit sent by slave\n", __func__
);
316 bus
->get_signals(bus
->cookie
, &clk
, &data
);
319 spin(bus
->timing
.high
);
320 // leave clock high for minimal time
322 bus
->set_signals(bus
->cookie
, 0, 1);
323 // pull clock low so slave waits for us before next bit
325 spin(bus
->timing
.f
+ bus
->timing
.low
);
326 // let it settle and leave it low for minimal time
327 // to make sure slave has finished bit transmission too
335 Send positive acknowledge afterwards if <acknowledge> is true,
336 else send negative one
339 receive_byte(const i2c_bus
*bus
, uint8
*resultByte
, bool acknowledge
)
344 // pull clock low to let slave wait for us
345 bus
->set_signals(bus
->cookie
, 0, 1);
347 for (i
= 7; i
>= 0; i
--) {
350 status_t status
= receive_bit(bus
, &bit
,
351 i
== 7 ? bus
->timing
.byte_timeout
: bus
->timing
.bit_timeout
);
355 byte
= (byte
<< 1) | bit
;
358 //SHOW_FLOW(3, "%x ", byte);
362 return send_bit(bus
, acknowledge
? 0 : 1, bus
->timing
.bit_timeout
);
366 //! Send multiple bytes
368 send_bytes(const i2c_bus
*bus
, const uint8
*writeBuffer
, ssize_t writeLength
)
370 TRACE("%s: (length = %ld)\n", __func__
, writeLength
);
372 for (; writeLength
> 0; --writeLength
, ++writeBuffer
) {
373 status_t status
= send_byte(bus
, *writeBuffer
, true);
382 //! Receive multiple bytes
384 receive_bytes(const i2c_bus
*bus
, uint8
*readBuffer
, ssize_t readLength
)
386 TRACE("%s: (length = %ld)\n", __func__
, readLength
);
388 for (; readLength
> 0; --readLength
, ++readBuffer
) {
389 status_t status
= receive_byte(bus
, readBuffer
, readLength
> 1);
398 // #pragma mark - exported functions
401 //! Combined i2c send+receive format
403 i2c_send_receive(const i2c_bus
*bus
, int slaveAddress
, const uint8
*writeBuffer
,
404 size_t writeLength
, uint8
*readBuffer
, size_t readLength
)
406 status_t status
= send_start_condition(bus
);
410 status
= send_slave_address(bus
, slaveAddress
, true);
414 status
= send_bytes(bus
, writeBuffer
, writeLength
);
418 status
= send_start_condition(bus
);
422 status
= send_slave_address(bus
, slaveAddress
, false);
426 status
= receive_bytes(bus
, readBuffer
, readLength
);
430 return send_stop_condition(bus
);
433 TRACE("%s: Cancelling transmission\n", __func__
);
434 send_stop_condition(bus
);
440 i2c_get100k_timing(i2c_timing
*timing
)
442 // AKA standard i2c mode
443 memcpy(timing
, &kTiming100k
, sizeof(i2c_timing
));
448 i2c_get400k_timing(i2c_timing
*timing
)
451 memcpy(timing
, &kTiming400k
, sizeof(i2c_timing
));