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

Please provide a simple a working and simple example of the new I2C API (IDFGH-14090) #14902

Open
3 tasks done
juantxorena opened this issue Nov 17, 2024 · 2 comments
Open
3 tasks done
Labels
Status: Opened Issue is new

Comments

@juantxorena
Copy link

Answers checklist.

  • I have read the documentation ESP-IDF Programming Guide and the issue is not addressed there.
  • I have updated my IDF branch (master or release) to the latest version and checked that the issue is present there.
  • I have searched the issue tracker for a similar issue and not found a similar issue.

General issue report

Since version v5.2 there's a new API for I2C devices, and the old one is deprecated. There's only an example in the repo, but it's IMHO overly complicated and not very applicable to a lot of people.

See the old example:
https://github.com/espressif/esp-idf/tree/release/v5.1/examples/peripherals/i2c/i2c_simple

It is a simple device where it's configured, and it's shown how to read from a register and write to a register. Very few lines of code, and enough to get a gist of the API.

And now compare this to the new example, for reading and writing from an EEPROM:
https://github.com/espressif/esp-idf/tree/master/examples/peripherals/i2c/i2c_eeprom

it's a mess with a ton of type definitions, apparently for no reason:


memory allocations where in the old one none was needed:
out_handle = (i2c_eeprom_handle_t)calloc(1, sizeof(*out_handle));
ESP_GOTO_ON_FALSE(out_handle, ESP_ERR_NO_MEM, err, TAG, "no memory for i2c eeprom device");
i2c_device_config_t i2c_dev_conf = {
.scl_speed_hz = eeprom_config->eeprom_device.scl_speed_hz,
.device_address = eeprom_config->eeprom_device.device_address,
};
if (out_handle->i2c_dev == NULL) {
ESP_GOTO_ON_ERROR(i2c_master_bus_add_device(bus_handle, &i2c_dev_conf, &out_handle->i2c_dev), err, TAG, "i2c new bus failed");
}
out_handle->buffer = (uint8_t*)calloc(1, eeprom_config->addr_wordlen + I2C_EEPROM_MAX_TRANS_UNIT);

managing a buffer manually:
uint8_t buf[LENGTH];
for (int i = 0; i < LENGTH; i++) {
buf[i] = i;
}
uint8_t read_buf[LENGTH];

etc. It just looks overly verbose and complicated.

So please provide a simple example for the new I2C API where it's shown how to:

  • Configure the master and the device
  • Read from a register
  • Write to a register
@espressif-bot espressif-bot added the Status: Opened Issue is new label Nov 17, 2024
@github-actions github-actions bot changed the title Please provide a simple a working and simple example of the new I2C API Please provide a simple a working and simple example of the new I2C API (IDFGH-14090) Nov 17, 2024
@suda-morris
Copy link
Collaborator

This example function should illustrate the "i2c write to a register" well: https://github.com/espressif/esp-idf/blob/master/examples/peripherals/i2c/i2c_tools/main/cmd_i2ctools.c#L206

This example function should illustrate the "i2c read from a register" well: https://github.com/espressif/esp-idf/blob/master/examples/peripherals/i2c/i2c_tools/main/cmd_i2ctools.c#L152

@suda-morris
Copy link
Collaborator

linked in #14315

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Opened Issue is new
Projects
None yet
Development

No branches or pull requests

3 participants