Pages: [1]   Go Down
Author Topic: I2C making empty read after Wire.endTransmission(true)  (Read 85 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

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:
      Wire.beginTransmission(_slaveNumber);
      Wire.write(page);
      Wire.write(reg);
      Wire.endTransmission(false); //A repeated start can be used to fetch the data
           
      Wire.requestFrom(_slaveNumber, dataNum);
           
      while(Wire.available()) {
        *data = Wire.read();
        data++;
      }
      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.



* i2c_bug.png (26.01 KB, 728x234 - viewed 7 times.)
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 111
Posts: 5236
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 111
Posts: 5236
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Thanks!
Logged

Pages: [1]   Go Up
Jump to: