ILI9341 & XPT2046 Touchscreen
Posted: Sat Mar 25, 2023 8:11 am
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.
Note that there seems too be some code in the CAL source to handle this.
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...
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)
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.
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


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."
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...
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)
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
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