From c92206909d362a955dc71c5ce9bc8a51723d4dc4 Mon Sep 17 00:00:00 2001 From: Kevin Chang Date: Mon, 17 Feb 2014 19:49:44 -0800 Subject: [PATCH] SAM Wire: fixed NACK/timeout behavior of requestFrom() and available() "readed" is no longer incremented in requestFrom() if TWI_WaitByteReceived() gets a NACK or times out. This corrects the behavior (return values) of requestFrom() and available() to match the Arduino reference. Fixes arduino/Arduino#1311 --- hardware/arduino/sam/libraries/Wire/Wire.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/hardware/arduino/sam/libraries/Wire/Wire.cpp b/hardware/arduino/sam/libraries/Wire/Wire.cpp index cbf46db90..f7080d138 100644 --- a/hardware/arduino/sam/libraries/Wire/Wire.cpp +++ b/hardware/arduino/sam/libraries/Wire/Wire.cpp @@ -125,8 +125,10 @@ uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop if (readed + 1 == quantity) TWI_SendSTOPCondition( twi); - TWI_WaitByteReceived(twi, RECV_TIMEOUT); - rxBuffer[readed++] = TWI_ReadByte(twi); + if (TWI_WaitByteReceived(twi, RECV_TIMEOUT)) + rxBuffer[readed++] = TWI_ReadByte(twi); + else + break; } while (readed < quantity); TWI_WaitTransferComplete(twi, RECV_TIMEOUT);