Skip to content

Commit

Permalink
Added support for XPT2046, which uses a different bus than TFT.
Browse files Browse the repository at this point in the history
  • Loading branch information
nopnop2002 committed Jun 7, 2023
1 parent 0d3ce0f commit 8c0dccd
Show file tree
Hide file tree
Showing 28 changed files with 226 additions and 93 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ project(ili9340)
# that fits the partition named 'storage'. FLASH_IN_PROJECT indicates that
# the generated image should be flashed when the entire project is flashed to
# the target with 'idf.py -p PORT flash
spiffs_create_partition_image(storage font FLASH_IN_PROJECT)
spiffs_create_partition_image(storage0 font FLASH_IN_PROJECT)
spiffs_create_partition_image(storage1 icons FLASH_IN_PROJECT)
spiffs_create_partition_image(storage2 images FLASH_IN_PROJECT)
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ Left:2.8" Right:2.4"
---

# 2.8" ILI9341 Built-In 320x240
Vendor part number is ESP32-2432S028R.

![config-ESP32-2432S028R](https://github.com/nopnop2002/esp-idf-ili9340/assets/6020549/2d6dfc02-747d-46fb-8881-65dffdadcf14)
![ESP32-2432S028R-1](https://github.com/nopnop2002/esp-idf-ili9340/assets/6020549/e9e4b502-4063-49b2-b58a-d60aeccdd380)
Expand Down Expand Up @@ -321,13 +322,21 @@ Previously it was called HSPI_HOST / VSPI_HOST, but now it is called SPI2_HOST /
---

# XPT2046 Touch Screen
A library of XPT2046 Touch Screen is included in this project.
There is a TFT equipped with XPT2046.
![XPT2046-3](https://user-images.githubusercontent.com/6020549/144333924-5236bff3-3f4d-4be4-8e99-b6e31878e4f3.jpg)

A library of XPT2046 Touch Screen is included in this project.
XPT2046 shares the TFT and SPI bus.
Use the menu to enable XPT2046.
![config-xpt2046-1](https://user-images.githubusercontent.com/6020549/167235381-0d72389f-51a0-417d-8693-542d0ac5482d.jpg)

This module also has an XPT2046.
![ESP32-2432S028R-2](https://github.com/nopnop2002/esp-idf-ili9340/assets/6020549/af351771-6c08-4949-b541-1c9d35e97c8e)

XPT2046 uses a different SPI bus than TFT.
SPI SCLK and MISO use a separate GPIO than TFT SCLK.
![config-xpt2046-3](https://github.com/nopnop2002/esp-idf-ili9340/assets/6020549/8942ead0-9020-4fcf-a2b1-4434d1d1c42a)

- Touch position accuacy
The coordinates read from XPT2046 are not stable.
The difference between the coordinates read last time and the coordinates read this time is determined, and if it is within this range, it is regarded as a valid coordinate.
Expand Down Expand Up @@ -387,7 +396,7 @@ Keep touching the point.
## Draw with touch
If there is no touch for 10 seconds, it will end.
![XPT2046-3](https://user-images.githubusercontent.com/6020549/145127161-c92ab0c9-a5d7-41f0-99c3-8a1981eb1597.JPG)

![XPT2046-4](https://github.com/nopnop2002/esp-idf-ili9340/assets/6020549/fa87dff0-2df3-4a4c-b104-1e71fa27833f)

---

Expand Down
Binary file added icons/facebook.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/git.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/instagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/linkedin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/twitter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/vkontakte.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/whatsapp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/youtube.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes
Binary file added images/facebook.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/git.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/instagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/linkedin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/twitter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/vkontakte.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/whatsapp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/youtube.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
70 changes: 51 additions & 19 deletions main/Kconfig.projbuild
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ menu "TFT Configuration"

config GPIO_RANGE_MAX
int
default 33 if IDF_TARGET_ESP32
default 39 if IDF_TARGET_ESP32
default 46 if IDF_TARGET_ESP32S2
default 48 if IDF_TARGET_ESP32S3
default 19 if IDF_TARGET_ESP32C3
Expand Down Expand Up @@ -152,7 +152,25 @@ menu "TFT Configuration"
help
Change BGR color filter to RGB color filter.

choice XPT2046
prompt "XPT2046 Touch Contoller"
default "XPT2046_DISABLE"
help
Select XPT2046 Touch Contoller.
config XPT2046_DISABLE
bool "Disable Touch Contoller"
config XPT2046_ENABLE_SAME_BUS
bool "Enable Touch Contoller using the same SPI bus as TFT"
help
Enable Touch Contoller using the same SPI bus as TFT.
config XPT2046_ENABLE_DIFF_BUS
bool "Enable Touch Contoller using a different SPI bus than TFT"
help
Enable Touch Contoller using a different SPI bus than TFT.
endchoice

choice SPI_HOST
depends on XPT2046_DISABLE || XPT2046_ENABLE_SAME
prompt "SPI peripheral that controls this bus"
default SPI2_HOST
help
Expand All @@ -168,50 +186,64 @@ menu "TFT Configuration"
USE SPI3_HOST. This is also called VSPI_HOST
endchoice

config XPT2046
bool "Enable XPT2046 Touch Contoller"
default false
help
Enable XPT2046 Touch Contolle.

config MISO_GPIO
depends on XPT2046
int "MISO GPIO number"
config XPT_MISO_GPIO
depends on XPT2046_ENABLE_SAME_BUS || XPT2046_ENABLE_DIFF_BUS
int "XPT2046 MISO GPIO number"
range 0 GPIO_RANGE_MAX
default 19 if IDF_TARGET_ESP32
default 37 if IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32S3
default 6
help
GPIO number (IOxx) to SPI MISO.
GPIO number (IOxx) to XPT2046 MISO.
Some GPIOs are used for other purposes (flash connections, etc.) and cannot be used to MISO.

config XPT_CS_GPIO
depends on XPT2046
depends on XPT2046_ENABLE_SAME_BUS || XPT2046_ENABLE_DIFF_BUS
int "XPT2046 CS GPIO number"
range 0 GPIO_RANGE_MAX
default 22 if IDF_TARGET_ESP32
default 21 if IDF_TARGET_ESP32
default 38 if IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32S3
default 7
help
GPIO number (IOxx) to XPT2046 CS.
Some GPIOs are used for other purposes (flash connections, etc.) and cannot be used to CS.
On the ESP32, GPIOs 35-39 are input-only so cannot be used as outputs.
On the ESP32-S2, GPIO 46 is input-only so cannot be used as outputs.

config XPT_IRQ_GPIO
depends on XPT2046
depends on XPT2046_ENABLE_SAME_BUS || XPT2046_ENABLE_DIFF_BUS
int "XPT2046 IRQ GPIO number"
range 0 GPIO_RANGE_MAX
default 21 if IDF_TARGET_ESP32
default 22 if IDF_TARGET_ESP32
default 39 if IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32S3
default 8
help
GPIO number (IOxx) to XPT2046 IRQ.
Some GPIOs are used for other purposes (flash connections, etc.) and cannot be used to IRQ.
On the ESP32, GPIO12 is used as a bootstrapping pin to select output voltage of an internal regulator which powers the flash chip (VDD_SDIO),

config XPT_SCLK_GPIO
depends on XPT2046_ENABLE_DIFF_BUS
int "XPT2046 SCLK GPIO number"
range 0 GPIO_RANGE_MAX
default 25 if IDF_TARGET_ESP32
default 40 if IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32S3
default 8
help
GPIO number (IOxx) to XPT2046 CLK.
Some GPIOs are used for other purposes (flash connections, etc.) and cannot be used to IRQ.

config XPT_MOSI_GPIO
depends on XPT2046_ENABLE_DIFF_BUS
int "XPT2046 MOSI GPIO number"
range 0 GPIO_RANGE_MAX
default 26 if IDF_TARGET_ESP32
default 41 if IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32S3
default 8
help
GPIO number (IOxx) to XPT2046 CLK.
Some GPIOs are used for other purposes (flash connections, etc.) and cannot be used to IRQ.

config XPT_ACCURACY
depends on XPT2046
depends on XPT2046_ENABLE_SAME_BUS || XPT2046_ENABLE_DIFF_BUS
int "Touch position accuracy"
range 2 10
default 5
Expand All @@ -221,7 +253,7 @@ menu "TFT Configuration"
The higher the value, the less accurate the position, but the less responsive it is.

config XPT_CHECK
depends on XPT2046
depends on XPT2046_ENABLE_SAME_BUS || XPT2046_ENABLE_DIFF_BUS
bool "Enable XPT2046 Touch Check"
default false
help
Expand Down
68 changes: 46 additions & 22 deletions main/ili9340.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@
#define _DEBUG_ 0

#if CONFIG_SPI2_HOST
#define HOST_ID SPI2_HOST
#define TFT_ID SPI2_HOST
#elif CONFIG_SPI3_HOST
#define HOST_ID SPI3_HOST
#define TFT_ID SPI3_HOST
#else
#define HOST_ID SPI2_HOST // When not to use menuconfig
#define TFT_ID SPI2_HOST // When not to use menuconfig
#define XPT_ID SPI3_HOST // When not to use menuconfig
#endif


//static const int GPIO_MOSI = 23;
//static const int GPIO_SCLK = 18;
//static const int TFT_MOSI = 23;
//static const int TFT_SCLK = 18;

static const int SPI_Command_Mode = 0;
static const int SPI_Data_Mode = 1;
Expand All @@ -34,21 +35,23 @@ static const int SPI_Data_Mode = 1;
static const int TFT_Frequency = SPI_MASTER_FREQ_40M;
////static const int TFT_Frequency = SPI_MASTER_FREQ_80M;

#if CONFIG_XPT2046
#if CONFIG_XPT2046_ENABLE_SAME_BUS || CONFIG_XPT2046_ENABLE_DIFF_BUS
static const int XPT_Frequency = 1*1000*1000;
//static const int XPT_Frequency = 2*1000*1000;
//static const int XPT_Frequency = 4*1000*1000;

//#define GPIO_MISO 19
//#define XPT_MISO 19
//#define XPT_CS 4
//#define XPT_IRQ 5
#endif

void spi_master_init(TFT_t * dev, int16_t GPIO_MOSI, int16_t GPIO_SCLK, int16_t TFT_CS, int16_t GPIO_DC, int16_t GPIO_RESET, int16_t GPIO_BL,
int16_t GPIO_MISO, int16_t XPT_CS, int16_t XPT_IRQ)
void spi_master_init(TFT_t * dev, int16_t TFT_MOSI, int16_t TFT_SCLK, int16_t TFT_CS, int16_t GPIO_DC, int16_t GPIO_RESET, int16_t GPIO_BL,
int16_t XPT_MISO, int16_t XPT_CS, int16_t XPT_IRQ, int16_t XPT_SCLK, int16_t XPT_MOSI)
{
esp_err_t ret;

ESP_LOGI(TAG, "TFT_MOSI=%d",TFT_MOSI);
ESP_LOGI(TAG, "TFT_SCLK=%d",TFT_SCLK);
ESP_LOGI(TAG, "TFT_CS=%d",TFT_CS);
gpio_reset_pin( TFT_CS );
gpio_set_direction( TFT_CS, GPIO_MODE_OUTPUT );
Expand Down Expand Up @@ -76,26 +79,26 @@ void spi_master_init(TFT_t * dev, int16_t GPIO_MOSI, int16_t GPIO_SCLK, int16_t
gpio_set_level( GPIO_BL, 0 );
}

#if CONFIG_XPT2046
spi_bus_config_t buscfg = {
.sclk_io_num = GPIO_SCLK,
.mosi_io_num = GPIO_MOSI,
.miso_io_num = GPIO_MISO,
#if CONFIG_XPT2046_ENABLE_SAME_BUS
spi_bus_config_t tft_buscfg = {
.sclk_io_num = TFT_SCLK,
.mosi_io_num = TFT_MOSI,
.miso_io_num = XPT_MISO,
.quadwp_io_num = -1,
.quadhd_io_num = -1
};
#else
spi_bus_config_t buscfg = {
.sclk_io_num = GPIO_SCLK,
.mosi_io_num = GPIO_MOSI,
spi_bus_config_t tft_buscfg = {
.sclk_io_num = TFT_SCLK,
.mosi_io_num = TFT_MOSI,
.miso_io_num = -1,
.quadwp_io_num = -1,
.quadhd_io_num = -1
};
#endif

ret = spi_bus_initialize( HOST_ID, &buscfg, SPI_DMA_CH_AUTO );
ESP_LOGD(TAG, "spi_bus_initialize=%d",ret);
ret = spi_bus_initialize( TFT_ID, &tft_buscfg, SPI_DMA_CH_AUTO );
ESP_LOGI(TAG, "spi_bus_initialize(TFT) ret=%d TFT_ID=%d",ret, TFT_ID);
assert(ret==ESP_OK);

spi_device_interface_config_t tft_devcfg={
Expand All @@ -106,14 +109,31 @@ void spi_master_init(TFT_t * dev, int16_t GPIO_MOSI, int16_t GPIO_SCLK, int16_t
};

spi_device_handle_t tft_handle;
ret = spi_bus_add_device( HOST_ID, &tft_devcfg, &tft_handle);
ret = spi_bus_add_device( TFT_ID, &tft_devcfg, &tft_handle);
ESP_LOGD(TAG, "spi_bus_add_device=%d",ret);
assert(ret==ESP_OK);
dev->_dc = GPIO_DC;
dev->_bl = GPIO_BL;
dev->_TFT_Handle = tft_handle;

#if CONFIG_XPT2046
#if CONFIG_XPT2046_ENABLE_DIFF_BUS
ESP_LOGI(TAG, "XPT_SCLK=%d",XPT_SCLK);
ESP_LOGI(TAG, "XPT_MOSI=%d",XPT_MOSI);
ESP_LOGI(TAG, "XPT_MISO=%d",XPT_MISO);
spi_bus_config_t xpt_buscfg = {
.sclk_io_num = XPT_SCLK,
.mosi_io_num = XPT_MOSI,
.miso_io_num = XPT_MISO,
.quadwp_io_num = -1,
.quadhd_io_num = -1
};

ret = spi_bus_initialize( XPT_ID, &xpt_buscfg, SPI_DMA_CH_AUTO );
ESP_LOGI(TAG, "spi_bus_initialize(XPT) ret=%d XPT_ID=%d",ret, XPT_ID);
assert(ret==ESP_OK);
#endif

#if CONFIG_XPT2046_ENABLE_SAME_BUS || CONFIG_XPT2046_ENABLE_DIFF_BUS
ESP_LOGI(TAG, "XPT_CS=%d",XPT_CS);
gpio_reset_pin( XPT_CS );
gpio_set_direction( XPT_CS, GPIO_MODE_OUTPUT );
Expand All @@ -138,7 +158,11 @@ void spi_master_init(TFT_t * dev, int16_t GPIO_MOSI, int16_t GPIO_SCLK, int16_t
};

spi_device_handle_t xpt_handle;
ret = spi_bus_add_device( HOST_ID, &xpt_devcfg, &xpt_handle);
#if CONFIG_XPT2046_ENABLE_SAME_BUS
ret = spi_bus_add_device( TFT_ID, &xpt_devcfg, &xpt_handle);
#else
ret = spi_bus_add_device( XPT_ID, &xpt_devcfg, &xpt_handle);
#endif
ESP_LOGD(TAG, "spi_bus_add_device=%d",ret);
assert(ret==ESP_OK);
dev->_XPT_Handle = xpt_handle;
Expand Down
4 changes: 2 additions & 2 deletions main/ili9340.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ typedef struct {
int16_t _max_yc; // Maximum y coordinate
} TFT_t;

void spi_master_init(TFT_t * dev, int16_t GPIO_MOSI, int16_t GPIO_SCLK, int16_t TFT_CS, int16_t GPIO_DC, int16_t GPIO_RESET, int16_t GPIO_BL,
int16_t GPIO_MISO, int16_t XPT_CS, int16_t XPT_IRQ);
void spi_master_init(TFT_t * dev, int16_t TFT_MOSI, int16_t TFT_SCLK, int16_t TFT_CS, int16_t GPIO_DC, int16_t GPIO_RESET, int16_t GPIO_BL,
int16_t XPT_MISO, int16_t XPT_CS, int16_t XPT_IRQ, int16_t XPT_SCLK, int16_t XPT_MOSI);
bool spi_master_write_byte(spi_device_handle_t SPIHandle, const uint8_t* Data, size_t DataLength);
bool spi_master_write_comm_byte(TFT_t * dev, uint8_t cmd);
bool spi_master_write_comm_word(TFT_t * dev, uint16_t cmd);
Expand Down
Loading

0 comments on commit 8c0dccd

Please sign in to comment.