STM32 I2C Lecture 41: I2C adding interrupt related macros and interrupt APIs

  • Post author:
  • Post category:Blog

 

I2C adding interrupt related macros and interrupt APIs

 

 


Let’s start implementing non-blocking data communication APIs to send and receive data with IT.

Steps:

1. Modify the handle structure of I2C, as shown in below code snippet.

/*
*Handle structure for I2Cx peripheral
*/
typedef struct
{
   I2C_RegDef_t *pI2Cx;
   I2C_Config_t I2C_Config;
       uint8_t *pTxBuffer; /* !< To store the app. Tx buffer address > */
       uint8_t *pRxBuffer; /* !< To store the app. Rx buffer address > */
       uint32_t TxLen; /* !< To store Tx len > */
       uint32_t RxLen; /* !< To store Tx len > */
       uint8_t TxRxState; /* !< To store Communication state > */
       uint8_t DevAddr; /* !< To store slave/device address > */
       uint32_t RxSize; /* !< To store Rx size > */
       uint8_t Sr; /* !< To store repeated start value > */
}I2C_Handle_t;

 

2. Define the application states.

/*
* I2C application states
*/
#define I2C_READY 0
#define I2C_BUSY_IN_RX 1
#define I2C_BUSY_IN_TX 2

Defining application states

 

3. Go to the I2C_driver.h file of the project and create the prototypes, as shown in Figure 3.

The parameters of the API will be the same, but the difference is the return type of the API should be uint8_t since these APIs return the application’s state. The application’s state may be ready, busy in transmission, or busy in reception, etc.

I2C adding interrupt related macros and interrupt APIs
Figure 3. Creation of prototypes for APIs.

 

4. Implement the APIs in the driver source file, as shown in Figure 4.

I2C adding interrupt related macros and interrupt APIs
Figure 4. Implementing the APIs in the driver source file.

 

It is necessary to complete a couple of other things before implementing the MasterSendDataIT and MasterReceiveDataIT APIs.

  • Add the IRQ number macros in a device-specific header file, as shown in Figure 5.
I2C adding interrupt related macros and interrupt APIs
Figure 5. Adding IRQ number macros.

 

If you want to add more macros related to other I2C peripherals like I2C2 or I2C3, then add those macros after IRQ_NO_I2C1_EV and after IRQ_NO_I2C1_ER macros (Figure 5) along with the appropriate IRQ numbers.

  • Implement I2C IRQ interrupt config and I2C IRQ priority config (Figure 6) in I2C_driver.c file.
I2C adding interrupt related macros and interrupt APIs
Figure 6. Prototype of I2C IRQ interrupt config and I2C IRQ priority config APIs in I2C_driver.h file.

 

In the following article, let’s do the Assignment: I2C interrupt APIs 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.