From 7e9f2fdfbd9057ff8194804b10adedf33a4a1581 Mon Sep 17 00:00:00 2001 From: Gert Galjoen Date: Wed, 21 Feb 2024 20:00:15 +0100 Subject: [PATCH] CYD up2date SoftSPI bit banging (credits to the unknown creator) Update README.md add Setup303_CYD Update User_Setup.h --- Extensions/Touch.cpp | 79 ++++++++++++++++++++++ README.md | 2 + TFT_eSPI.cpp | 14 ++-- User_Setups/Setup303_CYD_ESP32-2432S028R.h | 35 ++++++++++ 4 files changed, 126 insertions(+), 4 deletions(-) create mode 100644 User_Setups/Setup303_CYD_ESP32-2432S028R.h diff --git a/Extensions/Touch.cpp b/Extensions/Touch.cpp index 3d3ed825..e6a6829b 100644 --- a/Extensions/Touch.cpp +++ b/Extensions/Touch.cpp @@ -53,6 +53,32 @@ inline void TFT_eSPI::end_touch_read_write(void){ void TFT_eSPI::spi_begin_touch() {begin_touch_read_write();} void TFT_eSPI::spi_end_touch() { end_touch_read_write();} +/*************************************************************************************** +** Function name: Sspixfer +** Description: software SPI transfer. +***************************************************************************************/ +uint16_t SPIdout; +uint16_t SPIdin; + +void Sspixfer (uint16_t dout) // Exchange 8 bits of data over SPI +{ +// uint16_t tmp; // Data in + uint16_t SPImask = 0x80; // Set bit mask + SPIdout = dout; // Set data to go + for (uint8_t lpcnt = 0; lpcnt < 8; lpcnt ++) // Do 8 bits + { + digitalWrite (TOUCH_MOSI, ((SPIdout & SPImask) ? HIGH:LOW)); // Set up data out + SPImask = (SPImask >> 1); // Mask for next bit + digitalWrite(TOUCH_CLK,1); // CLK = 1 + // tmp = (tmp << 1); // Shift existing data along + SPIdin = (SPIdin << 1); // Shift existing data along + digitalWrite(TOUCH_CLK,0); // CLK = 0 + ets_delay_us(1); // Delay + // tmp |= digitalRead(TOUCH_MISO); // Shift data in + SPIdin |= digitalRead(TOUCH_MISO); // Shift data in + } // Go round again +} // All done + /*************************************************************************************** ** Function name: getTouchRaw ** Description: read raw touch position. Always returns true. @@ -60,6 +86,35 @@ void TFT_eSPI::spi_end_touch() { end_touch_read_write();} uint8_t TFT_eSPI::getTouchRaw(uint16_t *x, uint16_t *y){ uint16_t tmp; +#ifdef SOFTSPI + + digitalWrite(TOUCH_CS,0); + Sspixfer(0xD0); // Start new YP conversion + Sspixfer(0); // Read first 8 bits + Sspixfer(0xD0); // Read last 8 bits and start new YP conversion + Sspixfer(0); // Read first 8 bits + Sspixfer(0xD0); // Read last 8 bits and start new YP conversion + Sspixfer(0); // Read first 8 bits + Sspixfer(0xD0); // Read last 8 bits and start new YP conversion + Sspixfer(0); // Read first 8 bits + Sspixfer(0x90); // Read last 8 bits and start new XP conversion + + *x = SPIdin >>4; // Transfer and re-align data + + Sspixfer(0); // Read first 8 bits + Sspixfer(0x90); // Read last 8 bits and start new XP conversion + Sspixfer(0); // Read first 8 bits + Sspixfer(0x90); // Read last 8 bits and start new XP conversion + Sspixfer(0); // Read first 8 bits + Sspixfer(0x90); // Read last 8 bits and start new XP conversion + Sspixfer(0); // Read first 8 bits + Sspixfer(0); // Read last 8 bits + + *y = SPIdin >>4; // Transfer and re-align data + digitalWrite(TOUCH_CS,1); + +#else + begin_touch_read_write(); // Start YP sample request for x position, read 4 times and keep last sample @@ -93,6 +148,8 @@ uint8_t TFT_eSPI::getTouchRaw(uint16_t *x, uint16_t *y){ end_touch_read_write(); +#endif + return true; } @@ -101,16 +158,38 @@ uint8_t TFT_eSPI::getTouchRaw(uint16_t *x, uint16_t *y){ ** Description: read raw pressure on touchpad and return Z value. ***************************************************************************************/ uint16_t TFT_eSPI::getTouchRawZ(void){ +#if defined SOFTSPI + + int16_t tz = 0xFFF; + + digitalWrite(TOUCH_CS,0); + Sspixfer(0xb0); // Start new Z1 conversion + Sspixfer(0); // Read first 8 bits + Sspixfer(0xc0); // Read last 8 bits and start Z2 conversion + tz += (SPIdin >>4); + + Sspixfer(0); // Read first 8 bits + Sspixfer(0); // Read last 8 bits + tz -= (SPIdin >>4); + + digitalWrite(TOUCH_CS,1); + +// tz += Sspixfer16(0xc0) >> 3; // Read Z1 and start Z2 conversion +// tz -= Sspixfer16(0x00) >> 3; // Read Z2 + +#else begin_touch_read_write(); // Z sample request int16_t tz = 0xFFF; + spi.transfer(0xb0); // Start new Z1 conversion tz += spi.transfer16(0xc0) >> 3; // Read Z1 and start Z2 conversion tz -= spi.transfer16(0x00) >> 3; // Read Z2 end_touch_read_write(); +#endif if (tz == 4095) tz = 0; diff --git a/README.md b/README.md index 0c0c15bc..f9a32114 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +CYD version SoftSPI applied as found @ https://github.com/VaAndCob/ESP32-OBD2-Gauge.git + A ["Discussions"](https://github.com/Bodmer/TFT_eSPI/discussions) facility has been added for Q&A etc. Use the ["Issues"](https://github.com/Bodmer/TFT_eSPI/issues) tab only for problems with the library. Thanks! # News 1. The Create_font Processing sketch has been updated to automatically create a complete C header file. The automatic opening of the font folder can also be disabled within the Processing sketch. (Thanks to Pierre-Loup Martin). diff --git a/TFT_eSPI.cpp b/TFT_eSPI.cpp index d537faeb..2db7e931 100644 --- a/TFT_eSPI.cpp +++ b/TFT_eSPI.cpp @@ -533,10 +533,16 @@ TFT_eSPI::TFT_eSPI(int16_t w, int16_t h) void TFT_eSPI::initBus(void) { #ifdef TFT_CS - if (TFT_CS >= 0) { - pinMode(TFT_CS, OUTPUT); - digitalWrite(TFT_CS, HIGH); // Chip select high (inactive) - } + #if defined SOFTSPI // CYD changes + pinMode(TOUCH_MOSI, OUTPUT); // CYD changes + pinMode(TOUCH_MISO, INPUT); // CYD changes + pinMode(TOUCH_CLK, OUTPUT); // CYD changes + #else // CYD changes + if (TFT_CS >= 0) { + pinMode(TFT_CS, OUTPUT); + digitalWrite(TFT_CS, HIGH); // Chip select high (inactive) + } + #endif // CYD changes #endif // Configure chip select for touchscreen controller if present diff --git a/User_Setups/Setup303_CYD_ESP32-2432S028R.h b/User_Setups/Setup303_CYD_ESP32-2432S028R.h new file mode 100644 index 00000000..e0fd7e0c --- /dev/null +++ b/User_Setups/Setup303_CYD_ESP32-2432S028R.h @@ -0,0 +1,35 @@ +// See SetupX_Template.h for all options available +#define USER_SETUP_ID 303 + +#define ILI9341_2_DRIVER + +#define TFT_BL 21 +#define TFT_BACKLIGHT_ON HIGH +#define TFT_MOSI 13 +#define TFT_SCLK 14 +#define TFT_CS 15 +#define TFT_DC 2 +#define TFT_RST -1 + +#define SOFTSPI // Uncomment for software SPI +#define TOUCH_CS 33 // Chip select pin (T_CS) of touch screen +#define TOUCH_MOSI 32 // Separate Touch SoftSPI bus +#define TOUCH_MISO 39 // Separate Touch SoftSPI bus +#define TOUCH_CLK 25 // Separate Touch SoftSPI bus + +#define LOAD_GLCD // Font 1. Original Adafruit 8 pixel font needs ~1820 bytes in FLASH +#define LOAD_FONT2 // Font 2. Small 16 pixel high font, needs ~3534 bytes in FLASH, 96 characters +#define LOAD_FONT4 // Font 4. Medium 26 pixel high font, needs ~5848 bytes in FLASH, 96 characters +#define LOAD_FONT6 // Font 6. Large 48 pixel font, needs ~2666 bytes in FLASH, only characters 1234567890:-.apm +#define LOAD_FONT7 // Font 7. 7 segment 48 pixel font, needs ~2438 bytes in FLASH, only characters 1234567890:-. +#define LOAD_FONT8 // Font 8. Large 75 pixel font needs ~3256 bytes in FLASH, only characters 1234567890:-. +#define LOAD_GFXFF // FreeFonts. Include access to the 48 Adafruit_GFX free fonts FF1 to FF48 and custom fonts +#define SMOOTH_FONT + +#define SPI_FREQUENCY 40000000 + +#define SPI_READ_FREQUENCY 1600000 + +#define SPI_TOUCH_FREQUENCY 2500000 + +// #define SUPPORT_TRANSACTIONS \ No newline at end of file