From 82903b0bc6b2528d6dea6330249b7c498f2569a3 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 6739fa0385..a0fadc64d5 100644 --- a/components/esp_driver_i2c/i2c_master.c +++ b/components/esp_driver_i2c/i2c_master.c @@ -785,8 +785,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);