technoblogy / tiny-i2c

Minimal I2C master routines for all AVR microcontrollers.
162 stars 39 forks source link

I2C hangs in endless loop after releasing SCL #7

Closed markose closed 2 years ago

markose commented 5 years ago

Hello,

first of all thank you for this great library! We are using it on a Attiny261A in combination with a DS3231 RTC. Currently we have the problem, that it sometimes hangs in this LOOP forever:

PORT_USI_CL |= 1<<PIN_USI_SCL;              // Release SCL.
while (!(PIN_USI_CL & 1<<PIN_USI_SCL));     // Verify that SCL becomes high.

Did you ever investigate a similar problem and do you have any idea, why this might happen and how it might be resolved respectively.

Our Application has a battery driven power supply. We use a HD44780 Display and do PWM to regulate the operating voltage. We suppose that the problem might occur because of the PWM.

Kind regards, Marko

technoblogy commented 5 years ago

In the two lines you mention:

PORT_USI_CL |= 1<<PIN_USI_SCL; // Release SCL.
while (!(PIN_USI_CL & 1<<PIN_USI_SCL)); // Verify that SCL becomes high.

the first line is taking SCL high, and the second one is waiting until it’s high. The only thing I can think of that could cause the program to hang in the second loop is if some external circuitry is keeping SCL low, or if the internal pullup on SCL isn’t strong enough to take it high.

I would try adding 4.7kΩ pullup resistors between the SDA and SCL lines and Vcc, if you haven’t already got them, and see if that solves the problem.