This commit is contained in:
Ken Van Hoeylandt 2026-02-10 21:18:56 +01:00
parent d178d674f4
commit 3a4acd1313

View File

@ -14,25 +14,25 @@
#define TAG "esp32_i2c" #define TAG "esp32_i2c"
#define ACK_CHECK_EN 1 #define ACK_CHECK_EN 1
struct Esp32SpiInternal { struct Esp32I2cInternal {
Mutex mutex {}; Mutex mutex {};
GpioDescriptor* sda_descriptor = nullptr; GpioDescriptor* sda_descriptor = nullptr;
GpioDescriptor* scl_descriptor = nullptr; GpioDescriptor* scl_descriptor = nullptr;
Esp32SpiInternal(GpioDescriptor* sda_descriptor, GpioDescriptor* scl_descriptor) : Esp32I2cInternal(GpioDescriptor* sda_descriptor, GpioDescriptor* scl_descriptor) :
sda_descriptor(sda_descriptor), sda_descriptor(sda_descriptor),
scl_descriptor(scl_descriptor) scl_descriptor(scl_descriptor)
{ {
mutex_construct(&mutex); mutex_construct(&mutex);
} }
~Esp32SpiInternal() { ~Esp32I2cInternal() {
mutex_destruct(&mutex); mutex_destruct(&mutex);
} }
}; };
#define GET_CONFIG(device) ((Esp32I2cConfig*)device->config) #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 lock(data) mutex_lock(&data->mutex);
#define unlock(data) mutex_unlock(&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); error = i2c_driver_install(dts_config->port, esp_config.mode, 0, 0, 0);
if (error != ESP_OK) { if (error != ESP_OK) {
LOG_E(TAG, "Failed to install driver at port %d: %s", static_cast<int>(dts_config->port), esp_err_to_name(error));
check(gpio_descriptor_release(sda_descriptor) == ERROR_NONE); check(gpio_descriptor_release(sda_descriptor) == ERROR_NONE);
check(gpio_descriptor_release(scl_descriptor) == ERROR_NONE); check(gpio_descriptor_release(scl_descriptor) == ERROR_NONE);
LOG_E(TAG, "Failed to install driver at port %d: %s", static_cast<int>(dts_config->port), esp_err_to_name(error));
return ERROR_RESOURCE; return ERROR_RESOURCE;
} }
auto* data = new(std::nothrow) Esp32SpiInternal(sda_descriptor, scl_descriptor); auto* data = new(std::nothrow) Esp32I2cInternal(sda_descriptor, scl_descriptor);
if (data == nullptr) return ERROR_OUT_OF_MEMORY; 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); device_set_driver_data(device, data);
return ERROR_NONE; return ERROR_NONE;
@ -216,7 +220,7 @@ static error_t start(Device* device) {
static error_t stop(Device* device) { static error_t stop(Device* device) {
ESP_LOGI(TAG, "stop %s", device->name); ESP_LOGI(TAG, "stop %s", device->name);
auto* driver_data = static_cast<Esp32SpiInternal*>(device_get_driver_data(device)); auto* driver_data = static_cast<Esp32I2cInternal*>(device_get_driver_data(device));
i2c_port_t port = GET_CONFIG(device)->port; i2c_port_t port = GET_CONFIG(device)->port;
esp_err_t result = i2c_driver_delete(port); esp_err_t result = i2c_driver_delete(port);