Add I2C status on I2cSetDevice

Add I2C status on I2cSetDevice aiding on debugging bus timeouts
This commit is contained in:
Theo Arends 2022-09-09 12:53:36 +02:00
parent d2058c5d5c
commit cc11ee822e
1 changed files with 14 additions and 1 deletions

View File

@ -2324,6 +2324,14 @@ void I2cScan(uint32_t bus) {
// I2C_SCL_HELD_LOW_AFTER_READ 2 = I2C bus error. SCL held low beyond client clock stretch time // I2C_SCL_HELD_LOW_AFTER_READ 2 = I2C bus error. SCL held low beyond client clock stretch time
// I2C_SDA_HELD_LOW 3 = I2C bus error. SDA line held low by client/another_master after n bits // I2C_SDA_HELD_LOW 3 = I2C bus error. SDA line held low by client/another_master after n bits
// I2C_SDA_HELD_LOW_AFTER_INIT 4 = line busy. SDA again held low by another device. 2nd master? // I2C_SDA_HELD_LOW_AFTER_INIT 4 = line busy. SDA again held low by another device. 2nd master?
// 5 = bus busy. Timeout
// https://www.arduino.cc/reference/en/language/functions/communication/wire/endtransmission/
// 0: success
// 1: data too long to fit in transmit buffer
// 2: received NACK on transmit of address
// 3: received NACK on transmit of data
// 4: other error
// 5: timeout
uint8_t error = 0; uint8_t error = 0;
uint8_t address = 0; uint8_t address = 0;
@ -2415,7 +2423,12 @@ bool I2cSetDevice(uint32_t addr, uint32_t bus) {
return false; // If already active report as not present; return false; // If already active report as not present;
} }
myWire.beginTransmission((uint8_t)addr); myWire.beginTransmission((uint8_t)addr);
return (0 == myWire.endTransmission()); // return (0 == myWire.endTransmission());
uint32_t err = myWire.endTransmission();
if (err && (err != 2)) {
AddLog(LOG_LEVEL_DEBUG, PSTR("I2C: Error %d at 0x%02x"), err, addr);
}
return (0 == err);
} }
#endif // USE_I2C #endif // USE_I2C