Alternate pins for ILI9341 and SPI flash in T4.1

Status
Not open for further replies.

JotaEfe13

Well-known member
Hi. I am trying to figure out what are the available alternate SPI pins for this two devices, using the Teensy 4.1.

ILI9341 Display

I am using the ILI9341_t3n lib, but I think my doubt apply equally when using the ILI9341_t3 or the ILI9341_t3n (at least when using the default SPI bus).
This chart (https://www.pjrc.com/store/display_ili9341_touch.html) shows the connections:

ILI9341 PinTeensy 3.x and 4.x StandardTeensy 3.x Audio BoardNotes
CS1021Alternate Pins: 9, 15, 20, 21
D/C920Alternate Pins: 10, 15, 20, 21

I see that the alternate pins refer only to the alternate CS0 pins for Teensy 3.x, so I guess in the case of the Teensy 4.1 the alternate pins for CS would be 36 and 37, right?
What I am not sure about is the D/C pin. Pin 9 is showed as the default, which is a CS0 pin in the T3.x, but not in T4.x. And the alternate pins are again CS0 pins in the T3.x. So what does it mean? In T3.x is needed to use a CS0 pin, but in T4.x not? What pins are valid in T4.x for D/C then?



SPI Flash chip

I am using the SerialFlash library and I see the CS default is pin 6. Not being it marked as a CS0 pin for both the T3.x or T4.x, does it mean any digital pin can be used instead?
On the other hand, is it possible to use a different SPI bus with this library? or it would need some tweaks?

Thank you in advance.
 
I've got the ILI9341 working on SPI1 with :-

Code:
#define CS_PIN  0 // touch screen
#define TFT_DC  9
#define TFT_CS  10
#define TFT_MOSI 26
#define TFT_SCLK 27
#define TFT_MISO 1
#define TFT_RST 255

ILI9341_t3n tft = ILI9341_t3n( TFT_CS , TFT_DC, TFT_RST, TFT_MOSI, TFT_SCLK, TFT_MISO );

The problems is you can not get the touch screen to work on SPI1 .
 
I've got the ILI9341 working on SPI1 with :-

Code:
#define CS_PIN  0 // touch screen
#define TFT_DC  9
#define TFT_CS  10
#define TFT_MOSI 26
#define TFT_SCLK 27
#define TFT_MISO 1
#define TFT_RST 255

ILI9341_t3n tft = ILI9341_t3n( TFT_CS , TFT_DC, TFT_RST, TFT_MOSI, TFT_SCLK, TFT_MISO );

The problems is you can not get the touch screen to work on SPI1 .

The pin 10 is marked as a CS0 pin, so maybe that means you can use any digital pin as CS (at least with the ILI9341_t3n lib)?
And the same for the DC pin?
 
The pin 10 is marked as a CS0 pin, so maybe that means you can use any digital pin as CS (at least with the ILI9341_t3n lib)?
And the same for the DC pin?

Yes I think it is any digital pin , I've seen that somewhere that CS and DC can be any digital pin on teensy 4.1 .
Display easy , but getting the touch screen to work is a different matter , WHY ? , I think the ILI9341_t3n library controls the pins direct , but the touch screen uses the SPI library.
I've tried rewriting the touch library to use SPI1 , but could not get it working !!!
If you find I way to free pin 12 and have a working Display / touch screen let me know , I'm stumped .
 
Last edited:
Note: on T4.x the CS pins do not work the same way as they do with T3.x. That is there is no way to for example have SPI control turning on and off multiple CS pins at the same time. The stuff you can control is WHICH CS pin that SPI uses, NOT a mask of which ones (may be multiple like CS and DC)...
On T4 You can get a little speed up in the ILI9341_t3n library if the DC pin is a hardware CS pin.

T4.1-Cardlike.jpg

So yes the ILI9341_t3n works with any digital pins for CS and DC. And yes there are alternate pins for CS on the SPI buss. And they are actually different CS pins... What I mean is that they both map to different index of CS pin in the underlying SPI system.

Why I mention this is with SPI1 for example you have pins 0 and 38 which can be used as hardware SPI CS pins, but they both map to the same CS index and as such you can only use one of them in a sketch as a hardware CS at a time...

Touch screen... It has worked in the past with the ILI9341_t3n library (as well as the ILI9341_t3). Often times when I read that someone has issue with it, turns out they may be using the wrong library.

For example the ILI9341 display sold by Adafruit uses a different touch controller than the one sold by PJRC and several EBAY like companies.

So for example the touch paint program that comes with the Adafruit_ili9341 library does not work with the PJRC display... Nor the one that comes with the ILI9341_t3 library.
I had a Pull request for the ILI9341_t3 library that I issued a few years ago and finally went ahead and closed it last year without it being merged in.

ILI9341_t3n does have an example: touchpaint_xpt2046 which does (or at least did) work with the controller on the PJRC sold displays.
 
The product page mentioned: TSC2046 as the touch controller.
So next question is the TSC2046 compatible with the XPT2046?
Or is there another library for it? Not sure.

If me, I would probably run one of the example XPT2046 library test programs and see if it works.
Also in all cases, unless you have PU resistors on the CS pin of touch and display,
I would typically make sure I had something like:
Code:
pinMode(CS_PIN_TOUCH, OUTPUT);
digitalWrite(CS_PIN_TOUCH, HIGH);
pinMode(CS_PIN_TFT, OUTPUT);
digitalWrite(CS_PIN_TFT, HIGH);
...
Then setup the display and touch

The quick looks I did online make it feel like the two should be compatible.
 
If you find I way to free pin 12 and have a working Display / touch screen let me know , I'm stumped .
Unfortunately I am not using a display with touch screen, so I cannot check it :(


Note: on T4.x the CS pins do not work the same way as they do with T3.x. That is there is no way to for example have SPI control turning on and off multiple CS pins at the same time. The stuff you can control is WHICH CS pin that SPI uses, NOT a mask of which ones (may be multiple like CS and DC)...
On T4 You can get a little speed up in the ILI9341_t3n library if the DC pin is a hardware CS pin.

View attachment 23526

So yes the ILI9341_t3n works with any digital pins for CS and DC. And yes there are alternate pins for CS on the SPI buss. And they are actually different CS pins... What I mean is that they both map to different index of CS pin in the underlying SPI system.

Why I mention this is with SPI1 for example you have pins 0 and 38 which can be used as hardware SPI CS pins, but they both map to the same CS index and as such you can only use one of them in a sketch as a hardware CS at a time...
Thank you, KurtE. So, if I understand correctly, for example what the different pins marked as CS0 means that all they are available as CS pin for the SPI0, but only one can be used at the same time, as the "end destination" will be the same? So when you want to use more than one device in the same SPI bus, you should avoid using more than one pin marked as CS0? I thought it was just the opposite, so good to know.
 
Just doing a quick search I agree with @KurtE. From the Particle forum I did find this:
The resistive touch controller seems quite common and the XPT2046 can replace ADS7843E TSC2046 ADS7843 ADS7846 RS
which would make me think it is compatible.

EDIT: Reread your post. If you want to switch to using SPI1 all you need to do is specify it in the begin statement:
Code:
ts.begin(SPI1);
as opposed to just using
Code:
ts.begin();
 
Compiling .pio\build\teensy41\libf4f\Display\Display_helper.cpp.o
lib\Display\Display_helper.cpp: In function 'void tft_init()':
lib\Display\Display_helper.cpp:56:18: error: no matching function for call to 'XPT2046_Touchscreen::begin(SPIClass&)'
ts.begin(SPI1);
^
In file included from lib\Display\Display_helper.cpp:6:0:
C:\Users\peter\.platformio\packages\framework-arduinoteensy\libraries\XPT2046_Touchscreen/XPT2046_Touchscreen.h:46:7: note: candidate: bool XPT2046_Touchscreen::begin()
bool begin();
^
C:\Users\peter\.platformio\packages\framework-arduinoteensy\libraries\XPT2046_Touchscreen/XPT2046_Touchscreen.h:46:7: note: candidate expects 0 arguments, 1 provided
*** [.pio\build\teensy41\libf4f\Display\Display_helper.cpp.o] Error 1
 
Thank you, KurtE. So, if I understand correctly, for example what the different pins marked as CS0 means that all they are available as CS pin for the SPI0, but only one can be used at the same time, as the "end destination" will be the same? So when you want to use more than one device in the same SPI bus, you should avoid using more than one pin marked as CS0? I thought it was just the opposite, so good to know.

Note: What I was trying to say in my excel is
Pin 0 is marked CS0 (which is short hand for CS0-0 CS on SPI PCS[0])
Pin 37 is marked (CS0-1) So it is also SPI and it is the PCS[1] SPI index so different thin pin 0
Pin 36 is marked (CS0-2) So second PCS[2]
The differences in PCS are described in section 48.2.5

Again when the SPI is doing something the TCR register of SPI controls which one is the current one...

With SPI1: I have:
pin 0 is CS1 again short hand for CS1 so PCS[0]
pin 38 is also CS1-0 so again PCS[0] only one of these
So only one of these pins can function as PCS[0].

However again all of this is not relevant for the majority of SPI uses. In most cases most libraries do not change the CS pin to be in a special mode, but instead simply set them LOW when they are talking to SPI and set them HIGH when not using calls like: digitalWrite.
 
Are you using the the touchtest.ino sketch that is a example sketch in the teensy xpt2046 library?
Code:
#include <XPT2046_Touchscreen.h>
#include <SPI.h>

#define CS_PIN  8
// MOSI=11, MISO=12, SCK=13

//XPT2046_Touchscreen ts(CS_PIN);
#define TIRQ_PIN  2
//XPT2046_Touchscreen ts(CS_PIN);  // Param 2 - NULL - No interrupts
//XPT2046_Touchscreen ts(CS_PIN, 255);  // Param 2 - 255 - No interrupts
XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN);  // Param 2 - Touch IRQ Pin - interrupt enabled polling

void setup() {
  Serial.begin(38400);
  ts.begin();
  ts.setRotation(1);
  while (!Serial && (millis() <= 1000));
}

void loop() {
  if (ts.touched()) {
    TS_Point p = ts.getPoint();
    Serial.print("Pressure = ");
    Serial.print(p.z);
    Serial.print(", x = ");
    Serial.print(p.x);
    Serial.print(", y = ");
    Serial.print(p.y);
    delay(30);
    Serial.println();
  }
}
Tailor the pins to what you want. Looks like you are using PlatformIO so not sure about using that, I am using the Arduino IDE with Arduino 1.8.13 and Teensyduino 1.54-beta5. When I compile the example I get:
Code:
Opening Teensy Loader...
Sketch uses 40824 bytes (0%) of program storage space. Maximum is 8126464 bytes.
Global variables use 49844 bytes (9%) of dynamic memory, leaving 474444 bytes for local variables. Maximum is 524288 bytes.

EDIT are you added the touchscreen include to your sketch?
Code:
#include <XPT2046_Touchscreen.h>
#include <SPI.h>
 
Compile error - Looks like it is trying to use SPI1... And that functionality was added not that long ago to the Teensyduino build...
Not sure if in last official release or not. You may need to update your library if you actually need it on SPI1
 
Note: What I was trying to say in my excel is
Pin 0 is marked CS0 (which is short hand for CS0-0 CS on SPI PCS[0])
Pin 37 is marked (CS0-1) So it is also SPI and it is the PCS[1] SPI index so different thin pin 0
Pin 36 is marked (CS0-2) So second PCS[2]
The differences in PCS are described in section 48.2.5

Again when the SPI is doing something the TCR register of SPI controls which one is the current one...

With SPI1: I have:
pin 0 is CS1 again short hand for CS1 so PCS[0]
pin 38 is also CS1-0 so again PCS[0] only one of these
So only one of these pins can function as PCS[0].

However again all of this is not relevant for the majority of SPI uses. In most cases most libraries do not change the CS pin to be in a special mode, but instead simply set them LOW when they are talking to SPI and set them HIGH when not using calls like: digitalWrite.
I understand. Thank you again for such a neat explanation. :)
 
Also in all cases, unless you have PU resistors on the CS pin of touch and display,

I've had the touch screen working with the XPT2046_Touchscreen , until I moved from the default pins .
Pull up resistors for CS pins , not done that and it worked on default pins ...

What value resistor or can I just use the inbuilt digital pin ones ?
 
I've had the touch screen working with the XPT2046_Touchscreen , until I moved from the default pins .
Pull up resistors for CS pins , not done that and it worked on default pins ...

What value resistor or can I just use the inbuilt digital pin ones ?
Moved what from the default pins?
Again in many cases like this, it is best if you post a simple example sketch with your setup, that is not working.
For example if you simply moved the CS pins to different pins should make no difference...

However I should probably my example sketch, to do the CS pin as I mentioned. That is:
Code:
void setup(void) {
  while (!Serial && (millis() <= 1000));

  Serial.begin(9600);
  Serial.println(F("Touch Paint!"));

  tft.begin();
Should probably have:
Code:
void setup(void) {
  while (!Serial && (millis() <= 1000));

  Serial.begin(9600);
  Serial.println(F("Touch Paint!"));
[COLOR="#B22222"]
  pinMode(TOUCH_CS, OUTPUT);
  digitalWriteFast(TOUCH_CS, HIGH);[/COLOR]

  tft.begin();
Note: the default pin numbers for CS and DC in this sketch should probably also be updated to more normal for different setups...
Like again:
Code:
#if defined(KURTS_FLEXI)
#define TFT_DC 22
#define TFT_CS 15
#define TFT_RST -1
#define TFT_SCK 14
#define TFT_MISO 12
#define TFT_MOSI 7
#define DEBUG_PIN 13
#define TOUCH_CS  8
#else
[COLOR="#B22222"]#define TFT_DC  10
#define TFT_CS 9[/COLOR]
#define TFT_RST 7
#define TFT_SCK 13
#define TFT_MISO 12
#define TFT_MOSI 11
#define TOUCH_CS  8
#endif
The age old problem that many of these examples get changed many times trying lots of configurations and hard to know what to set as default

As for external PU resistors, Could probably get away with 10k, Or something like 4.xK...
 
Status
Not open for further replies.
Back
Top