Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CYD ESP32-2432S028 with different SPI bus for touch #3186

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions Extensions/Touch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,68 @@ 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.
***************************************************************************************/
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
Expand Down Expand Up @@ -93,6 +148,8 @@ uint8_t TFT_eSPI::getTouchRaw(uint16_t *x, uint16_t *y){

end_touch_read_write();

#endif

return true;
}

Expand All @@ -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;

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
CYD version now in: User_Setups\Setup303_CYD_ESP32-2432S028R.h
SoftSPI applied as found @ https://forum.arduino.cc/t/esp32-2432s028r-all-in-one-display-touch-spi-problems/1059746/20

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).
Expand Down
14 changes: 10 additions & 4 deletions TFT_eSPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions User_Setups/Setup303_CYD_ESP32-2432S028R.h
Original file line number Diff line number Diff line change
@@ -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