Transaction I2C [SOLVED]

Thread Starter

MTech1

Joined Feb 15, 2023
161
In general, I am trying to understand what happens in one transaction in I2C communication. I think a combined read and write transaction refers to a scenario where both read and write operations occur in a single transaction.

Let's illustrate this concept with an example involving a slave device that operates at addresses 0xD0 for write mode and 0xD1 for read mode. This slave device contains an internal register located at address 0x01 that we want to interact with.

1. Start Condition: The transaction begins with the initiation of a start condition.

2. Address (Write): The master sends the slave address (0xD0) with the write bit (0) set to commence the write operation. The slave device acknowledges (ACK) the reception of this address.

3. Register Address: Subsequently, the master sends the internal register address (0x01) to specify where the data should be written. The slave device acknowledges (ACK) this address.

4. Data Write: The master transmits the data to be written, and the slave device acknowledges (ACK) the received data.

5. Repeated Start Condition: Instead of using a stop condition, the master sends a repeated start condition without releasing control of the bus.

6. Address (Read): Now, the master sends the slave address (0xD1) with the read bit (1) set, signifying a read operation. The slave device acknowledges (ACK) this address.

7. Data Read: The slave device then sends data from the specified register, with the master acknowledging (ACK) each received byte.

8. Stop Condition (Read): Finally, the transaction concludes with a stop condition.

I'd appreciate it if you could correct the sequence or some suggestions about the transaction.

Thank you!
 

BobaMosfet

Joined Jul 1, 2009
2,105
Read section 20.5-20.8 (at least) . It's the best description of i2c/TWI that I've found. Read and reread. Study the drawings carefully- it explains _everything_ in the most beautiful way.
 

Attachments

Thread Starter

MTech1

Joined Feb 15, 2023
161
Read section 20.5-20.8 (at least) . It's the best description of i2c/TWI that I've found. Read and reread. Study the drawings carefully- it explains _everything_ in the most beautiful way.
Thank you. I have gone through the entire section 20, but that doesn't address the problem I am trying to understand. I believe one transaction involves read and write operations.

Let's make it simple: there are conditions START, STOP, Repeated START, SEND ADDRESS R/W, SEND DATA in I2C protocol.

What would be the sequence in one transaction if you want to read data from a slave device like EEPROM or RTC?
 

BobaMosfet

Joined Jul 1, 2009
2,105
Thank you. I have gone through the entire section 20, but that doesn't address the problem I am trying to understand. I believe one transaction involves read and write operations.

Let's make it simple: there are conditions START, STOP, Repeated START, SEND ADDRESS R/W, SEND DATA in I2C protocol.

What would be the sequence in one transaction if you want to read data from a slave device like EEPROM or RTC?
Re-read. The sections I reference cover EVERYTHING about i2c signaling. Understand the drawings. They cover a lot that the words convey poorly (albeit accurately).

You must do a write to specify the address of the device you are talking to. Then you do a read when it starts sending data back. NOT a single transaction. Understand that TWI is nothing more than 2 signals. And what it means when considering both signals in a high and/or low state between them. Don't overthink this.

Try to master single mode before you think about multi-mode.
 
Last edited:

Thread Starter

MTech1

Joined Feb 15, 2023
161
Re-read. The sections I reference cover EVERYTHING about i2c signaling. Understand the drawings.
Here is my understanding between start and stop condition for one transaction. I hope my understanding is correct

1. START Condition: The communication begins with a START condition. The master device initiates the communication by pulling the SDA (Serial Data) line low while SCL (Serial Clock) is high, signaling the start of the transmission.

2. WRITE (W) Operation: After the START condition, the master sends the address byte of the slave device it wants to communicate with, along with the write (W) bit. This indicates that the master intends to write data to the slave.

3. Data Transmission: The master then sends one or more data bytes to the slave device, depending on the requirements of the communication. The slave acknowledges the receipt of each byte.

4. REPEATED START Condition: Instead of sending a STOP condition after writing, the master can choose to send a REPEATED START condition, signaling the beginning of a new data transfer sequence without releasing control of the bus.

5. READ Operation (R): After the REPEATED START condition, the master can send the address byte of slave device, this time with the read (R) bit set. This indicates that the master intends to read data from the slave.

6. Data Reception: The master then reads one or more data bytes from the slave device, and the slave acknowledges the receipt of each byte.

7. STOP Condition: The communication concludes with a STOP condition. The master releases the SDA line while SCL is high, signaling the end of the transmission.
 

JohnInTX

Joined Jun 26, 2012
4,787
Pretty good with this change:
6. Data Reception: The master then reads one or more data bytes from the slave device, and the slave acknowledges the receipt of each byte.
6. Data Reception: The master then reads one or more data bytes from the slave device, and the slave master acknowledges the receipt of each byte. When the last byte is read by the master it issues a NAK instead of ACK indicating to the slave that it will read no more bytes.

Also, once a START condition is sent successfully, no other device may attempt to use the I2C bus until a STOP condition is issued. The REPEAT START condition allows the second part of the transaction to begin (changing from write to read in this example) without another master taking the bus.
 

Thread Starter

MTech1

Joined Feb 15, 2023
161
I've reviewed the specifications multiple times, and I was a bit confused about sequence of operations. I decided to ask for clarification,

Pretty good with this change:
I would like to expand more. I've examined the RTC DS1307 datasheet, which contains multiple time-keeping registers. For our purpose, I'll focus on writing to the seconds (00h) and date (04h) registers. I want to write 00 and 01 value

Here's the write-read sequence:

1. Master generates a START condition.
2. Master sends the slave address with the write bit set.
3. Data Transmission: Master writes

[At this point, the specific data bytes 00 and 01 should be sent only at seconds and Date register, but I don't have any idea about these values would store at specific address.]

4. Master generates a REPEATED START condition to switch from write to read mode.
5. Master sends the slave address with the read bit set.
6. Data Reception: The master reads data from the RTC.

[At this point, if I want to read content of seconds and Date register , but I don't have idea.]
7. Master generates a STOP condition to conclude the communication.

@JohnInTX would you please help me with step 3 and step 4.
 

Ian0

Joined Aug 7, 2020
9,497
To get data out of the device, you have to write to it to tell it which address you want to start at.
So you send START followed by 0xD0 (to identify the device you are writing to) followed by 00 (the address of the first byte you want).
Then you send the repeated start condition (alternatively, you could send a STOP followed by a START) followed by 0xD1 (identifying the device you are reading from).
Then you start reading data. You will get the byte you selected.
If you keep reading, you will get the next byte in sequence, followed by the next etc. until you send a NACK on the last byte you want followed by a STOP.
 

Thread Starter

MTech1

Joined Feb 15, 2023
161
To get data out of the device, you have to write to it to tell it which address you want to start at.
So you send START followed by 0xD0 (to identify the device you are writing to) followed by 00 (the address of the first byte you want).
Let's keep it simple. The basic sequence is START + 0xD0 + Next ?.

Next, Do we need to send the address of the seconds register, which is 0x00. Or content ( 0x00) we want to set in the seconds register.
 

Ian0

Joined Aug 7, 2020
9,497
Let's keep it simple. The basic sequence is START + 0xD0 + Next ?.

Next, Do we need to send the address of the seconds register, which is 0x00. Or content ( 0x00) we want to set in the seconds register.
If you are writing to the device, first send the device address, then the address you want to write to, then the data for that address, then, if you want, the data for the next address.
 

Thread Starter

MTech1

Joined Feb 15, 2023
161
If you are writing to the device, first send the device address, then the address you want to write to, then the data for that address, then, if you want, the data for the next address.
Does it makes sense

START + 0xD0 + 0x00 + 0x00 + 0x04 + 0x01 + Repeated START + 0xD1 + 0x00 + Read seconds + 0x04 + Read Day + STOP

In this sequence:

  1. START initiates the communication.
  2. 0xD0 is the device address with the write bit (0).
  3. 0x00 is the address of the seconds register.
  4. 0x00 is the data we want to write into the seconds register.
  5. 0x04 is the address of the Date register.
  6. 0x01 is the data we want to write into the Date register.
  7. Repeated START allows us to switch from write to read mode without releasing control of the bus.
  8. 0xD1 is the device address with the read bit (1).
  9. 0x00 is the address of the seconds register for reading.
  10. 'Read seconds' indicates the data received from the seconds register.
  11. 0x04 is the address of the Date register for reading.
  12. 'Read Date' represents the data received from the Date register.
  13. STOP concludes the communication.
 

Ian0

Joined Aug 7, 2020
9,497
Does it makes sense

START + 0xD0 + 0x00 + 0x00 + 0x04 + 0x01 + Repeated START + 0xD1 + 0x00 + Read seconds + 0x04 + Read Day + STOP

In this sequence:

  1. START initiates the communication.
  2. 0xD0 is the device address with the write bit (0).
  3. 0x00 is the address of the seconds register.
  4. 0x00 is the data we want to write into the seconds register.
  5. 0x04 is the address of the Date register.
  6. 0x01 is the data we want to write into the Date register.
  7. Repeated START allows us to switch from write to read mode without releasing control of the bus.
  8. 0xD1 is the device address with the read bit (1).
  9. 0x00 is the address of the seconds register for reading.
  10. 'Read seconds' indicates the data received from the seconds register.
  11. 0x04 is the address of the Date register for reading.
  12. 'Read Date' represents the data received from the Date register.
  13. STOP concludes the communication.
No. That would put 00 in seconds, 04 in minutes, and 01 in hours, because it writes sequentially.
 

Thread Starter

MTech1

Joined Feb 15, 2023
161
No. That would put 00 in seconds, 04 in minutes, and 01 in hours, because it writes sequentially.
This is my revised version where one for setting the seconds and another for setting the Date register and reading.

1. Set Seconds and Read:
- START + 0xD0 + 0x00 + 0x00 + Repeated START + 0xD1 + 0x00 + Read seconds + STOP

2. Set Date and Read Day:
- START + 0xD0 + 0x04 + 0x01 + Repeated START + 0xD1 + 0x04 + Read Day + STOP
 

Ian0

Joined Aug 7, 2020
9,497
No
You have to do the reads and writes separately.
Otherwise, what happens is the address pointer is incremented when you write to seconds, so that it points at the minutes register. When you read, it would read the minutes.

So, to recap.
To write: Write the device address, the address pointer, write the data, then STOP.
To read. write the device address and the address pointer. Repeated start, write the device address and read he data. Then STOP.
If you try to do them both together, the address pointer won’t be pointing where you think it might be.
 

Thread Starter

MTech1

Joined Feb 15, 2023
161
So, to recap.
To write: Write the device address, the address pointer, write the data, then STOP.
To read. write the device address and the address pointer. Repeated start, write the device address and read he data. Then STOP.
Set Seconds:
- START + 0xD0 + 0x00 + 0x00 + STOP

Read Seconds:
- START + 0xD0 + 0x00 + Repeated START + 0xD1 + 0x00 + Read seconds + STOP

In this version we first set the seconds register and then, in a separate transaction, read its value
 

Ian0

Joined Aug 7, 2020
9,497
Set Seconds:
- START + 0xD0 + 0x00 + 0x00 + STOP

Read Seconds:
- START + 0xD0 + 0x00 + Repeated START + 0xD1 + 0x00 + Read seconds + STOP

In this version we first set the seconds register and then, in a separate transaction, read its value
The one in orange isn't required. It starts reading at the register that you told it in the previous stage.
 

Thread Starter

MTech1

Joined Feb 15, 2023
161
The one in orange isn't required. It starts reading at the register that you told it in the previous stage.
As you mentioned, we have transition sequences for writing and reading for DS1307 slave device.

Are they common sequences , can we apply the same sequence for different slave devices, or do different slaves follow different sequence?
 

Ian0

Joined Aug 7, 2020
9,497
As you mentioned, we have transition sequences for writing and reading for DS1307 slave device.

Are they common sequences , can we apply the same sequence for different slave devices, or do different slaves follow different sequence?
They don't have to, but often they do.
For instance, reading and writing a small (<256 byte) memory is exactly the same reading and writing to the DS1307, with the exception that memories are all addressed at 0xA0/0xA1.
Larger memories are similar, but not quite the same, in that they have a two byte address instead of a single byte.
 

Thread Starter

MTech1

Joined Feb 15, 2023
161
Larger memories are similar, but not quite the same, in that they have a two byte address instead of a single byte.
I've looked the datasheets for the EEPROM, and while it seems to follow a similar sequence as the DS1307 for I2C communication, I'm having trouble identifying the specific memory locations where data should be stored. I want to understand the start and end addresses for data storage.

Can you help clarify this in the datasheet?
 

Attachments

Top