STM32 I2C Lecture 31 : Implementation of I2C master receive data API: Part 1

  • Post author:
  • Post category:Blog

 

Implementation of I2C master receive data API: Part 1

 

 


Let’s implement the I2C master receive data API. Before writing code, first, download the file shown in Figure 1 and then copy it to driver source code.

Figure 1. Procedure to implement I2C_MasterReceiveData API.

 

  • Copy the I2C_MasterReceiveData API and paste it in the I2C driver.h file (Figure 2) to give the function prototype in the header file.
Figure 2. I2C_MasterReceiveData API prototype.                                                                                                                                    
  • Copy the entire function and paste it into the I2C driver.c file, as shown in Figure 3.
Figure 3. I2C_MasterReceiveData API Definition part.

 

Now let’s start coding according to the comments.

 

Steps to implement I2C_MasterReceiveData API:

1. Generate start condition: To generate the start condition, copy the master send data and paste it into the master receive data API, as shown in Figure 4.

Figure 4. Code to generate start condition.

 

2. Confirm that the start generation is completed. This is done by using a while loop, as shown in Figure 5.

Figure 5. Code to confirm the completion of start generation.

 

3. Send the address of the slave with R/W bit set to 1 because the master is reading the data from the slave (Figure 6). The R/W bit can be set to 1, as shown in Figure 7.

Figure 6. Code to send the address of the slave with R/W.

 

Figure 7. Code to set the R/W bit.

 

4. Wait until the address phase is completed by checking the ADDR flag in the SR1 register, as shown in Figure 8.

Figure 8. Code to check the status of the ADDR flag.

 

5. Procedure to read only one byte:

  • Disable the Acking by creating the I2C_ManageAcking() API to manage the Acking (Figure 9). Then go to the driver.h file and paste the I2C_ManageAcking() API in other peripheral control APIs section, as shown in Figure 10.
Implementation of I2C master receive data API: Part 1
Figure 9. Call to I2C_ManageAcking() API in I2C receive data function.

 

Implementation of I2C master receive data API: Part 1
Figure 10. Prototype for I2C_ManageAcking() API in other peripheral control APIs.

 

Implement I2C_ManageAcking() API in driver.c, as shown in Figure 11. The Acking is managed by using ACK_ENABLE and ACK_DISABLE macros. If it is ACK enable, then set the 10th bit of the CR1 register. Otherwise, clear that 10th bit.

Implementation of I2C master receive data API: Part 1
Figure 11. I2C_ManageAcking() API implementation.

 

  • Clear the ADDR flag (Figure 12).
Implementation of I2C master receive data API: Part 1
Figure 12. Code to clear the ADDR flag.

 

  • Wait until RXNE becomes 1 (Figure 13).
Implementation of I2C master receive data API: Part 1
Figure 13. Code to wait until RXNE becomes 1.

 

  • Generate the stop condition, as shown in Figure 14. 
Implementation of I2C master receive data API: Part 1
Figure 14. Code to generate the stop condition.

  

  • Read the data into the buffer: Read the data register value into pRXBuffer, as shown in Figure 15.
Implementation of I2C master receive data API: Part 1
Figure 15. Code to read the data into the buffer.

 

You can generate the stop condition either after confirming RXNE or before confirming RXNE. Therefore, slight changes to the code are made, as shown in Figure 16.

 

Implementation of I2C master receive data API: Part 1
Figure 16. Code to read only one byte.

 

Fastbit Embrdded Brain Acdemy Courses

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.

Leave a Reply