I2C – Everything you need to know
There is no need for a wordy introduction to I2C protocol. We all know it’s main parts – 2 wires, multiple slaves, sometimes multiple masters, up to 5MHz of speed. Often so have we all implemented an I2C connection. Still, every now and then, there’s that module that just won’t work. This time, we will do an in-depth research about the I2C protocol, and try to cover as much ground as possible.
Overview of the design
I2C works with it’s two wires, the SDA(data line) and SCL(clock line). Both these lines are open-drain, but are pulled-up with resistors. Usually there is one master and one or multiple slaves on the line, although there can be multiple masters, but we’ll talk about that later. Both masters and slaves can transmit or receive data, therefore, a device can be in one of these four states: master transmit, master receive, slave transmit, slave receive.
Start from the start
The master initiates the communication by sending a START bit, this bit alerts all the slaves that some data is coming and they need to listen. After the start bit, 7 bits of a unique slave address is sent. Each slave has it’s own slave address, this way, only one slave will respond to the data. The last sent bit is the read/write bit. If this bit is 0, it means that the master wishes to write data to a register of the slave, if this bit is 1, it means that the master wishes to read data from a register of a slave. Note that all the data is sent MSB first.
The start bit is generated by pulling the SDA line low, while the SCL is high. And the stop bit is indicated by releasing the SDA to high, along with the SCL being high. So, whenever a module wants to initialize communication, it has to assert SDA line. In our I2C library, which you can find in our compilers, the start bit is sent by calling the “I2C_Start();” function.
A module can also initiate what is called a repeated start. The repeated start differs from the start condition in the way that there is no stop condition before it. This way, the slave knows that the incoming bytes are parts of the same message/conversation.
Sending a message
Data is transferred on the bus in packages of 8 bits. Whenever a device receives a byte, it has to send an acknowledgment bit (ACK bit) back to the master ( this also goes for the first, address, byte). This bit signalizes if the device has received data successfully. Once the sending device is done sending the data through the bus, it will generate a STOP bit, to signalize the end of that conversation, or generate a repeated start, to hold the bus again for some other data transmission.
Read more: I2C – Everything you need to know