I've just been doing some tests using the Two Wire Interface to communicate between two UNOs. Since I've never used TWI before I took advantage of the "slaveSender" and "masterReader" example sketches.
Everything worked as expected until I tried to build in some robustness to handle the slave device being unavailable. I ran into a problem with Wire.requestFrom() hanging indefinitely.
Here is the section of code of interest (running on the master device). This code runs once each time I press a push button BTW.
if (Wire.requestFrom(8, 6)) { // request 6 bytes from slave device #8
for (int k=0; k<6; k++) {
char c = Wire.read();
Serial.print(c);
}
}
else Serial.print("TWI slave not responding");
The slave device is programed to respond with "HELLO ", so everything works perfectly if the "slave" UNO board is up and running. I hit a push button and the masterReader requests the data, the slave sends the data, and "HELLO " gets printed.
If however I power off the slave board then the masterReader sketch hangs indefinitely at the Wire.requestFrom() statement when I press the button. Further, even after I restore power to the slave board it (the master) remains completely unresponsive. I can reset the slave but still no response. Only after reseting the master does everything come back to life.
BTW. I've done some debugging to trace the point where it hangs and I'm 100% certain it is at the Wire.requestFrom() statement and not anywhere else.
No other failures are causing any problems (apart from this one where the slave loses power). Here is a summary of the failures I've tested.
1. Hold reset on slave while requesting data: Prints "TWI slave not responding".
2. Disconnect only SCL and then request data: Prints "TWI slave not responding".
3. Disconnect only SDA and then request data: Prints "TWI slave not responding".
4. Disconnect both SCL and SDA then request data: Prints "TWI slave not responding".
5. Slave power off and then request data: Master hangs indefinitely and is not recoverable (after power is restored) by any combination of events other than a Master reset.