Browse Source

Add I2C retries in INA226 to prevent publishing 0's on a single read failure

v1.13.0-BW
alexklimaj 3 years ago committed by Daniel Agar
parent
commit
017f860f44
  1. 17
      src/drivers/power_monitor/ina226/ina226.cpp

17
src/drivers/power_monitor/ina226/ina226.cpp

@ -102,14 +102,19 @@ int INA226::read(uint8_t address, int16_t &data) @@ -102,14 +102,19 @@ int INA226::read(uint8_t address, int16_t &data)
{
// read desired little-endian value via I2C
uint16_t received_bytes;
const int ret = transfer(&address, 1, (uint8_t *)&received_bytes, sizeof(received_bytes));
int ret = PX4_ERROR;
if (ret == PX4_OK) {
data = swap16(received_bytes);
for (size_t i = 0; i < 3; i++) {
ret = transfer(&address, 1, (uint8_t *)&received_bytes, sizeof(received_bytes));
} else {
perf_count(_comms_errors);
PX4_DEBUG("i2c::transfer returned %d", ret);
if (ret == PX4_OK) {
data = swap16(received_bytes);
break;
} else {
perf_count(_comms_errors);
PX4_DEBUG("i2c::transfer returned %d", ret);
}
}
return ret;

Loading…
Cancel
Save