Does I2C have any data loss prevention mechanism? If so, how does I2C recover?
I2C has several mechanisms to prevent data loss:
- multi-master collision detection and arbitration
- ACK/NACK after each byte
- clock stretching (slave slows down the master)
SMBus adds a couple of enhancements:
- Packet Error Check (PEC), a CRC sent after each transfer
- limits on clock stretch and maximum clock period
- time-out to prevent bus lock-up
Taken together, these mechanisms can be built upon to make robust I2C communications in single-master or multi-master systems.
There is however no built-in mechanism in I2C or SMBus to guarantee delivery of data. That level of error recovery would be implemented at a higher level. It is up to the driver and application to monitor I2C transactions and determine what to do if an error occurs, be it I2C or SMBus.
Is it possible to implement a guaranteed-delivery protocol over I2C? Sure, with some work. I see references / requests for PPP over I2C for example, which would use TCP/IP layered on top of the link.
I2C does not provide any means of error checking as it was originally intended to communicate between components on a single board where the probability of error is very low, the acronym I2C actually stands for Inter-Integrated Circuit.
However, since its inception, I2C has been used for many applications and has been extended in capability. One of those extensions is called SMBus (System Management Bus). It is backward compatible with I2C but does provide a mechanism for error checking referred to as PEC (Packet Error Checking) where a CRC can be added to the data packet. It also supports a timeout mechanism to avoid bus lockup that can occur with I2C.
The worst-case comm. errors are lockup conditions such as when a clock pulse was missed and the sender keeps the data bit active. The receiver could reset, but the sender doesn't know.
There are many scenarios of lock-up, so the system design must ensure there is a recovery mechanism.
- always implement a well-defined time-out period when waiting for any I2C event
- if a time-out occurs, either reset all the slave devices or issue a sequence of at least ten clock cycles on the I2C SCL signal (one more than a byte transfer)
- it might be handled by software or hardware.
Communication errors usually exist due to hardware design faults from inadequate EMI protection or unexpected interference. They are intended to be used in simple IC to IC channels in a safe environment.
Proper EMI design from the environment can be simple with guarding ground tracks or planes and adequate pull-up current but risk of interference increases with length and lack of awareness of impulse noise.