Chapter 2. API Reference
Table 3: Other Configurable I2C Communication Parameters
Parameters to Change Function
High time and low time for SCL pulses i2c_set_period()
SCL and SDA signal timing used during generation of start signals i2c_set_start_timing()
SCL and SDA signal timing used during generation of stop signals i2c_set_stop_timing()
Timing relationship between SCL and SDA signals when slave samples,
as well as when master toggles
i2c_set_data_timing()
I2C timeout i2c_set_timeout()
Choice between transmitting / receiving the LSB or MSB first, choose
one of the modes defined in i2c_trans_mode_t
i2c_set_data_mode()
Each of the above functions has a _get_ counterpart to check the currently set value. For example, to check the I2C
timeout value, call i2c_get_timeout().
To check the default parameter values which are set during the driver configuration process, please refer to the file
driver/i2c.c and look for defines with the suffix _DEFAULT.
You can also select different pins for SDA and SCL signals and alter the configuration of pull-ups with the function
i2c_set_pin(). If you want to modify already entered values, use the function i2c_param_config().
Note: ESP32-S2’s internal pull-ups are in the range of tens of kOhm, which is, in most cases, insufficient for use
as I2C pull-ups. Users are advised to use external pull-ups with values described in the I2C specification.
Error Handling The majority of I2C driver functions either return ESP_OK on successful completion or a specific
error code on failure. It is a good practice to always check the returned values and implement error handling. The
driver also prints out log messages that contain error details, e.g., when checking the validity of entered configuration.
For details please refer to the file driver/i2c.c and look for defines with the suffix _ERR_STR.
Use dedicated interrupts to capture communication failures. For instance, if a slave stretches the clock for too long
while preparing the data to send back to master, the interrupt I2C_TIME_OUT_INT will be triggered. For detailed
information, see Interrupt Handling.
In case of a communication failure, you can reset the internal hardware buffers by calling the functions
i2c_reset_tx_fifo() and i2c_reset_rx_fifo() for the send and receive buffers respectively.
Delete Driver When the I2C communication is established with the function i2c_driver_install() and
is not required for some substantial amount of time, the driver may be deinitialized to release allocated resources by
calling i2c_driver_delete().
Before calling i2c_driver_delete() to remove i2c driver, please make sure that all threads have stopped using
the driver in any way, because this function does not guarantee thread safety.
Application Example
I2C master and slave example: peripherals/i2c.
API Reference
Header File
• components/driver/include/driver/i2c.h
Espressif Systems 285
Submit Document Feedback
Release v4.4