Author Topic: I2C Communication  (Read 976 times)

0 Members and 1 Guest are viewing this topic.

Offline Kittu20Topic starter

  • Regular Contributor
  • *
  • Posts: 96
  • Country: in
I2C Communication
« on: January 11, 2023, 06:02:04 pm »
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 Operation

Master generates the start condition
Master send the slave address with write mode.
If slave does not accept the address it will generate the NAK signal. Stop communication
If 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/ NACK

I 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?
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5914
  • Country: es
Re: I2C Communication
« Reply #1 on: January 11, 2023, 06:20:08 pm »
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 a sequence of stop+ start condition without previous stop.
« Last Edit: January 11, 2023, 07:36:56 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline Berni

  • Super Contributor
  • ***
  • Posts: 4957
  • Country: si
Re: I2C Communication
« Reply #2 on: January 11, 2023, 07:12:55 pm »
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.
 
The following users thanked this post: DavidAlfa

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5914
  • Country: es
Re: I2C Communication
« Reply #3 on: January 11, 2023, 07:38:00 pm »
True  :-+ it's been a while since I used i2c.
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline Kittu20Topic starter

  • Regular Contributor
  • *
  • Posts: 96
  • Country: in
Re: I2C Communication
« Reply #4 on: January 11, 2023, 08:03:15 pm »
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
« Last Edit: January 11, 2023, 08:05:52 pm by Kittu20 »
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5914
  • Country: es
Re: I2C Communication
« Reply #5 on: January 11, 2023, 08:21:43 pm »
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.
« Last Edit: January 11, 2023, 11:54:16 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline woofy

  • Frequent Contributor
  • **
  • Posts: 335
  • Country: gb
    • Woofys Place
Re: I2C Communication
« Reply #6 on: January 11, 2023, 10:36:38 pm »
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 start
send i2c address with WR
slave sends ACK
send internal address
slave sends ACK
send restart
send i2c address again with RD
slave sends ACK
The 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 lines
master sends ACK  } for each byte read except last byte where you send a NAK to tell the slave no further data is required
send stop
 


Offline Berni

  • Super Contributor
  • ***
  • Posts: 4957
  • Country: si
Re: I2C Communication
« Reply #7 on: January 12, 2023, 07:03:25 am »
The repeated start condition is typically only required on doing read operations from a chip.


Quote
 START + SLAVE Address + W + AC/ NAK + Address of Internal Register + ACK/ NAK + send Data to selected register + ACK/ NAK + STOP
For 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)
 

Offline bidrohini

  • Regular Contributor
  • *
  • Posts: 201
  • Country: bd
Re: I2C Communication
« Reply #8 on: January 12, 2023, 02:10:23 pm »
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.html
This will help especially if you're working with STM32.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf