I2C Bus hangs after reset

cancel
Showing results for 
Search instead for 
Did you mean: 

I2C Bus hangs after reset

4,481 Views
JimDon
Senior Contributor III
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:

Code:
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
Labels (1)
0 Kudos
3 Replies

174 Views
Kr_Blue
Contributor I

In my opinion, these files are likely to help you.

 

0 Kudos

174 Views
JimDon
Senior Contributor III

 

void delay(){  int i;  for( i = 0 ; i < 5000 ; ++i)    asm {      nop ; nop ; nop ;nop    };}void I2CInit(void){  int i = 0;    PTCDD |= 3;  PTCPE |= 3;  PTCD   = 3;  for( i = 0 ; i < 30 ; ++i )    {      delay();      PTCD ^= 1;        }  PTCD |= 3;  PTCDD &= ~3; 

I had this problem again, and had to to this to un-hang the external chip... 

 

0 Kudos

174 Views
Henko
Contributor I

I had this problem years ago with a Coldfire.... what I did then was to generate SCL pulses until SDA became high (normally up to 8 bits is sufficient). Then generate a STOP condition.

 

Consider yourself lucky, because that Coldire I was working with that time didn't have a GPIO option on the I2C module's SCL/SDA pin and the I2C controller didn't support freeing the bus, so a HW mod (solder GPIOs to SCL SDA was required.

 

Henko

0 Kudos