ST7789 Driver for esp-idf
The demo video by Dmitry Andreev.
https://www.youtube.com/watch?v=aOyaK0pUiPk&t
ESP-IDF V4.4/V5.x.
git clone -b v4.4 https://github.com/nopnop2002/esp-idf-st7789
cd esp-idf-st7789/
idf.py set-target {esp32/esp32s2/esp32s3/esp32c3}
idf.py menuconfig
idf.py flash
Note for ESP32-S2
The tjpgd library is not included in the ESP32-S2 ROM because the ROM of the ESP32-S2 is small.
Therefore, JPEG files cannot be displayed.
Note for ESP32-C3
For some reason, there are development boards that cannot use GPIO06, GPIO08, GPIO09, GPIO19 for SPI clock pins.
According to the ESP32-C3 specifications, these pins can also be used as SPI clocks.
I used a raw ESP-C3-13 to verify that these pins could be used as SPI clocks.
git clone https://github.com/nopnop2002/esp-idf-st7789
cd esp-idf-st7789/
idf.py set-target {esp32/esp32s2/esp32s3/esp32c2/esp32c3/esp32c6}
idf.py menuconfig
idf.py flash
Note for ESP32-S2/ESP32-C2
The tjpgd library is not included in the ESP32-S2/ESP32-C2 ROM.
However, you can use this IDF component registry.
JPEG files can be displayed.
Note for ESP32-C6
ESP-IDF V5.1 is required when using ESP32-C6.
JPEG file(ESP32/ESP32S2/ESP32C3)
There is 2 kinds of marking.
The pin marking is written as SCL/SDA, so it looks like i2c, but it is SPI.
SCL is SCLK, SDA is MOSI.
Note for display remains black
This module may require a large current to reset.
This may be improved by following the steps below.
- Pull up RES with 100 ohms.
- Write the firmware.
- Press the reset button several times.
According to the ST7789 datasheet, the minimum SPI clock cycle time is 16ns.
Therefore, the maximum SPI clock frequency is 62.5MHz.
The SPI clock frequency used by this project is 20MHz.
Higher SPI clock frequencies can be specified using spi_clock_speed()
.
When using higher SPI clock frequencies, you need to be careful about the length of the wire cable.
//int speed = 40000000; // 40MHz
int speed = 60000000; // 60MHz
spi_clock_speed(speed);
spi_master_init(&dev, CONFIG_MOSI_GPIO, CONFIG_SCLK_GPIO, CONFIG_CS_GPIO, CONFIG_DC_GPIO, CONFIG_RESET_GPIO, CONFIG_BL_GPIO);
lcdInit(&dev, CONFIG_WIDTH, CONFIG_HEIGHT, CONFIG_OFFSETX, CONFIG_OFFSETY);
- Benchmarking using ESP32 & 1.3 inch TFT Without Frame Buffer.
Clock up has little effect.
20MHz | 40MHz | 60MHz | |
---|---|---|---|
FillTest | 1150 | 1090 | 1090 |
ColorBarTest | 50 | 50 | 50 |
ArrowTest | 280 | 250 | 250 |
LineTest | 2190 | 2150 | 2150 |
CircleTest | 1940 | 1910 | 1910 |
RoundRectTest | 1980 | 1940 | 1940 |
DirectionTest | 450 | 430 | 430 |
HorizontalTest | 1070 | 1040 | 1040 |
VerticalTest | 1070 | 1040 | 1040 |
FillRectTest | 190 | 150 | 150 |
ColorTest | 260 | 220 | 220 |
CodeTest | 1070 | 1040 | 1040 |
BMPTest | 7160 | 7130 | 7130 |
JPEGTest | 2550 | 2530 | 2530 |
PNGTest | 2850 | 2830 | 2830 |
QRTest | 220 | 170 | 170 |
- Benchmarking using ESP32 & 1.3 inch TFT With Frame Buffer.
The effect of clocking up varies depending on the test case.
20MHz | 40MHz | 60MHz | |
---|---|---|---|
FillTest | 1150 | 1090 | 1090 |
ColorBarTest | 70 | 50 | 50 |
ArrowTest | 60 | 30 | 30 |
LineTest | 50 | 30 | 30 |
CircleTest | 60 | 30 | 30 |
RoundRectTest | 50 | 30 | 30 |
DirectionTest | 70 | 50 | 50 |
HorizontalTest | 70 | 40 | 40 |
VerticalTest | 70 | 40 | 40 |
FillRectTest | 60 | 30 | 30 |
ColorTest | 60 | 30 | 30 |
CodeTest | 100 | 80 | 80 |
BMPTest | 7490 | 7470 | 7470 |
JPEGTest | 2550 | 2530 | 2530 |
PNGTest | 2850 | 2830 | 2830 |
QRTest | 120 | 100 | 100 |
The ESP32 series has three SPI BUSs.
SPI1_HOST is used for communication with Flash memory.
You can use SPI2_HOST and SPI3_HOST freely.
When you use SDSPI(SD Card via SPI), SDSPI uses SPI2_HOST BUS.
When using this module at the same time as SDSPI or other SPI device using SPI2_HOST, it needs to be changed to SPI3_HOST.
When you don't use SDSPI, both SPI2_HOST and SPI3_HOST will work.
Previously it was called HSPI_HOST / VSPI_HOST, but now it is called SPI2_HOST / SPI3_HOST.
When FrameBuffer is enabled, all output will be stored in the internal FrameBuffer and reflected to the device with lcdDrawFinish
.
If you don't use FrameBuffer, lcdDrawFinish
does nothing.
If your main purpose is to display text, it's well worth using FrameBuffer.
If your main purpose is to display images, there is no value in using FrameBuffer.
Enabling FrameBuffer does not make image display faster.
This is because image analysis takes time.
ESP32C2 has too small memory to use this function.
Note that using FrameBuffer consumes memory.
Memory allocate errors may occur with TFTs with large resolutions.
ESP32S2 has less RAM, but some ESP32S2 have PSRAM.
If your SoC has PSRAM, you can avoid running out of memory by enabling PSRAM.
Benchmarking using ESP32 & 1.3 inch TFT
Disable Frame Buffer | Enable Frame Buffer | |
---|---|---|
FillTest | 1150 | 1150 |
ColorBarTest | 50 | 70 |
ArrowTest | 280 | 60 |
LineTest | 2190 | 50 |
CircleTest | 1940 | 60 |
RoundRectTest | 1980 | 50 |
DirectionTest | 450 | 70 |
HorizontalTest | 1070 | 70 |
VerticalTest | 1070 | 70 |
FillRectTest | 190 | 60 |
ColorTest | 260 | 60 |
CodeTest | 1070 | 100 |
BMPTest | 7160 | 7060 |
JPEGTest | 2550 | 2550 |
PNGTest | 2850 | 2840 |
QRTest | 220 | 120 |
Details can be found here.
The ESP-IDF component includes Tiny JPEG Decompressor.
The document of Tiny JPEG Decompressor is here.
This can reduce the image to 1/2 1/4 1/8.
The ESP-IDF component includes part of the miniz library, such as mz_crc32.
But it doesn't support all of the miniz.
The document of miniz library is here.
And I ported the pngle library from here.
This can reduce the image to any size.
This project uses the following as default fonts:
- font/ILGH16XB.FNT // 8x16Dot Gothic
- font/ILGH24XB.FNT // 12x24Dot Gothic
- font/ILGH32XB.FNT // 16x32Dot Gothic
- font/ILMH16XB.FNT // 8x16Dot Mincyo
- font/ILMH24XB.FNT // 12x24Dot Mincyo
- font/ILMH32XB.FNT // 16x32Dot Mincyo
From 0x00 to 0x7f, the characters image of Alphanumeric are stored.
From 0x80 to 0xff, the characters image of Japanese are stored.
Changing this file will change the font.
You can add your original fonts.
The format of the font file is the FONTX format.
Your font file is put in font directory.
When you build the firmware, the font files are uploaded to the SPIFFS partition.
Please refer this page about FONTX format.
There is a font file editor.
This can be done on Windows 10.
You can create your own font files.
Developer page is here.
step1)
download Font File Editor(FONTX Editor) from here.
step2)
download BDF font file from Internet.
I downloaded from here.
fontxedit.exe can ONLY import Monospaced bitmap fonts file.
Monospaced bitmap fonts can also be downloaded here.
step3)
import the BDF font file into your fontxedit.exe.
this tool can convert from BDF to FONTX.
step5)
check font pattern.
when you have made any changes, press the apply button.
step6)
save as .fnt file from your fontedit.exe.
step7)
upload your font file to $HOME/esp-idf-st7789/fonts directory.
step8)
add font to use
FontxFile fx32L[2];
InitFontx(fx32L,"/spiffs/LATIN32B.FNT",""); // 16x32Dot LATIN
Font file that From 0x80 to 0xff, the characters image of Japanese are stored.
Font file that From 0x80 to 0xff, the characters image of Latin are stored.
u8g2 library contains many BDF fonts.
This is a 24x24 font converted from emoticons21.bdf.
This is a 16x16 font converted from Scroll-o-Sprites.bdf.
step1)
Download WFONTX64.exe from here.
Developer page is here.
step2)
Select ttf font.
Please note that if you select a proportional font, some fonts may not convert correctly.
If you select a proportional font, some fonts will need to be modified using fontxedit.exe.
Monospaced fonts can be converted correctly.
You can find Monospaced fonts here.
step3)
Enter Height, Width, FontX2 name.
Specify half of Height for Width.
Specify your favorite font name in the FontX2 name field using up to 8 characters.
step4)
Specify the file name to save.
step5)
Specify the font style as required.
step6)
Press the RUN button to convert TTF fonts to FONTX format.
step7)
upload your font file to $HOME/esp-idf-st7789/fonts directory.
step8)
add font to use
FontxFile fx16G[2];
FontxFile fx24G[2];
FontxFile fx32G[2];
//InitFontx(fx16G,"/spiffs/ILGH16XB.FNT",""); // 8x16Dot Gothic
//InitFontx(fx24G,"/spiffs/ILGH24XB.FNT",""); // 12x24Dot Gothic
//InitFontx(fx32G,"/spiffs/ILGH32XB.FNT",""); // 16x32Dot Gothic
InitFontx(fx16G,"/spiffs/Gigi16.FNT",""); // 8x16Dot Gigi
InitFontx(fx24G,"/spiffs/Gigi24.FNT",""); // 12x24Dot Gigi
InitFontx(fx32G,"/spiffs/Gigi32.FNT",""); // 16x32Dot Gigi
Change here.
#define RED rgb565(255, 0, 0) // 0xf800
#define GREEN rgb565( 0, 255, 0) // 0x07e0
#define BLUE rgb565( 0, 0, 255) // 0x001f
#define BLACK rgb565( 0, 0, 0) // 0x0000
#define WHITE rgb565(255, 255, 255) // 0xffff
#define GRAY rgb565(128, 128, 128) // 0x8410
#define YELLOW rgb565(255, 255, 0) // 0xFFE0
#define CYAN rgb565( 0, 156, 209) // 0x04FA
#define PURPLE rgb565(128, 0, 128) // 0x8010
You can use these if you need some input.
-
MPR121 Capacitive Touch switch
https://github.com/nopnop2002/esp-idf-mpr121 -
TTP229 Capacitive Touch switch
https://github.com/nopnop2002/esp-idf-ttp229