I2C Communication - Page 1
Forum section: electronics / general

author: Kittu20
author_rank: Regular Contributor
timestamp: January 11, 2023, 06:02:04 pm
content: I am trying to understand the fundamental concept of i2c communication protocol. I am confused at some places I don't understand what exactly happens so I am explaining my understanding please correct me.I2C write OperationMaster generates the start conditionMaster send the slave address with write mode.If slave does not accept the address it will generate the NAK signal. Stop communicationIf slave accept the address it will generate the ACK signals.Next master send address of internal register. If slave accept the address it will generate the ACK signals. If not then Stop communication.Next master send data to selected address register. Slave generate ACK/ NACKI don't understand where the restart condition should be in this sequence. So, after each byte is sent, does slave generates an ACK/NAK signal and the master checks for an ACK/NAK signal?

author: DavidAlfa
author_rank: Super Contributor
timestamp: January 11, 2023, 06:20:08 pm
content: Master sends start, device address+rw bit, data.... slave keeps ACKing until wants no more data (Or invalid address etc).Master should check the response, sending stop/restart on NACKs condition.Master also can stop the transfer anytime by sending a stop or a restart, doesn't have to keep sending data until receiving a NACK.A restart is just asequence of stop+start condition without previous stop.

author: Berni
author_rank: Super Contributor
timestamp: January 11, 2023, 07:12:55 pm
content: Restart is actually a repeated start condition.It is sent by as the name sugests by repeating the same start condition, but not having a stop.Most chips don't care if you do a stop+start or a restart between the transfers, but a few rare chips care.

author: DavidAlfa
author_rank: Super Contributor
timestamp: January 11, 2023, 07:38:00 pm
content: Trueit's been a while since I used i2c.

author: Kittu20
author_rank: Regular Contributor
timestamp: January 11, 2023, 08:03:15 pm
content: When I am writing data to slave. you can see in my sequence i never use restart condition i am just using only one start condition.Do i need to use restart condition somewhere in my sequence?START + SLAVE Address + W + AC/ NAK + Address of Internal Register + ACK/ NAK + send Data to selected register + ACK/ NAK + STOP

author: DavidAlfa
author_rank: Super Contributor
timestamp: January 11, 2023, 08:21:43 pm
content: No, you would send a restart to inmediately write to other register, for example.Anyways i2c is only start/stop/i2c addr/(N)ACK, normally the device has its own protocol running on top, so always check the datasheet.

author: woofy
author_rank: Frequent Contributor
timestamp: January 11, 2023, 10:36:38 pm
content: Your understanding is correct for a write operation. The restart is used when reading data. This is because to read data from a specific address you first have to write that address to the slave device. So the read sequence is:send startsend i2c address with WRslave sends ACKsend internal addressslave sends ACKsend restartsend i2c address again with RDslave sends ACKThe master now continues to send the clock pulses on SCL but the slave sends data to the master on SDA and it is the master that now sends ACK pulses.master reads data } repeat these to linesmaster sends ACK } for each byte read except last byte where you send a NAK to tell the slave no further data is requiredsend stop

author: Berni
author_rank: Super Contributor
timestamp: January 12, 2023, 07:03:25 am
content: The repeated start condition is typically only required on doing read operations from a chip.QuoteFor write operations you just send everything in one transfer surrounded by start and stop at the ends. So that one does look valid for some of the chips out there.Just read the chips documentation on how it should be done and it will explain everything. If something is not working use an oscilloscope or logic analyzer to check the communication (They are so cheep these days that there is little excuse to not having one if you deal with electronics a lot)

author: bidrohini
author_rank: Regular Contributor
timestamp: January 12, 2023, 02:10:23 pm
content: You have already got many good suggestions. For a better understanding, you can take a look at this too:https://www.theengineeringprojects.com/2021/11/i2c-communication-with-stm32.htmlThis will help especially if you're working with STM32.