I2C Sometimes Freezes

Hi,

i have some trouble with my I2C bus, because it freezes.
Sometimes 3 times a day; then another week running without problems i can't find where the problem is.

i've got 7 Arduino nano's with I2C extenders; for home automation

When the bus freezes; i've have to find the arduino which make the problem and press the reset button; then the bus releases and everything is working again.

i've added the following code to twi.c trying to release the bus and reset the connection

//TimeOUT
uint8_t twi_tout(uint8_t ini)

{
    if (ini == 1) {
        twi_toutc = millis() + 2000;
        twi_timeoutenable = 1;
    }
    else if (ini == 2) {
        twi_timeoutenable = 0;
    }

	if (twi_timeoutenable == 1) {
        if (twi_toutc < millis()) {
            twi_timeoutenable = 0;

            twi_releaseBus();
            //twi_stop();
            twi_init();
            return 1;
        }
	}
    return 0;
}

before in and after every do-loop sequence i've added the following rules:

  // wait for read operation to complete
  twi_tout(1);
//  while(TWI_MRX == twi_state){
    if (twi_tout(0) == 1) return 0;
 //   continue;
 // }
  twi_tout(2);

; this works right; the arduino will function stand alone if the bus freezes
but i can't use the I2C network features.

is there any way to release the bus and reset the I2C connection for this slave or master?
( i thought that twi_releaseBus() and twi_init(); would solve the problem; but it isn't)

another thing i thought about; when i increase the speed of the bus; would it help or is the I2c then incompatible for other I2C devices?

Reinald

Increasing the speed is often no problem. Most hardware accepts a I2C clock of 400kHz or more.
If that will help ? It depends if the freezing is caused by hardware or software.

Do you have Arduino as I2C Slave ? Could you show the sketch ?
The receiveEvent() and requestEvent() functions should be very fast and short. They should not contain calls to the Serial library or any delay().

Do you test the return value of Wire.endTransmission() and Wire.requestFrom() or Wire.available() ? You should test that to check if the data is valid. Some use a while-loop to wait for Wire.avaiable(), that is wrong.

The Wire library could halt a sketch. There are a few working forked Wire libraries on Github with a timeout.

Hi, thanks for your answer,

i've slowed down the I2C bus with 50%; there are less data errors.
i reviewed my code and found another problem; too much code in the Wire.Receive function. i've fixed that.

I hope all the problems are solved.

Reinald