In this lesson, you will learn how to use the Repeated Start condition to link I2C write and read operations into a single, uninterrupted atomic transaction.
The Problem with Releasing the Bus
In standard I2C communication, a complete transaction typically consists of a Start condition, the data transfer, and a Stop condition. Once you send a Stop condition, the bus is effectively "released." If another master on your bus is waiting to communicate, it could seize the bus the moment you let go.
If you are performing a "Write-then-Read" sequence—such as telling a sensor which register you want to read, and then immediately reading its value—releasing the bus between those two steps is dangerous. A competing master might jump in, change the register pointer, or confuse the sensor, causing your read to return garbage data.
The Repeated Start Solution
To prevent this, I2C provides the Repeated Start condition. Instead of sending a Stop condition after your initial write, you immediately issue another Start condition.
By skipping the Stop, you maintain control over the bus. Because the bus is never released, no other master can claim it until your entire multi-part operation is complete. This makes your sequence atomic, meaning the peripheral device sees the write and the subsequent read as one continuous, unbroken event.
Conceptual Implementation
Think of it as a specialized transition in your state machine. Instead of the standard Stop sequence, you jump straight back to the Start routine.
// Atomic Write-then-Read logic
I2C_Start();
I2C_Write(DEVICE_ADDRESS | WRITE_BIT);
I2C_Write(REG_ADDRESS); // Tell sensor what to read
// Instead of I2C_Stop(), we go straight to the next start
I2C_Repeated_Start();
I2C_Write(DEVICE_ADDRESS | READ_BIT);
data = I2C_Read(); // Read the requested register safely
I2C_Stop(); // Finalize the transaction
By using the Repeated_Start() function, you ensure the hardware keeps the SDA and SCL lines in a state that signals a new message without signaling the end of the current session. This guarantees that your transaction remains consistent, even on a busy multi-master bus.
Key Takeaway: The Repeated Start condition allows you to chain multiple I2C operations together without releasing the bus, ensuring your data remains synchronized and protected from interference.