STM32H750 & XPT2046 Touchscreen in Arduino IDE

Post here all questions related to STM32 core if you can't find a relevant section!
Post Reply
ddano007
Posts: 22
Joined: Mon Oct 05, 2020 9:08 am

STM32H750 & XPT2046 Touchscreen in Arduino IDE

Post by ddano007 »

I was using Teensy 3.2 with 2.8" 240x320 TFT Touchscreen in this configuration
https://www.pjrc.com/store/display_ili9341_touch.html
and it was working OK.
Now I`m going to use STM32H750/743 processor with this display, but I`m not able to make touch work.
Display is working OK, but I`m receiving on touch
Pressure = 4095, x = 8191, y = 8191
from serial ( USB ). Configuration is the same as above Teensy: both ILI9341 and XPT2046 share the same SPI1, of course with different CS pins.
I`m using Adafruit libraries for display and PaulStoffregen`s XPT2046_Touchscreen library.
Does anyone have some tip?
Thanks.

#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <XPT2046_Touchscreen.h>
#include <SPI.h>

#include <Fonts/ContextRepriseCondensedSSiCondensed16pt7b.h> //vyherca
#include <Fonts/ContextRepriseCondensedSSiCondensed45pt7b.h> //vyherca

#define CS_PIN PIN_A3 //PC3_C
#define TFT_CLK 8 //PB3
#define TFT_MOSI 6 //PB5
#define TFT_MISO 7 //PB4
#define TFT_DC PIN_A2 //PC2_C
#define TFT_CS 20 //PA15
#define TFT_RST 44 //PE4

#define TIRQ_PIN 15 //PD1

int DispDrv = PD0;
int DisplayOrientation = 1;
//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

XPT2046_Touchscreen ts(CS_PIN);

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);

void setup() {
SysClkHalfSpeed();
pinMode ( DispDrv, OUTPUT );
digitalWrite(DispDrv, HIGH);

Serial.begin(38400);
tft.begin();
tft.setRotation(1);
tft.fillScreen(ILI9341_BLACK);
ts.begin();
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(ILI9341_BLACK);
tft.setTextColor(ILI9341_YELLOW);
tft.setFont(&ContextRepriseCondensedSSiCondensed45pt7b);
tft.setCursor(60, 80);
tft.print("Touch");
}
tft.fillRect(100, 150, 140, 60, ILI9341_BLACK);
tft.setTextColor(ILI9341_GREEN);
tft.setFont(&ContextRepriseCondensedSSiCondensed16pt7b);
tft.setCursor(100, 150);
tft.print("X = ");
tft.print(p.x);
tft.setCursor(100, 180);
tft.print("Y = ");
tft.print(p.y);
Serial.print("Pressure = ");
Serial.print(p.z);
Serial.print(", x = ");
Serial.print(p.x);
Serial.print(", y = ");
Serial.println(p.y);
} else {
if (wastouched) {
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_RED);
tft.setFont(&ContextRepriseCondensedSSiCondensed16pt7b);
tft.setCursor(120, 50);
tft.print("No");
tft.setCursor(80, 120);
tft.print("Touch");
}
Serial.println("no touch");
}
wastouched = istouched;
delay(100);
}
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: STM32H750 & XPT2046 Touchscreen in Arduino IDE

Post by ag123 »

i've got a fork of Adafruit ILI9341 library here
viewtopic.php?p=5912#p5912
it may not work on H7xx, use the Adafruit original library if there are issues
but for that speed it needs libmaple core, due to SPI with DMA
https://github.com/rogerclarkmelbourne/Arduino_STM32
and unfortunately, it (libmaple core) won't run on H7xx
you would need to use the 'official' STM core with H7xx
https://github.com/stm32duino/Arduino_Core_STM32
https://github.com/stm32duino/Arduino_Core_STM32/wiki

there are a few different things here, you would need to be sure that your H750 is 'working ok' in the first place.
e.g. that it should blink a led etc, and you may need to check that SPI works after all.
H7xx series has quite a complex clock generator setup which allows it to use different frequencies for IO and system clock.
i.e. system clock can vary while keeping IO frequencies same etc, I ran into some issues and shelved some experiments.

for the LCD, I remembered that it is necessary to reset the LCD by pulling the ILI9341 reset pin low and high like for few ms at least.
then go ahead and initialize the LCD (run the tft lcd library to setup the LCD) etc.
some libraries toggle the ILI9341 reset pin to reset the LCD during init, so connect the LCD reset pin as appropriate if the library does that.
oh and turn on backlight (i.e. the led), otherwise it is just 'black'

XPT2046 is a different thing I've not meddled with it myself, hence, I'd leave that. But that that is separate from the LCD and probably needs its own library to deal with it.
Last edited by ag123 on Fri Oct 20, 2023 10:02 am, edited 1 time in total.
ddano007
Posts: 22
Joined: Mon Oct 05, 2020 9:08 am

Re: STM32H750 & XPT2046 Touchscreen in Arduino IDE

Post by ddano007 »

ag123 wrote: Fri Oct 20, 2023 9:44 am i've got a fork of Adafruit ILI9341 library
...
XPT2046 is a different thing I've not meddled with it myself, hence, I'd leave that. But that that is separate from the LCD and probably needs its own library to deal with it.
Thanks for replay. As I mentioned above, ILI9341 library is working OK. The problem is XPT2046; it`s not working, and I`m not able to find any XPT2046 library for Arduino IDE working on STM32H7 :(
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: STM32H750 & XPT2046 Touchscreen in Arduino IDE

Post by ag123 »

well github and google is your friend
https://github.com/PaulStoffregen/XPT2046_Touchscreen
https://github.com/topics/xpt2046

https://ldm-systems.ru/f/doc/catalog/HY ... PT2046.pdf
and it'd seem XPT2046 uses SPI as well. Hence, the options are use 2 different SPI or use 2 different CS pins and multiplex the SPI access.
you may need to rework the codes if they didn't compile correctly with the SPI library or that you wanted to use a different SPI port.
ddano007
Posts: 22
Joined: Mon Oct 05, 2020 9:08 am

Re: STM32H750 & XPT2046 Touchscreen in Arduino IDE

Post by ddano007 »

ag123 wrote: Fri Oct 20, 2023 10:03 am well github and google is your friend
https://github.com/PaulStoffregen/XPT2046_Touchscreen
...
you may need to rework the codes if they didn't compile correctly with the SPI library or that you wanted to use a different SPI port.
Well, that PaulStoffregen`s library I was using `till now :roll:
The second Github link seems not be usable in Arduino IDE.
And my programming skills are not so great to re- design libraries, that`s why I`m using Arduino :mrgreen:

HOWEVER
When I modified my code adding
SPIClass SPI_1(6, 7, 8, TFT_CS);
and modified ts.begin() to
ts.begin(SPI_1);
touch start work but ... display stop work :lol:

1, It looks like I must use two different SPI ports, display and touch are not able to share the same SPI on STM32H7
2, also touch produce some strange data, example:

Pressure = 4112, x = 2427, y = 2193
Pressure = 4112, x = 2426, y = 2200
Pressure = 4103, x = 2480, y = 2215
Pressure = 4112, x = 2277, y = 2247
Pressure = 5840, x = 3807, y = 3632
Pressure = 5869, x = 3809, y = 3631
Pressure = 5855, x = 3791, y = 3627
Pressure = 5847, x = 3819, y = 3609
Pressure = 5627, x = 439, y = 446
Pressure = 6094, x = 449, y = 401
Pressure = 6114, x = 443, y = 402
Pressure = 5234, x = 425, y = 371

It looks like raw data without recalculated to display dimensions. Well, I`m going to look into touch library :evil:
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: STM32H750 & XPT2046 Touchscreen in Arduino IDE

Post by ag123 »

my guess would be that PaulStoffregen's repo is probably good to go

and apparently, the 'conflict' is that CS pin, which I'd guess you would need to handle it outside the library.
e.g. that you need to select TFT_CS or XPT_CS using a digitalWrite() etc.
i'd guess a problem is the TFT library is controlling the CS pin, that would likely be a source of the conflict.
It may take digging thru the library codes to see that it should release the CS pin between SPI ops.
Otherwise, if both are selected, there could be conflicts. e.g. XPT commands goes to TFT and vice versa.

using 2 SPI ports is the 'easy' way out if you have them.
ddano007
Posts: 22
Joined: Mon Oct 05, 2020 9:08 am

Re: STM32H750 & XPT2046 Touchscreen in Arduino IDE

Post by ddano007 »

Well, using two SPI ports solved the problem, display on SPI_1 and tuch on SPI_4. Great thanks for help.

#include <Wire.h>
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <XPT2046_Touchscreen.h>
#include <SPI.h>
...
// touch SPI_4
#define TOUCH_CLK PE2
#define TOUCH_MOSI PE6
#define TOUCH_MISO PE5
#define TOUCH_CS PE4

// display SPI_1
#define TFT_CLK PB3
#define TFT_MOSI PB5
#define TFT_MISO PB4
#define TFT_DC PC2_C
#define TFT_CS PA15
#define TFT_RST PE4
...
// MOSI MISO SCLK
SPIClass SPI_1(TFT_MOSI, TFT_MISO, TFT_CLK, TFT_CS);
// MOSI MISO SCLK
SPIClass SPI_4(TOUCH_MOSI, TOUCH_MISO, TOUCH_CLK);

XPT2046_Touchscreen ts(TOUCH_CS);
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
...
void setup() {
tft.begin();
tft.setRotation(1);
tft.fillScreen(ILI9341_BLACK);
ts.begin(SPI_4);
ts.setRotation(1);
...
}
Post Reply

Return to “General discussion”