STM32 I2C Lecture 52: I2C error IRQ handler implementation

  • Post author:
  • Post category:Blog

 

 

 

I2C error IRQ handler implementation

 

 


In this article, let’s learn about the implementation of I2C_ER_IRQHandling.

Exercise:

1. Download the file (Figure 1) attached to the resource section.

I2C error IRQ handler implementation
Figure 1. Implementation of I2C_ER_IRQHandling.

 

2. In the downloaded file, the code is partially written, and it contains some comments (Figure 2), according to which you have to implement the code.

I2C error IRQ handler implementation
Figure 2. Comments for implementing I2C_ER_IRQHandling.

 

3. Steps for implementation:

  • When the I2C error IRQ handling (I2C_ER_IRQHandling()) is called first, you need to check what kind of error has happened.
  • Check for the bus error by checking the bus error flag in the status register 1 (Figure 3). If it is set, then clear the bus error flag, then notify the application about the error by calling the I2C_ApplicationEventCallback(), and the event I2C_ERROR_BERR is passed as a parameter of I2C_ApplicationEventCallback(), as shown in Figure 3. BERR is nothing but a bus error.
I2C error IRQ handler implementation
Figure 3. Check for the bus error.

 

  • The procedure to clear the bus error flag is given in the document itself. Check the BERR flag of the status register 1 shown in Figure 4.

If the BERR flag is set, then it means that misplaced start or stop condition. This flag is set by the hardware when the interface detects an SDA’s rising or falling edge while SCL is high, occurring in a non-valid position during a byte transfer, and the hardware will not clear it automatically. The software clears it. But this flag will get cleared automatically when you disable the peripheral (PE=0).

I2C error IRQ handler implementation
Figure 4. BERR flag of the status register 1.

 

  • The bus error flag is cleared, as shown in Figure 5.
I2C error IRQ handler implementation
Figure 5. Code to clear the bus error flag.

 

  • Similar things you have to do for other errors.
  • Copy the definition of all the event flags which will be sent to the application (Figure 6) and paste it into the header file driver.h, as shown in Figure 7.
Definition of the event flags
Figure 6. Definition of the event flags.

 

Pasting the event flag definitions into the header file driver.h
Figure 7. Pasting the event flag definitions into the header file driver.h.

 

In the following article, let’s do the exercise :Testing I2C interrupt APIs.

 

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.