mirror of
https://github.com/espressif/esp-idf.git
synced 2026-08-15 13:22:48 +03:00
fix(i2c): release platform mutex on intr/pm_lock delete failure
ESP_RETURN_ON_ERROR inside the s_i2c_platform.mutex critical section returns without releasing the mutex, permanently blocking all I2C bus operations. Replace with ESP_GOTO_ON_ERROR that jumps to a cleanup label releasing the mutex before return.
This commit is contained in:
@@ -119,21 +119,28 @@ esp_err_t i2c_acquire_bus_handle(i2c_port_num_t port_num, i2c_bus_handle_t *i2c_
|
||||
|
||||
esp_err_t i2c_release_bus_handle(i2c_bus_handle_t i2c_bus)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
int port_num = i2c_bus->port_num;
|
||||
i2c_clock_source_t clk_src = i2c_bus->clk_src;
|
||||
bool do_deinitialize = false;
|
||||
_lock_acquire(&s_i2c_platform.mutex);
|
||||
if (s_i2c_platform.buses[port_num]) {
|
||||
s_i2c_platform.count[port_num]--;
|
||||
if (s_i2c_platform.count[port_num] == 0) {
|
||||
if (s_i2c_platform.count[port_num] > 1) {
|
||||
s_i2c_platform.count[port_num]--;
|
||||
} else {
|
||||
do_deinitialize = true;
|
||||
s_i2c_platform.buses[port_num] = NULL;
|
||||
if (i2c_bus->intr_handle) {
|
||||
ESP_RETURN_ON_ERROR(esp_intr_free(i2c_bus->intr_handle), TAG, "delete interrupt service failed");
|
||||
ESP_GOTO_ON_ERROR(esp_intr_free(i2c_bus->intr_handle), err, TAG, "delete interrupt service failed");
|
||||
i2c_bus->intr_handle = NULL;
|
||||
}
|
||||
#if CONFIG_PM_ENABLE
|
||||
if (i2c_bus->pm_lock) {
|
||||
ESP_RETURN_ON_ERROR(esp_pm_lock_delete(i2c_bus->pm_lock), TAG, "delete pm_lock failed");
|
||||
esp_pm_lock_delete(i2c_bus->pm_lock);
|
||||
i2c_bus->pm_lock = NULL;
|
||||
}
|
||||
#endif
|
||||
s_i2c_platform.count[port_num] = 0;
|
||||
s_i2c_platform.buses[port_num] = NULL;
|
||||
// Disable I2C module
|
||||
I2C_RCC_ATOMIC() {
|
||||
i2c_ll_enable_bus_clock(port_num, false);
|
||||
@@ -156,9 +163,11 @@ esp_err_t i2c_release_bus_handle(i2c_bus_handle_t i2c_bus)
|
||||
if (do_deinitialize) {
|
||||
ESP_LOGD(TAG, "delete bus %d", port_num);
|
||||
}
|
||||
|
||||
ESP_RETURN_ON_FALSE(s_i2c_platform.count[port_num] == 0, ESP_ERR_INVALID_STATE, TAG, "Bus not freed entirely");
|
||||
return ESP_OK;
|
||||
|
||||
err:
|
||||
_lock_release(&s_i2c_platform.mutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
esp_err_t i2c_select_periph_clock(i2c_bus_handle_t handle, i2c_clock_source_t clk_src)
|
||||
|
||||
Reference in New Issue
Block a user