Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NACK and ACK responses on I2c bus

My recent project requires the use of i2c communication using a single master with multiple slaves. I know that with each data byte (actual data) sent by master,the slave responds with Nack\Ack(1,0). I am confused that how this Nack and ACK are interpreted. I searched the web but i didn't got clear picture about this. My understanding is something like this.

ACK- I have successfully received the data. Send me more data. NACK- I haven't received the data.Send again. Is this something like this or I am wrong. Please clarify and suggest the right answer.

Thanks Amit kumar

like image 951
amit kr Avatar asked Dec 11 '22 17:12

amit kr


1 Answers

You really should read the I2C specification here, but briefly, there are two different cases to consider for ACK/NACK:

  1. After sending the slave address: when the I2C master sends the address of the slave to talk to (including the read/write bit), a slave which recognizes its address sends an ACK. This tells the master that the slave it is trying to reach is actually on the bus. If no slave devices recognize the address, the result is a NACK. In this case, the master must abort the request as there is no one to talk to. This is not generally something that can be fixed by retrying.

  2. Within a transfer: after the side reading a byte (master on a receive or slave on a send) receives a byte, it must send an ACK. The major exception is if the receiver is controlling the number of bytes sent, it must send a NACK after the last byte to be sent. For example, on a slave-to-master transfer, the master must send a NACK just before sending a STOP condition to end the transfer. (This is required by the spec.)

It may also be that the receiver can send a NACK if there is an error; I don't remember if this is allowed by the spec.

But the bottom line is that a NACK either indicates a fatal condition which cannot be retried or is simply an indication of the end of a transfer.

BTW, the case where a receiving device needs more time to process is never indicated by a NACK. Instead, a slave device either does "clock stretching" (or the master simply delays generating the clock) or it uses a higher-layer protocol to request retrying.

Edit 6/8/19: As pointed out by @DavidLedger, there are I2C flash devices that use NACK to indicate that the flash is internally busy (e.g. completing a write operation). I went back to the I2C standard (see above) and found the following:

There are five conditions that lead to the generation of a NACK:

  1. No receiver is present on the bus with the transmitted address so there is no device to respond with an acknowledge.

  2. The receiver is unable to receive or transmit because it is performing some real-time function and is not ready to start communication with the master.

  3. During the transfer, the receiver gets data or commands that it does not understand.

  4. During the transfer, the receiver cannot receive any more data bytes.

  5. A master-receiver must signal the end of the transfer to the slave transmitter.

Therefore, these NACK conditions are valid per the standard.

Short delays, particularly within a single operation will normally use clock stretching but longer delays, particularly between operations, as well as invalid operations, my well produce a NACK.

like image 55
DoxyLover Avatar answered Dec 28 '22 09:12

DoxyLover