STM32 I2C Lecture 10: I2C API Prototypes

  • Post author:
  • Post category:Blog

 

I2C API Prototypes

 

 

In this article, let’s discuss the creation of API prototypes.

The best thing for creating API prototypes is to go to the SPI driver.h and copy some of the APIs. Copy all the APIs in the API section of SPI driver.h (Figure 1) and paste them into the I2C driver.h file, and replace SPI keyword with I2C, as shown in Figure 2.

API Prototypes
Figure 1. APIs in the SPI driver.h file.

 

API Prototypes
Figure 2. APIs pasted into the I2C driver.h file.

 

Its own data send and receive APIs can be written for the I2C peripheral, therefore remove all the APIs in the data send and receive section (Figure 3).

Data send and receive section
Figure 3. Data send and receive section.

 

In the IRQ configuration and ISR handling section, I2C interrupt config and priority config is required, but remove IRQ handling because, in the case of I2C, it’s slightly different from SPI (Figure 4).

IRQ configuration and ISR handling section
Figure 4. IRQ configuration and ISR handling section.

 

After that, in the other peripheral control APIs section, remove all the APIs excluding the peripheral control and get flag status APIs (Figure 5) because I2C also has a couple of flags.

API Prototypes
Figure 5. Other peripheral control APIs section.

 

Keep the application callback API shown in Figure 6, which will be used while implementing the interrupt-based APIs.

Application callback API
Figure 6. Application callback API.

These are some of the APIs that have to be implemented.

 

Exercises:

  1. Implement I2C peripheral clock control, which is very easy, and the concept is the same as the SPI. This is to enable or disable the peripheral clock.
  2. Implement the I2C_DeInit.
  3. No code change is required for I2C interrupt config and priority config; they are the same as SPI or GPIO. So, no need to change the old code. Use it as it is.
  4. Implement I2C peripheral control. The peripheral control is to enable or disable the peripheral. As SPI, the I2C also has a peripheral control bit. There is a PE bit in the control register (Figure 7). PE stands for peripheral enable, and this bit can be used to enable or disable the peripheral.

 

I2C control register.
Figure 7. I2C control register.

 

In the following article, let’s see I2C Init Implementation Steps .

 

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.