Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple I2c Addresses on same bus (IDFGH-14214) #15011

Open
panilsunil opened this issue Dec 10, 2024 · 2 comments
Open

Multiple I2c Addresses on same bus (IDFGH-14214) #15011

panilsunil opened this issue Dec 10, 2024 · 2 comments
Assignees
Labels
Resolution: NA Issue resolution is unavailable Status: Done Issue is done internally Type: Feature Request Feature request for IDF

Comments

@panilsunil
Copy link

Is your feature request related to a problem?

I am using ESP32-S3-Touch-LCD-4.3B. This device have 26 Address on the bus, which needs to add each and every address using i2c_master_bus_add_device. Which sums up 26 (i2c_master_dev_handle_t) Devs handles. Ref: https://www.waveshare.com/wiki/ESP32-S3-Touch-LCD-4.3B

Describe the solution you'd like.

By adding these tow functions in i2c_master.c we can change the device address on the fly.
uint16_t i2c_master_get_device_address(i2c_master_dev_handle_t i2c_dev)
{
ESP_RETURN_ON_FALSE(i2c_dev != NULL, ESP_ERR_INVALID_ARG, TAG, "i2c handle not initialized");
return i2c_dev->device_address;
}

esp_err_t i2c_master_set_device_address(i2c_master_dev_handle_t i2c_dev, uint16_t device_address)
{
ESP_RETURN_ON_FALSE(i2c_dev != NULL, ESP_ERR_INVALID_ARG, TAG, "i2c handle not initialized");
i2c_dev->device_address = device_address;
return ESP_OK;
}

Describe alternatives you've considered.

If we can change the address of the (i2c_master_dev_handle_t) dynamically, then the task can be achieved using a small number of (i2c_master_dev_handle_t) dev Handles.,

Additional context.

image

@panilsunil panilsunil added the Type: Feature Request Feature request for IDF label Dec 10, 2024
@github-actions github-actions bot changed the title Multiple I2c Addresses on same bus Multiple I2c Addresses on same bus (IDFGH-14214) Dec 10, 2024
@espressif-bot espressif-bot added the Status: Opened Issue is new label Dec 10, 2024
@nopnop2002
Copy link

nopnop2002 commented Dec 10, 2024

Legacy's i2c driver allows us to change the device address on the fly.

int _i2c_master_write_byte(int i2c_address) {
	i2c_cmd_handle_t cmd = i2c_cmd_link_create();
	i2c_master_start(cmd);
	i2c_master_write_byte(cmd, (i2c_address << 1) | I2C_MASTER_WRITE, true);
	i2c_master_stop(cmd);

	esp_err_t res = i2c_master_cmd_begin(I2C_NUM, cmd, I2C_TICKS_TO_WAIT);
	if (res == ESP_OK) {
		ESP_LOGI(TAG, "i2c_master_cmd_begin successfully");
	} else {
		ESP_LOGE(TAG, "i2c_master_cmd_begin failed. code: 0x%.2X", res);
	}
	i2c_cmd_link_delete(cmd);
}

void app_main(void) {
	i2c_config_t i2c_config = {
		.mode = I2C_MODE_MASTER,
		.sda_io_num = sda,
		.scl_io_num = scl,
		.sda_pullup_en = GPIO_PULLUP_ENABLE,
		.scl_pullup_en = GPIO_PULLUP_ENABLE,
		.master.clk_speed = I2C_MASTER_FREQ_HZ
	};
	ESP_ERROR_CHECK(i2c_param_config(I2C_NUM, &i2c_config));
	ESP_ERROR_CHECK(i2c_driver_install(I2C_NUM, I2C_MODE_MASTER, 0, 0, 0));

        _i2c_master_write_byte(0x20);
        _i2c_master_write_byte(0x21);
        _i2c_master_write_byte(0x22);
        _i2c_master_write_byte(0x23);
}

@suda-morris
Copy link
Collaborator

The new i2c master driver give the "transmit" function to the "i2c device" instead of the "i2c bus". This is by design. Because we can't guarantee all the devices will share the same configurations like "SCL frequency", "SCL wait time", "10bit address or not". These parameters belong to the I2c Device not the I2C bus.

But by adding a i2c_master_device_set_address may still be useful if e.g. I have 4 temperature they're the same just with different hw address. Then I only have to create one handle shared by all these 4 sensors 🤔 @mythbuster5

@espressif-bot espressif-bot added Status: Done Issue is done internally Resolution: NA Issue resolution is unavailable and removed Status: Opened Issue is new labels Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: NA Issue resolution is unavailable Status: Done Issue is done internally Type: Feature Request Feature request for IDF
Projects
None yet
Development

No branches or pull requests

5 participants