shared_bus

Example of the component esp-idf-lib/i2cdev v2.3.0
# Sharing the bus with another ESP-IDF component

Many components do not use `i2cdev`. They are built on the ESP-IDF I2C master
driver directly: they take an `i2c_master_bus_handle_t` and create their own
devices on it. `esp_lcd` panels are the usual example, but the same is true for
sensors ported from other projects.

The bus in i2cdev is private: it is created on the first operation on a port and
lives until `i2cdev_done()`. Another component cannot guess its handle, so it has
to be handed over:

```c
i2cdev_init();

i2c_dev_t sensor = { .port = I2C_NUM_0, .addr = 0x48, .cfg = { ... } };
i2c_dev_create_mutex(&sensor);
(void)i2c_dev_check_present(&sensor);   /* the first operation creates the bus */

i2c_master_bus_handle_t bus;
ESP_ERROR_CHECK(i2cdev_get_shared_handle(I2C_NUM_0, (void **)&bus));

/* the other component joins the bus */
esp_lcd_new_panel_io_i2c(bus, &io_config, &io_handle);
```

This example does the same, but its "other component" is a ten-line stand-in
instead of a real display, so it runs on any board:

* `i2cdev` probes its device through `i2c_dev_probe()`;
* the stand-in adds a device with `i2c_master_bus_add_device()` on the shared
  handle and reads a register with `i2c_master_transmit_receive()`;
* both are used in one loop.

## What is and what is not guaranteed

The ESP-IDF driver holds the bus lock around every transfer, so transactions of
both sides never overlap on the wire. That is the only thing that is shared.
Per-device access is not: an `i2c_dev_t` must be used under
`I2C_DEV_TAKE_MUTEX()`/`I2C_DEV_GIVE_MUTEX()`, and the other component
serializes access to its own device handle.

Cleanup order matters, because the other component's device is unknown to
i2cdev:

```c
esp_lcd_panel_io_del(io_handle);   /* the external device leaves the bus */
i2c_dev_delete_mutex(&sensor);     /* the i2cdev device leaves the bus */
i2cdev_done();                     /* now the bus is deleted */
```

## This needs the new driver

Sharing is a feature of the new `i2c_master` driver. With the legacy
`driver/i2c.h` driver the port is owned by `i2c_driver_install()`, there is no
bus handle to give away, and `i2cdev_get_shared_handle()` returns
`ESP_ERR_NOT_SUPPORTED`. The two drivers cannot coexist on one port either, so
there is no workaround: a project that has to share the bus must use the new
driver, that is ESP-IDF 5.3 or newer, without
`CONFIG_I2CDEV_USE_LEGACY_DRIVER`.

## Build and run

```
idf.py build
idf.py -p /dev/ttyUSB0 flash monitor
```

The output shows both sides of the bus talking to the same address:

```
I (312) shared_bus: i2cdev device 0x48 on port 0: present
I (322) shared_bus: shared bus handle: 0x3fc9...
I (332) shared_bus: other driver 0x48 joined: present
I (342) shared_bus: 0: i2cdev probe ESP_OK, other driver read 0x01: ESP_OK (0x85)
```

## Configuration

| Option | Default | Meaning |
|---|---|---|
| `EXAMPLE_I2C_CLOCK_HZ` | 100000 | Bus clock, Hz |
| `EXAMPLE_I2C_MASTER_SDA` | board default | SDA pin |
| `EXAMPLE_I2C_MASTER_SCL` | board default | SCL pin |
| `EXAMPLE_OTHER_ADDR` | 0x48 | Address the stand-in driver uses |
| `EXAMPLE_OTHER_REG` | 0x01 | Register it reads |
| `EXAMPLE_ROUNDS` | 5 | How many rounds to run |

The pins are declared in `main/Kconfig.projbuild`, so the defaults are the usual
ones for the selected chip and the example builds on its own, without the rest of
the component's source tree.

To create a project from this example, run:

idf.py create-project-from-example "esp-idf-lib/i2cdev=2.3.0:shared_bus"

or download archive (~5.05 KB)