• AVR Freaks

How to recover from potential I2C bus lockup?

Author
aschen0866
Super Member
  • Total Posts : 4505
  • Reward points : 0
  • Joined: 2006/01/08 22:18:32
  • Location: San Diego
  • Status: offline
2006/07/15 13:02:19 (permalink)
0

How to recover from potential I2C bus lockup?

I am using the I2C module on PIC18F8680 in the single-master mode. I have three slave devices - one RTC chip (Maxim DS1340) and two serial EEPROMs (Microchip 24LC512).  During the development, I notice that sometimes I2C bus can be stuck low, especially the SDA line, due to an unexpected function exit. This mainly happens when running the code in ICE. For example, the code hits a breakpoint in the I2C read function, and I reset the ICE without exiting the function gracefully. From this point forward, the external I2C device thinks itself is still in the "read mode" waiting for the SCL to come while holding the SDA line low. My concern is that this scenario can happen in the real application environment. For instance, WDT causes a reset while some I2C read might be in progress. Is there a way to recover from this kind of situation? Also, I am wondering if the SCL line can get stuck low too.
#1

13 Replies Related Threads

    jspaarg
    Super Member
    • Total Posts : 2926
    • Reward points : 0
    • Joined: 2005/05/25 16:47:08
    • Location: PA, now MN via NJ,AZ,OR,CA
    • Status: offline
    RE: How to recover from potential I2C bus lockup? 2006/07/16 08:10:15 (permalink)
    0
    Generally
      SSPCON2bits.RSEN = 1;           // initiate bus restart condition
    will work

    PM personal questions and observations only. Keep technical questions to the forums where everyone can benefit.
    When it comes to binary, there are 10 kinds of people: those who understand, and those who don't.
    #2
    aschen0866
    Super Member
    • Total Posts : 4505
    • Reward points : 0
    • Joined: 2006/01/08 22:18:32
    • Location: San Diego
    • Status: offline
    RE: How to recover from potential I2C bus lockup? 2006/07/16 22:28:04 (permalink)
    0
    ORIGINAL: jspaarg

    Generally
      SSPCON2bits.RSEN = 1;           // initiate bus restart condition
    will work

     
    I haven't tried yet, but I am thinking since a slave device can potentially be holding the SDA line low (waiting for SCL from the master to send the next byte), how could this restart condition even be recognized on the bus?
     
    Is it safe to assume that the only time a SDA line can get stuck low after I2C initialization in a single-master configuration is because one of the slave devices is left in the read mode? If this is correct, then I am wondering if I can, for instance, send an ACK to get a dummy read, then send a NACK and finally send a STOP to reset the bus to idle.
    #3
    danish.ali
    Super Member
    • Total Posts : 1714
    • Reward points : 0
    • Joined: 2004/11/16 02:02:02
    • Location: Surrey, UK
    • Status: offline
    RE: How to recover from potential I2C bus lockup? 2006/07/17 01:01:41 (permalink)
    0
    If a slave is driving SDA low you can't create a stop condition.
    But what you can do is (temporarily) re-assign SCL as a general-purpose I/O pin. And then issue clock pulses until the slave releases SDA.

    The slave, if it is sending out data (the only reason it will drive SDA for more than one clock cycle) will expect an ACK after 8 data bits. If you don't give that ACK, the slave will release the bus.
    If one of the data bits was logic 1 then you might choose to issue the stop condition and stop the slave even sooner. But I have never been *so* pushed for time during start-up (the only time the I2C is likely to be in an undefined state is if the cpu crashed half-way through I2C communications).

    So as part of my normal I2C startup, I put SDA as an input and send 9 clock pulses down the SCL pin. Only then do I configure the pins to link to the MSSP hardware.

    Hope this helps,
    Danish
    #4
    aschen0866
    Super Member
    • Total Posts : 4505
    • Reward points : 0
    • Joined: 2006/01/08 22:18:32
    • Location: San Diego
    • Status: offline
    RE: How to recover from potential I2C bus lockup? 2006/07/17 08:54:08 (permalink)
    0
    Thanks for the help.
     
    It would be nice if all slave I2C devices have a hardwired reset line ...
    #5
    jspaarg
    Super Member
    • Total Posts : 2926
    • Reward points : 0
    • Joined: 2005/05/25 16:47:08
    • Location: PA, now MN via NJ,AZ,OR,CA
    • Status: offline
    RE: How to recover from potential I2C bus lockup? 2006/07/17 11:09:14 (permalink)
    0
    You can accomplish that by putting a small relay on the power line and turning it off then on.
     

    PM personal questions and observations only. Keep technical questions to the forums where everyone can benefit.
    When it comes to binary, there are 10 kinds of people: those who understand, and those who don't.
    #6
    aschen0866
    Super Member
    • Total Posts : 4505
    • Reward points : 0
    • Joined: 2006/01/08 22:18:32
    • Location: San Diego
    • Status: offline
    RE: How to recover from potential I2C bus lockup? 2006/07/17 12:24:23 (permalink)
    0
    ORIGINAL: jspaarg

    You can accomplish that by putting a small relay on the power line and turning it off then on.


     
    We used to call that a suicide switch. grin
    #7
    Peter Camilleri
    Super Member
    • Total Posts : 429
    • Reward points : 0
    • Joined: 2011/10/05 12:19:57
    • Location: Whitby, Ontario
    • Status: offline
    Re: RE: How to recover from potential I2C bus lockup? 2012/04/09 10:24:59 (permalink)
    0
    My general startup/recovery procedure is to set the I2C as I/O lines and send out clock pulses while checking the data line. I wait for ten consecutive ones with no intervening zeroes before I switch (back) to the MSSP.  

    Best regards;

    Peter Camilleri
    teuthida-technologies.com/
    #8
    drh
    30+ years
    • Total Posts : 1054
    • Reward points : 0
    • Joined: 2004/07/12 11:43:22
    • Location: Hemet, Calif. USA
    • Status: offline
    Re: RE: How to recover from potential I2C bus lockup? 2012/04/09 11:49:25 (permalink)
    0
    Download this http://www.nxp.com/docume..user_manual/UM10204.pdf and look at page 20.

    David
    #9
    Peter Camilleri
    Super Member
    • Total Posts : 429
    • Reward points : 0
    • Joined: 2011/10/05 12:19:57
    • Location: Whitby, Ontario
    • Status: offline
    Re: RE: How to recover from potential I2C bus lockup? 2012/04/09 12:36:11 (permalink)
    0
    I have observed cases that require more than nine clocks to recover. In one case, two devices somehow became active. We captured with a logic analyzer.

    The clocking until 9 (or 10) ones seen is more robust.

    Best regards;

    Peter Camilleri
    teuthida-technologies.com/
    #10
    BradN
    New Member
    • Total Posts : 14
    • Reward points : 0
    • Joined: 2011/11/04 01:18:31
    • Location: 0
    • Status: offline
    Re: RE: How to recover from potential I2C bus lockup? 2012/04/14 12:37:50 (permalink)
    0
    If you think your I2C error handling is pretty good, try replacing your pull-up resistors with something like 30K and finger fuzz the clk/data lines and see if your code can recover the bus reliably.

    An interesting thing is that the bus still runs with such a terrible pull-up because of the MSSP's clock stretching (provided the I2C slaves have similar controllers - this works at least with MCP23008 slaves and I suspect many microchip I2C peripherals will be the same).
    #11
    Peter Camilleri
    Super Member
    • Total Posts : 429
    • Reward points : 0
    • Joined: 2011/10/05 12:19:57
    • Location: Whitby, Ontario
    • Status: offline
    Re: RE: How to recover from potential I2C bus lockup? 2012/04/14 16:17:24 (permalink)
    0
    hmmmm.... I have visions of a portable I2C torture tester with settings....

    hmmmm.....

    Could be useful.


    Best regards;

    Peter Camilleri
    teuthida-technologies.com/
    #12
    DavidBLit
    Super Member
    • Total Posts : 1579
    • Reward points : 0
    • Joined: 2012/02/18 13:08:48
    • Location: The Land of Confusion
    • Status: offline
    Re: RE: How to recover from potential I2C bus lockup? 2012/04/14 17:05:03 (permalink)
    0


    Except that you might be dismayed to find how many I2C interfaces (master and slave) really are junk.



    Yeah, "//Code and stuff".
    #13
    Peter Camilleri
    Super Member
    • Total Posts : 429
    • Reward points : 0
    • Joined: 2011/10/05 12:19:57
    • Location: Whitby, Ontario
    • Status: offline
    Re: RE: How to recover from potential I2C bus lockup? 2012/04/15 06:12:50 (permalink)
    0
    The whole idea of the tester would be to enable better interfaces through enhanced testing...

    I think this may be my next little side project.

    Best regards;

    Peter Camilleri
    teuthida-technologies.com/
    #14
    Jump to:
    © 2019 APG vNext Commercial Version 4.5