STM32 I2C Lecture 43: Implementation of I2C interrupt-based APIs

  • Post author:
  • Post category:Blog

 

Implementation of I2C interrupt-based APIs

 

The code for MasterSendDataIT API is shown in Figure 1.

Figure 1. Code for MasterSendDataIT API.

 

Steps of implementation:

1. Save all the details into the header, as shown in Figure 2.

Figure 2. Code to save all the details into the header.

 

2. Trigger the start condition (Figure 3). When the start condition is triggered, the SB or start bit will be set to 1, causing an interrupt. After that, sending other data like address phase and other things will be taken care of by the interrupt service routine or ISR.

Implementation of I2C interrupt-based APIs
Figure 3. Code to trigger start condition.

 

3. Enable all the required control bits, as shown in Figure 4.

Implementation of I2C interrupt-based APIs
Figure 4. Code to enable the control bits.

 

Look at Figure 5. The first interrupt which is going to be triggered in this case will be SB interrupt. Therefore, the SB flag will be set, and since the control bit ITEVFEN is already enabled, the interrupt will be triggered on the IRQ number 31, and the interrupt handler will handle the triggered interrupt.

After completing the interrupt phase, you can send other data like addresses and other things.

The I2C_MasterReceiveDataIT is similar to master send data, but the difference is you have to mention the RX buffer instead of the TX buffer and set the state to I2C_BUSY_IN_RX, as shown in Figure 5.

Implementation of I2C interrupt-based APIs
Figure 5. Code for MasterReceiveDataIT API.

 

4. Compile the code to confirm whether the build is proper or not.

Implementation of I2C interrupt-based APIs
Figure 6. Code compilation.

Observe Figure 6, the compilation is successful, and there are no errors in the code.

In the following article, let’s see I2C IRQ handler implementation

 

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.