From c39b3d95a8c936aed5d5760d4465d7264a014bbb Mon Sep 17 00:00:00 2001 From: Ken Van Hoeylandt Date: Thu, 18 Jun 2026 18:39:43 +0200 Subject: [PATCH] Refactor I2C --- .../Source/devices/Display.cpp | 6 ++- .../cyd-2432s032c/Source/devices/Display.cpp | 6 ++- .../cyd-3248s035c/Source/devices/Display.cpp | 6 ++- .../Source/devices/St7701Display.cpp | 6 ++- .../cyd-8048s043c/Source/devices/Display.cpp | 6 ++- .../Source/devices/Display.cpp | 6 ++- .../Source/devices/Display.cpp | 6 ++- .../Source/devices/Display.cpp | 6 ++- .../Source/devices/Display.cpp | 9 +++-- .../Source/devices/Display.cpp | 6 ++- Devices/lilygo-tdeck/Source/Configuration.cpp | 6 ++- .../lilygo-tdeck/Source/devices/Display.cpp | 6 ++- .../Source/devices/TdeckKeyboard.cpp | 19 +++------- .../Source/devices/TdeckKeyboard.h | 7 ++++ .../Source/devices/Display.cpp | 6 ++- .../m5stack-tab5/Source/devices/Display.cpp | 6 ++- .../Source/devices/Display.cpp | 6 ++- Drivers/GT911/Source/Gt911Touch.cpp | 17 ++++----- Drivers/GT911/Source/Gt911Touch.h | 10 ++--- Tactility/Include/Tactility/hal/i2c/I2c.h | 15 -------- .../Include/Tactility/hal/i2c/I2cCompat.h | 38 ------------------- .../Include/Tactility/hal/i2c/I2cDevice.h | 1 - 22 files changed, 100 insertions(+), 100 deletions(-) delete mode 100644 Tactility/Include/Tactility/hal/i2c/I2c.h delete mode 100644 Tactility/Include/Tactility/hal/i2c/I2cCompat.h diff --git a/Devices/btt-panda-touch/Source/devices/Display.cpp b/Devices/btt-panda-touch/Source/devices/Display.cpp index 24607968..c534a556 100644 --- a/Devices/btt-panda-touch/Source/devices/Display.cpp +++ b/Devices/btt-panda-touch/Source/devices/Display.cpp @@ -3,11 +3,15 @@ #include #include #include +#include +#include std::shared_ptr createTouch() { // Note for future changes: Reset pin is 41 and interrupt pin is 40 + auto* i2c = device_find_by_name("i2c0"); + check(i2c); auto configuration = std::make_unique( - I2C_NUM_0, + i2c, 800, 480 ); diff --git a/Devices/cyd-2432s032c/Source/devices/Display.cpp b/Devices/cyd-2432s032c/Source/devices/Display.cpp index f771c1dd..d0bb5ddd 100644 --- a/Devices/cyd-2432s032c/Source/devices/Display.cpp +++ b/Devices/cyd-2432s032c/Source/devices/Display.cpp @@ -3,10 +3,14 @@ #include #include #include +#include +#include static std::shared_ptr createTouch() { + auto* i2c = device_find_by_name("i2c0"); + check(i2c); auto configuration = std::make_unique( - I2C_NUM_0, + i2c, LCD_HORIZONTAL_RESOLUTION, LCD_VERTICAL_RESOLUTION ); diff --git a/Devices/cyd-3248s035c/Source/devices/Display.cpp b/Devices/cyd-3248s035c/Source/devices/Display.cpp index f9615824..d67a8cf6 100644 --- a/Devices/cyd-3248s035c/Source/devices/Display.cpp +++ b/Devices/cyd-3248s035c/Source/devices/Display.cpp @@ -3,10 +3,14 @@ #include #include #include +#include +#include static std::shared_ptr createTouch() { + auto* i2c = device_find_by_name("i2c0"); + check(i2c); auto configuration = std::make_unique( - I2C_NUM_0, + i2c, LCD_HORIZONTAL_RESOLUTION, LCD_VERTICAL_RESOLUTION ); diff --git a/Devices/cyd-4848s040c/Source/devices/St7701Display.cpp b/Devices/cyd-4848s040c/Source/devices/St7701Display.cpp index e9c14889..154849a2 100644 --- a/Devices/cyd-4848s040c/Source/devices/St7701Display.cpp +++ b/Devices/cyd-4848s040c/Source/devices/St7701Display.cpp @@ -3,6 +3,8 @@ #include #include #include +#include +#include #include #include @@ -221,8 +223,10 @@ lvgl_port_display_rgb_cfg_t St7701Display::getLvglPortDisplayRgbConfig(esp_lcd_p std::shared_ptr St7701Display::getTouchDevice() { if (touchDevice == nullptr) { + auto* i2c = device_find_by_name("i2c0"); + check(i2c); auto configuration = std::make_unique( - I2C_NUM_0, + i2c, 480, 480 ); diff --git a/Devices/cyd-8048s043c/Source/devices/Display.cpp b/Devices/cyd-8048s043c/Source/devices/Display.cpp index 2d1e61e2..c2ecaae7 100644 --- a/Devices/cyd-8048s043c/Source/devices/Display.cpp +++ b/Devices/cyd-8048s043c/Source/devices/Display.cpp @@ -3,12 +3,16 @@ #include #include #include +#include +#include std::shared_ptr createTouch() { // Note for future changes: Reset pin is 38 and interrupt pin is 18 // or INT = NC, schematic and other info floating around is kinda conflicting... + auto* i2c = device_find_by_name("i2c_internal"); + check(i2c); auto configuration = std::make_unique( - I2C_NUM_0, + i2c, 800, 480 ); diff --git a/Devices/elecrow-crowpanel-advance-35/Source/devices/Display.cpp b/Devices/elecrow-crowpanel-advance-35/Source/devices/Display.cpp index 3771b46c..8c7dbc61 100644 --- a/Devices/elecrow-crowpanel-advance-35/Source/devices/Display.cpp +++ b/Devices/elecrow-crowpanel-advance-35/Source/devices/Display.cpp @@ -3,10 +3,14 @@ #include #include #include +#include +#include static std::shared_ptr createTouch() { + auto* i2c = device_find_by_name("i2c0"); + check(i2c); auto configuration = std::make_unique( - I2C_NUM_0, + i2c, 320, 480 ); diff --git a/Devices/elecrow-crowpanel-advance-50/Source/devices/Display.cpp b/Devices/elecrow-crowpanel-advance-50/Source/devices/Display.cpp index 3476d82d..26f9bdc7 100644 --- a/Devices/elecrow-crowpanel-advance-50/Source/devices/Display.cpp +++ b/Devices/elecrow-crowpanel-advance-50/Source/devices/Display.cpp @@ -2,12 +2,16 @@ #include #include +#include +#include std::shared_ptr createTouch() { // Note for future changes: Reset pin is 38 and interrupt pin is 18 // or INT = NC, schematic and other info floating around is kinda conflicting... + auto* i2c = device_find_by_name("i2c0"); + check(i2c); auto configuration = std::make_unique( - I2C_NUM_0, + i2c, 800, 480 ); diff --git a/Devices/elecrow-crowpanel-basic-50/Source/devices/Display.cpp b/Devices/elecrow-crowpanel-basic-50/Source/devices/Display.cpp index 6a9f70e7..8149bfa4 100644 --- a/Devices/elecrow-crowpanel-basic-50/Source/devices/Display.cpp +++ b/Devices/elecrow-crowpanel-basic-50/Source/devices/Display.cpp @@ -3,12 +3,16 @@ #include #include #include +#include +#include std::shared_ptr createTouch() { // Note for future changes: Reset pin is 38 and interrupt pin is 18 // or INT = NC, schematic and other info floating around is kinda conflicting... + auto* i2c = device_find_by_name("i2c0"); + check(i2c); auto configuration = std::make_unique( - I2C_NUM_0, + i2c, 800, 480 ); diff --git a/Devices/guition-jc1060p470ciwy/Source/devices/Display.cpp b/Devices/guition-jc1060p470ciwy/Source/devices/Display.cpp index 151b05f4..08724d46 100644 --- a/Devices/guition-jc1060p470ciwy/Source/devices/Display.cpp +++ b/Devices/guition-jc1060p470ciwy/Source/devices/Display.cpp @@ -5,21 +5,22 @@ #include #include #include +#include +#include constexpr auto LCD_PIN_RESET = GPIO_NUM_0; // Match P4 EV board reset line constexpr auto LCD_PIN_BACKLIGHT = GPIO_NUM_23; constexpr auto LCD_HORIZONTAL_RESOLUTION = 1024; constexpr auto LCD_VERTICAL_RESOLUTION = 600; -constexpr auto TOUCH_I2C_PORT = I2C_NUM_0; -constexpr auto TOUCH_I2C_SDA = GPIO_NUM_7; -constexpr auto TOUCH_I2C_SCL = GPIO_NUM_8; constexpr auto TOUCH_PIN_RESET = GPIO_NUM_NC; constexpr auto TOUCH_PIN_INTERRUPT = GPIO_NUM_NC; static std::shared_ptr createTouch() { + auto* i2c = device_find_by_name("i2c_internal"); + check(i2c); auto configuration = std::make_unique( - TOUCH_I2C_PORT, + i2c, LCD_HORIZONTAL_RESOLUTION, LCD_VERTICAL_RESOLUTION, false, // swapXY diff --git a/Devices/guition-jc8048w550c/Source/devices/Display.cpp b/Devices/guition-jc8048w550c/Source/devices/Display.cpp index 2d1e61e2..06ddbc34 100644 --- a/Devices/guition-jc8048w550c/Source/devices/Display.cpp +++ b/Devices/guition-jc8048w550c/Source/devices/Display.cpp @@ -3,12 +3,16 @@ #include #include #include +#include +#include std::shared_ptr createTouch() { // Note for future changes: Reset pin is 38 and interrupt pin is 18 // or INT = NC, schematic and other info floating around is kinda conflicting... + auto* i2c = device_find_by_name("i2c0"); + check(i2c); auto configuration = std::make_unique( - I2C_NUM_0, + i2c, 800, 480 ); diff --git a/Devices/lilygo-tdeck/Source/Configuration.cpp b/Devices/lilygo-tdeck/Source/Configuration.cpp index 615b7b7a..e0c56eaa 100644 --- a/Devices/lilygo-tdeck/Source/Configuration.cpp +++ b/Devices/lilygo-tdeck/Source/Configuration.cpp @@ -7,16 +7,20 @@ #include #include +#include +#include bool initBoot(); using namespace tt::hal; static std::vector> createDevices() { + auto* i2c_internal = device_find_by_name("i2c_internal"); + check(i2c_internal); return { createPower(), createDisplay(), - std::make_shared(), + std::make_shared(i2c_internal), std::make_shared(), std::make_shared(), createSdCard() diff --git a/Devices/lilygo-tdeck/Source/devices/Display.cpp b/Devices/lilygo-tdeck/Source/devices/Display.cpp index 92a679b1..f25e1eb2 100644 --- a/Devices/lilygo-tdeck/Source/devices/Display.cpp +++ b/Devices/lilygo-tdeck/Source/devices/Display.cpp @@ -3,10 +3,14 @@ #include #include #include +#include +#include static std::shared_ptr createTouch() { + auto* i2c = device_find_by_name("i2c0"); + check(i2c); auto configuration = std::make_unique( - I2C_NUM_0, + i2c, 240, 320, true, diff --git a/Devices/lilygo-tdeck/Source/devices/TdeckKeyboard.cpp b/Devices/lilygo-tdeck/Source/devices/TdeckKeyboard.cpp index a3ecec77..ec32b025 100644 --- a/Devices/lilygo-tdeck/Source/devices/TdeckKeyboard.cpp +++ b/Devices/lilygo-tdeck/Source/devices/TdeckKeyboard.cpp @@ -1,27 +1,18 @@ #include "TdeckKeyboard.h" -#include "../../../../TactilityKernel/include/tactility/drivers/i2c_controller.h" - #include #include #include -#include #include #include -#include #include -#include +#include using tt::hal::findFirstDevice; static const auto LOGGER = tt::Logger("TdeckKeyboard"); -constexpr auto TDECK_KEYBOARD_I2C_BUS_HANDLE = I2C_NUM_0; -constexpr auto TDECK_KEYBOARD_SLAVE_ADDRESS = 0x55; - -static bool keyboard_i2c_read(uint8_t* output) { - return tt::hal::i2c::masterRead(TDECK_KEYBOARD_I2C_BUS_HANDLE, TDECK_KEYBOARD_SLAVE_ADDRESS, output, 1, 100 / portTICK_PERIOD_MS); -} +constexpr uint8_t TDECK_KEYBOARD_SLAVE_ADDRESS = 0x55; /** * The callback simulates press and release events, because the T-Deck @@ -40,7 +31,8 @@ static void keyboard_read_callback(lv_indev_t* indev, lv_indev_data_t* data) { data->key = 0; data->state = LV_INDEV_STATE_RELEASED; - if (keyboard_i2c_read(&read_buffer)) { + auto* keyboard = static_cast(lv_indev_get_user_data(indev)); + if (i2c_controller_read(keyboard->getI2cController(), TDECK_KEYBOARD_SLAVE_ADDRESS, &read_buffer, 1, 100 / portTICK_PERIOD_MS) == ERROR_NONE) { if (read_buffer == 0 && read_buffer != last_buffer) { if (LOGGER.isLoggingDebug()) { LOGGER.debug("Released {}", last_buffer); @@ -94,6 +86,5 @@ bool TdeckKeyboard::stopLvgl() { } bool TdeckKeyboard::isAttached() const { - auto controller = device_find_by_name("i2c_internal"); - return i2c_controller_has_device_at_address(controller, TDECK_KEYBOARD_SLAVE_ADDRESS, 100) == ERROR_NONE; + return i2c_controller_has_device_at_address(i2cController, TDECK_KEYBOARD_SLAVE_ADDRESS, 100) == ERROR_NONE; } diff --git a/Devices/lilygo-tdeck/Source/devices/TdeckKeyboard.h b/Devices/lilygo-tdeck/Source/devices/TdeckKeyboard.h index 7204f1a0..e9599498 100644 --- a/Devices/lilygo-tdeck/Source/devices/TdeckKeyboard.h +++ b/Devices/lilygo-tdeck/Source/devices/TdeckKeyboard.h @@ -3,15 +3,22 @@ #include #include +struct Device; + class TdeckKeyboard final : public tt::hal::keyboard::KeyboardDevice { + Device* i2cController; lv_indev_t* deviceHandle = nullptr; public: + explicit TdeckKeyboard(Device* i2cController) : i2cController(i2cController) {} + std::string getName() const override { return "T-Deck Keyboard"; } std::string getDescription() const override { return "I2C keyboard"; } + Device* getI2cController() const { return i2cController; } + bool startLvgl(lv_display_t* display) override; bool stopLvgl() override; bool isAttached() const override; diff --git a/Devices/m5stack-papers3/Source/devices/Display.cpp b/Devices/m5stack-papers3/Source/devices/Display.cpp index 42cc2f7f..acfd5f59 100644 --- a/Devices/m5stack-papers3/Source/devices/Display.cpp +++ b/Devices/m5stack-papers3/Source/devices/Display.cpp @@ -1,10 +1,14 @@ #include "Display.h" #include #include +#include +#include std::shared_ptr createTouch() { + auto* i2c = device_find_by_name("i2c_internal"); + check(i2c); auto configuration = std::make_unique( - I2C_NUM_0, + i2c, 540, 960, true, // swapXy diff --git a/Devices/m5stack-tab5/Source/devices/Display.cpp b/Devices/m5stack-tab5/Source/devices/Display.cpp index b26fe70e..c56b6307 100644 --- a/Devices/m5stack-tab5/Source/devices/Display.cpp +++ b/Devices/m5stack-tab5/Source/devices/Display.cpp @@ -8,6 +8,8 @@ #include #include #include +#include +#include #include #include @@ -19,8 +21,10 @@ constexpr auto LCD_PIN_RESET = GPIO_NUM_NC; constexpr auto LCD_PIN_BACKLIGHT = GPIO_NUM_22; static std::shared_ptr createGt911Touch() { + auto* i2c = device_find_by_name("i2c0"); + check(i2c); auto configuration = std::make_unique( - I2C_NUM_0, + i2c, 720, 1280, false, // swapXY diff --git a/Devices/waveshare-s3-touch-lcd-43/Source/devices/Display.cpp b/Devices/waveshare-s3-touch-lcd-43/Source/devices/Display.cpp index 49aba831..30090af1 100644 --- a/Devices/waveshare-s3-touch-lcd-43/Source/devices/Display.cpp +++ b/Devices/waveshare-s3-touch-lcd-43/Source/devices/Display.cpp @@ -2,12 +2,16 @@ #include #include +#include +#include std::shared_ptr createTouch() { // Note for future changes: Reset pin is 38 and interrupt pin is 18 // or INT = NC, schematic and other info floating around is kinda conflicting... + auto* i2c = device_find_by_name("i2c0"); + check(i2c); auto configuration = std::make_unique( - I2C_NUM_0, + i2c, 800, 480 ); diff --git a/Drivers/GT911/Source/Gt911Touch.cpp b/Drivers/GT911/Source/Gt911Touch.cpp index 06fbf660..6aafe5e2 100644 --- a/Drivers/GT911/Source/Gt911Touch.cpp +++ b/Drivers/GT911/Source/Gt911Touch.cpp @@ -1,31 +1,30 @@ #include "Gt911Touch.h" #include -#include #include #include +#include +#include static const auto LOGGER = tt::Logger("GT911"); bool Gt911Touch::createIoHandle(esp_lcd_panel_io_handle_t& outHandle) { esp_lcd_panel_io_i2c_config_t io_config = ESP_LCD_TOUCH_IO_I2C_GT911_CONFIG(); - /** - * When the interrupt pin is low, the address is 0x5D. Otherwise it is 0x14. - * There is not reset pin, and the current driver fails when you only specify the interrupt pin. - * Because of that, we don't use the interrupt pin and we'll simply scan the bus instead: - */ - if (tt::hal::i2c::masterHasDeviceAtAddress(configuration->port, ESP_LCD_TOUCH_IO_I2C_GT911_ADDRESS)) { + auto* i2c = configuration->i2cController; + + if (i2c_controller_has_device_at_address(i2c, ESP_LCD_TOUCH_IO_I2C_GT911_ADDRESS, pdMS_TO_TICKS(10)) == ERROR_NONE) { io_config.dev_addr = ESP_LCD_TOUCH_IO_I2C_GT911_ADDRESS; - } else if (tt::hal::i2c::masterHasDeviceAtAddress(configuration->port, ESP_LCD_TOUCH_IO_I2C_GT911_ADDRESS_BACKUP)) { + } else if (i2c_controller_has_device_at_address(i2c, ESP_LCD_TOUCH_IO_I2C_GT911_ADDRESS_BACKUP, pdMS_TO_TICKS(10)) == ERROR_NONE) { io_config.dev_addr = ESP_LCD_TOUCH_IO_I2C_GT911_ADDRESS_BACKUP; } else { LOGGER.error("No device found on I2C bus"); return false; } - return esp_lcd_new_panel_io_i2c(configuration->port, &io_config, &outHandle) == ESP_OK; + auto port = static_cast(i2c->config)->port; + return esp_lcd_new_panel_io_i2c(port, &io_config, &outHandle) == ESP_OK; } bool Gt911Touch::createTouchHandle(esp_lcd_panel_io_handle_t ioHandle, const esp_lcd_touch_config_t& configuration, esp_lcd_touch_handle_t& panelHandle) { diff --git a/Drivers/GT911/Source/Gt911Touch.h b/Drivers/GT911/Source/Gt911Touch.h index f737b390..fcb8da4e 100644 --- a/Drivers/GT911/Source/Gt911Touch.h +++ b/Drivers/GT911/Source/Gt911Touch.h @@ -2,11 +2,11 @@ #include #include -#include -#include #include +struct Device; + class Gt911Touch final : public EspLcdTouch { public: @@ -15,7 +15,7 @@ public: public: Configuration( - i2c_port_t port, + ::Device* i2cController, uint16_t xMax, uint16_t yMax, bool swapXy = false, @@ -25,7 +25,7 @@ public: gpio_num_t pinInterrupt = GPIO_NUM_NC, unsigned int pinResetLevel = 0, unsigned int pinInterruptLevel = 0 - ) : port(port), + ) : i2cController(i2cController), xMax(xMax), yMax(yMax), swapXy(swapXy), @@ -37,7 +37,7 @@ public: pinInterruptLevel(pinInterruptLevel) {} - i2c_port_t port; + ::Device* i2cController; uint16_t xMax; uint16_t yMax; bool swapXy; diff --git a/Tactility/Include/Tactility/hal/i2c/I2c.h b/Tactility/Include/Tactility/hal/i2c/I2c.h deleted file mode 100644 index 4b1c0394..00000000 --- a/Tactility/Include/Tactility/hal/i2c/I2c.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#include "./I2cCompat.h" -#include "Tactility/Lock.h" - -#include - -namespace tt::hal::i2c { - -constexpr TickType_t defaultTimeout = 10 / portTICK_PERIOD_MS; - -/** @return true when a device is detected at the specified address */ -bool masterHasDeviceAtAddress(i2c_port_t port, uint8_t address, TickType_t timeout = defaultTimeout); - -} // namespace diff --git a/Tactility/Include/Tactility/hal/i2c/I2cCompat.h b/Tactility/Include/Tactility/hal/i2c/I2cCompat.h deleted file mode 100644 index d4cd78b8..00000000 --- a/Tactility/Include/Tactility/hal/i2c/I2cCompat.h +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once - -#ifdef ESP_PLATFORM - -#include -#include - -#else - -#include - -enum i2c_port_t { - I2C_NUM_0 = 0, - I2C_NUM_1, - LP_I2C_NUM_0, - I2C_NUM_MAX, -}; - -enum i2c_mode_t { - I2C_MODE_MASTER, - I2C_MODE_MAX, -}; - -struct i2c_config_t { - i2c_mode_t mode; - int sda_io_num; - int scl_io_num; - bool sda_pullup_en; - bool scl_pullup_en; - union { - struct { - uint32_t clk_speed; - } master; - }; - uint32_t clk_flags; -}; - -#endif diff --git a/Tactility/Include/Tactility/hal/i2c/I2cDevice.h b/Tactility/Include/Tactility/hal/i2c/I2cDevice.h index 67b85195..97ad0249 100644 --- a/Tactility/Include/Tactility/hal/i2c/I2cDevice.h +++ b/Tactility/Include/Tactility/hal/i2c/I2cDevice.h @@ -2,7 +2,6 @@ #include #include -#include "I2c.h" namespace tt::hal::i2c {