I2C bus locked up - my solution

General discussion relating to the library modules supplied with the compiler

Moderators: David Barker, Jerry Messina

Post Reply
matherp
Registered User
Registered User
Posts: 31
Joined: Tue May 31, 2011 2:59 pm
Location: Cambridge

I2C bus locked up - my solution

Post by matherp » Sun Jun 19, 2011 12:00 pm

When repeatedly programming a chip it is occasionally the case that an i2c peripheral is in the middle of a write when the re-programming takes place. If the peripheral chip is holding SDA low then the i2c start condition will not work to reset the peripheral and the program will probably not run properly until a power-off/power-on.

The following minor change to i2c.Initialize appears to solve this.

Code: Select all

****************************************************************************
* Name    : Initialize                                                     *
* Purpose : Initialize SSP module for I2C bus                              *
****************************************************************************
}         
Public Sub Initialize(pBaudrate As Byte = I2C_100_KHZ, pSlew As Byte = I2C_SLEW_OFF)
  SSPAddress = pBaudrate     // set baudrate
  SSPStatus = pSlew          // POR state, optional slew
  SSPControl2 = $00          // POR state
  Input(SCL)                 // set SCL (clock pin) to input
  Input(SDA)                  // set SDA (data pin) to input
// clear locked up peripheral
  If SDA<>1 Then //there must be an i2c device holding SDA low 
    Output(SCL)
    While SDA<>1 //clock in bits from the stuck peripheral until SDA is high
        Low(SCL)
        DelayUS(100)
        High(SCL)
        DelayUS(100)
    Wend
    Input(SCL)    // set SDA (data pin) to input
  EndIf
//
  SSPControl1 = $28          // master mode, enable synchronous serial port
End Sub

Post Reply