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

This commit is contained in:
alexklimaj
2022-04-09 14:23:52 -04:00
committed by Daniel Agar
parent 777540bd02
commit 017f860f44
+11 -6
View File
@@ -102,14 +102,19 @@ int INA226::read(uint8_t address, int16_t &data)
{ {
// read desired little-endian value via I2C // read desired little-endian value via I2C
uint16_t received_bytes; 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) { for (size_t i = 0; i < 3; i++) {
data = swap16(received_bytes); ret = transfer(&address, 1, (uint8_t *)&received_bytes, sizeof(received_bytes));
} else { if (ret == PX4_OK) {
perf_count(_comms_errors); data = swap16(received_bytes);
PX4_DEBUG("i2c::transfer returned %d", ret); break;
} else {
perf_count(_comms_errors);
PX4_DEBUG("i2c::transfer returned %d", ret);
}
} }
return ret; return ret;