Thursday, November 15, 2012

I2C (Inter Integrated Circuit Bus) : Serial Communication

I2C quick tutorial

I2C (sometimes 2 is in superscript) and read as "eye-two-see" or "eye-square-see" is a serial communication  protocol introduced by Philips semiconductors in early 80s.

I2C is a 2-wire Interface (TWI) with mostly a single master and multiple slaves , there are multiple master configurations possible too.

I2C signals

  1. Data :  SDA  ===========> Always bi-directional
  2. Clock:  SCL ===========> Bi-directional only in Multi-Master Mode

Ground : GND ==========> Common Ground between the two Devices
5V supply

By convention , I2C operates on 5V signals. So make sure you put a level translation buffer in between  if your device is 3.3V tolerant.

Maximum allowable capacitance on the lines is 400 pF and typical device capacitance is 10 pF. So how many devices are supported on single I2C Bus ?

The communication is always Master initiated, Master (typically a Microcontroller) places the address of Slave device with which Master intends to communicate on the Bus. All Slave devices monitor the SDA pin to determine if Master is sending their address. Only the Slave with Matched address can communicate with Master.

The advantages of I2C other than it being a very low cost to implement are
  • Supports multiple devices on only 3 wires
  • Is implemented in Hardware as well as Software (Bit-Banged I2C)
  • Supports Multi Master configuration
  • Many examples available on the Internet
If I had to belittle I2C I would say , The disadvantages of I2C are
  • Short Distances only
  • Slow Clock speeds (Typically 100Khz)
  • Faster Clocked devices cannot coexist with Slower devices on the same bus

I2C Typical Connections
Both SCL and SDA are Open-Drain drivers, which means that the device can drive it low, but cannot drive it High. To drive the line High, you must provide a pull up through a resistor as seen in the above figure. You only need one pull up for SDA and one for SCL irrespective of how many devices are connected to the bus.

Data Transfer Protocol over I2C

When I2C master wishes to communicate with slave devices, it stars by generating a "Start Condition" on the SDA line. "Start Condition" is defined as SDA line going low while SCL is high. Similarly when Master devices wishes to end the transfer, it generates a "Stop Condition" on the line. Stop Condition is defined as SDA going High when SCL is High.

The receiving device ACKnowledges the transfer after every 8 bits (MSB first), by driving SDA Low for Positive ACK and driving SDA High. This means that there are 9 clock cycles per 8-bits of data. 8-bit data and 1-bit for ACK. If the receiver is not ready to receive any more data, it should keep SDA high for 9th bit so the transmitter aborts the transfer.

Data can change while the SCL is low, but it should not change with SCL is high.

Device Addressing

Device Addressing is another section of I2C that confuses new comers. Addresses on I2C bus are 7-bit, but as explained before, the transfers are all 8-bit + 1-bit for ACK, then where did this 7-bit address come from ? Here is how.

In the images above, it is a little bit more clear as in how the addressing is done in I2C. 7-Bit slave address (128 slave devices addressable ) , 8th bit for is it going to be a Read (1) or a Write (0) command, and 9th bit for ACK from the receiver. Always remember, Even address (8th bit zero)  for WRITE and Odd address (8th bit one) for READ.

Example 1
Here is an example where Master device is writing to a Slave. It starts by addressing a Slave device by its 7-bit Address and putting 8th-bit Low indicating a Write Command.

The "Green" bounded parts are Bits transfered by Master, and everything else is transfered by Slave.

Example 2
Master wants to read from Slave

But it is rarely this simple. Devices have multiple internal registers to which you want to write to or read from, so how do you do that ?

Writing to an internal register of an I2C slave

  1. Start Condition (Put automatically if Hardware I2C is used)

  2. Send I2C address of the slave with R/W bit low (even address) for write.
  3. Send the address of the internal register
  4. Send data byte
  5. Send more data bytes if needed
  6. Send Stop Condition (Automatic if Hardware I2C is being used) 

Reading from an Internal Register of an I2C Slave device

Reading from an I2C slave device having internal registers is a wee bit tricky but not tricky enough to get you boggled up. Read from the slave device starts with a write operation in which we write the internal register address to the slave device which we want to read from.

  1. Start condition
  2. Send I2C address of the slave with R/W bit Low (even Address) for write.
  3. Send the address of the internal register
  4. Send "Start Condition" again, referred to as (Restart) in this scenario
  5. Send the I2C address of the slave device with R/W bit High (Odd address) for read operation.
  6. Read data byte
  7. Read more data byes if needed
  8. Send a stop condition.

I hope this helped :) leave your questions in comments.

No comments:

Post a Comment