From 1de78a6dc627399789078f702c8622dfaabdacbe Mon Sep 17 00:00:00 2001 From: Ken Van Hoeylandt Date: Tue, 2 Sep 2025 23:32:17 +0200 Subject: [PATCH] Board refactor and driver fix --- Boards/CYD-2432S024C/Source/CYD2432S024C.cpp | 1 - Boards/CYD-2432S028R/Source/CYD2432S028R.cpp | 29 ++++++++++++------- .../YellowDisplay.cpp => devices/Display.cpp} | 16 ++++------ .../YellowConstants.h => devices/Display.h} | 15 ++-------- .../CYD-2432S028R/Source/devices/SdCard.cpp | 21 ++++++++++++++ .../{hal/YellowSdCard.h => devices/SdCard.h} | 2 +- .../CYD-2432S028R/Source/hal/YellowDisplay.h | 6 ---- .../CYD-2432S028R/Source/hal/YellowSdCard.cpp | 26 ----------------- 8 files changed, 49 insertions(+), 67 deletions(-) rename Boards/CYD-2432S028R/Source/{hal/YellowDisplay.cpp => devices/Display.cpp} (76%) rename Boards/CYD-2432S028R/Source/{hal/YellowConstants.h => devices/Display.h} (63%) create mode 100644 Boards/CYD-2432S028R/Source/devices/SdCard.cpp rename Boards/CYD-2432S028R/Source/{hal/YellowSdCard.h => devices/SdCard.h} (66%) delete mode 100644 Boards/CYD-2432S028R/Source/hal/YellowDisplay.h delete mode 100644 Boards/CYD-2432S028R/Source/hal/YellowSdCard.cpp diff --git a/Boards/CYD-2432S024C/Source/CYD2432S024C.cpp b/Boards/CYD-2432S024C/Source/CYD2432S024C.cpp index 69ff9831..d4daa65b 100644 --- a/Boards/CYD-2432S024C/Source/CYD2432S024C.cpp +++ b/Boards/CYD-2432S024C/Source/CYD2432S024C.cpp @@ -18,7 +18,6 @@ static tt::hal::DeviceVector createDevices() { }; } - const tt::hal::Configuration cyd_2432s024c_config = { .initBoot = initBoot, .createDevices = createDevices, diff --git a/Boards/CYD-2432S028R/Source/CYD2432S028R.cpp b/Boards/CYD-2432S028R/Source/CYD2432S028R.cpp index e1a461b1..974af813 100644 --- a/Boards/CYD-2432S028R/Source/CYD2432S028R.cpp +++ b/Boards/CYD-2432S028R/Source/CYD2432S028R.cpp @@ -1,14 +1,18 @@ #include "CYD2432S028R.h" -#include "hal/YellowDisplay.h" -#include "hal/YellowConstants.h" -#include "hal/YellowSdCard.h" +#include "devices/Display.h" +#include "devices/SdCard.h" #include #include #include +// SPI Transfer +#define CYD_SPI_TRANSFER_SIZE_LIMIT (CYD2432S028R_LCD_DRAW_BUFFER_SIZE * LV_COLOR_DEPTH / 8) +// Display backlight (PWM) +#define CYD2432S028R_LCD_PIN_BACKLIGHT GPIO_NUM_21 + using namespace tt::hal; -bool initBoot() { +static bool initBoot() { //Set the RGB Led Pins to output and turn them off ESP_ERROR_CHECK(gpio_set_direction(GPIO_NUM_4, GPIO_MODE_OUTPUT)); //Red ESP_ERROR_CHECK(gpio_set_direction(GPIO_NUM_16, GPIO_MODE_OUTPUT)); //Green @@ -22,16 +26,21 @@ bool initBoot() { return driver::pwmbacklight::init(CYD2432S028R_LCD_PIN_BACKLIGHT); } +static DeviceVector createDevices() { + return { + createDisplay(), + createSdCard() + }; +} + const Configuration cyd_2432s028r_config = { .initBoot = initBoot, - .createDisplay = createDisplay, - .sdcard = createYellowSdCard(), - .power = nullptr, + .createDevices = createDevices, .i2c = {}, .spi { //Display spi::Configuration { - .device = CYD2432S028R_LCD_SPI_HOST, + .device = SPI2_HOST, .dma = SPI_DMA_CH_AUTO, .config = { .mosi_io_num = GPIO_NUM_13, @@ -56,7 +65,7 @@ const Configuration cyd_2432s028r_config = { // SDCard spi::Configuration { - .device = CYD2432S028R_SDCARD_SPI_HOST, + .device = SPI3_HOST, .dma = SPI_DMA_CH_AUTO, .config = { .mosi_io_num = GPIO_NUM_23, @@ -74,7 +83,7 @@ const Configuration cyd_2432s028r_config = { .isr_cpu_id = ESP_INTR_CPU_AFFINITY_AUTO, .intr_flags = 0 }, - .initMode = tt::hal::spi::InitMode::ByTactility, + .initMode = spi::InitMode::ByTactility, .isMutable = false, .lock = tt::lvgl::getSyncLock() // esp_lvgl_port owns the lock for the display }, diff --git a/Boards/CYD-2432S028R/Source/hal/YellowDisplay.cpp b/Boards/CYD-2432S028R/Source/devices/Display.cpp similarity index 76% rename from Boards/CYD-2432S028R/Source/hal/YellowDisplay.cpp rename to Boards/CYD-2432S028R/Source/devices/Display.cpp index 82534fa5..dea602b6 100644 --- a/Boards/CYD-2432S028R/Source/hal/YellowDisplay.cpp +++ b/Boards/CYD-2432S028R/Source/devices/Display.cpp @@ -1,13 +1,9 @@ -#include "YellowDisplay.h" +#include "Display.h" #include "Xpt2046SoftSpi.h" -#include "YellowConstants.h" #include #include -static const char* TAG = "YellowDisplay"; - -// Global to hold reference (only needed if calling stop() later) -static std::unique_ptr touch; +constexpr auto* TAG = "CYD"; static std::shared_ptr createTouch() { auto configuration = std::make_unique( @@ -23,17 +19,15 @@ static std::shared_ptr createTouch() { ); // Allocate the driver - touch = std::make_unique(std::move(configuration)); + auto touch = std::make_shared(std::move(configuration)); // Start the driver if (!touch->start()) { ESP_LOGE(TAG, "Touch driver start failed"); return nullptr; } - - return std::shared_ptr(touch.get(), [](tt::hal::touch::TouchDevice*) { - // No delete needed; `touch` is managed above - }); + + return touch; } std::shared_ptr createDisplay() { diff --git a/Boards/CYD-2432S028R/Source/hal/YellowConstants.h b/Boards/CYD-2432S028R/Source/devices/Display.h similarity index 63% rename from Boards/CYD-2432S028R/Source/hal/YellowConstants.h rename to Boards/CYD-2432S028R/Source/devices/Display.h index 96aa39d1..1fbc536a 100644 --- a/Boards/CYD-2432S028R/Source/hal/YellowConstants.h +++ b/Boards/CYD-2432S028R/Source/devices/Display.h @@ -1,7 +1,7 @@ #pragma once -// Display backlight (PWM) -#define CYD2432S028R_LCD_PIN_BACKLIGHT GPIO_NUM_21 +#include "Tactility/hal/display/DisplayDevice.h" +#include // Display #define CYD2432S028R_LCD_SPI_HOST SPI2_HOST @@ -12,10 +12,6 @@ #define CYD2432S028R_LCD_PIN_CS GPIO_NUM_15 #define CYD2432S028R_LCD_PIN_DC GPIO_NUM_2 -// Touch -#define CYD2432S028R_TOUCH_SPI_HOST SPI3_HOST -#define CYD2432S028R_TOUCH_PIN_CS GPIO_NUM_33 - // Touch (Software SPI) #define CYD_TOUCH_MISO_PIN GPIO_NUM_39 #define CYD_TOUCH_MOSI_PIN GPIO_NUM_32 @@ -23,9 +19,4 @@ #define CYD_TOUCH_CS_PIN GPIO_NUM_33 #define CYD_TOUCH_IRQ_PIN GPIO_NUM_36 -// SDCard -#define CYD2432S028R_SDCARD_SPI_HOST SPI3_HOST -#define CYD2432S028R_SDCARD_PIN_CS GPIO_NUM_5 - -// SPI Transfer -#define CYD_SPI_TRANSFER_SIZE_LIMIT (CYD2432S028R_LCD_DRAW_BUFFER_SIZE * LV_COLOR_DEPTH / 8) +std::shared_ptr createDisplay(); diff --git a/Boards/CYD-2432S028R/Source/devices/SdCard.cpp b/Boards/CYD-2432S028R/Source/devices/SdCard.cpp new file mode 100644 index 00000000..0a4cdba0 --- /dev/null +++ b/Boards/CYD-2432S028R/Source/devices/SdCard.cpp @@ -0,0 +1,21 @@ +#include "SdCard.h" +#include +#include + +using tt::hal::sdcard::SpiSdCardDevice; + +std::shared_ptr createSdCard() { + auto config = std::make_unique( + GPIO_NUM_5, + GPIO_NUM_NC, + GPIO_NUM_NC, + GPIO_NUM_NC, + SdCardDevice::MountBehaviour::AtBoot, + std::make_shared(), + std::vector(), + SPI3_HOST + ); + + return std::make_shared(std::move(config)); +} + diff --git a/Boards/CYD-2432S028R/Source/hal/YellowSdCard.h b/Boards/CYD-2432S028R/Source/devices/SdCard.h similarity index 66% rename from Boards/CYD-2432S028R/Source/hal/YellowSdCard.h rename to Boards/CYD-2432S028R/Source/devices/SdCard.h index 772f7119..4da9f5b9 100644 --- a/Boards/CYD-2432S028R/Source/hal/YellowSdCard.h +++ b/Boards/CYD-2432S028R/Source/devices/SdCard.h @@ -4,5 +4,5 @@ using tt::hal::sdcard::SdCardDevice; -std::shared_ptr createYellowSdCard(); +std::shared_ptr createSdCard(); diff --git a/Boards/CYD-2432S028R/Source/hal/YellowDisplay.h b/Boards/CYD-2432S028R/Source/hal/YellowDisplay.h deleted file mode 100644 index 036603c9..00000000 --- a/Boards/CYD-2432S028R/Source/hal/YellowDisplay.h +++ /dev/null @@ -1,6 +0,0 @@ -#pragma once - -#include "Tactility/hal/display/DisplayDevice.h" -#include - -std::shared_ptr createDisplay(); diff --git a/Boards/CYD-2432S028R/Source/hal/YellowSdCard.cpp b/Boards/CYD-2432S028R/Source/hal/YellowSdCard.cpp deleted file mode 100644 index 49c81c3a..00000000 --- a/Boards/CYD-2432S028R/Source/hal/YellowSdCard.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include "YellowSdCard.h" -#include "YellowConstants.h" -#include -#include - -using tt::hal::sdcard::SpiSdCardDevice; - -std::shared_ptr createYellowSdCard() { - auto* configuration = new SpiSdCardDevice::Config( - CYD2432S028R_SDCARD_PIN_CS, - GPIO_NUM_NC, - GPIO_NUM_NC, - GPIO_NUM_NC, - SdCardDevice::MountBehaviour::AtBoot, - std::make_shared(), - std::vector(), - CYD2432S028R_SDCARD_SPI_HOST - ); - - auto* sdcard = (SdCardDevice*) new SpiSdCardDevice( - std::unique_ptr(configuration) - ); - - return std::shared_ptr(sdcard); -} -