Skip to content

Commit

Permalink
Allow ESP8266 to use multiple i2c busses (esphome#6145)
Browse files Browse the repository at this point in the history
Co-authored-by: Jesse Hills <[email protected]>
  • Loading branch information
LouDou and jesserockz authored Feb 22, 2024
1 parent fd03d87 commit 76a3ffc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
28 changes: 21 additions & 7 deletions esphome/components/i2c/i2c_bus_arduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void ArduinoI2CBus::setup() {
}
next_bus_num++;
#elif defined(USE_ESP8266)
wire_ = &Wire; // NOLINT(cppcoreguidelines-prefer-member-initializer)
wire_ = new TwoWire(); // NOLINT(cppcoreguidelines-owning-memory)
#elif defined(USE_RP2040)
static bool first = true;
if (first) {
Expand All @@ -35,6 +35,16 @@ void ArduinoI2CBus::setup() {
}
#endif

this->set_pins_and_clock_();

this->initialized_ = true;
if (this->scan_) {
ESP_LOGV(TAG, "Scanning i2c bus for active devices...");
this->i2c_scan_();
}
}

void ArduinoI2CBus::set_pins_and_clock_() {
#ifdef USE_RP2040
wire_->setSDA(this->sda_pin_);
wire_->setSCL(this->scl_pin_);
Expand All @@ -43,12 +53,8 @@ void ArduinoI2CBus::setup() {
wire_->begin(static_cast<int>(sda_pin_), static_cast<int>(scl_pin_));
#endif
wire_->setClock(frequency_);
initialized_ = true;
if (this->scan_) {
ESP_LOGV(TAG, "Scanning i2c bus for active devices...");
this->i2c_scan_();
}
}

void ArduinoI2CBus::dump_config() {
ESP_LOGCONFIG(TAG, "I2C Bus:");
ESP_LOGCONFIG(TAG, " SDA Pin: GPIO%u", this->sda_pin_);
Expand Down Expand Up @@ -82,6 +88,10 @@ void ArduinoI2CBus::dump_config() {
}

ErrorCode ArduinoI2CBus::readv(uint8_t address, ReadBuffer *buffers, size_t cnt) {
#if defined(USE_ESP8266)
this->set_pins_and_clock_(); // reconfigure Wire global state in case there are multiple instances
#endif

// logging is only enabled with vv level, if warnings are shown the caller
// should log them
if (!initialized_) {
Expand Down Expand Up @@ -120,6 +130,10 @@ ErrorCode ArduinoI2CBus::readv(uint8_t address, ReadBuffer *buffers, size_t cnt)
return ERROR_OK;
}
ErrorCode ArduinoI2CBus::writev(uint8_t address, WriteBuffer *buffers, size_t cnt, bool stop) {
#if defined(USE_ESP8266)
this->set_pins_and_clock_(); // reconfigure Wire global state in case there are multiple instances
#endif

// logging is only enabled with vv level, if warnings are shown the caller
// should log them
if (!initialized_) {
Expand Down Expand Up @@ -164,7 +178,7 @@ ErrorCode ArduinoI2CBus::writev(uint8_t address, WriteBuffer *buffers, size_t cn
return ERROR_UNKNOWN;
case 2:
case 3:
ESP_LOGVV(TAG, "TX failed: not acknowledged");
ESP_LOGVV(TAG, "TX failed: not acknowledged: %d", status);
return ERROR_NOT_ACKNOWLEDGED;
case 5:
ESP_LOGVV(TAG, "TX failed: timeout");
Expand Down
1 change: 1 addition & 0 deletions esphome/components/i2c/i2c_bus_arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class ArduinoI2CBus : public I2CBus, public Component {

private:
void recover_();
void set_pins_and_clock_();
RecoveryCode recovery_result_;

protected:
Expand Down

0 comments on commit 76a3ffc

Please sign in to comment.