From 799ba675eb523afc2c2b3314e5c04bfe180869e6 Mon Sep 17 00:00:00 2001 From: gemu Date: Sun, 23 Apr 2023 10:19:31 +0200 Subject: [PATCH] add xpt2046 option for spi bus 2 (#18480) * allow spi bus 2 * xpt spi bus 2 * xpt option bus * xpt option spi bus 2 * exclude c3 * exclude s2 * fix s3 * typo --- .../XPT2046_Touchscreen.cpp | 25 ++++++++++++++-- .../XPT2046_Touchscreen/XPT2046_Touchscreen.h | 6 ++-- tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino | 29 +++++++++++++++++-- .../tasmota_xdsp_display/xdsp_04_ili9341.ino | 2 +- .../xdsp_17_universal.ino | 14 +++++++-- 5 files changed, 66 insertions(+), 10 deletions(-) diff --git a/lib/lib_display/XPT2046_Touchscreen/XPT2046_Touchscreen.cpp b/lib/lib_display/XPT2046_Touchscreen/XPT2046_Touchscreen.cpp index 3b9bc4077..3972f0a49 100755 --- a/lib/lib_display/XPT2046_Touchscreen/XPT2046_Touchscreen.cpp +++ b/lib/lib_display/XPT2046_Touchscreen/XPT2046_Touchscreen.cpp @@ -30,10 +30,31 @@ static XPT2046_Touchscreen *isrPinptr; void isrPin(void); -bool XPT2046_Touchscreen::begin(SPIClass &wspi) -{ +bool XPT2046_Touchscreen::begin(SPIClass &wspi) { + +#ifdef ESP32 + if (!bus) { + _pspi = &wspi; + } else { +#ifndef CONFIG_IDF_TARGET_ESP32C3 +#ifndef CONFIG_IDF_TARGET_ESP32S2 + if (bus == 1) { + _pspi = new SPIClass(HSPI); + } else { +#ifndef CONFIG_IDF_TARGET_ESP32S3 + _pspi = new SPIClass(VSPI); +#endif + } +#endif +#endif + } + //Serial.printf("sclk=%d :: miso=%d, mosi=%d, irq=%d, bus=%d ", sclk, miso, mosi, tirqPin, bus); + _pspi->begin(sclk, miso, mosi, -1); +#else _pspi = &wspi; _pspi->begin(); +#endif + pinMode(csPin, OUTPUT); digitalWrite(csPin, HIGH); if (255 != tirqPin) { diff --git a/lib/lib_display/XPT2046_Touchscreen/XPT2046_Touchscreen.h b/lib/lib_display/XPT2046_Touchscreen/XPT2046_Touchscreen.h index 279bebe8e..30e5066dd 100755 --- a/lib/lib_display/XPT2046_Touchscreen/XPT2046_Touchscreen.h +++ b/lib/lib_display/XPT2046_Touchscreen/XPT2046_Touchscreen.h @@ -47,8 +47,8 @@ public: class XPT2046_Touchscreen { public: - constexpr XPT2046_Touchscreen(uint8_t cspin, uint8_t tirq=255) - : csPin(cspin), tirqPin(tirq) { } + constexpr XPT2046_Touchscreen(uint8_t cspin, uint8_t tirq=255, uint8_t bus=0, uint8_t sclk=0, uint8_t miso=0, uint8_t mosi=0) + : csPin(cspin), tirqPin(tirq), bus(bus), sclk(sclk), miso(miso), mosi(mosi) { } bool begin(SPIClass &wspi = SPI); #if defined(_FLEXIO_SPI_H_) bool begin(FlexIOSPI &wflexspi); @@ -66,7 +66,7 @@ public: private: void update(); - uint8_t csPin, tirqPin, rotation=1; + uint8_t csPin, tirqPin, rotation=1, bus=0, sclk=0, miso=0, mosi=0; int16_t xraw=0, yraw=0, zraw=0; uint32_t msraw=0x80000000; SPIClass *_pspi = nullptr; diff --git a/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino b/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino index 69843572b..01bbe97ca 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino @@ -228,6 +228,8 @@ bool GT911_Touch_Init(TwoWire *i2c, int8_t irq_pin, int8_t rst_pin, uint16_t xs, if (ESP_OK == GT911_touchp->begin(i2c, irq_pin, rst_pin, xs, ys)) { AddLog(LOG_LEVEL_INFO, PSTR("TI: GT911")); GT911_found = true; + } else { + AddLog(LOG_LEVEL_INFO, PSTR("TI: GT911 failed")); } return GT911_found; } @@ -247,8 +249,31 @@ void GT911_CheckTouch(void) { #include XPT2046_Touchscreen *XPT2046_touchp; -bool XPT2046_Touch_Init(uint16_t CS) { - XPT2046_touchp = new XPT2046_Touchscreen(CS); +bool XPT2046_Touch_Init(uint16_t CS, int8_t irqpin, uint8_t bus) { + int8_t sclk = -1; + int8_t mosi = -1; + int8_t miso = -1; + uint8_t xbus = bus; + bus &= 1; + #ifdef ESP32 + if (PinUsed(GPIO_SPI_CLK, bus) && PinUsed(GPIO_SPI_MISO, bus) && PinUsed(GPIO_SPI_MOSI, bus)) { + // must init SPI with pins + sclk = Pin(GPIO_SPI_CLK, bus); + miso = Pin(GPIO_SPI_MISO, bus); + mosi = Pin(GPIO_SPI_MOSI, bus); + } + #endif // ESP32 + + #ifdef ESP8266 + if (PinUsed(GPIO_SPI_CLK) && PinUsed(GPIO_SPI_MISO) && PinUsed(GPIO_SPI_MOSI)) { + // must init SPI with pins + sclk = Pin(GPIO_SPI_CLK); + miso = Pin(GPIO_SPI_MISO); + mosi = Pin(GPIO_SPI_MOSI); + } + #endif // ESP8266 + + XPT2046_touchp = new XPT2046_Touchscreen(CS, irqpin, xbus, sclk, miso, mosi); XPT2046_found = XPT2046_touchp->begin(); if (XPT2046_found) { AddLog(LOG_LEVEL_INFO, PSTR("TS: XPT2046")); diff --git a/tasmota/tasmota_xdsp_display/xdsp_04_ili9341.ino b/tasmota/tasmota_xdsp_display/xdsp_04_ili9341.ino index 96dae6558..169e223c2 100644 --- a/tasmota/tasmota_xdsp_display/xdsp_04_ili9341.ino +++ b/tasmota/tasmota_xdsp_display/xdsp_04_ili9341.ino @@ -132,7 +132,7 @@ void ILI9341_InitDriver() #endif // ESP32 #ifdef USE_XPT2046 - XPT2046_Touch_Init(Pin(GPIO_XPT2046_CS)); + XPT2046_Touch_Init(Pin(GPIO_XPT2046_CS),-1,0); #endif tft_init_done = true; diff --git a/tasmota/tasmota_xdsp_display/xdsp_17_universal.ino b/tasmota/tasmota_xdsp_display/xdsp_17_universal.ino index c3154cd60..c784ed31c 100644 --- a/tasmota/tasmota_xdsp_display/xdsp_17_universal.ino +++ b/tasmota/tasmota_xdsp_display/xdsp_17_universal.ino @@ -392,9 +392,19 @@ int8_t cs; #ifdef USE_XPT2046 cp = strstr(ddesc, ":TS,"); if (cp) { - cp+=4; + cp += 4; uint8_t touch_cs = replacepin(&cp, Pin(GPIO_XPT2046_CS)); - XPT2046_Touch_Init(touch_cs); + int8_t irqpin = -1; + if (*(cp - 1) == ',') { + irqpin = strtol(cp, &cp, 10); + } + uint8_t bus = 1; + if (*cp == ',') { + cp++; + bus = strtol(cp, &cp, 10); + if (bus < 1) bus = 1; + } + XPT2046_Touch_Init(touch_cs, irqpin, bus - 1); } #endif // USE_XPT2046