ashwatermelon

ashwatermelon

ESP-IDF Learning Notes Section Two: I2C

I'm back again. In this lesson, we will discuss how to use the I2C peripheral of the ESP32 and port the OLED code from Jiangnan University.
First, the OLED code from Jiangnan University uses software I2C, so let's talk about how to port the software I2C.
The files we need to modify are mainly OLED.c and OLED_Font.h.
First, for OLED.c, we mainly need to change the initialization function OLED_I2C_Init() and the two macro definitions OLED_W_SCL(x) and OLED_W_SDA(x).
We first import the "driver/gpio.h" header file (which I forgot to mention last time). According to the original initialization content of OLED_I2C_Init(), we initialize two pins, one for SCL and one for SDA, both configured as open-drain output with pull-up mode.
Then, the macro definitions OLED_W_SCL(x) and OLED_W_SDA(x) are changed from the original GPIO_WriteBit() to gpio_set_level(). This completes the modification of OLED.c.
The final effect is as follows:

image

Next, we need to modify the OLED_Font.h file, which requires adding curly braces to each group of the two-dimensional array. I don't know why this is necessary, but the compiler requires it ().

image

After completing these steps, the code is successfully ported. Now, let me explain how to use the I2C peripheral.
First, the ESP-WROOM-32 has only one I2C peripheral, and the pin definition diagram is as follows:

image

Pin 22 is SCL, and pin 21 is SDA.
So how do we configure the I2C peripheral?
First, we need to configure the i2c_config_t structure, which includes the following parameters:
I2C_CONFIG_ST.clk_flags
Clock selection

    I2C_CONFIG_ST.master.clk_speed  
    Clock speed  

    I2C_CONFIG_ST.mode  
    I2C mode (master/slave)  

    I2C_CONFIG_ST.scl_io_num  
    SCL pin number  

    I2C_CONFIG_ST.scl_pullup_en  
    Whether to use internal pull-up for SCL pin  

    I2C_CONFIG_ST.sda_io_num  
    SDA pin number  

    I2C_CONFIG_ST.sda_pullup_en  
    Whether to use internal pull-up for SDA pin  

After configuring the structure, call the i2c_param_config() function to initialize I2C.
Then we call i2c_driver_install() to install the I2C driver.
Next, we call i2c_cmd_link_create() to create an I2C command buffer and use an i2c_cmd_handle_t variable to store the handle.
Then we use i2c_master_start() to start I2C, and i2c_master_write_byte() and i2c_master_read_byte() for reading and writing.
Finally, we use i2c_master_stop() to end I2C, and i2c_master_cmd_begin() to send the commands in the buffer, then i2c_cmd_link_delete() to clear the buffer.

This is the complete process of using the I2C peripheral. This is the old version of the code; the new version has been deprecated. However, since I couldn't get the new version to work, I ended up using the old version.
Here is the actual code:

image

It is still modified based on the Jiangnan University source code, changing the software part to hardware.
Here is the final effect:

IMG20250913152059

That's all for today. Goodbye.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.