Jim Donelson

I2C Bus hangs after reset

Discussion created by Jim Donelson on Jan 2, 2008
Latest reply on Mar 24, 2011 by Henk Stegeman
I wrote some code to interface to an I2C chip, and discovered that if reset, the I2C bus would hang about 50% of the time. (the code is in a tight loop reading the device).

It happens because the device I was talking to gets stuck the in the middle of a transfer, and drives SDA low. It's not really the modules fault.

This causes the I2C module to believe it has lost arbitration every time you try to start a transfer.
(If the master drives a high, but sees a low, so it lost arbitration)
The solution is to set the port bits to output and drive them high before setting up the I2C port:

void InitI2C(void){   // This will unhang the I2C module if reset while sending        DDRJ |= 0xc0;  // make the clock & data be output.   PTJ  |= 0xc0;  // Drive them hi   DDRJ &= ~0xC0; // Set them back to input.      IBCR = 0;   IBCR |= IBCR_IBEN_MASK;    IBFD = 0x1f; // 100K at 24 MHz     IBAD = 0x7e;   }


Message Edited by JimDon on 2008-01-02 12:20 PM