esphome / issues

Issue Tracker for ESPHome
https://esphome.io/
290 stars 35 forks source link

Multiple I2C busses don't work as expected on ESP8266 #2558

Open nekromant opened 2 years ago

nekromant commented 2 years ago

The problem

When specifying more than one I2C bus on a board only the last one is used. (In my case, the busses share the same SDA pin, but different SCL pins, like a poor man's i2c multiplexer. But this is not really what matters.)

It seems that internally in esphome: (See api/i2c.cpp line 19) uses one global Wire class for all transactions.

  this->wire_ = &Wire;

In the case of multiple busses that will be subsequently reinitialized here:

void I2CComponent::setup() {
   this->wire_->begin(this->sda_pin_, this->scl_pin_);
   this->wire_->setClock(this->frequency_);

Thus Wire will work with only the last bus in array.

Possible solutions (as I see it):

So far I'm doing it in my custom component for the sake of simplicity. Not sending a PR, since I'm not sure what is the best option here.

Which version of ESPHome has the issue?

2021.9.3

What type of installation are you using?

pip

Which version of Home Assistant has the issue?

2021.7.1

What platform are you using?

ESP8266

Board

esp8266 wroom on a custom board

Component causing the issue

api/i2c

Example YAML snippet

i2c:
  - id: i2c_1
    sda: 0
    scl: 4
  - id: i2c_2
    sda: 0
    scl: 5
  - id: i2c_3
    sda: 0
    scl: 12

Anything in the logs that might be useful for us?

No response

Additional information

No response

probot-esphome[bot] commented 2 years ago

Hey there @esphome/core, mind taking a look at this issue as it has been labeled with an integration (esp8266) you are listed as a code owner for? Thanks! (message by CodeOwnersMention)

probot-esphome[bot] commented 2 years ago

Hey there @esphome/core, mind taking a look at this issue as it has been labeled with an integration (i2c) you are listed as a code owner for? Thanks! (message by CodeOwnersMention)

github-actions[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

OttoWinter commented 2 years ago

The ESP8266 arduino framework doesn't seem to support multiple I2C buses, at least the code here accesses the global twi_ functions everywhere and doesn't seem to distinguish between instances.

Would be good to catch this in validation stage already though

ways commented 2 years ago

1581 is a duplicate of this. (other issue is locked, and it's useful to find this post when searching)