STM32 I2C Lecture 9: I2C User Configurable Macros

  • Post author:
  • Post category:Blog

 

I2C User Configurable Macros

 

 

The process of configuring and handling I2C drivers often involves creating specific structures and macros for customization. Below, we provide a more structured and detailed explanation of the steps involved in configuring I2C drivers using user-configurable macros:

1. Configuration and Handle Structure:

In the i2c_driver.h file (as shown in Figure 1), a configuration and handle structure is created. This structure will be used to customize various parameters of the I2C driver.

Figure 1. Creation of configuration and handle structure in i2c_driver.h file.

 

2. Adding Headers:

  • Import the stm32f407xx.h header from SPI_driver.h into every driver.h file in your project (as shown in Figure 2). This header provides access to STM32F407xx specific definitions and registers.
Figure 2. stm32f407xx.h header added in every driver.h file.

 

3. Including i2c_driver.h:

  • Add the i2c_driver.h file at the end of the device-specific header or stm32f407xx.h (as shown in Figure 3). This ensures that the I2C driver configurations are available for your project.
Figure 3. i2c_driver.h file added at the end of the stm32f407xx.h.

 

4. Initializing I2C Driver Parameters:

  • The i2c_driver.c file starts empty (as in Figure 4), and now you will initialize various member elements of the I2C driver structure.
Figure 4. i2c_driver.c file.

 

5. Initialize I2C_SCLSpeed:

  • The speed of the I2C bus can be set to either normal or fast mode. To achieve this, you create macros for initializing the I2C_SCLSpeed member variable.
  • Two options are provided: normal mode (100KHz) and fast mode (up to 400KHz), as shown in Figure 5.1.1.
  • Additional macros can be created to specify speeds between 100KHz and 400KHz, as depicted in Figure 5.1.2.

 

I2C user configurable macros
Figure 5.1.1. Initializing member element I2C_SCLSpeed.

 

Figure 5.1.2. Creating more macros to get the value between 100KHz and 400KHz.

 

6. Initialize I2C_DeviceAddress:

  • The I2C_DeviceAddress is of type uint8_t and represents the 7-bit address of the slave device. Since it is user-defined, no specific initialization option is needed.

7.Initialize I2C_AckControl:

  • The I2C acknowledgment control is configured by setting or clearing the 10th bit of the CR1 register. By default, automatic acknowledgment is disabled (10th bit = 0), meaning no acknowledgment is returned for received bytes.
  • To enable acknowledgment, the 10th bit of the CR1 register should be set to 1, as shown in Figure 5.3.
I2C user configurable macros
Figure 5.3. Initializing I2C_AckControl.

 

8. Initialize I2CFMDutyCycle:

  • This parameter is used to initialize the duty cycle of the serial clock in fast mode.
  • You can refer to the 14th bit of the CCR register (Figure 5.4.1) to set the duty cycle.
  • Two macros, I2C_FM_DUTY_2 and I2C_FM_DUTY_16_9, are created to specify the two types of duty cycles (2 and 16/9), as demonstrated in Figure 5.4.2.
I2C user configurable macros
Figure 5.4.1. I2C_CCR

 

I2C user configurable macros
Figure 5.4.2. Initializing I2CFMDutyCycle.

The above four are the options to initialize the configurable items.

 

In the following article, let’s see I2C API Prototypes.

 

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.