Skip to content

Commit aef8ec7

Browse files
Merge pull request #33 from KurtE/SPIn_and_flexSPI
Allow use to choose a different SPI port and also ability to use my F…
2 parents 26b691b + 2ceeeb3 commit aef8ec7

2 files changed

Lines changed: 85 additions & 23 deletions

File tree

XPT2046_Touchscreen.cpp

Lines changed: 70 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@
3030
static XPT2046_Touchscreen *isrPinptr;
3131
void isrPin(void);
3232

33-
bool XPT2046_Touchscreen::begin()
33+
bool XPT2046_Touchscreen::begin(SPIClass &wspi)
3434
{
35-
SPI.begin();
35+
_pspi = &wspi;
36+
_pspi->begin();
3637
pinMode(csPin, OUTPUT);
3738
digitalWrite(csPin, HIGH);
3839
if (255 != tirqPin) {
@@ -43,6 +44,25 @@ bool XPT2046_Touchscreen::begin()
4344
return true;
4445
}
4546

47+
#if defined(_FLEXIO_SPI_H_)
48+
#define FLEXSPI_SETTING FlexIOSPISettings(2000000, MSBFIRST, SPI_MODE0)
49+
bool XPT2046_Touchscreen::begin(FlexIOSPI &wflexspi)
50+
{
51+
_pspi = nullptr; // make sure we dont use this one...
52+
_pflexspi = &wflexspi;
53+
_pflexspi->begin();
54+
pinMode(csPin, OUTPUT);
55+
digitalWrite(csPin, HIGH);
56+
if (255 != tirqPin) {
57+
pinMode( tirqPin, INPUT );
58+
attachInterrupt(digitalPinToInterrupt(tirqPin), isrPin, FALLING);
59+
isrPinptr = this;
60+
}
61+
return true;
62+
}
63+
#endif
64+
65+
4666
ISR_PREFIX
4767
void isrPin( void )
4868
{
@@ -100,30 +120,58 @@ static int16_t besttwoavg( int16_t x , int16_t y , int16_t z ) {
100120
void XPT2046_Touchscreen::update()
101121
{
102122
int16_t data[6];
103-
123+
int z;
104124
if (!isrWake) return;
105125
uint32_t now = millis();
106126
if (now - msraw < MSEC_THRESHOLD) return;
107-
108-
SPI.beginTransaction(SPI_SETTING);
109-
digitalWrite(csPin, LOW);
110-
SPI.transfer(0xB1 /* Z1 */);
111-
int16_t z1 = SPI.transfer16(0xC1 /* Z2 */) >> 3;
112-
int z = z1 + 4095;
113-
int16_t z2 = SPI.transfer16(0x91 /* X */) >> 3;
114-
z -= z2;
115-
if (z >= Z_THRESHOLD) {
116-
SPI.transfer16(0x91 /* X */); // dummy X measure, 1st is always noisy
117-
data[0] = SPI.transfer16(0xD1 /* Y */) >> 3;
118-
data[1] = SPI.transfer16(0x91 /* X */) >> 3; // make 3 x-y measurements
119-
data[2] = SPI.transfer16(0xD1 /* Y */) >> 3;
120-
data[3] = SPI.transfer16(0x91 /* X */) >> 3;
127+
if (_pspi) {
128+
_pspi->beginTransaction(SPI_SETTING);
129+
digitalWrite(csPin, LOW);
130+
_pspi->transfer(0xB1 /* Z1 */);
131+
int16_t z1 = _pspi->transfer16(0xC1 /* Z2 */) >> 3;
132+
z = z1 + 4095;
133+
int16_t z2 = _pspi->transfer16(0x91 /* X */) >> 3;
134+
z -= z2;
135+
if (z >= Z_THRESHOLD) {
136+
_pspi->transfer16(0x91 /* X */); // dummy X measure, 1st is always noisy
137+
data[0] = _pspi->transfer16(0xD1 /* Y */) >> 3;
138+
data[1] = _pspi->transfer16(0x91 /* X */) >> 3; // make 3 x-y measurements
139+
data[2] = _pspi->transfer16(0xD1 /* Y */) >> 3;
140+
data[3] = _pspi->transfer16(0x91 /* X */) >> 3;
141+
}
142+
else data[0] = data[1] = data[2] = data[3] = 0; // Compiler warns these values may be used unset on early exit.
143+
data[4] = _pspi->transfer16(0xD0 /* Y */) >> 3; // Last Y touch power down
144+
data[5] = _pspi->transfer16(0) >> 3;
145+
digitalWrite(csPin, HIGH);
146+
_pspi->endTransaction();
147+
}
148+
#if defined(_FLEXIO_SPI_H_)
149+
else if (_pflexspi) {
150+
_pflexspi->beginTransaction(FLEXSPI_SETTING);
151+
digitalWrite(csPin, LOW);
152+
_pflexspi->transfer(0xB1 /* Z1 */);
153+
int16_t z1 = _pflexspi->transfer16(0xC1 /* Z2 */) >> 3;
154+
z = z1 + 4095;
155+
int16_t z2 = _pflexspi->transfer16(0x91 /* X */) >> 3;
156+
z -= z2;
157+
if (z >= Z_THRESHOLD) {
158+
_pflexspi->transfer16(0x91 /* X */); // dummy X measure, 1st is always noisy
159+
data[0] = _pflexspi->transfer16(0xD1 /* Y */) >> 3;
160+
data[1] = _pflexspi->transfer16(0x91 /* X */) >> 3; // make 3 x-y measurements
161+
data[2] = _pflexspi->transfer16(0xD1 /* Y */) >> 3;
162+
data[3] = _pflexspi->transfer16(0x91 /* X */) >> 3;
163+
}
164+
else data[0] = data[1] = data[2] = data[3] = 0; // Compiler warns these values may be used unset on early exit.
165+
data[4] = _pflexspi->transfer16(0xD0 /* Y */) >> 3; // Last Y touch power down
166+
data[5] = _pflexspi->transfer16(0) >> 3;
167+
digitalWrite(csPin, HIGH);
168+
_pflexspi->endTransaction();
169+
121170
}
122-
else data[0] = data[1] = data[2] = data[3] = 0; // Compiler warns these values may be used unset on early exit.
123-
data[4] = SPI.transfer16(0xD0 /* Y */) >> 3; // Last Y touch power down
124-
data[5] = SPI.transfer16(0) >> 3;
125-
digitalWrite(csPin, HIGH);
126-
SPI.endTransaction();
171+
#endif
172+
// If we do not have either _pspi or _pflexspi than bail.
173+
else return;
174+
127175
//Serial.printf("z=%d :: z1=%d, z2=%d ", z, z1, z2);
128176
if (z < 0) z = 0;
129177
if (z < Z_THRESHOLD) { // if ( !touched ) {

XPT2046_Touchscreen.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
#include "Arduino.h"
2727
#include <SPI.h>
2828

29+
#if defined(__IMXRT1062__)
30+
#if __has_include(<FlexIOSPI.h>)
31+
#include <FlexIOSPI.h>
32+
#endif
33+
#endif
34+
2935
#if ARDUINO < 10600
3036
#error "Arduino 1.6.0 or later (SPI library) is required"
3137
#endif
@@ -43,7 +49,11 @@ class XPT2046_Touchscreen {
4349
public:
4450
constexpr XPT2046_Touchscreen(uint8_t cspin, uint8_t tirq=255)
4551
: csPin(cspin), tirqPin(tirq) { }
46-
bool begin();
52+
bool begin(SPIClass &wspi = SPI);
53+
#if defined(_FLEXIO_SPI_H_)
54+
bool begin(FlexIOSPI &wflexspi);
55+
#endif
56+
4757
TS_Point getPoint();
4858
bool tirqTouched();
4959
bool touched();
@@ -59,6 +69,10 @@ class XPT2046_Touchscreen {
5969
uint8_t csPin, tirqPin, rotation=1;
6070
int16_t xraw=0, yraw=0, zraw=0;
6171
uint32_t msraw=0x80000000;
72+
SPIClass *_pspi = nullptr;
73+
#if defined(_FLEXIO_SPI_H_)
74+
FlexIOSPI *_pflexspi = nullptr;
75+
#endif
6276
};
6377

6478
#ifndef ISR_PREFIX

0 commit comments

Comments
 (0)