From 0a098f49d4236ecff8b21cf7066fcf572847c454 Mon Sep 17 00:00:00 2001 From: "C.S.M" Date: Wed, 6 Nov 2024 13:52:45 +0800 Subject: [PATCH] fix(i2c): Add bus handle check so that it will not be panic when there is no free bus, Closes https://github.com/espressif/esp-idf/issues/14819 --- components/esp_driver_i2c/i2c_master.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/components/esp_driver_i2c/i2c_master.c b/components/esp_driver_i2c/i2c_master.c index 5b1ef7c723..44a6cff388 100644 --- a/components/esp_driver_i2c/i2c_master.c +++ b/components/esp_driver_i2c/i2c_master.c @@ -784,8 +784,12 @@ static esp_err_t i2c_master_bus_destroy(i2c_master_bus_handle_t bus_handle) { ESP_RETURN_ON_FALSE(bus_handle, ESP_ERR_INVALID_ARG, TAG, "no memory for i2c master bus"); i2c_master_bus_handle_t i2c_master = bus_handle; - i2c_common_deinit_pins(i2c_master->base); - if (i2c_release_bus_handle(i2c_master->base) == ESP_OK) { + esp_err_t err = ESP_OK; + if (i2c_master->base) { + i2c_common_deinit_pins(i2c_master->base); + err = i2c_release_bus_handle(i2c_master->base); + } + if (err == ESP_OK) { if (i2c_master) { if (i2c_master->bus_lock_mux) { vSemaphoreDeleteWithCaps(i2c_master->bus_lock_mux);