Chapter 2. API Reference
5. Customized Configuration - adjust default I2C communication parameters (timings, bit order, etc.)
6. Error Handling - how to recognize and handle driver configuration and communication errors
7. Delete Driver- release resources used by the I2C driver when communication ends
Configuration To establish I2C communication, start by configuring the driver. This is done by setting the param-
eters of the structure i2c_config_t:
• Set I2C mode of operation - slave or master from i2c_mode_t
• Configure communication pins
– Assign GPIO pins for SDA and SCL signals
– Set whether to enable ESP32-S2’s internal pull-ups
• (Master only) Set I2C clock speed
• (Slave only) Configure the following
– Whether to enable 10 bit address mode
– Define slave address
After that, initialize the configuration for a given I2C port. For this, call the function i2c_param_config() and
pass to it the port number and the structure i2c_config_t.
Configuration example (master):
int i2c_master_port = 0;
i2c_config_t conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = I2C_MASTER_SDA_IO, // select GPIO specific to your␣
,→project
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_io_num = I2C_MASTER_SCL_IO, // select GPIO specific to your␣
,→project
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = I2C_MASTER_FREQ_HZ, // select frequency specific to your␣
,→project
// .clk_flags = 0, /*!< Optional, you can use I2C_SCLK_SRC_FLAG_*␣
,→flags to choose i2c source clock here. */
};
Configuration example (slave):
int i2c_slave_port = I2C_SLAVE_NUM;
i2c_config_t conf_slave = {
.sda_io_num = I2C_SLAVE_SDA_IO, // select GPIO specific to your␣
,→project
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_io_num = I2C_SLAVE_SCL_IO, // select GPIO specific to your␣
,→project
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.mode = I2C_MODE_SLAVE,
.slave.addr_10bit_en = 0,
.slave.slave_addr = ESP_SLAVE_ADDR, // address of your project
};
At this stage, i2c_param_config() also sets a few other I2C configuration parameters to default values that are
defined by the I2C specification. For more details on the values and how to modify them, see Customized Configura-
tion.
Source Clock Configuration Clock sources allocator is added for supporting different clock sources. The clock
allocator will choose one clock source that meets all the requirements of frequency and capability (as requested in
i2c_config_t::clk_flags).
When i2c_config_t::clk_flags is 0, the clock allocator will select only according to the desired frequency.
If no special capabilities are needed, such as APB, you can configure the clock allocator to select the source clock only
Espressif Systems 281
Submit Document Feedback
Release v4.4