diff --git a/Devices/lilygo-tdisplay-s3/lilygo,tdisplay-s3.dts b/Devices/lilygo-tdisplay-s3/lilygo,tdisplay-s3.dts index 4713d519..27a7261f 100644 --- a/Devices/lilygo-tdisplay-s3/lilygo,tdisplay-s3.dts +++ b/Devices/lilygo-tdisplay-s3/lilygo,tdisplay-s3.dts @@ -17,7 +17,7 @@ spi0 { compatible = "espressif,esp32-spi"; host = ; - pin-mosi = <&gpio0 7 GPIO_FLAG_NONE>; - pin-sclk = <&gpio0 6 GPIO_FLAG_NONE>; + pin-mosi = <7>; + pin-sclk = <6>; }; }; diff --git a/Firmware/CMakeLists.txt b/Firmware/CMakeLists.txt index 82d9c788..6d10c571 100644 --- a/Firmware/CMakeLists.txt +++ b/Firmware/CMakeLists.txt @@ -52,8 +52,6 @@ add_custom_command( OUTPUT "${GENERATED_DIR}/devicetree.c" "${GENERATED_DIR}/devicetree.h" COMMAND pip install lark==1.3.1 pyyaml==6.0.3 - COMMAND echo python "${CMAKE_SOURCE_DIR}/Buildscripts/DevicetreeCompiler/compile.py" - "${DEVICETREE_LOCATION}" "${GENERATED_DIR}" COMMAND python "${CMAKE_SOURCE_DIR}/Buildscripts/DevicetreeCompiler/compile.py" "${DEVICETREE_LOCATION}" "${GENERATED_DIR}" WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" diff --git a/Platforms/PlatformEsp32/Source/drivers/esp32_i2c.cpp b/Platforms/PlatformEsp32/Source/drivers/esp32_i2c.cpp index 3dd567c7..d4cbe943 100644 --- a/Platforms/PlatformEsp32/Source/drivers/esp32_i2c.cpp +++ b/Platforms/PlatformEsp32/Source/drivers/esp32_i2c.cpp @@ -13,7 +13,7 @@ #define ACK_CHECK_EN 1 struct Esp32SpiInternal { - Mutex mutex { 0 }; + Mutex mutex {}; GpioDescriptor* sda_descriptor = nullptr; GpioDescriptor* scl_descriptor = nullptr; @@ -164,7 +164,7 @@ static error_t start(Device* device) { auto* scl_descriptor = gpio_descriptor_acquire(scl_spec.gpio_controller, scl_spec.pin, GPIO_OWNER_GPIO); if (!scl_descriptor) { - LOG_E(TAG, "Failed to acquire pin %u", sda_spec.pin); + LOG_E(TAG, "Failed to acquire pin %u", scl_spec.pin); gpio_descriptor_release(sda_descriptor); return ERROR_RESOURCE; } @@ -173,7 +173,7 @@ static error_t start(Device* device) { check(gpio_descriptor_get_native_pin_number(sda_descriptor, &sda_pin) == ERROR_NONE); check(gpio_descriptor_get_native_pin_number(scl_descriptor, &scl_pin) == ERROR_NONE); - gpio_flags_t sda_flags, scl_flags;; + gpio_flags_t sda_flags, scl_flags; check(gpio_descriptor_get_flags(sda_descriptor, &sda_flags) == ERROR_NONE); check(gpio_descriptor_get_flags(scl_descriptor, &scl_flags) == ERROR_NONE); @@ -192,11 +192,15 @@ static error_t start(Device* device) { esp_err_t error = i2c_param_config(dts_config->port, &esp_config); if (error != ESP_OK) { LOG_E(TAG, "Failed to configure 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); return ERROR_RESOURCE; } error = i2c_driver_install(dts_config->port, esp_config.mode, 0, 0, 0); if (error != ESP_OK) { + 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; } diff --git a/Platforms/PlatformEsp32/Source/drivers/esp32_spi.cpp b/Platforms/PlatformEsp32/Source/drivers/esp32_spi.cpp index 54266711..ac21fcb4 100644 --- a/Platforms/PlatformEsp32/Source/drivers/esp32_spi.cpp +++ b/Platforms/PlatformEsp32/Source/drivers/esp32_spi.cpp @@ -20,7 +20,7 @@ struct Esp32SpiInternal { RecursiveMutex mutex = {}; bool initialized = false; - Esp32SpiInternal(GpioDescriptor* pDescriptor, GpioDescriptor* pGpioDescriptor) { + explicit Esp32SpiInternal() { recursive_mutex_construct(&mutex); } @@ -51,7 +51,7 @@ static error_t unlock(Device* device) { static error_t start(Device* device) { ESP_LOGI(TAG, "start %s", device->name); - auto* data = new (std::nothrow) Esp32SpiInternal(nullptr, nullptr); + auto* data = new (std::nothrow) Esp32SpiInternal(); if (!data) return ERROR_OUT_OF_MEMORY; device_set_driver_data(device, data); diff --git a/TactilityKernel/Include/tactility/drivers/gpio_controller.h b/TactilityKernel/Include/tactility/drivers/gpio_controller.h index 1b3e5170..2251e2f0 100644 --- a/TactilityKernel/Include/tactility/drivers/gpio_controller.h +++ b/TactilityKernel/Include/tactility/drivers/gpio_controller.h @@ -32,7 +32,7 @@ struct GpioControllerApi { /** * @brief Configures the options for a GPIO pin. * @param[in,out] descriptor the pin descriptor - * @param[in] options configuration flags (direction, pull-up/down, etc.) + * @param[in] flags configuration flags (direction, pull-up/down, etc.) * @return ERROR_NONE if successful */ error_t (*set_flags)(struct GpioDescriptor* descriptor, gpio_flags_t flags); @@ -40,7 +40,7 @@ struct GpioControllerApi { /** * @brief Gets the configuration options for a GPIO pin. * @param[in] descriptor the pin descriptor - * @param[out] options pointer to store the configuration flags + * @param[out] flags pointer to store the configuration flags * @return ERROR_NONE if successful */ error_t (*get_flags)(struct GpioDescriptor* descriptor, gpio_flags_t* flags); diff --git a/TactilityKernel/Include/tactility/drivers/gpio_descriptor.h b/TactilityKernel/Include/tactility/drivers/gpio_descriptor.h index f7dfeb79..383961aa 100644 --- a/TactilityKernel/Include/tactility/drivers/gpio_descriptor.h +++ b/TactilityKernel/Include/tactility/drivers/gpio_descriptor.h @@ -2,6 +2,8 @@ #include "gpio.h" +struct Device; + struct GpioDescriptor { /** @brief The controller that owns this pin */ struct Device* controller; diff --git a/TactilityKernel/Source/drivers/gpio_controller.cpp b/TactilityKernel/Source/drivers/gpio_controller.cpp index 72572cd7..4b3f208e 100644 --- a/TactilityKernel/Source/drivers/gpio_controller.cpp +++ b/TactilityKernel/Source/drivers/gpio_controller.cpp @@ -16,23 +16,28 @@ extern "C" { struct GpioControllerData { struct Mutex mutex {}; uint32_t pin_count; - struct GpioDescriptor* descriptors; + struct GpioDescriptor* descriptors = nullptr; - explicit GpioControllerData(Device* device, uint32_t pin_count, void* controller_context) : pin_count(pin_count) { + explicit GpioControllerData(uint32_t pin_count) : pin_count(pin_count) { mutex_construct(&mutex); - descriptors = (struct GpioDescriptor*)calloc(pin_count, sizeof(struct GpioDescriptor)); - check(descriptors); + } + error_t init_descriptors(Device* device, void* controller_context) { + descriptors = (struct GpioDescriptor*)calloc(pin_count, sizeof(struct GpioDescriptor)); + if (!descriptors) return ERROR_OUT_OF_MEMORY; for (uint32_t i = 0; i < pin_count; ++i) { descriptors[i].controller = device; descriptors[i].pin = (gpio_pin_t)i; descriptors[i].owner_type = GPIO_OWNER_NONE; descriptors[i].controller_context = controller_context; } + return ERROR_NONE; } ~GpioControllerData() { - free(descriptors); + if (descriptors != nullptr) { + free(descriptors); + } mutex_destruct(&mutex); } }; @@ -42,6 +47,8 @@ struct GpioDescriptor* gpio_descriptor_acquire( gpio_pin_t pin_number, enum GpioOwnerType owner ) { + check(owner != GPIO_OWNER_NONE); + auto* data = (struct GpioControllerData*)device_get_driver_data(controller); mutex_lock(&data->mutex); @@ -74,8 +81,14 @@ error_t gpio_controller_get_pin_count(struct Device* device, uint32_t* count) { } error_t gpio_controller_init_descriptors(struct Device* device, uint32_t pin_count, void* controller_context) { - auto* data = new(std::nothrow) GpioControllerData(device, pin_count, controller_context); + auto* data = new(std::nothrow) GpioControllerData(pin_count); if (!data) return ERROR_OUT_OF_MEMORY; + + if (data->init_descriptors(device, controller_context) != ERROR_NONE) { + delete data; + return ERROR_OUT_OF_MEMORY; + } + device_set_driver_data(device, data); return ERROR_NONE; }