STM32 I2C Lecture 61 – Exercise: Modifying I2C transactions

  • Post author:
  • Post category:Blog

 

Exercise: Modifying I2C transactions

 

 


In this article, let’s discuss the procedure to make the slave send more than 32 bytes of data to the Arduino master. For this, you have to modify the earlier application.

Steps:

1. Create a new application and give the name 014i2c_slave_tx_string2.c, as shown in Figure 1.

Modifying I2C transactions
Figure 1. Creating a new application.

 

2. You can see that the Tx_buf shown in Figure 2 has a large number of data, approximately it is around 400 bytes. Now let’s see how the master can fetch this data from the slave using I2C transactions.

Modifying I2C transactions
Figure 2. Contents of Tx_buf.

 

3. For this application, you have to make certain modifications to the earlier application. Now let’s discuss them one by one.

4. First, the master executes the I2C transactions to read the 4 bytes of length information. Now the length information will be 4 bytes instead of 1 byte, which you used in the previous exercise.

a. Master sends the command 0X51 (Figure 3) to the slave to read the length information. 

Modifying I2C transactions
Figure 3. Sequence diagram for data write (Master sending command to slave).

 

b. After sending the command, the master executes data read I2C transaction and slave replies 4 bytes of length information. For example, if the length is 0X480, then the slave returns the length information to the Arduino code, as shown in Figure 4.

Modifying I2C transactions
Figure 4. Sequence diagram for data read (Master reading response from the slave).

 

5. After that, the I2C transactions to read length bytes of data from the slave are performed.

  1. First, the master sends the 0X52 command to read the data (nothing but data write transaction shown in Figure 3).
  2. If the length is lesser than or equal to 32 bytes, then only one read transaction will be performed by the master (Figure 5). Otherwise, more read transactions will be performed by the master (Figure 6).
Modifying I2C transactions
Figure 5. Data read transaction performed by the master when the length < =32.

 

Modifying I2C transactions
Figure 6. Data read transactions performed by the master when the length >32.

For example, let’s say the length is 65. Since, in one read transaction, it is possible for the master to read only 32 bytes, you need 3 I2C read transactions to read the data of length 65 bytes.

All the steps discussed above are already implemented in the 014-application shown in Figure 1.

 

6. On the Arduino side, you have to use the application 003ICMasterRxStringLen shown in Figure 7.

Modifying I2C transactions
Figure 7. Arduino code (003ICMasterRxStringLen).

 

7. Now let’s test this application. First, download the code into the master, which is our Arduino board, by clicking on the upload button (Figure 8).

Modifying I2C transactions
Figure 8. Downloading the code into Arduino board.

 

8. Go back to the application (STM code).

9. Compile the code (Figure 9).

Modifying I2C transactions
Figure 9. Compiling the code.

 

10.  Open the serial monitor (Figure 10).

Modifying I2C transactions
Figure 10. Opening serial monitor.

 

11. Send character ‘s’ to begin the transaction (Figure 11).

Modifying I2C transactions
Figure 11. Sending character ‘s’.

 

12. Look at Figure 12. The master has fetched 406 bytes of data from the slave.

Modifying I2C transactions
Figure 12. Data fetched from the slave.

Go through the code which is there in the application 014. This is the modified version of application 013. Both the applications are almost the same. But some modifications are done since the length of the data is changed.

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.

Leave a Reply