ILI9341 & XPT2046 Touchscreen

Use this section to discuss your embedded Flowcode projects.
mnfisher
Valued Contributor
Posts: 953
http://meble-kuchenne.info.pl
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 507 times

Flowcode v10 ILI9341 & XPT2046 Touchscreen

Post by mnfisher »

Little time to program at the moment :-( Pressures of work :(

Ben's note about 80MHz SPI on esp32 got me to dust this little project off - and sort of get it working.

First the problems...

I attempted to get the ili9341 display with XPT2046 touch controller working on the same SPI channel. This should be possible - they have a separate CS pin - and for economy of wiring (and as I like to play with chips with less pins:-) ) However adding the display component and touch component doesn't work (this on esp32) - attaching two devices to the same SPI channel doesn't work.
E (1089) spi: SPI2 already claimed by spi master.
E (1089) spi_master: spi_bus_initialize(232): host already in use


Note that there seems too be some code in the CAL source to handle this.

Code: Select all

#ifdef MX_SPI_CHANNEL_IN_USE

	//if the channel is already in use then redirect the macro calls to the first instance of the channel

	#warning "SPI Hardware Channel has already been created by another instance. Relying on previous instance configuration."
However this doesn't seem to work..

The following demonstrates : Attempting to initialise the XPT controller gives the above error from the esp32 - not initialising it (it only initialises the cal_spi) - gives a similar message and reset...
touch 1.fcfx
(15.81 KiB) Downloaded 104 times
So - not to be deterred (and now the component source code is available - thanks Matrix) - I created some 'combined' code by mixing the xpt2046 component into the display component...

After much fiddling using the touch interrupt (it gets a lot of 'noise'?) - I went back to just 'polling' the touch controller. So - now we have the world's worst 'paint' program.

First - another issue. The touch controller is not very accurate.... So I added a 'calibration' to my code (this should be pulled to a separate macro and the calibration data saved to EEPROM to stop it being needed on every run)
ili9341 touch.fcfx
(206.88 KiB) Downloaded 108 times
This 'works' as a very simple 'paint' program - drawing a small square at each touch point...

Another issue...

The touch controller works at 6MHz - the display at upto 80MHz (32MHz with my choice of pins...) - the CAL SPI - set speed takes a byte (prescaler) - it's not immediately obvious how this relates to the SPI speed on the esp32.

Code: Select all

CALFUNCTION(void, FC_CAL_SPI_SetPrescaler_, (MX_UINT16 Prescaler))
{
	MX_PRESCALE_VAR_X = Prescaler;
	// MX_UINT8 SPIConfig = 0;

	#if (MX_SPI_CHANNEL_X > 0)
		if (Prescaler == 0)			//1:2
			MX_PRESCALE_VAR_X =	4;
		if (Prescaler == 1)			//1:4
			MX_PRESCALE_VAR_X =	0;
		if (Prescaler == 2)			//1:8
Handles a small subset of the 'available' speeds - an alternative 'set speed' method ((or some documentation?) would be good here....

So I cope with this by using 6MHz for display and touch.

It 'sort of' works - it is possible to draw on the display using a finger or stylus... However the touch controller - seems very 'noisy' - returning spurious 'touch' signals. This frustrates my plan for using the touch (an onscreen 'keypad') - and my initial attempt to filter this by checking for 'pressure > value' doesn't cut it....

Any one any experience with these - or spot what I do wrong? I've tried to make the code have a much 'thinner' interface to the SPI controller than the original component - but only went so far with this - it might be I've missed something on the interface?

Martin

BenR
Matrix Staff
Posts: 1739
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 440 times
Been thanked: 603 times

Re: ILI9341 & XPT2046 Touchscreen

Post by BenR »

Hi Martin,

One way I have overcome similar noise is to do two or three samples at high speed and then if there is a valid touch check that all samples saw it and that all are within a certin amount of each other.

If it helps you can look at the source code for the resistive touch component as it uses the same method.
https://www.flowcode.co.uk/wiki/index.p ... l_Library)

As for the SPI prescaler on the ESP32, the property allows us to specify a three digit number from 000 to 999 and so the value specifies the SPI rate in steps of 100KHz for example 001 would be 100KHz and 500 would be 50MHz.

mnfisher
Valued Contributor
Posts: 953
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 507 times

Re: ILI9341 & XPT2046 Touchscreen

Post by mnfisher »

Thanks Ben,

I'll have a look at the resistive touch component...

One issue with SetPrescaler - in the CAL SPI component - takes a byte argument. I'd assumed the values from the prescaler 'selection' were the values to use?

Martin

jgu1
Posts: 615
Joined: Thu Dec 03, 2020 8:25 pm
Location: Denmark
Has thanked: 507 times
Been thanked: 132 times

Re: ILI9341 & XPT2046 Touchscreen

Post by jgu1 »

Hi Martin!

I noticed this:
"I attempted to get the ili9341 display with XPT2046 touch controller working on the same SPI channel. This should be possible - they have a separate CS pin - and for economy of wiring (and as I like to play with chips with less pins:-) ) However adding the display component and touch component doesn't work (this on esp32) - attaching two devices to the same SPI channel doesn't work."

Do I understand this that all the pin from the SPI XPT2046 use the same as the SPI on the ILI display except the CS pin, which need an output to the display and the touch?

Do you have any idea why it not work for you, see your point, least 4 pin more availble would be nice ;) :D

Br jorgen

mnfisher
Valued Contributor
Posts: 953
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 507 times

Re: ILI9341 & XPT2046 Touchscreen

Post by mnfisher »

Hi Jorgen,

Yes - it should be possible to connect several devices to the same SPI channel. The relevant CS (chip select) pin is pulled low to choose the device addressed. The touch sensor uses separate pins, but they can be connected to the matching pins for the display.

So the display has the potential to use 3 SPI channels (it has a SD card too) - but to keep things 'neat' I tried to just use one - hitting my first hurdle when I added the xpt2046 component. I worked around this by 'mashing' the two components together (the xpt component is pretty small)- but I'm not sure I'll try the SD card on the same channel..

The fact that it wasn't possible to use the component, as is, I think is a bug in the cal _spi implementation.

Martin

jgu1
Posts: 615
Joined: Thu Dec 03, 2020 8:25 pm
Location: Denmark
Has thanked: 507 times
Been thanked: 132 times

Re: ILI9341 & XPT2046 Touchscreen

Post by jgu1 »

Well ok Martin, thank´s for the explanation :)

br Jorgen

BenR
Matrix Staff
Posts: 1739
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 440 times
Been thanked: 603 times

Re: ILI9341 & XPT2046 Touchscreen

Post by BenR »

Hello,

Just as a test would you mind trying to remove this line of code from the bottom of the CAL SPI file.

#undef MX_SPI_CHANNEL_IN_USE

It looks like it's disabling the multi use mode and I can't remember why. I'll do some more investigation here but we have training this week so it's hard to get desk time to sit and have a go.

mnfisher
Valued Contributor
Posts: 953
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 507 times

Re: ILI9341 & XPT2046 Touchscreen

Post by mnfisher »

Hi Ben,

Tried - and with a full rebuild - no change. Still gives
E (1558) spi: SPI2 already claimed by spi master.
E (1558) spi_master: spi_bus_initialize(232): host already in use
I couldn't see how the pre-processor magic works :-(

Martin

BenR
Matrix Staff
Posts: 1739
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 440 times
Been thanked: 603 times

Re: ILI9341 & XPT2046 Touchscreen

Post by BenR »

Hi Martin,

Many thanks for letting me know. I've had a bit of a play and edited the file which is now on the update system.

Let me know how you get on with this one.

mnfisher
Valued Contributor
Posts: 953
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 507 times

Re: ILI9341 & XPT2046 Touchscreen

Post by mnfisher »

Hi Ben,

Still crashes - although the messages about the SPI being claimed are gone...
The above sample crashes with a WDT reset - so SampleSensor doesn't seem to be getting a reply...

I feel it might be connected to the CS pin - but will try attaching an oscilloscope and see if I can spot what's happening

Martin

Post Reply