Seeed-Studio / Arduino_Software_I2C

18 stars 11 forks source link

library actively pulls HIGH on open-drain SDA and SCL, may cause hardware problems? #3

Closed bxparks closed 3 years ago

bxparks commented 3 years ago

It is my understanding that the SDA and SCL lines in the I2C protocol are open-drain, meaning that there is a transistor that pulls the lines LOW, but pull-up resistors are required to pull HIGH. It is also my understanding that the lines must not be actively pulled HIGH by the master or slave devices, because that would essentially cause a short-circuit between the transistor that is actively pulling HIGH, and the transistor that is actively pulling LOW.

This library implements the SoftwareI2C::sdaSet() and SoftwareI2C::sclSet() methods using a digitalWrite(), which would cause the microcontroller to actively pull HIGH if ucDta is HIGH. The use of digitalWrite(pin, HIGH) does not seem right.

The SDA line is a bidirectional line. When the master is sending data to the slave, the master sends 8 bits, then on the 9th bit, the slave sends the ACK (LOW)/NACK (HIGH) bit. If there is a bug in the software, or something goes wrong in the logic circuit on the slave, it may be possible for the master to pull the line HIGH at the same time as the slave device is pulling LOW for the ACK. If this causes more than 20-40 mA of current to flow, for a long enough time, then I think this could cause damage to the either the master or the slave device.

Almost every software I2C library that I have examined (8-10 of them so far) uses a digitalWrite(pin, LOW) for active LOW, but a pinMode(pin, INPUT) to transition into high impedance mode, which then relies on the pull-up resistor for the passive HIGH condition. I have found 2 exceptions: this library and one other.

Anyway, if your engineers say that this is not a problem, then I will have learned something new. Thanks for your consideration.

Pillar1989 commented 3 years ago

@bxparks I don't think 20-40 mA of current can cause the hardware problem. Hardware shouldn't be this vulnerable.