Go Down

Topic: I2C making empty read after Wire.endTransmission(true) (Read 131 times) previous topic - next topic



I'm using the code shown below to interface with an external I2C device. First I send some data to set the coordinates of the register that I want to read, then I make a read operation and after that there is a "empty write" operation which shouldn't happen.

Code: [Select]

      Wire.endTransmission(false); //A repeated start can be used to fetch the data
      Wire.requestFrom(_slaveNumber, dataNum);
      while(Wire.available()) {
        *data = Wire.read();
      Wire.endTransmission(true);    // stop transmitting - after this there are no more calls
to i2c library but there is that empty write

This could be a bug in the i2c library which has gone unnoticed, it's pretty visible using a sniffer as you can see in the attached image. It shows a read to the same registers of an external device every second. As you can see the empty write comes just after the read operation. Take the third line as example (first empty read): the previous read operation starts at time stamp 305.484 and lasts for 409us, so ending at 305.484+409 = 305.893, and the empty write starts at 305.923, just 30us after the previous one.


The last Wire.endTransmission() isn't necessary. Just remove that line and your code might start to work. If not, post the complete sketch, so we may help you.


My code does work, that empty write isn't causing any malfunction but it caught my eye when debugging with the sniffer. If I remove the last Wire.endTransmission(true) then how does it sen the stop condition?


The RequestFrom() does send the stop condition. The endTransmission() method is used only in write actions, you must not use it in read actions.


Ok, I see, the RequestFrom() puts all bytes in a buffer and sends the STOP condition before having them readable through Wire.available().


Go Up