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.
2. Adding Headers:
- Import the
stm32f407xx.h
header fromSPI_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.
3. Including i2c_driver.h
:
- Add the
i2c_driver.h
file at the end of the device-specific header orstm32f407xx.h
(as shown in Figure 3). This ensures that the I2C driver configurations are available for your project.
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.
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.
6. Initialize I2C_DeviceAddress
:
- The
I2C_DeviceAddress
is of typeuint8_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.
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
andI2C_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.
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