When Twi::readFrom() is passed a 0 length the code does not test for this condition and reads many bytes instead of reading none.
What follows is patched code that solves this problem.
unsigned char Twi::readFrom(unsigned char address, unsigned char* buf, unsigned int len, unsigned char sendStop)
{
unsigned int i;
if (!write_start())
{
return 4; //line busy
}
if (!write_byte(((address << 1) | 1) & 0xFF))
{
if (sendStop)
{
write_stop();
}
return 2;//received NACK on transmit of address
}
// MJN: ??? Handle case of 0 bytes requested by
// checking for 0 and reading nothing.
if (len > 0) {
// End of MJN fragment
for (i = 0; i < (len - 1); i++)
{
buf[i] = read_byte(false);
}
buf[len - 1] = read_byte(true);
// MJN: Close conditional
};
// MJN: end of MJN fragment
// More code follows that needs no changes.