There are two groups of devices at play. One group works on data, but may need information to be stored or recalled from external sources. This is the master. The master is able to call on other devices that work on data themselves to store or recall data for it. This group of devices are called slaves. Don’t worry, there’s nothing sinister at play here – that’s really just the terminology for this model. The communication channel that binds them all is called the bus. You can think of the bus as something akin to a giant tin-can telephone network. The nature of this network is that only one conversation can go on between the master and one slave device at a time over this one wire. There’s also a sort of rhythm and tempo to this communication. These are the serial data and serial clock lines, respectively. For the sake of keeping this description less complicated, I’ll keep any clock-related things out of the picture from here on out. In terms of data and communication, all the data communicated is 8-bit, but there are also certain signals the slaves and master can give during the conversation, which you’ll see as we go on. There can also be multiple masters on the bus, but for this example, I’ll limit it to only one master device.
Slaves work a bit interestingly. First off, the master can identify each one of them by their 7-bit address. Second, the data they have is very neatly organized for the master’s sake. Imagine a slave having N cabinets, each cabinet able to fit 8 bits of information in them. Each cabinet is labelled from 0 to N-1 and arranged neatly in a circle. Sometimes, the master might tell them which cabinet to store/retrieve data from, but other times, the master might want to get data right where it left off. The slave has a way to remember where in its collection of data to read from/write to – an address pointer. In reality, this is a portion of memory meant to keep track of where the slave should read from/write to, but for the sake of the analogy, imagine the slave leaving the cabinet to read from next open. To keep up with the master, all the slave needs to do if there’s multiple bytes of information to take in or move out is to move on to the next cabinet, or, if the slave is at cabinet M, it moves on to cabinet M+1. Even if the master is done, the slave will keep the next cabinet open and ready. Since the cabinets were arranged in a circle, the slave can seamlessly transition from cabinet N-1 to 0. When the master is not calling on a slave, a slave can work on and manipulate the data it has all on its own.
Now, let’s get an actual I2C conversation going. The master needs something from a slave. Before it’s able to call on a slave, it waits for the bus to go quiet, so its communications don’t get caught in any noise on the line. The master then starts the conversation with a START signal, basically equivalent to letting all the slaves know to listen up. The master then presents its first 8 bit communique, a 7-bit address of a slave, and a 1-bit command. It’s like calling on a slave to do something. The slave in the bus that has the corresponding address gives an ACKNOWLEDGE (ACK), an “ok” to the master to proceed. The conversation goes one of two ways depending on the command, but in general, one will be sending bytes of information, while the recipient will be acknowledging after receiving each packet. If the command bit is a 0, the master is doing all the talking since this is a write operation. After initial acknowledgement from the slave, the master will dictate an address, which cabinet the slave should begin writing to. It then will begin transmitting as many bytes of data as it wants before giving a STOP signal, letting the slaves know the conversation is over. However, if the command bit is a 1, the slave is doing all the talking. The slave will start at whatever cabinet is open, and begin dictating as many bytes as the master acknowledges, before giving the NOT ACKNOWLEDGE (NACK) signal, followed by a STOP signal, terminating the conversation.