Question about SPI bus assignment for TFT_eSPI, touch screen, and SD card for CYD

Unfortunately, with the idea posted above, I found that I couldn't save a screenshot of the LCD to the SD card.

My basic requirement is not only that the LCD, touch panel and SD card all work, but also to save a screenshot of the image displayed on the LCD to the SD card.

After a long search, I found three solutions related to my question. I'd post them all.

Please note that the essence of these solutions is how to put the LCD, touch panel and SD card onto two SPI buses, because in the CYD, the LCD, touch panel and SD card are all connected to separate GPIO pins.

In this post, all solutions assume that "ESP32-2432S028R CYD" is selected as the board type.

I hope this thread helps anyone having trouble with CYD and SD cards.


Solution 1 ... Use LovyanGFX.

In the sample configuration file, you can set the touch panel's SPI settings (here) to cfg.spi_host = -1 as follows, which will allow the touch panel to be read with bit-banging rather than via the SPI bus.

  // For SPI connection
  cfg.spi_host = -1; // Select the SPI (HSPI_HOST or VSPI_HOST) or only XPT2046 can be set to -1.
  cfg.freq = 1000000; // Set the SPI clock
  cfg.pin_sclk = CYD_TP_CLK; // SCLK pin number
  cfg.pin_mosi = CYD_TP_MOSI; // MOSI pin number
  cfg.pin_miso = CYD_TP_MISO; // MISO pin number
  cfg.pin_cs = CYD_TP_CS; // CS pin number

Moreover, if you define LGFX_AUTODETECT just before including the header file, you can use the convenient function that the library automatically recognizes CYD at runtime and sets the necessary parameters instead of building a troublesome configuration file.

#define LGFX_AUTODETECT
#include <LovyanGFX.h>

I set the SPI clock frequency for SD card to 50MHz as follows:

#define SPI_SD_FREQUENCY 50000000 // 50 MHz
static SPIClass sd_spi = SPIClass(SD_SPI_BUS); // VSPI
...
void setup() {
  ...
  sd_spi.begin(SD_SCK, SD_MISO, SD_MOSI, SD_CS);
  if (SD.begin(SD_CS, sd_spi, SPI_SD_FREQUENCY)) {
    Serial.println("SD card is successfully mounted.");
  } else {
    Serial.println("Failed to mount SD card.");
    return false;
  }
...
}

According to the benchmarks I performed, LovyanGFX is faster than TFT_eSPI.

So this is my favorite solution.


Solution 2 ... Use TFT_eSPI and XPT2046_Bitbang_Slim

This is the method introduced in "witnessmenow/ESP32-Cheap-Yellow-Display", and like LovyanGFX, it accesses the touch panel by bit-banging.

You can use it instead of the XPT2046_Touchscreen library, but it has neither setRotation() nor the ability to filter out outliers, so it is a library that is somewhat tedious to actually use.

I managed to make it usable by writing a wrapper class that solved these problems.


Solution 3 ... Use TFT_eSPI and XPT2046_Touchscreen

This is the most orthodox method, and was posted in "ESP32 touchscreen does not work,,,," (thanks to @horace).

The program in the above thread requires the "ESP32 Dev Module" board type, but I have confirmed that it works with User_Setup.h written for "ESP32-2432S028R CYD".

The key is to leave TFT_MISO undefined in User_Setup.h, connect the LCD and touch panel to the same SPI bus, and connect the SD card to another SPI bus (see diagram for example, where HSPI and VSPI are interchangeable).

Unfortunately, by removing MISO from the LCD, it is not possible to read pixel values ​​from the LCD and therefore not possible to take a screen capture.

But I think this method will make many people who are used to TFT_eSPI happy.