From 33e8d1e0b02b668a30e6de153a758ed904a8c7f2 Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Thu, 8 Apr 2021 14:28:26 -0700 Subject: [PATCH 1/2] dreiver/i2c: delete i2c cmd_mux semaphore more cleanly Merges https://github.com/espressif/esp-idf/pull/6848 --- components/driver/i2c.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/components/driver/i2c.c b/components/driver/i2c.c index 522555c866..584b5b3e8a 100644 --- a/components/driver/i2c.c +++ b/components/driver/i2c.c @@ -401,7 +401,9 @@ esp_err_t i2c_driver_delete(i2c_port_t i2c_num) p_i2c->intr_handle = NULL; if (p_i2c->cmd_mux) { + // Let any command in progress finish. xSemaphoreTake(p_i2c->cmd_mux, portMAX_DELAY); + xSemaphoreGive(p_i2c->cmd_mux); vSemaphoreDelete(p_i2c->cmd_mux); } if (p_i2c_obj[i2c_num]->cmd_evt_queue) { From b807f2a6667da32f4b7425f9c57ddb16c8441e4e Mon Sep 17 00:00:00 2001 From: laokaiyao Date: Fri, 7 May 2021 18:03:10 +0800 Subject: [PATCH 2/2] driver/i2c: add notes to i2c deleteing function --- components/driver/include/driver/i2c.h | 3 +++ docs/en/api-reference/peripherals/i2c.rst | 1 + 2 files changed, 4 insertions(+) diff --git a/components/driver/include/driver/i2c.h b/components/driver/include/driver/i2c.h index a2076cc459..ac4f419daa 100644 --- a/components/driver/include/driver/i2c.h +++ b/components/driver/include/driver/i2c.h @@ -97,6 +97,9 @@ esp_err_t i2c_driver_install(i2c_port_t i2c_num, i2c_mode_t mode, size_t slv_rx_ /** * @brief I2C driver delete * + * @note This function does not guarantee thread safety. + * Please make sure that no thread will continuously hold semaphores before calling the delete function. + * * @param i2c_num I2C port number * * @return diff --git a/docs/en/api-reference/peripherals/i2c.rst b/docs/en/api-reference/peripherals/i2c.rst index 32be8cbb3a..3a647e6fa3 100644 --- a/docs/en/api-reference/peripherals/i2c.rst +++ b/docs/en/api-reference/peripherals/i2c.rst @@ -351,6 +351,7 @@ Delete Driver When the I2C communication is established with the function :cpp:func:`i2c_driver_install` and is not required for some substantial amount of time, the driver may be deinitialized to release allocated resources by calling :cpp:func:`i2c_driver_delete`. +Before calling :cpp:func:`i2c_driver_delete` to remove i2c driver, please make sure that all threads have stopped using the driver in any way, because this function does not guarantee thread safety. Application Example -------------------