ESP32 C3 supermini - ST7789 - TFT_eSPI

After several days and numerous attempts I finally managed to get the ST7789 1.14" display working.
These are the versions I used.

  • Arduino IDE: 2.3.6
  • TFT_eSPI (bodmer) : 2.5.43
  • ESP32 board (expressif): 3.3.4
  • To compile the sketch use the Board "ESP32C3 dev module"

Some information:
The ESP32 C3 Supermini module differs from other modules in that it has a limited number of GPIOs (for space reasons). This means it has only one way to declare the SPI channel (VSPI = HSPI), which is SPI 2.
The ST7789 monitor used has 8 pins (no MISO) so can work only in Half Duplex:

TFT PIN <-------------------> ESP32C3 PIN
GND 2 (gnd)
VCC 3 (3,3v)
SCL (CLK) 4
SDA (MOSI) 6
RES (RESET) 0 (or any free GPIO)
DC 1 (or any free GPIO)
CS (CHIP SELECT) 7
BLK 3 (3,3v always backlight on) or not connect (its the same)

==================================================================

Replace the User_setup.h (on TFT_eSPI folder) with the follow:

// configuration for ESP32 C3 Super Mini
#define USER_SETUP_INFO "User_Setup"
//
#define ST7789_DRIVER
//
#define TFT_RGB_ORDER TFT_BGR // the label monitor show RGB but i dont know why.
#define TFT_WIDTH  135
#define TFT_HEIGHT 240
#define TFT_MISO -1 // mean not present
#define TFT_MOSI 6
#define TFT_SCLK 4
#define TFT_CS   7 // Chip select control pin
#define TFT_DC   1 // Data Command control pin
#define TFT_RST  0 // Reset pin (could connect to RST pin)
//
#define LOAD_GLCD // Font 1. Original Adafruit 8 pixel font needs \~1820 bytes in FLASH
#define LOAD_FONT2 // Font 2. Small 16 pixel high font, needs \~3534 bytes in FLASH, 96 characters
#define LOAD_FONT4 // Font 4. Medium 26 pixel high font, needs \~5848 bytes in FLASH, 96 characters
#define LOAD_FONT6 // Font 6. Large 48 pixel font, needs \~2666 bytes in FLASH, only characters 1234567890:-.apm
#define LOAD_FONT7 // Font 7. 7 segment 48 pixel font, needs \~2438 bytes in FLASH, only characters 1234567890:-.
#define LOAD_FONT8 // Font 8. Large 75 pixel font needs \~3256 bytes in FLASH, only characters 1234567890:-.
#define LOAD_GFXFF // FreeFonts. Include access to the 48 Adafruit_GFX free fonts FF1 to FF48 and custom fonts
#define SMOOTH_FONT
//
#define SPI_FREQUENCY       40000000
#define SPI_READ_FREQUENCY  20000000

============================================================

SOLUTION 1 (EASY Solution):

install the - ESP32 board (expressif): 2.0.14

============================================================

SOLUTION 2 (to leave the current ESP32 board version 3.3.4):

For anyone with even a basic understanding of file editing, these are the most important changes in the file

=========================================================:

// ESP32 specific SPI port selection - only SPI2_HOST available on C3
#define SPI_PORT 2

=========================================================

////////////////////////////////////////////////////////////////////////////////////////
// Make sure SPI default pins are assigned if not specified by user or set to -1
////////////////////////////////////////////////////////////////////////////////////////
#if !defined (TFT_PARALLEL_8_BIT)
#define TFT_MOSI 6
#define TFT_SCLK 4
#endif

===========================================================

REMOVE THE PART WHERE IF MISO = -1 the MISO = MOSI (that is totally wrong)

==========================================================

DOWN BELOW THE FILE (for all that not like lost time),

replace the TFT_eSPI_ESP32_C3.h (on TFT_eSPI/Processor folder) with the follow:

////////////////////////////////////////////////////
    // TFT_eSPI driver functions for ESP32 processors //
    ////////////////////////////////////////////////////

// Temporarily a separate file to TFT_eSPI_ESP32.h until board package low level API stabilises

#ifndef *TFT_eSPI_ESP32H*
#define *TFT_eSPI_ESP32H*

#if !defined(DISABLE_ALL_LIBRARY_WARNINGS)
#warning >>>>------>> DMA is not supported on the ESP32 C3 (possible future update)
#endif

// Processor ID reported by getSetup()
#define PROCESSOR_ID 0x32

// Include processor specific header
#include "soc/spi_reg.h"
#include "driver/spi_master.h"
#include "hal/gpio_ll.h"

#if !defined(CONFIG_IDF_TARGET_ESP32C3) && !defined(CONFIG_IDF_TARGET_ESP32S2) && !defined(CONFIG_IDF_TARGET_ESP32)
#define CONFIG_IDF_TARGET_ESP32
#endif

#ifndef VSPI
#define VSPI FSPI
#endif

// Fix IDF problems with ESP32C3
#if CONFIG_IDF_TARGET_ESP32C3
// Fix ESP32C3 IDF bug for missing definition (VSPI/FSPI only tested at the moment)
#ifndef REG_SPI_BASE
#define REG_SPI_BASE(i) DR_REG_SPI2_BASE
#endif

// Fix ESP32C3 IDF bug for name change
#ifndef SPI_MOSI_DLEN_REG
#define SPI_MOSI_DLEN_REG(x) SPI_MS_DLEN_REG(x)
#endif
#endif

// SUPPORT_TRANSACTIONS is mandatory for ESP32 so the hal mutex is toggled
#if !defined (SUPPORT_TRANSACTIONS)
#define SUPPORT_TRANSACTIONS
#endif

// ESP32 specific SPI port selection - only SPI2_HOST available on C3
#define SPI_PORT 2

#ifdef RPI_DISPLAY_TYPE
#define CMD_BITS (16-1)
#else
#define CMD_BITS (8-1)
#endif

// Initialise processor specific SPI functions, used by init()
#define INIT_TFT_DATA_BUS // Not used

// Define a generic flag for 8-bit parallel
#if defined (ESP32_PARALLEL) // Specific to ESP32 for backwards compatibility
#if !defined (TFT_PARALLEL_8_BIT)
#define TFT_PARALLEL_8_BIT // Generic parallel flag
#endif
#endif

// Ensure ESP32 specific flag is defined for 8-bit parallel
#if defined (TFT_PARALLEL_8_BIT)
#if !defined (ESP32_PARALLEL)
#define ESP32_PARALLEL
#endif
#endif

// Processor specific code used by SPI bus transaction startWrite and endWrite functions
#if !defined (ESP32_PARALLEL)
#if (TFT_SPI_MODE == SPI_MODE1) || (TFT_SPI_MODE == SPI_MODE2)
#define SET_BUS_WRITE_MODE \*\_spi_user = SPI_USR_MOSI | SPI_CK_OUT_EDGE
#define SET_BUS_READ_MODE  \*\_spi_user = SPI_USR_MOSI | SPI_USR_MISO | SPI_DOUTDIN | SPI_CK_OUT_EDGE
#else
#define SET_BUS_WRITE_MODE \*\_spi_user = SPI_USR_MOSI
#define SET_BUS_READ_MODE  \*\_spi_user = SPI_USR_MOSI | SPI_USR_MISO | SPI_DOUTDIN
#endif
#else
// Not applicable to parallel bus
#define SET_BUS_WRITE_MODE
#define SET_BUS_READ_MODE
#endif

// Code to check if DMA is busy, used by SPI bus transaction transaction and endWrite functions
#if !defined(TFT_PARALLEL_8_BIT) && !defined(SPI_18BIT_DRIVER)
#define ESP32_DMA
// Code to check if DMA is busy, used by SPI DMA + transaction + endWrite functions
#define DMA_BUSY_CHECK  dmaWait()
#else
#define DMA_BUSY_CHECK
#endif

#if defined(TFT_PARALLEL_8_BIT)
#define SPI_BUSY_CHECK
#else
#define SPI_BUSY_CHECK while (\*\_spi_cmd&SPI_USR)
#endif

// If smooth font is used then it is likely SPIFFS will be needed
#ifdef SMOOTH_FONT
// Call up the SPIFFS (SPI FLASH Filing System) for the anti-aliased fonts
#define FS_NO_GLOBALS
#include <FS.h>
#include "SPIFFS.h" // ESP32 only
#define FONT_FS_AVAILABLE
#endif

////////////////////////////////////////////////////////////////////////////////////////
// Define the DC (TFT Data/Command or Register Select (RS))pin drive code
////////////////////////////////////////////////////////////////////////////////////////
#ifndef TFT_DC
#define DC_C // No macro allocated so it generates no code
#define DC_D // No macro allocated so it generates no code
#else
#if defined (TFT_PARALLEL_8_BIT)
// TFT_DC, by design, must be in range 0-31 for single register parallel write
#if (TFT_DC >= 0) &&  (TFT_DC < 32)
#define DC_C GPIO.out_w1tc.val = (1 << TFT_DC)
#define DC_D GPIO.out_w1ts.val = (1 << TFT_DC)
#elif (TFT_DC >= 32)
#define DC_C GPIO.out_w1tc.val = (1 << (TFT_DC- 32))
#define DC_D GPIO.out_w1ts.val = (1 << (TFT_DC- 32))
#else
#define DC_C
#define DC_D
#endif
#else
#if (TFT_DC >= 32)
#ifdef RPI_DISPLAY_TYPE  // RPi displays need a slower DC change
#define DC_C GPIO.out_w1ts.val = (1 << (TFT_DC - 32)); 
GPIO.out_w1tc.val = (1 << (TFT_DC - 32))
#define DC_D GPIO.out_w1tc.val = (1 << (TFT_DC - 32)); 
GPIO.out_w1ts.val = (1 << (TFT_DC - 32))
#else
#define DC_C GPIO.out_w1tc.val = (1 << (TFT_DC - 32))//;GPIO.out_w1tc.val = (1 << (TFT_DC - 32))
#define DC_D GPIO.out_w1ts.val = (1 << (TFT_DC - 32))//;GPIO.out_w1ts.val = (1 << (TFT_DC - 32))
#endif
#elif (TFT_DC >= 0)
#if defined (RPI_DISPLAY_TYPE)
#if defined (ILI9486_DRIVER)
// RPi ILI9486 display needs a slower DC change
#define DC_C GPIO.out_w1tc.val = (1 << TFT_DC); 
GPIO.out_w1tc.val = (1 << TFT_DC)
#define DC_D GPIO.out_w1tc.val = (1 << TFT_DC); 
GPIO.out_w1ts.val = (1 << TFT_DC)
#else
// Other RPi displays need a slower C->D change
#define DC_C GPIO.out_w1tc.val = (1 << TFT_DC)
#define DC_D GPIO.out_w1tc.val = (1 << TFT_DC); 
GPIO.out_w1ts.val = (1 << TFT_DC)
#endif
#else
#define DC_C GPIO.out_w1tc.val = (1 << TFT_DC)//;GPIO.out_w1tc.val = (1 << TFT_DC)
#define DC_D GPIO.out_w1ts.val = (1 << TFT_DC)//;GPIO.out_w1ts.val = (1 << TFT_DC)
#endif
#else
#define DC_C
#define DC_D
#endif
#endif
#endif

////////////////////////////////////////////////////////////////////////////////////////
// Define the CS (TFT chip select) pin drive code
////////////////////////////////////////////////////////////////////////////////////////
#ifndef TFT_CS
#define TFT_CS -1  // Keep DMA code happy
#define CS_L       // No macro allocated so it generates no code
#define CS_H       // No macro allocated so it generates no code
#else
#if defined (TFT_PARALLEL_8_BIT)
#if TFT_CS >= 32
#define CS_L GPIO.out_w1tc.val = (1 << (TFT_CS - 32))
#define CS_H GPIO.out_w1ts.val = (1 << (TFT_CS - 32))
#elif TFT_CS >= 0
#define CS_L GPIO.out_w1tc.val = (1 << TFT_CS)
#define CS_H GPIO.out_w1ts.val = (1 << TFT_CS)
#else
#define CS_L
#define CS_H
#endif
#else
#if (TFT_CS >= 32)
#ifdef RPI_DISPLAY_TYPE  // RPi display needs a slower CS change
#define CS_L GPIO.out_w1ts.val = (1 << (TFT_CS - 32)); 
GPIO.out_w1tc.val = (1 << (TFT_CS - 32))
#define CS_H GPIO.out_w1tc.val = (1 << (TFT_CS - 32)); 
GPIO.out_w1ts.val = (1 << (TFT_CS - 32))
#else
#define CS_L GPIO.out_w1tc.val = (1 << (TFT_CS - 32)); GPIO.out_w1tc.val = (1 << (TFT_CS - 32))
#define CS_H GPIO.out_w1ts.val = (1 << (TFT_CS - 32))//;GPIO.out_w1ts.val = (1 << (TFT_CS - 32))
#endif
#elif (TFT_CS >= 0)
#ifdef RPI_DISPLAY_TYPE  // RPi display needs a slower CS change
#define CS_L GPIO.out_w1ts.val = (1 << TFT_CS); GPIO.out_w1tc.val = (1 << TFT_CS)
#define CS_H GPIO.out_w1tc.val = (1 << TFT_CS); GPIO.out_w1ts.val = (1 << TFT_CS)
#else
#define CS_L GPIO.out_w1tc.val = (1 << TFT_CS); GPIO.out_w1tc.val = (1 << TFT_CS)
#define CS_H GPIO.out_w1ts.val = (1 << TFT_CS)//;GPIO.out_w1ts.val = (1 << TFT_CS)
#endif
#else
#define CS_L
#define CS_H
#endif
#endif
#endif

////////////////////////////////////////////////////////////////////////////////////////
// Define the WR (TFT Write) pin drive code
////////////////////////////////////////////////////////////////////////////////////////
#if defined (TFT_WR)
#if (TFT_WR >= 32)
// Note: it will be \~1.25x faster if the TFT_WR pin uses a GPIO pin lower than 32
#define WR_L GPIO.out_w1tc.val = (1 << (TFT_WR - 32))
#define WR_H GPIO.out_w1ts.val = (1 << (TFT_WR - 32))
#elif (TFT_WR >= 0)
// TFT_WR, for best performance, should be in range 0-31 for single register parallel write
#define WR_L GPIO.out_w1tc.val = (1 << TFT_WR)
#define WR_H GPIO.out_w1ts.val = (1 << TFT_WR)
#else
#define WR_L
#define WR_H
#endif
#else
#define WR_L
#define WR_H
#endif

////////////////////////////////////////////////////////////////////////////////////////
// Define the touch screen chip select pin drive code
////////////////////////////////////////////////////////////////////////////////////////
#ifndef TOUCH_CS
#define T_CS_L // No macro allocated so it generates no code
#define T_CS_H // No macro allocated so it generates no code
#else // XPT2046 is slow, so use slower digitalWrite here
#define T_CS_L digitalWrite(TOUCH_CS, LOW)
#define T_CS_H digitalWrite(TOUCH_CS, HIGH)
#endif

////////////////////////////////////////////////////////////////////////////////////////
// Make sure SPI default pins are assigned if not specified by user or set to -1
////////////////////////////////////////////////////////////////////////////////////////
#if !defined (TFT_PARALLEL_8_BIT)
#define TFT_MOSI 6
#define TFT_SCLK 4
#endif

////////////////////////////////////////////////////////////////////////////////////////
// Macros for all other SPI displays
////////////////////////////////////////////////////////////////////////////////////////
/\* Old macros
// ESP32 low level SPI writes for 8, 16 and 32-bit values
// to avoid the function call overhead
#define TFT_WRITE_BITS(D, B) 
WRITE_PERI_REG(SPI_MOSI_DLEN_REG(SPI_PORT), B-1); 
WRITE_PERI_REG(SPI_W0_REG(SPI_PORT), D); 
SET_PERI_REG_MASK(SPI_CMD_REG(SPI_PORT), SPI_USR); 
while (READ_PERI_REG(SPI_CMD_REG(SPI_PORT))&SPI_USR);

// Write 8 bits
#define tft_Write_8(C) TFT_WRITE_BITS(C, 8)

// Write 16 bits with corrected endianness for 16-bit colours
#define tft_Write_16(C) TFT_WRITE_BITS((C)<<8 | (C)>>8, 16)

// Write 16 bits
#define tft_Write_16S(C) TFT_WRITE_BITS(C, 16)

// Write 32 bits
#define tft_Write_32(C) TFT_WRITE_BITS(C, 32)

// Write two address coordinates
#define tft_Write_32C(C,D) TFT_WRITE_BITS((uint16_t)((D)<<8 | (D)>>8)<<16 | (uint16_t)((C)<<8 | (C)>>8), 32)

// Write same value twice
#define tft_Write_32D(C) TFT_WRITE_BITS((uint16_t)((C)<<8 | (C)>>8)<<16 | (uint16_t)((C)<<8 | (C)>>8), 32)
//*/
//* Replacement slimmer macros
#if !defined(CONFIG_IDF_TARGET_ESP32C3)
#define TFT_WRITE_BITS(D, B) \*\_spi_mosi_dlen = B-1;  
\*\_spi_w = D;              
*\_spi_cmd = SPI_USR;      
while (*\_spi_cmd & SPI_USR);
#else
#define TFT_WRITE_BITS(D, B) \*\_spi_mosi_dlen = B-1;  
\*\_spi_w = D;              
*\_spi_cmd = SPI_UPDATE;   
while (*\_spi_cmd & SPI_UPDATE);  
*\_spi_cmd = SPI_USR;      
while (*\_spi_cmd & SPI_USR);
#endif
// Write 8 bits
#define tft_Write_8(C) TFT_WRITE_BITS(C, 8)

// Write 16 bits with corrected endianness for 16-bit colours
#define tft_Write_16(C) TFT_WRITE_BITS((C)<<8 | (C)>>8, 16)

// Future option for transfer without wait
#if !defined(CONFIG_IDF_TARGET_ESP32C3)
#define tft_Write_16N(C) \*\_spi_mosi_dlen = 16-1;    
\*\_spi_w = ((C)<<8 | (C)>>8); 
\*\_spi_cmd = SPI_USR;
#else
#define tft_Write_16N(C) \*\_spi_mosi_dlen = 16-1;    
\*\_spi_w = ((C)<<8 | (C)>>8); 
*\_spi_cmd = SPI_UPDATE;      
while (*\_spi_cmd & SPI_UPDATE);     
\*\_spi_cmd = SPI_USR;
#endif

// Write 16 bits
#define tft_Write_16S(C) TFT_WRITE_BITS(C, 16)

// Write 32 bits
#define tft_Write_32(C) TFT_WRITE_BITS(C, 32)

// Write two address coordinates
#define tft_Write_32C(C,D)  TFT_WRITE_BITS((uint16_t)((D)<<8 | (D)>>8)<<16 | (uint16_t)((C)<<8 | (C)>>8), 32)

// Write same value twice
#define tft_Write_32D(C) TFT_WRITE_BITS((uint16_t)((C)<<8 | (C)>>8)<<16 | (uint16_t)((C)<<8 | (C)>>8), 32)

//\*/

#ifndef tft_Write_16N
#define tft_Write_16N tft_Write_16
#endif

////////////////////////////////////////////////////////////////////////////////////////
// Macros to read from display using SPI or software SPI
////////////////////////////////////////////////////////////////////////////////////////
#if !defined (TFT_PARALLEL_8_BIT)
// Read from display using SPI or software SPI
// Use a SPI read transfer
#define tft_Read_8() spi.transfer(0)
#endif

// Concatenate a byte sequence A,B,C,D to CDAB, P is a uint8_t pointer
#define DAT8TO32(P) ( (uint32_t)P\[0\]<<8 | P\[1\] | P\[2\]<<24 | P\[3\]<<16 )

#endif // Header end

============================================================

I hope that this can help you, but consider that this is only my expeerience, maybe for

other configurations this guide could be not work.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.