I have an Arduino as I2C Slave which returns 4 byte after a request from the Master.
However sometimes 0 bytes are received by the Master.
I run into this problem with an ATmega8L as Slave, but the same problem happens with a Arduino Uno as Slave. A Mega 2560 is the Master with onboard pull-up resistors of 10k.
The Slave acts as a sensor, with a (simulated) register address that has to be set first.
Changing the pull-up resistors from 10k to 2k doesn't change anything.
Adding 470pF to SDA or SCL doesn't change it.
Adding more decoupling capacitors or different ground paths makes no difference.
Using a ATmegaL at 8MHz makes it worse.
Using 5V or 3.3V (with level shifter) for that ATMegaL doesn't make a difference.
The problem gets a lot worse if don't release the bus after the (simulated) register address is set. Wire.endTransmission(); -> 1 in 50 to 1 in 1000 is wrong. Wire.endTransmission(false); -> they are all wrong !
Code for Master
(the Master has 'avail' being zero, no other errors)
UPDATE 2: This fixes the problem with the "Wire.endTransmission(false);" causing it to fail always.
The timing problem that occurs only now and then (and more with 8MHz I2C slave) still exists. The bug notified on Github:
UPDATE 3: The sketch above has been tested on Teensy 2.0 (ATmega32U4) and Teensy 3.0 (MK20DX128 ARM chip). The problem when using the repeated start with "Wire.endTransmission(false);" also exists in the library for Teensy 2.0, but without it there is no error at all. Teensy 3.0 runs also without that error.
I don't know what to think of it: I have always an error within a number of minutes, but the Teensy 2.0 with avr chip doesn't have them.
I hooked up a couple official Arduino boards. The slave code is running on a Mega 2560 and the master is running on an Uno R1.
So far, it's looking like no errors:
good response, ok=524, err=0
EDIT: looks like the Mega2560 & Uno are having occasional problems....
good response, ok=1253, err=2
good response, ok=1254, err=2
good response, ok=1255, err=2
avail = 0, ok=1255, err=3
good response, ok=1256, err=3
good response, ok=1257, err=3
good response, ok=1258, err=3
What you found is the problem: 'avail' being zero.
I had made changes to the I2C library, so I downloaded a fresh 1.0.5 and used your sketches with Mega 2560 (R1 or R2) as Slave and Uno (R1 or R2) as Master.
Would you believe it, the error occurs all the time! Only 1 in 100 was good.
I added a delay in the Master between Wire.endtransmission() and Wire.requestFrom(). That seems to solve the problem.
byte error = Wire.endTransmission(); // release bus (works)
delay(1); // ---> seems to fix timing problem
...
Instead of that "delay(1);", I tried changing the delay at the bottom of the loop to "delay(random( 200, 1500));", that did not fix the problem.
To find the minimal delay, I reduced the sketch and found that about 6 microseconds is enough with a 16MHz Master and 16MHz Slave.
According to the timing info by Nick Gammon ( http://gammon.com.au/interrupts ), one or two interrupt service routines could be executed in 6 microseconds.
...
Wire.endTransmission(); // release bus (works)
delayMicroseconds( 6); // seems to fix the timing problem.
Wire.requestFrom( 0x2B, 4, true); // release bus after this
...
It's strange that your boards showed so many errors. On these two Arduino boards, it seems to be about 1 error in every 300 to 400 tests.
I can't get the error to happen on any Teensy board, even the AVR ones. But Teensy2 does have a much more efficient timer0 interrupt for millis() and the USB communication doesn't interrupt the CPU for every byte like Arduino Uno's hardware serial does. Since adding a small delay makes this work on Arduino, it seems doubly strange the problem never happens on Teensy2 where interrupts aren't taking as much CPU time.
Hopefully someone will investigate and truly get to the bottom of what's going on here?
Yes, let's hope so.
I'm not investigating it further. I just use the delay.
Thanks for all you help.
I have this also without use of the Serial library and without USB connection.
I made the system led turn on (in the Master and in the Slave) if that error occurs, to be able to run it without my computer.
Perhaps that optimization for timer0 has effect on this.
This came to my mind: The functions endTransmission() and requestFrom() need time for the parameters and stack and so. Suppose there will be a total time of 4us for that. With de delay of 6us, the total between the 'real' action is 10us. That is remarkable, since 10us is a clock pulse of the I2C at 100kHz.
It's not a bug per se but an aspect of the hardware for the Atmega 328. Per the datasheet on page 326, there is a parameter called tBUF which is the Bus free time between a STOP and START condition and is spec'd at 4.7us for 100kHz bus speed and 1.3us for bus speeds above 100kHz. It works very well using the repeated start instead, but you need to modify the Wire library on the slave device to handle the repeated starts.