STM32 I2C Lecture 55: I2C slave programming discussion

  • Post author:
  • Post category:Blog

 

I2C slave programming discussion

 

 

In this article, let’s understand how to program for I2C slave. 

Now let’s change the mode (Figure 1). For example, consider the Arduino board as master and STM32 as a slave device.

I2C slave programming discussion
Figure 1. Master slave communication.

 

Differences between the I2C master and the I2C slave (Figure 2):

I2C slave programming discussion
Figure 2. Difference between I2C master and slave.

 

 

For example, when the I2C master executes a read transaction, that is nothing but a request for data. It indicates that the master wants to read some data from the slave. When an I2C master executes or initiates a write transaction, that is nothing but a receive data for the slave. 

The slave has to serve only two events, one is request for data, which is nothing but a read transaction from the master and another event is receive data, which happens whenever an I2C master wants to write some data to the slave. Therefore, in the I2C slave code, you have to receive only two events. They are – request for data and receive data, and then the action is taken according to the event received.

  1. You have to create a send data API to request data from the master. 
  2. You have to create a receive data API to receive the data from the master.

In slave mode, the I2C driver (STM32 I2C driver) will send a request for data and receive data events to the slave application, as shown in Figure 3. Once the application receives a request for data, it will send the data and then receives the data from the master.

I2C slave programming discussion
Figure 3. Events received by the slave.

 

Now let’s create the APIs for a request for data and receive data events. 

Steps:

1. Go to the driver.h file and create a prototype of APIs for request for data and receive data events, as shown in Figure 4.

I2C slave programming discussion
Figure 4. Prototype of the APIs.

 

2. Whenever the request is received, then only the slave will send the data. The length and other information passed as the arguments of the API are not necessary since the slave sends the data byte by byte. Therefore, remove all the things passed as an argument of I2C_SlaveSendData() API shown in Figure 4.

The I2C_SlaveSendData() API will accept only two parameters (Figure 5), one is the I2C RegDef pointer, and another is the value or data to be sent.

I2C slave programming discussion
Figure 5. Prototype of I2C_SlaveSendData() API.

 

3. Whenever a receive data event is received from the master, a slave is going to receive the data. Therefore, you don’t need pass length and other information only pass I2C RegDef pointer as a parameter of I2C_SlaveReceiveData() API, as shown in Figure 6.

Since I2C_SlaveReceiveData() API returns the data received from the master, the return type will be uint8_t.

I2C slave programming discussion
Figure 6. Prototype of I2C_SlaveReceiveData() API.

 

4. Implement the I2C_SlaveSendData() and I2C_SlaveReceiveData() APIs in the driver.c file before I2C event IRQ handling.

Steps of implementation:

  • In the I2C_SlaveSendData() API, load the data into the DR (Data register), as shown in Figure 7 since you have to send the data.
Implementation of I2C_SlaveSendData() API
Figure 7. Implementation of I2C_SlaveSendData() API.

 

  • In the I2C_SlaveReceiveData() API, you just have to return the data. Therefore, return the content of the data register, as shown in Figure 8.
Implementation of I2C_SlaveReceiveData() API
Figure 8. Implementation of I2C_SlaveReceiveData() API.

 

In the following article, let’s see I2C transfer sequence diagram for slave transmitter.

 

FastBit Embedded Brain Academy Courses

Click here: https://fastbitlab.com/course1

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.