XPT2046 Library for SPI-1

peter247

Member
I want to use MQS and a ILI9341 Display so with the help of MichaelMeissner and KurtE I've managed to move to the SPI1 bus for the display , but found that my touch driver now doesn't work , which I think is still using the SPI pins .

Is there a better XPT2046 Library which will switch to SPI1 OR can I just change all the SPIs to SPI1s

Code:
/* Touchscreen library for XPT2046 Touch Controller Chip
 * Copyright (c) 2015, Paul Stoffregen, paul@pjrc.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice, development funding notice, and this permission
 * notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "XPT2046_Touchscreen.h"

#define Z_THRESHOLD     400
#define Z_THRESHOLD_INT	75
#define MSEC_THRESHOLD  3
#define SPI_SETTING     SPISettings(2000000, MSBFIRST, SPI_MODE0)

static XPT2046_Touchscreen 	*isrPinptr;
void isrPin(void);

bool XPT2046_Touchscreen::begin()
{
	SPI.begin();
	pinMode(csPin, OUTPUT);
	digitalWrite(csPin, HIGH);
	if (255 != tirqPin) {
		pinMode( tirqPin, INPUT );
		attachInterrupt(digitalPinToInterrupt(tirqPin), isrPin, FALLING);
		isrPinptr = this;
	}
	return true;
}

ISR_PREFIX
void isrPin( void )
{
	XPT2046_Touchscreen *o = isrPinptr;
	o->isrWake = true;
}

TS_Point XPT2046_Touchscreen::getPoint()
{
	update();
	return TS_Point(xraw, yraw, zraw);
}

bool XPT2046_Touchscreen::tirqTouched()
{
	return (isrWake);
}

bool XPT2046_Touchscreen::touched()
{
	update();
	return (zraw >= Z_THRESHOLD);
}

void XPT2046_Touchscreen::readData(uint16_t *x, uint16_t *y, uint8_t *z)
{
	update();
	*x = xraw;
	*y = yraw;
	*z = zraw;
}

bool XPT2046_Touchscreen::bufferEmpty()
{
	return ((millis() - msraw) < MSEC_THRESHOLD);
}

static int16_t besttwoavg( int16_t x , int16_t y , int16_t z ) {
  int16_t da, db, dc;
  int16_t reta = 0;
  if ( x > y ) da = x - y; else da = y - x;
  if ( x > z ) db = x - z; else db = z - x;
  if ( z > y ) dc = z - y; else dc = y - z;

  if ( da <= db && da <= dc ) reta = (x + y) >> 1;
  else if ( db <= da && db <= dc ) reta = (x + z) >> 1;
  else reta = (y + z) >> 1;   //    else if ( dc <= da && dc <= db ) reta = (x + y) >> 1;

  return (reta);
}

// TODO: perhaps a future version should offer an option for more oversampling,
//       with the RANSAC algorithm https://en.wikipedia.org/wiki/RANSAC

void XPT2046_Touchscreen::update()
{
	int16_t data[6];

	if (!isrWake) return;
	uint32_t now = millis();
	if (now - msraw < MSEC_THRESHOLD) return;
	
	SPI.beginTransaction(SPI_SETTING);
	digitalWrite(csPin, LOW);
	SPI.transfer(0xB1 /* Z1 */);
	int16_t z1 = SPI.transfer16(0xC1 /* Z2 */) >> 3;
	int z = z1 + 4095;
	int16_t z2 = SPI.transfer16(0x91 /* X */) >> 3;
	z -= z2;
	if (z >= Z_THRESHOLD) {
		SPI.transfer16(0x91 /* X */);  // dummy X measure, 1st is always noisy
		data[0] = SPI.transfer16(0xD1 /* Y */) >> 3;
		data[1] = SPI.transfer16(0x91 /* X */) >> 3; // make 3 x-y measurements
		data[2] = SPI.transfer16(0xD1 /* Y */) >> 3;
		data[3] = SPI.transfer16(0x91 /* X */) >> 3;
	}
	else data[0] = data[1] = data[2] = data[3] = 0;	// Compiler warns these values may be used unset on early exit.
	data[4] = SPI.transfer16(0xD0 /* Y */) >> 3;	// Last Y touch power down
	data[5] = SPI.transfer16(0) >> 3;
	digitalWrite(csPin, HIGH);
	SPI.endTransaction();
	//Serial.printf("z=%d  ::  z1=%d,  z2=%d  ", z, z1, z2);
	if (z < 0) z = 0;
	if (z < Z_THRESHOLD) { //	if ( !touched ) {
		// Serial.println();
		zraw = 0;
		if (z < Z_THRESHOLD_INT) { //	if ( !touched ) {
			if (255 != tirqPin) isrWake = false;
		}
		return;
	}
	zraw = z;
	
	// Average pair with least distance between each measured x then y
	//Serial.printf("    z1=%d,z2=%d  ", z1, z2);
	//Serial.printf("p=%d,  %d,%d  %d,%d  %d,%d", zraw,
		//data[0], data[1], data[2], data[3], data[4], data[5]);
	int16_t x = besttwoavg( data[0], data[2], data[4] );
	int16_t y = besttwoavg( data[1], data[3], data[5] );
	
	//Serial.printf("    %d,%d", x, y);
	//Serial.println();
	if (z >= Z_THRESHOLD) {
		msraw = now;	// good read completed, set wait
		switch (rotation) {
		  case 0:
			xraw = 4095 - y;
			yraw = x;
			break;
		  case 1:
			xraw = x;
			yraw = y;
			break;
		  case 2:
			xraw = y;
			yraw = 4095 - x;
			break;
		  default: // 3
			xraw = 4095 - x;
			yraw = 4095 - y;
		}
	}
}
 
I believe the that library supports changing which SPI on the begin method...

That is if you do something like:
Code:
XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN);  // Param 2 - Touch IRQ Pin - interrupt enabled polling
...
void setup() {
...
    ts.begin(SPI1);
...
 
I believe the that library supports changing which SPI on the begin method...

That is if you do something like:
Code:
XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN);  // Param 2 - Touch IRQ Pin - interrupt enabled polling
...
void setup() {
...
    ts.begin(SPI1);
...


Looks like you may be wrong on this one ?

Tried that and all I get is "C:\Users\peter\.platformio\packages\framework-arduinoteensy\libraries\XPT2046_Touchscreen/XPT2046_Touchscreen.h:46:7: note: candidate expects 0 arguments, 1 provided"
 
Inthe meantime I got a ILI9488 board with working XPT2046 chip and got
the same problem and found a solution: you need the latest version of XPT2046_Touchscreen from github, only then you will be able to call
Code:
ts.begin(SPI1);
For me this was not enough, because I connected the cable to the alternate MISO1 pin 39. This is also possible, but only when calling
Code:
SPI1.setMISO(39);
right in the beginning of the setup() function. I spent hours for this.

Conclusion:
XPT2046_Touchscreen works fine with ILI9488_t3 and you need no extra hardware like tristate buffers, pullup resistors or whatever.
Just leave SDO(MISO) disconnected and connect T_DO to MISO1(pin39).
Connect SDI(MOSI) and T_DIN both to MOSI1(pin26).
Connect SCK and T_CLK both to SCK1(pin27).

Furthermore I connect CS to pin38 and DC/RS to pin9.
And T_CS to pin25. And T_IRQ to pin24.

With these connections this is my working sketch:

Code:
#include <XPT2046_Touchscreen.h>
#include <SPI.h>

#define CS_PIN  25

#define TIRQ_PIN  24
XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN);


#define USE_ILI9488

#ifdef USE_ILI9488
#include "ILI9488_t3.h"
ILI9488_t3  tft(&SPI1,
                38, //uint8_t _CS,
                9, // uint8_t _DC,
                255, //uint8_t _RST=255,
                26, //uint8_t _MOSI=11,
                27, //uint8_t _SCLK=13,
                255); //uint8_t _MISO=12);
#else
#include "ILI9341_t3n.h"
ILI9341_t3n tft(38, //uint8_t _CS,
                9, // uint8_t _DC,
                255, //uint8_t _RST = 255,
                26, //uint8_t _MOSI=11,
                27, //uint8_t _SCLK=13,
                255); //uint8_t _MISO=12);
#endif

static inline uint16_t Color565(uint8_t r, uint8_t g, uint8_t b) {
  return ((r & (uint16_t)0xF8) << 8) | ((g & (uint16_t)0xFC) << 3) | (b >> 3);
}

void setup() {
  pinMode(CS_PIN,OUTPUT);
  digitalWrite(CS_PIN,1);
  pinMode(38,OUTPUT);
  digitalWrite(38,1);
  delay(1);

  SPI1.setMISO(39);
  Serial.begin(38400);
  tft.begin();
  tft.setRotation(1);
  tft.fillScreen(Color565(0,0,0));
  ts.begin(SPI1);
  ts.setRotation(1);
  while (!Serial && (millis() <= 1000));
}

boolean wastouched = true;

void loop() {
  boolean istouched = ts.touched();
  if (istouched) {
    TS_Point p = ts.getPoint();
    if (!wastouched) {
      tft.fillScreen(Color565(0,0,0));
      tft.setTextColor(Color565(255,255,0));
      tft.setCursor(60, 80);
      tft.print("Touch");
    }
    tft.fillRect(100, 150, 140, 60, Color565(0,0,0));
    tft.setTextColor(Color565(0,255,0));
    tft.setCursor(100, 150);
    tft.print("X = ");
    tft.print(p.x);
    tft.setCursor(100, 180);
    tft.print("Y = ");
    tft.print(p.y);
    Serial.print(", x = ");
    Serial.print(p.x);
    Serial.print(", y = ");
    Serial.println(p.y);
  } else {
    if (wastouched) {
      tft.fillScreen(0x0000);
      tft.setTextColor(Color565(255,0,0));
      tft.setCursor(120, 50);
      tft.print("No");
      tft.setCursor(80, 120);
      tft.print("Touch");
    }
    Serial.println("no touch");
  }
  wastouched = istouched;
  delay(100);
}
 
Thank you very much Johannes for the SPI1.setMISO(39);
it saved my life :eek:

(i use TFT_eSPI library with a teensy 4.1 and a ILI9341 screen, and had the same problem : screen working, but not the touch interface)
 
Back
Top