espressif / esp-idf

Espressif IoT Development Framework. Official development framework for Espressif SoCs.
Apache License 2.0
13.84k stars 7.32k forks source link

Why isn't the new I2C API deemed as thread safe? (IDFGH-14050) #14869

Closed wuyuanyi135 closed 2 weeks ago

wuyuanyi135 commented 2 weeks ago

Answers checklist.

General issue report

According to the doc, only bus creation was deemed as thread safe. Tracing the source code I found that the API are all semaphore-guarded that prevent racing access on the same bus:

For example: https://github.com/espressif/esp-idf/blob/ce6085349f8d5a95fc857e28e2d73d73dd3629b5/components/esp_driver_i2c/i2c_master.c#L898

If so, why are we recommended to put extra locks as per the doc?

mythbuster5 commented 2 weeks ago

Yes, the doc is wrong and it will be updated soon

The factory function :cpp:func:`i2c_new_master_bus` and :cpp:func:`i2c_new_slave_device` are guaranteed to be thread safe by the driver, which means that the functions can be called from different RTOS tasks without protection by extra locks.

I2C master operation functions are also guaranteed to be thread safe by bus operation semaphore.

- :cpp:func:`i2c_master_transmit`
- :cpp:func:`i2c_master_multi_buffer_transmit`
- :cpp:func:`i2c_master_transmit_receive`
- :cpp:func:`i2c_master_receive`
- :cpp:func:`i2c_master_probe`
wuyuanyi135 commented 2 weeks ago

@mythbuster5 Thank you for the confirmation