Skip to content

Commit cb9931f

Browse files
Set available() to 0 when Wire.requestFrom fails (earlephilhower#259)
Wire::requestFrom() returns the number of bytes read from the slave. In the case of error, the slave can end up returning a very large integer for PICO_GENERIC_ERROR which would then be used as the # of bytes read causing crashes and errors. Running TalkingToMyself without connecting the I2C ports would show this behavior. Now, when PICO_GENERIC_ERROR is returned, set the read-back buffer len to 0 explicitly.
1 parent 6b6254e commit cb9931f

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

libraries/Wire/Wire.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ void TwoWire::beginTransmission(uint8_t addr) {
192192
}
193193
_addr = addr;
194194
_buffLen = 0;
195+
_buffOff = 0;
195196
_txBegun = true;
196197
}
197198

@@ -201,6 +202,9 @@ size_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool stopBit) {
201202
}
202203

203204
_buffLen = i2c_read_blocking_until(_i2c, address, _buff, quantity, !stopBit, make_timeout_time_ms(50));
205+
if (_buffLen == PICO_ERROR_GENERIC) {
206+
_buffLen = 0;
207+
}
204208
_buffOff = 0;
205209
return _buffLen;
206210
}

0 commit comments

Comments
 (0)