I2C handling

atm_turbo
Posts: 2
Joined: Thu Jun 13, 2024 1:58 pm

I2C handling

Postby atm_turbo » Sat Nov 23, 2024 3:14 pm

Hello everyone.

I have an LCD display with touch controller which is under managed_components. The touch driver uses I2C communication.
I need also connect multiple I2C devices in my code, so i use the same logic for all the components:

FRAM:

Code: Select all

	i2c_master_bus_config_t i2c_mst_config = {
	    .clk_source = I2C_CLK_SRC_DEFAULT,
	    .i2c_port = I2C_NUM_1,
	    .scl_io_num = CONFIG_I2C_FRAM_SCL_PIN,
	    .sda_io_num = CONFIG_I2C_FRAM_SDA_PIN,
	    .glitch_ignore_cnt = 7,
	    .flags.enable_internal_pullup = true,
	};

	i2c_master_bus_handle_t bus_handle;
	ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_mst_config, &bus_handle));

	i2c_device_config_t dev_cfg = {
	    .dev_addr_length = I2C_ADDR_BIT_LEN_7,
	    .device_address = CONFIG_I2C_FRAM_ADDRESS,
	    .scl_speed_hz = 400000,
	};

	ESP_ERROR_CHECK(i2c_master_bus_add_device(bus_handle, &dev_cfg, &s_hMemory));
Sensors IC:

Code: Select all

	i2c_master_bus_config_t i2c_mst_config = {
	    .clk_source = I2C_CLK_SRC_DEFAULT,
	    .i2c_port = I2C_NUM_1,
	    .scl_io_num = CONFIG_I2C_HOME_SENSORS_SCL_PIN,
	    .sda_io_num = CONFIG_I2C_HOME_SENSORS_SDA_PIN,
	    .glitch_ignore_cnt = 7,
	    .flags.enable_internal_pullup = true,
	};

	i2c_master_bus_handle_t bus_handle;
	ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_mst_config, &bus_handle));

	i2c_device_config_t dev_cfg = {
	    .dev_addr_length = I2C_ADDR_BIT_LEN_7,
	    .device_address = CONFIG_I2C_HOME_SENSORS_ADDRESS,
	    .scl_speed_hz = 100000,
	};

	ESP_ERROR_CHECK(i2c_master_bus_add_device(bus_handle, &dev_cfg, &s_hSensors));
The pins config is the same but i get the error:

Code: Select all

E (3253) i2c.common: I2C bus id(1) has already been acquired
E (3253) i2c.common: acquire bus failed
E (3263) i2c.master: i2c_new_master_bus(804): I2C bus acquire failed
E (3263) i2c.common: i2c_release_bus_handle(154): Bus not freed entirely
ESP_ERROR_CHECK failed: esp_err_t 0x103 (ESP_ERR_INVALID_STATE) at 0x400d9130
As far as i understand i can't call i2c_new_master_bus for the same I2C port. How to handle properly this situation. I have some components which require I2C but they failed when getting bus handle. SPI master doesn't require bus handle to add a device but I2C requires this. Is there a function which returns I2C handle by port?

MicroController
Posts: 1759
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: I2C handling

Postby MicroController » Sun Nov 24, 2024 1:09 pm

Just in case it's not already obvious: In order to be able to share one I2C bus, the components using I2C should accept an i2c_master_bus_handle_t passed to them as argument on intialization.

Who is online

Users browsing this forum: No registered users and 50 guests