Skip to content

Commit 67eab5c

Browse files
authored
Merge pull request #494 from maidnl/i2c_empty_bus_timeout
Reducing excessive I2C timeout
2 parents 77a24c6 + 809c48e commit 67eab5c

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

libraries/Wire/Wire.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ TwoWire::TwoWire(int scl, int sda, WireAddressMode_t am /*= ADDRESS_MODE_7_BITS*
191191
is_master(true),
192192
is_sci(false),
193193
address_mode(am),
194-
timeout(1000),
194+
timeout_us(1000),
195195
transmission_begun(false),
196196
data_too_long(false),
197197
rx_index(0),
@@ -465,7 +465,7 @@ void TwoWire::end(void) {
465465

466466

467467
/* -------------------------------------------------------------------------- */
468-
uint8_t TwoWire::read_from(uint8_t address, uint8_t* data, uint8_t length, unsigned int timeout_ms, bool sendStop) {
468+
uint8_t TwoWire::read_from(uint8_t address, uint8_t* data, uint8_t length, unsigned int timeout_us, bool sendStop) {
469469
/* -------------------------------------------------------------------------- */
470470
/* ??? does this function make sense only for MASTER ???? */
471471

@@ -480,8 +480,8 @@ uint8_t TwoWire::read_from(uint8_t address, uint8_t* data, uint8_t length, unsig
480480
err = m_read(&m_i2c_ctrl,data,length,!sendStop);
481481
}
482482
}
483-
uint32_t const start = millis();
484-
while(((millis() - start) < timeout_ms) && bus_status == WIRE_STATUS_UNSET && err == FSP_SUCCESS) {
483+
uint32_t const start = micros();
484+
while(((micros() - start) < timeout_us) && bus_status == WIRE_STATUS_UNSET && err == FSP_SUCCESS) {
485485

486486
}
487487
}
@@ -494,7 +494,7 @@ uint8_t TwoWire::read_from(uint8_t address, uint8_t* data, uint8_t length, unsig
494494
}
495495

496496
/* -------------------------------------------------------------------------- */
497-
uint8_t TwoWire::write_to(uint8_t address, uint8_t* data, uint8_t length, unsigned int timeout_ms, bool sendStop) {
497+
uint8_t TwoWire::write_to(uint8_t address, uint8_t* data, uint8_t length, unsigned int timeout_us, bool sendStop) {
498498
/* -------------------------------------------------------------------------- */
499499
uint8_t rv = END_TX_OK;
500500
fsp_err_t err = FSP_ERR_ASSERTION;
@@ -508,8 +508,8 @@ uint8_t TwoWire::write_to(uint8_t address, uint8_t* data, uint8_t length, unsign
508508
err = m_write(&m_i2c_ctrl,data,length,!sendStop);
509509
}
510510
}
511-
uint32_t const start = millis();
512-
while(((millis() - start) < timeout_ms) && bus_status == WIRE_STATUS_UNSET && err == FSP_SUCCESS) {
511+
uint32_t const start = micros();
512+
while(((micros() - start) < timeout_us) && bus_status == WIRE_STATUS_UNSET && err == FSP_SUCCESS) {
513513

514514
}
515515

@@ -642,7 +642,7 @@ void TwoWire::beginTransmission(int address) {
642642
/* -------------------------------------------------------------------------- */
643643
uint8_t TwoWire::endTransmission(bool sendStop) {
644644
/* -------------------------------------------------------------------------- */
645-
uint8_t ret = write_to(master_tx_address, tx_buffer, tx_index, timeout, sendStop);
645+
uint8_t ret = write_to(master_tx_address, tx_buffer, tx_index, timeout_us, sendStop);
646646
transmission_begun = false;
647647
return ret;
648648
}
@@ -687,7 +687,7 @@ size_t TwoWire::requestFrom(uint8_t address, size_t quantity, uint32_t iaddress,
687687
quantity = I2C_BUFFER_LENGTH;
688688
}
689689
// perform blocking read into buffer
690-
uint8_t read = read_from(address, rx_buffer, quantity, timeout, sendStop);
690+
uint8_t read = read_from(address, rx_buffer, quantity, timeout_us, sendStop);
691691
// set rx buffer iterator vars
692692
rx_index = read;
693693
rx_extract_index = 0;
@@ -835,7 +835,12 @@ void TwoWire::flush(void) {
835835
while(bus_status != WIRE_STATUS_TX_COMPLETED && bus_status != WIRE_STATUS_TRANSACTION_ABORTED) {}
836836
}
837837

838-
838+
/* -------------------------------------------------------------------------- */
839+
void TwoWire::setWireTimeout(unsigned int _timeout_us, bool reset_on_timeout) {
840+
/* -------------------------------------------------------------------------- */
841+
(void)reset_on_timeout;
842+
timeout_us = _timeout_us;
843+
}
839844

840845

841846
#if WIRE_HOWMANY > 0

libraries/Wire/Wire.h

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@ class TwoWire : public arduino::HardwareI2C {
124124
void onRequest( void (*)(void) );
125125

126126
void setBusStatus(WireStatus_t);
127+
/* set timeout in us for I2C communication (default is 1000 us)
128+
the second parameter has been added for compatibility but it has no effect
129+
130+
Please note: on uno R4 (both minima and wifi) the timeout has only effect
131+
when I2C pull up are NOT mounted (if they are mounted, as they should, the
132+
I2C is immediately stopped due to a NACK and so the timeout has no effect).
133+
On Portenta C33 however timeout is used in both cases (with or without
134+
I2C pull up) because at low level the NACK does not immediately stop the I2C
135+
communication. */
136+
void setWireTimeout(unsigned int _timeout_us = 1000, bool reset_on_timeout = false);
127137

128138
inline size_t write(unsigned long n) { return write((uint8_t)n); }
129139
inline size_t write(long n) { return write((uint8_t)n); }
@@ -168,8 +178,15 @@ class TwoWire : public arduino::HardwareI2C {
168178
int channel;
169179
bool is_sci;
170180
WireAddressMode_t address_mode;
171-
172-
unsigned int timeout;
181+
/* I2C timeout in micro seconds
182+
183+
Please note: on uno R4 (both minima and wifi) the timeout has only effect
184+
when I2C pull up are NOT mounted (if they are mounted, as they should, the
185+
I2C is immediately stopped due to a NACK and so the timeout has no effect).
186+
On Portenta C33 however timeout is used in both cases (with or without
187+
I2C pull up) because at low level the NACK does not immediately stop the I2C
188+
communication. */
189+
unsigned int timeout_us;
173190
bool transmission_begun;
174191
bool data_too_long;
175192

@@ -235,4 +252,4 @@ extern TwoWire Wire3;
235252
#endif
236253

237254
#endif
238-
#endif
255+
#endif

0 commit comments

Comments
 (0)