Closed
Description
From arduino-1.5.2\hardware\arduino\sam\libraries\Wire\Wire.cpp:
int TwoWire::available(void) {
return rxBufferLength - rxBufferIndex;
}
rxBufferLength
is set in requestFrom()
:
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop) {
if (quantity > BUFFER_LENGTH)
quantity = BUFFER_LENGTH;
// perform blocking read into buffer
int readed = 0;
TWI_StartRead(twi, address, 0, 0);
do {
// Stop condition must be set during the reception of last byte
if (readed + 1 == quantity)
TWI_SendSTOPCondition( twi);
TWI_WaitByteReceived(twi, RECV_TIMEOUT); // *** Even if this times out...
rxBuffer[readed++] = TWI_ReadByte(twi); // *** this will still be incremented
} while (readed < quantity);
TWI_WaitTransferComplete(twi, RECV_TIMEOUT);
// set rx buffer iterator vars
rxBufferIndex = 0;
rxBufferLength = readed;
return readed;
}
Whether or not TWI_WaitByteReceived()
times out, readed
is still incremented. This means that available()
will always return the number of bytes requested, whether or not the bytes were actually read.