espressif / esp-adf

Espressif Audio Development Framework
Other
1.49k stars 667 forks source link

Add support for safe I2C actions from outside this library (AUD-5257) #1165

Open X-Ryl669 opened 4 months ago

X-Ryl669 commented 4 months ago

Currently, it's not possible to have esp-adf with other code base using I2C.

This is because another code base will call esp-idf's i2c_driver_install on some i2c_port and so will esp_adf via the esp_peripherals/driver/i2c_bus component.

I2C bus does check if its own driver is already installed with the same configuration and skip it in that case but it doesn't check if some other code installed it. In that case, it'll error out and any code depending on this i2c_bus will fail later one, breaking the application.

All I2C related stuff is private/static in i2c_bus.c so it's not possible for the other code to figure out what is the state of the i2c_port without potentially breaking it when installing the driver. i2c_driver_install will error out if it's already installed so the other code will also error out, preventing any later I2C work to be done.

If the other code is modified to skip driver installation, then it'll likely break since there is no way to access the mutex protecting the bus from outside the i2c_bus.c file nor is there a way to get the i2c_port_t instance.

This patch allows to run I2C related code from outside this component without exposing the inner working of this component. It's done by running a user provided callback function with the mutex taken and the right i2c_port_t instance for the bus.

I think it's the cleanest solution that can allow heterogeneous code to work together (for example ESPHome).

CLAassistant commented 4 months ago

CLA assistant check
All committers have signed the CLA.