Search code examples
microcontrolleri2c

i2c protocol NACK ambiguity


I have a question about I2C protocol. I found this on the wikipedia page.

"If the transmitter sees a 1 bit (NACK), it learns that:

1) The slave is unable to accept the data. 2) No such slave 3) Command not understood 4) Unable to accept any more data."

The first and fourth points seems to contradict each other. The scenario is: I am trying to communicate between microcontrollers hence, one will act as a master and the other a slave.I am transmitting 10 bytes from master and slave can receive only 5 bytes in my implementation.

So my question is according to i2c protocol when should slave sends a NACK ? 1) After receiving the 5th byte. 2) After receiving the 6th byte.


Solution

  • This is a very good question. I've reviewed the actual I2C specification (http://www.nxp.com/documents/user_manual/UM10204.pdf) and one of the conditions it lists for NACK is "During the transfer, the receiver cannot receive any more data bytes". However, every piece of code I've seen for transmitting from a master considers a received NACK as an error.

    My experience is that slave devices do not NACK excessive bytes: they either wrap around internally or silently drop extra data.

    My feeling is that if you do wish to NACK excess data, you should NACK the 6th byte (where you expect 5).

    Note that this is a different case from master receiving data where it is required to NACK the last byte, just before the STOP. This is described in a sepate line-item in the specification: "A master-receiver must signal the end of the transfer to the slave transmitter".

    All of this information is in section 3.1.6 in the specification.