Add functions to check for I2C bus speed and set it

This commit is contained in:
oscgonfer 2025-07-11 14:39:26 +02:00
parent f43fbe0068
commit b289eaa91e
3 changed files with 57 additions and 16 deletions

View File

@ -110,26 +110,43 @@ uint16_t ScanI2CTwoWire::getRegisterValue(const ScanI2CTwoWire::RegisterLocation
return value;
}
/// for SEN5X detection
bool probeSEN5X(const ScanI2C::DeviceAddress& addr, TwoWire* i2cBus) {
uint8_t cmd[] = { 0xD0, 0x33 }; // Read Serial Number command
uint8_t rxBuf[9] = {0};
bool ScanI2CTwoWire::setClockSpeed(I2CPort port, uint32_t speed) {
i2cBus->beginTransmission(addr.address);
i2cBus->write(cmd, 2);
if (i2cBus->endTransmission() != 0)
return false;
DeviceAddress addr(port, 0x00);
TwoWire *i2cBus;
delay(20); // wait for response
#if WIRE_INTERFACES_COUNT == 2
if (port == I2CPort::WIRE1) {
i2cBus = &Wire1;
} else {
#endif
i2cBus = &Wire;
#if WIRE_INTERFACES_COUNT == 2
}
#endif
if (i2cBus->requestFrom(addr.address, (uint8_t)9) != 9)
return false;
for (int i = 0; i < 9 && i2cBus->available(); ++i)
rxBuf[i] = i2cBus->read();
return true;
return i2cBus->setClock(speed);
}
uint32_t ScanI2CTwoWire::getClockSpeed(I2CPort port) {
DeviceAddress addr(port, 0x00);
TwoWire *i2cBus;
#if WIRE_INTERFACES_COUNT == 2
if (port == I2CPort::WIRE1) {
i2cBus = &Wire1;
} else {
#endif
i2cBus = &Wire;
#if WIRE_INTERFACES_COUNT == 2
}
#endif
return i2cBus->getClock();
}
/// for SEN5X detection
String readSEN5xProductName(TwoWire* i2cBus, uint8_t address) {
uint8_t cmd[] = { 0xD0, 0x14 };
uint8_t response[48] = {0};

View File

@ -29,6 +29,9 @@ class ScanI2CTwoWire : public ScanI2C
size_t countDevices() const override;
bool setClockSpeed(ScanI2C::I2CPort, uint32_t);
uint32_t getClockSpeed(ScanI2C::I2CPort);
protected:
FoundDevice firstOfOrNONE(size_t, DeviceType[]) const override;

View File

@ -472,6 +472,7 @@ void setup()
Wire.setSCL(I2C_SCL);
Wire.begin();
#elif defined(I2C_SDA) && !defined(ARCH_RP2040)
LOG_INFO("Starting Bus with (SDA) %d and (SCL) %d: ", I2C_SDA, I2C_SCL);
Wire.begin(I2C_SDA, I2C_SCL);
#elif defined(ARCH_PORTDUINO)
if (settingsStrings[i2cdev] != "") {
@ -544,6 +545,26 @@ void setup()
i2cScanner->scanPort(ScanI2C::I2CPort::WIRE);
#endif
#ifdef I2C_CLOCK_SPEED
uint32_t currentClock;
currentClock = i2cScanner->getClockSpeed(ScanI2C::I2CPort::WIRE);
LOG_INFO("Clock speed: %uHz on WIRE", currentClock);
LOG_DEBUG("Setting Wire with defined clock speed, %uHz...", I2C_CLOCK_SPEED);
if(!i2cScanner->setClockSpeed(ScanI2C::I2CPort::WIRE, I2C_CLOCK_SPEED)) {
LOG_ERROR("Unable to set clock speed on WIRE");
} else {
currentClock = i2cScanner->getClockSpeed(ScanI2C::I2CPort::WIRE);
LOG_INFO("Set clock speed: %uHz on WIRE", currentClock);
}
// LOG_DEBUG("Starting Wire with defined clock speed, %d...", I2C_CLOCK_SPEED);
// if(!i2cScanner->setClockSpeed(ScanI2C::I2CPort::WIRE1, I2C_CLOCK_SPEED)) {
// LOG_ERROR("Unable to set clock speed on WIRE1");
// } else {
// LOG_INFO("Set clock speed: %d on WIRE1", I2C_CLOCK_SPEED);
// }
#endif
auto i2cCount = i2cScanner->countDevices();
if (i2cCount == 0) {
LOG_INFO("No I2C devices found");