I am trying to implement an interrupt driven TWI master in an ATMega328P using the AVR315 app. note as a guide. I am using Atmel Studio 6.1 beta and an Arduino Pro 328 (with the Arduino bootloader) as the dev. board.
Everything works fine when I talk to a slave that exists on the bus. The problem is that when I try to read or write to a non-existent slave the SDA and SCL lines are stuck low following the NACK to the address. They stay low until the next write to the bus when they go high (SDA then SCL) right before the next start condition.
The code from AVR315 doesn't do any error handling for this condition (TWI_MTX_ADR_NACK and TWI_MRX_ADR_NACK) and it just terminates the ISR state machine as normal.
I have tried to make an I2C stop condition happen following the address NACK by setting TWSTO but it seems that the TWI controller will not release the bus lines. Setting bit-7 in TWDR will set SDA high which confirms that the drivers are still active. The only way I've been able to release the lines is to momentarily disable the TWI controller which doesn't result in a proper stop condition on the bus.
The datasheet is not clear on what should be done when an address is NACKed. The suggested responses listed in Table 21-2 for status code 0x20 (addr. NACK) match those for code 0x18 (addr. ACK).
I want to have the master handle this situation properly in the event that a slave can't be reached. Can anyone think of what may be happening here?