Skip to content
Prev Previous commit
Next Next commit
Fixed a bug in timeout logic where millis() was used instead of micros()
  • Loading branch information
HanzHager authored Oct 9, 2025
commit 7a5325db374a632cd9fb5e762d162da53f0cd1a2
12 changes: 8 additions & 4 deletions libraries/Wire/Wire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ uint8_t TwoWire::read_from(uint8_t address, uint8_t* data, uint8_t length, uint3
}

uint32_t const start = micros();
while (((timeout_us == 0ul) || ((millis() - start) < timeout_us)) &&
while (((timeout_us == 0ul) || ((micros() - start) < timeout_us)) &&
bus_status == WIRE_STATUS_UNSET && err == FSP_SUCCESS) {
}
if ((err == FSP_SUCCESS) && (bus_status == WIRE_STATUS_UNSET)) {
Expand Down Expand Up @@ -522,7 +522,7 @@ uint8_t TwoWire::write_to(uint8_t address, uint8_t* data, uint8_t length, uint32
}

uint32_t const start = micros();
while (((timeout_us == 0ul) || ((millis() - start) < timeout_us)) &&
while (((timeout_us == 0ul) || ((micros() - start) < timeout_us)) &&
bus_status == WIRE_STATUS_UNSET && err == FSP_SUCCESS) {
}

Expand Down Expand Up @@ -680,6 +680,7 @@ void TwoWire::clearWireTimeoutFlag(void){
void TwoWire::handleTimeout(bool reset){
/* -------------------------------------------------------------------------- */
timed_out_flag = true;

if (reset) { //TBD; What do we do here? like fixHungWire()?
// TBD, Is this the way to go to reset the bus?
// Do we need more to handle devices that hangs the bus?
Expand All @@ -689,15 +690,18 @@ void TwoWire::handleTimeout(bool reset){
}
// TDB, Is this the right way to get back after reset?
//if(m_open != nullptr) {
// if(FSP_SUCCESS == m_open(&m_i2c_ctrl,&m_i2c_cfg)) {
// fsp_err_t err = m_open(&m_i2c_ctrl,&m_i2c_cfg);
// if(FSP_SUCCESS == err) {
// init_ok &= true;
// }
//}
}
}
}





/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* TRANSMISSION BEGIN
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
Expand Down
Loading