Inter-Integrated Circuits — I2C Basics (Part 2)
The Serial Communication protocol
Case 1: Slave is at the receiver’s end
Even in this case, there are two possible cases–
- CASE 1A: The Slave-receiver does not acknowledge the Slave address (hey wait, what’s an address? We’ll get to it shortly). In that case, it simply leaves the SDA line HIGH. Now the Master-transmitter either generates a Stop sequence or attempts a repeated Start sequence.
- CASE 1B: The Slave-receiver acknowledges the Slave address, but after some time it is unable to receive any data and leaves the SDA line HIGH during the ACK pulse. Even in this case, the Master-transmitter does the same — either generate a Stop sequence, or attempt a repeated Start sequence.
Case 2: Master is at the receiver’s end
Now this is a tricky situation. In this case, the Master is the one generating ACK, as well as responsible for generating Start/Stop sequence. Now how does that work out, especially when the transaction ends?
In this case, in order to signal the Slave-transmitter the end of data, the Master-receiver does NOT generate any ACK on the last byte clocked out of the Slave-transmitter. In this case, the Slave-transmitter must let go of the SDA line to allow Master to generate a Stop or a repeated Start sequence.
Makes sense? If it is confusing, hopefully it will make more sense when we actually program it for real.
I2C Device Addressing
Somewhere in the beginning of the tutorial, I mentioned that we can hook up a lot of devices to the I2C bus. But the Master can talk with only one of the Slaves at a time. Now how does that happen?
This is pretty similar to the situation inside a classroom. There is one teacher (Master) and a ridiculous number of students (Slaves). The teacher wants to ask a question to one of the students. How does she do that? Well, we do have a name, right? All the students have a unique name (address), and the teacher calls out the name first, right? Hey Rahul, could you explain why do we need i2c for Arduino?
Well, this is exactly what happens in case of I2C bus transaction. Every device hooked up to the bus has a unique address. As per the I2C specifications, this address is either 7 bits long, or 10 bits long. 10-bit addresses are rare, and since I am lazy, I am gonna skip it for now.
When we have 7-bit address, we can have up to a maximum of 27 = 128 devices hooked up to the I2C bus, with addresses 0 to 127. Now when the Master calls out to the Slave(s), it still needs to send out 8 bits of data. In this case, the Master appends an extra Read/Write (R/W’) bit to the 7 bits of address (note that W’ means Write complemented). Thus, the R/W’ bit is added to the LSB of the data byte. So now, the data sent looks somethings like this
I2C Data Transfer Protocol
Now that we are familiar with the I2C bus transactions and device addressing, let’s see how to transfer data using the I2C protocol and have a 10,000 foot view of the entire bus transaction.
Timing Diagram
Let’s look at the timing diagram of an entire transaction — from start to stop!
Let’s analyze it first. Before we begin, we all know what these slanted lines mean, right? The slanted lines are a representation of slew rate of the device/bus/system. Ideally, whenever a signal changes its state (from high to low or vice-versa), it is supposed to do so immediately. But in real scenario, it is almost impossible for it to happen without a time lag. The slanted lines refer to these time lags, also known as slew.
Alright, back to where we were.
- The transaction starts with a START sequence.
- After the START sequence, the Master has to send the address of the Slave it wants to talk to. That’s the 7-bit ADDRESS (MSB first) followed by R/W’ bit determining whether you want to read from the device or write into it.
- The Slave responds by acknowledging the address. Yay! If it doesn’t send the ACK, we know what the Master does, right?
- Once the Slave acknowledges the address, it means that it is now ready to send/receive data to/from the Master. Thus begins the data transfer. The DATA is always of 8 bits (MSB first), and the receiver has to send the ACK signal after each byte of data received.
- When the transaction is over, the Master ends it by generating a STOP sequence. Alternatively, Master could also begin with a repeated START.
There are three possible cases of data transfer–
- Case 1: Master-transmitter to Slave-receiver
- Case 2: Slave-transmitter to Master-receiver
- Case 3: Bi-directional (R/W) in same data transfer
See these 3 cases here
Check out about author’s life in (My story right now) section at the end of each post from Jan 2020.
(My story right now) section is at the end of the Part 3 of this series
See you next time
SS
Thanks for reading.
Until next time
Peace, Love and Gratitude.