I've written a rather complex I2C driver to enable 4 IC's to communicate (mega64, mega88's and RTC). Everything is interrupt-based - the data is pushed into a fifo and then the rest of the program continue as normal.
The AVR's use Master-transmit (MrTx) and Slave-receive (SlRx) to communicate and then the mega64 uses master-receive (MrRx) to communicate with the RTC.
I experienced 1 problem that cost be a lot of time (and headache pills):
The THEORY:
is when MCU1 does a MrTx to MCU2, MCU2 will go into SlRx mode and when it receives the STOP condition it will set a flag. When the data has not been extracted by the time MCU2 is again addressed as slave (TWSR = 0x60), it must return from the ISR, but this time instead of clearing the TWINT bit it must clear TWIE - thus disabling I2C-INT, but leaving the flag set.
Because TWINT is not cleared the SCL line will be held low and prevent whoever the master is from sending more data. The moment the previous SlRx data is extracted by the main program (SlGet() is called) TWIE will be set. This will cause the TW-ISR to execute immediately which will then clear TWINT and this will release the bus (SCL) so the first data byte can
be received.
The REALITY:
is that everything works out as planned, but the first data-byte being sent by MrTx is always lost. It seems to me that after MCU2 is addressed and I clear TWIE, it continues to receive the first data-byte and then halts the bus but does not put that first data-byte into TWDR.
In PRACTICE:
I now clear TWIE after receiving a STOP/RESTART condition (0xA0). This effectively halts the bus the moment a complete frame has been received by a Slave and prevents two other MCU's to exchange data until MCU2 has extracted its received data.
This method works, but as you can think is less than ideal.
Has anybody ever tried a multi-master handshaking scheme like this with the AVR. At this moment it seems to me like a hardware bug in the TW module.
Besides that I must say that I2C is a brilliantly thought out protocol. Viva Philips! I wonder on what basis did Atmel win the I2C court case against Philips and I wonder how much compatability guarantee comes with the 'I2C' stamp (which AVR doesn't carry)? Like the USB-compatible logo for instance.