8 years, 9 months ago.

How do one execute an I2C read correctly?

From the documentation, by setting the 4th parameter of I2C::read() and I2C::write() functions, we could decide whether or not a Stop Condition is generated at the end. By default, a stop condition should be generated.

However, according to this tutorial, to accomplish a read, the author first call I2C::write() to issue the sub address with only three parameter. This should mean a Stop condition is generated between the writing of the sub-address and the issuing of the read command.

What exactly is going on here? Is it OK for the read condition to be generated at such timing?

1 Answer

8 years, 9 months ago.

The fourth parameter is a boolean that controls whether of not you want a ''repeated start" for the next I2C operation. A repeated Start means that you skip the Stop condition for the current operation. By default the boolean is false, so by default you will generate a Stop at the end of the current operation. In most cases the slave device will accept a Stop and still maintain the selected subaddress so that the next Read will return the desired data. The real purpose of the Repeated Start is to make sure that no other I2C master device can hijack the bus and the slave device between the end of a Write and the Start of the subsequent Read. This can happen when the current Master releases the I2C bus by generating a Stop and is then too slow to generate the subsequent Start and Read. Obviously that would result in unpredictable outcomes when the overtaken Master tries to resume the interrupted Read operation later on and expects that the Slave is still ready to send the data from the previously selected subaddress. So in a single master case you can usually generate a Stop and then continue with a new Start without problems.However, always check the Slave datasheet. Some could have different behaviour and reset the subaddress after receiving a Stop.

Accepted Answer