From 3a4acd1313f0ab5a53ce35eeb643596a3b1d86eb Mon Sep 17 00:00:00 2001 From: Ken Van Hoeylandt Date: Tue, 10 Feb 2026 21:18:56 +0100 Subject: [PATCH] Fixes --- .../Source/drivers/esp32_i2c.cpp | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/Platforms/PlatformEsp32/Source/drivers/esp32_i2c.cpp b/Platforms/PlatformEsp32/Source/drivers/esp32_i2c.cpp index c1ce347f..42330eea 100644 --- a/Platforms/PlatformEsp32/Source/drivers/esp32_i2c.cpp +++ b/Platforms/PlatformEsp32/Source/drivers/esp32_i2c.cpp @@ -14,25 +14,25 @@ #define TAG "esp32_i2c" #define ACK_CHECK_EN 1 -struct Esp32SpiInternal { +struct Esp32I2cInternal { Mutex mutex {}; GpioDescriptor* sda_descriptor = nullptr; GpioDescriptor* scl_descriptor = nullptr; - Esp32SpiInternal(GpioDescriptor* sda_descriptor, GpioDescriptor* scl_descriptor) : + Esp32I2cInternal(GpioDescriptor* sda_descriptor, GpioDescriptor* scl_descriptor) : sda_descriptor(sda_descriptor), scl_descriptor(scl_descriptor) { mutex_construct(&mutex); } - ~Esp32SpiInternal() { + ~Esp32I2cInternal() { mutex_destruct(&mutex); } }; #define GET_CONFIG(device) ((Esp32I2cConfig*)device->config) -#define GET_DATA(device) ((Esp32SpiInternal*)device_get_driver_data(device)) +#define GET_DATA(device) ((Esp32I2cInternal*)device_get_driver_data(device)) #define lock(data) mutex_lock(&data->mutex); #define unlock(data) mutex_unlock(&data->mutex); @@ -201,14 +201,18 @@ static error_t start(Device* device) { error = i2c_driver_install(dts_config->port, esp_config.mode, 0, 0, 0); if (error != ESP_OK) { + LOG_E(TAG, "Failed to install driver at port %d: %s", static_cast(dts_config->port), esp_err_to_name(error)); check(gpio_descriptor_release(sda_descriptor) == ERROR_NONE); check(gpio_descriptor_release(scl_descriptor) == ERROR_NONE); - LOG_E(TAG, "Failed to install driver at port %d: %s", static_cast(dts_config->port), esp_err_to_name(error)); return ERROR_RESOURCE; } - auto* data = new(std::nothrow) Esp32SpiInternal(sda_descriptor, scl_descriptor); - if (data == nullptr) return ERROR_OUT_OF_MEMORY; + auto* data = new(std::nothrow) Esp32I2cInternal(sda_descriptor, scl_descriptor); + if (data == nullptr) { + check(gpio_descriptor_release(sda_descriptor) == ERROR_NONE); + check(gpio_descriptor_release(scl_descriptor) == ERROR_NONE); + return ERROR_OUT_OF_MEMORY; + } device_set_driver_data(device, data); return ERROR_NONE; @@ -216,7 +220,7 @@ static error_t start(Device* device) { static error_t stop(Device* device) { ESP_LOGI(TAG, "stop %s", device->name); - auto* driver_data = static_cast(device_get_driver_data(device)); + auto* driver_data = static_cast(device_get_driver_data(device)); i2c_port_t port = GET_CONFIG(device)->port; esp_err_t result = i2c_driver_delete(port);