Skip to content

[I2C] Harden error management #608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions cores/arduino/stm32/twi.c
Original file line number Diff line number Diff line change
Expand Up @@ -723,16 +723,19 @@ i2c_status_e i2c_master_write(i2c_t *obj, uint8_t dev_address,
while ((HAL_I2C_GetState(&(obj->handle)) != HAL_I2C_STATE_READY)
&& (ret == I2C_OK)) {
delta = (HAL_GetTick() - tickstart);
if (delta > I2C_TIMEOUT_TICK) {
uint32_t err = HAL_I2C_GetError(&(obj->handle));
if ((delta > I2C_TIMEOUT_TICK)
|| ((err & HAL_I2C_ERROR_TIMEOUT) == HAL_I2C_ERROR_TIMEOUT)) {
ret = I2C_TIMEOUT;
} else if (HAL_I2C_GetError(&(obj->handle)) != HAL_I2C_ERROR_NONE) {
} else if (err != HAL_I2C_ERROR_NONE) {
ret = I2C_ERROR;
}
}
}
/* When Acknowledge failure occurs (Slave don't acknowledge it's address)
Master restarts communication */
} while (HAL_I2C_GetError(&(obj->handle)) == HAL_I2C_ERROR_AF && delta < I2C_TIMEOUT_TICK);
} while (((HAL_I2C_GetError(&(obj->handle)) & HAL_I2C_ERROR_AF) == HAL_I2C_ERROR_AF)
&& (delta < I2C_TIMEOUT_TICK));

return ret;
}
Expand Down Expand Up @@ -784,16 +787,19 @@ i2c_status_e i2c_master_read(i2c_t *obj, uint8_t dev_address, uint8_t *data, uin
while ((HAL_I2C_GetState(&(obj->handle)) != HAL_I2C_STATE_READY)
&& (ret == I2C_OK)) {
delta = (HAL_GetTick() - tickstart);
if (delta > I2C_TIMEOUT_TICK) {
uint32_t err = HAL_I2C_GetError(&(obj->handle));
if ((delta > I2C_TIMEOUT_TICK)
|| ((err & HAL_I2C_ERROR_TIMEOUT) == HAL_I2C_ERROR_TIMEOUT)) {
ret = I2C_TIMEOUT;
} else if (HAL_I2C_GetError(&(obj->handle)) != HAL_I2C_ERROR_NONE) {
} else if (err != HAL_I2C_ERROR_NONE) {
ret = I2C_ERROR;
}
}
}
/* When Acknowledge failure occurs (Slave don't acknowledge it's address)
Master restarts communication */
} while (HAL_I2C_GetError(&(obj->handle)) == HAL_I2C_ERROR_AF && delta < I2C_TIMEOUT_TICK);
} while (((HAL_I2C_GetError(&(obj->handle)) & HAL_I2C_ERROR_AF) == HAL_I2C_ERROR_AF)
&& (delta < I2C_TIMEOUT_TICK));

return ret;
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/Wire/src/Wire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ uint8_t TwoWire::endTransmission(uint8_t sendStop)
//
uint8_t TwoWire::endTransmission(void)
{
return endTransmission(true);
return endTransmission((uint8_t)true);
}

// must be called in:
Expand Down