How to end sending data over I2C by Slave or Master?

But I wonder how does this handshaking takes place between a Master and Slave when the Slave is sender and the Master is receiver and only the Slave (sender) knows when there is no more data to send to the receiver?

This isn't supposed to happen. i2c is a very defined protocol and each slave device should be known to each master. Typically, the master requests the slave to transmit, and either the message size is fixed, the message is sequential, or the message includes how many bytes will be sent.

  1. Master writes a register address, then master switches to read and the slave sends a byte. That's all the master wants, sends a stop.

  2. Master writes a register address, then master switches to read and the slave sends a byte, then the master reads again, so the slave sends the next byte (or same byte). The master reads how ever many it wants, arbitrarily, then sends a stop.

  3. Master writes a register address, then master switches to read and the slave sends a byte. That byte tells the master how many bytes that register should have. Master reads and slave sends that many, then master stops.

All communication is controlled by the master. The slave does nothing the master doesn't want it to do. The master controls the speed of the clock (clock stretching not withstanding) and how many bytes are read. At no time should the slave try forcing the data line when the master did not tell it to. The data structure should be known before hand.


Those are the two options.

If slave receiver does not accept more data it can NAK the byte, but master is still responsible for sending the stop condition.

For a slave transmitter, it can't signal anything to stop, it must be known beforehand, or it could be encoded in the data, e.g. text string is terminated with a zero so slave can transmit zeroes until master stops.

Usually, this is rare and there is a known protocol so master must always know beforehand how many bytes it will read or write in a single transaction, even if it means transferring first a fixed size header between devices how much to transfer.


Let us say a Slave or Master is sending multiple bytes to the receiver on I2C bus and the number of bytes is not defined before hand. So then how will the sender tell the receiver that it has no more data to send?

If Master is the sender, then he knows how many bytes have to be sent. Master will signal the end of his data transfer by sending a STOP condition after sending the last byte, and terminate the transaction.

Note that Master can do this anytime. Slave can send NACK to say that it doesn't want to receive anymore data. But only Master decides whether to stop this transaction.

But I wonder how does this handshaking takes place between a Master and Slave when the Slave is sender and the Master is receiver and only the Slave (sender) knows when there is no more data to send to the receiver?

If Slave is the sender, Master should beforehand know how many bytes have to be received. Master will signal this by sending a NACK after receiving the last byte so that the slave will no longer drive the SDA line. Master can now initiate a STOP condition and terminate the transaction.

Have a good read here : TEXAS i2c