Skip to content

Commit d26a358

Browse files
tommagTom Magnier
andauthored
I2C lib: Handle timeout in write() (#2458)
Co-authored-by: Tom Magnier <[email protected]>
1 parent aaaa99d commit d26a358

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

libraries/Wire/src/Wire.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,17 @@ size_t TwoWire::write(uint8_t ucData) {
409409
}
410410

411411
if (_slave) {
412-
// Wait for a spot in the TX FIFO
413-
while (0 == (_i2c->hw->status & (1 << 1))) { /* noop wait */ }
414-
_i2c->hw->data_cmd = ucData;
415-
return 1;
412+
// Wait for a spot in the TX FIFO and return in case of timeout
413+
auto end = make_timeout_time_ms(_timeout);
414+
while ((i2c_get_write_available(_i2c) == 0) && !time_reached(end)) { /* noop wait */ }
415+
416+
if (i2c_get_write_available(_i2c) > 0) {
417+
_i2c->hw->data_cmd = ucData;
418+
return 1;
419+
} else {
420+
_handleTimeout(_reset_with_timeout);
421+
return 0;
422+
}
416423
} else {
417424
if (!_txBegun || (_buffLen == sizeof(_buff))) {
418425
return 0;

0 commit comments

Comments
 (0)