I spent a little time on writing some code to read the XPT2046. I followed this example ...
https://www.ccsinfo.com/forum/viewtopic.php?t=58912 for A PIC Chip.
He also mentions ...
"This is a functional driver you can use on the XPT2046 or the
TI TSC2046, it MAY also work with the
ADS7843"
I can read the Data (x,y,z). My XY rotation seems to be off from the ILI9341. Could be my bit manipulation or the Select Case.
I did not calibrate anything. I just wanted to see if I could pull the point location off of the screen which it seems to do. But not in the right orientation.
I did not use an interupt. I am just polling the tirq pin with a timer.
Thanks to whoever ported the rSPI32 Library it worked great ! ?
I will post the code but it is very raw and not a driver just some quick and dirty code to read the point on the touch screen.
Probably lots of Code Smells in there as well! ?
"If" I clean it up I will update the code on this thread. Enjoy!
[CODE lang="b4x" title="Use At Your Own Risk ! XPT2046 read code

"]
#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 600
#End Region
Sub Process_Globals
Public Serial1 As Serial
Private ILI As AdafruitILI93411
Private Timer1 As Timer
Public tirqPin As Pin
Public X_COMMAND_BYTE As Byte = 0xD0 ' get X values
Public Y_COMMAND_BYTE As Byte = 0x90 ' get Y values
Public Z1_COMMAND_BYTE As Byte = 0xB1 ' get Z1 value
Public Z2_COMMAND_BYTE As Byte = 0xC1 ' get Z2 value
Public MSB_BIT_MASK As Byte = 0x7F ' Bit mask For MSB byte
Public Z_THRESHOLD As Byte = 400
Public Z_THRESHOLD_INT As Int = 75
Public X_Val, Y_Val As UInt
Public Z_Val, Z1, Z2 As Int
Public xraw, yraw As Int
Public ScreenRotation As Byte = 3 'screen rotation
Public TOUCH_FLAG As Boolean = False
End Sub
' Followed the example below from "cbarberis" Written for a PIC Chip.
'https://www.ccsinfo.com/forum/viewtopic.php?t=58912
'//For resolution 240x320,the calibration parameter is 663,-13,894,-30
' XFAC 663
' XOFFSET (-13)
' YFAC 894
' YOFFSET (-30)
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
Delay(100)
''
Log("ILI9341 Init")
ILI.Initialize(5,16,17)
ILI.SetRotation(1)
' LED Pin Always ON, tied to 3.3V protoboard powersupply, power for TFT not driven by ESP8266
' Remember to tie the Grounds together

Delay(120) ''''''' Only INIT the TFT Once and the 120 ms delay IS needed or it fails ****
Log("ILI9341 Init Done")
Delay(120) ''''''' Only INIT the TFT Once and the 120 ms delay IS needed or it fails ****
'
ScreenSet
'
SPI.CheckIfThisBoardIsSupported
'CHANGE HERE THE PIN NUMBERS (SCK, MISO, MOSI, CS)
'SS/CS =15 MISO=12 MOSI=13 SCK =14 T_IRQ = 0
SPI.ESP32_SPI_Set_Pins(14,12,13,15)
SPI.CSPinDeActivate(SPI.CS__SPI)
'
tirqPin.Initialize(0,tirqPin.MODE_INPUT)
Timer1.Initialize("TickSub_1",100)
Timer1.Enabled = True
End Sub
Sub TickSub_1
If tirqPin.digitalread = True Then
ReadTouch_XYZData
If Z_Val > Z_THRESHOLD_INT Then
Log("XRaw value: ",xraw)
Log("YRaw value: ",yraw)
Log("X value: ",X_Val)
Log("Y value: ",Y_Val)
Log("Z value: ",Z_Val)
Log("Done XYZ Data")
End If
End If
End Sub
Sub ReadTouch_XYZData ' read 12 Bit data from SPI bus after sending a command byte For X,Y,Z1&Z2 measurements
Dim MSB, LSB As Byte = 0 ' was Uin_8t
' output_low(PIN_B8);
' delay_us(2);
' /////////////////////Gather Z pressure readings////////////////////////////////////
SPI.Begin_Transaction(2000000, SPI.MSBFIRST, SPI.SPI_MODE0)
SPI.CSPinActivate(SPI.CS__SPI)
SPI.Transfer_Byte(Z1_COMMAND_BYTE) ' send command byte
MSB = SPI.Transfer_Byte(0)
LSB = SPI.Transfer_Byte(0)
' Z1 = (((MSB & MSB_BIT_MASK) << 8)+ LSB) >> 3;
Z1 = Bit.ShiftLeft(Bit.And(MSB,MSB_BIT_MASK),8)
Z1 = Z1 + LSB
Z1 = Bit.ShiftRight(Z1,3)
'
SPI.Transfer_Byte(Z2_COMMAND_BYTE) ' send command byte
MSB = SPI.Transfer_Byte(0)
LSB = SPI.Transfer_Byte(0)
' Z2 = (((MSB & MSB_BIT_MASK) << 8)+ LSB) >> 3;
Z2 = Bit.ShiftLeft(Bit.And(MSB,MSB_BIT_MASK),8)
Z2 = Z2 + LSB
Z2 = Bit.ShiftRight(Z2,3)
'
Z_Val = Z1 + 4095
Z_Val = Z_Val - Z2
'
If(Z_Val >= Z_THRESHOLD) Then ' If we have more than the pressure threshold Then read X&Y data
SPI.Transfer_Byte(X_COMMAND_BYTE) ' send command byte
MSB = SPI.Transfer_Byte(0)
LSB = SPI.Transfer_Byte(0)
xraw = Bit.ShiftLeft(Bit.And(MSB,MSB_BIT_MASK),8)
xraw = xraw + LSB
xraw = Bit.ShiftRight(xraw,3)
SPI.Transfer_Byte(Y_COMMAND_BYTE) ' send command byte
MSB = SPI.Transfer_Byte(0)
LSB = SPI.Transfer_Byte(0)
yraw = Bit.ShiftLeft(Bit.And(MSB,MSB_BIT_MASK),8)
yraw = yraw + LSB
yraw = Bit.ShiftRight(yraw,3)
End If
'output_high(PIN_B8); // in Case Z_Val did Not qualify lets bring CS high
SPI.CSPinDeActivate(SPI.CS__SPI)
SPI.End_Transaction
' We need To know the display rotation To compensate For X,Y locations
Select Case True ' We need To know the display rotation To compensate For X,Y locations
Case ScreenRotation = 1
X_Val = 4095 - yraw
Y_Val = 4095 - xraw
Case ScreenRotation = 2
X_Val = xraw
Y_Val = yraw
Case ScreenRotation = 3
X_Val = yraw
Y_Val = 4095 - xraw
Case ScreenRotation = 4
X_Val = 4095 - xraw
Y_Val = 4095 - yraw
End Select
End Sub
Sub ScreenSet
Log("Blue")
ILI.FillScreen(ILI.COLOR_BLUE)
Delay(500)
Log("Done")
ILI.FillScreen(ILI.COLOR_WHITE)
Delay(500)
Log("Draw Text")
Delay(500)
ILI.GFX.ConfigureText(2, ILI.COLOR_BLACK, True)
ILI.GFX.SetCursor(2, 2)
ILI.GFX.DrawText("This is a test")
Log("Draw Text Done")
Log("Height: ", ILI.GFX.Height-10)
Log("Width: ", ILI.GFX.Width)
End Sub
'//For resolution 240x320,the calibration parameter is 663,-13,894,-30
'//For resolution 320x480,the calibration parameter is 852,-14,1284,-30
'
'#define XFAC 663 //852
'#define XOFFSET (-13) //(-14)
'#define YFAC 894 //1284
'#define YOFFSET (-30)
''[/CODE]