Wire library question

Can’t seem to find the answer Goggling. Example code is often not a good source as only the happy path is coded properly.

When Arduino is acting as a master and requesting a single byte read, your code would look like:

// Request the byte
Wire.requestFrom(add, 1);

// Determine if we have the byte */
n = Wire.available();

Question: Is the requestFrom blocking? By the time you return from the call has the peripheral attempted to clock in all the bytes and ACKS? Or does one need to spin in a wait loop for some timeout period waiting for the bytes to arrive?

The reason I ask is that under a stress test, I will occasionally not get the needed single byte I requested. Wire.available will return a zero. The failure rate changes from 10% of the time to close to 0% based on what code I have loaded. I have not been able to determine a pattern yet. In the stress test, the failure rate is low (single failure in 100,000 iterations). When I use the read method in the real application it fails 10% of the time.

Should I be coding something more like?:

Wire.requestFrom(add, 1); // request the read

/* Wait for byte to show up */
for (i=0; i<1000; i++) {
  if (Wire.available() == 1) break;
}

if (i == 1000) {
  // Failed
} else {
 b = Wire.read();
}

Bump.

Yes, it is blocking. The Wire library that Arduino uses is blocking, but the AVR TWI is not. Check out the wire library reference below for more details.

Here’s the [Arduino environment and [Atmel chip references (starts at pg 209 for twi).](http://www.atmel.com/images/atmel-8271-8-bit-avr-microcontroller-atmega48a-48pa-88a-88pa-168a-168pa-328-328p_datasheet_summary.pdf)](Arduino Playground - HomePage)