Home

Awesome

See https://github.com/russhughes/s3lcd for a much faster Framebuffered version of the driver using the ESP_LCD esp-idf component. It's a beta release and has minor differences from this driver.

ESP_LCD MicroPython driver for the TTGO T-Display-S3 st7789 display

Warning: This is a work in progress and may contain bugs and/or incorrect documentation.

This driver is based on devbis' st7789_mpy driver. I modified the original driver for one of my projects to add:

Included are 12 bitmap fonts derived from classic pc text mode fonts, 26 Hershey vector fonts and several example programs for different devices.

Pre-compiled firmware

The firmware directory contains pre-compiled MicroPython v1.20.0 firmware compiled using ESP IDF v4.4.4 The firmware includes the st7789 C driver and several frozen python font files. See the README.md file in the fonts folder for more information about the font files.

Thanks go out to:

-- Russ

Overview

This is a driver for MicroPython to handle cheap displays based on the ST7789 chip. The driver is written in C.

<p align="center"> <img src="https://raw.githubusercontent.com/russhughes/st7789_mpy/master/docs/ST7789.jpg" alt="ST7789 display photo"/> </p>

Setup MicroPython Build Environment in Ubuntu 20.04.2

See the MicroPython README.md if you run into any build issues not directly related to the st7789 driver. The recommended MicroPython build instructions may have changed.

Update and upgrade Ubuntu using apt-get if you are using a new install of Ubuntu or the Windows Subsystem for Linux.

sudo apt-get -y update
sudo apt-get -y upgrade

Use apt-get to install the required build tools.

sudo apt-get -y install build-essential libffi-dev git pkg-config cmake virtualenv python3-pip python3-virtualenv

Install a compatible esp-idf SDK

The MicroPython README.md states: "The ESP-IDF changes quickly, and MicroPython only supports certain versions. I have had good luck using IDF v4.4.3

Clone the esp-idf SDK repo -- this usually takes several minutes.

git clone -b v4.4.3 --recursive https://github.com/espressif/esp-idf.git
cd esp-idf/
git pull

If you already have a copy of the IDF, you can checkout a version compatible with MicroPython and update the submodules using:

$ cd esp-idf
$ git checkout v4.4.3
$ git submodule update --init --recursive

Install the esp-idf SDK.

./install.sh

Source the esp-idf export.sh script to set the required environment variables. You must source the file and not run it using ./export.sh. You will need to source this file before compiling MicroPython.

source export.sh
cd ..

Clone the MicroPython repo.

git clone https://github.com/micropython/micropython.git

Clone the st7789 driver repo.

git clone https://github.com/russhughes/st7789s3_mpy.git

Update the git submodules and compile the MicroPython cross-compiler

cd micropython/
git submodule update --init
cd mpy-cross/
make
cd ..
cd ports/esp32

Copy any .py files you want to include in the firmware as frozen python modules to the modules subdirectory in ports/esp32. Be aware there is a limit to the flash space available. You will know you have exceeded this limit if you receive an error message saying the code won't fit in the partition or if your firmware continuously reboots with an error.

For example:

cp ../../../st7789s3_mpy/fonts/bitmap/vga1_16x16.py modules
cp ../../../st7789s3_mpy/fonts/truetype/NotoSans_32.py modules
cp ../../../st7789s3_mpy/fonts/vector/scripts.py modules

Build the MicroPython firmware with the driver and frozen .py files in the modules directory. If you did not add any .py files to the modules directory, you can leave out the FROZEN_MANIFEST and FROZEN_MPY_DIR settings.

make USER_C_MODULES=../../../../st7789s3_mpy/st7789/micropython.cmake FROZEN_MANIFEST="" FROZEN_MPY_DIR=$UPYDIR/modules

Erase and flash the firmware to your device. Set PORT= to the ESP32's usb serial port. I could not get the USB serial port to work under the Windows Subsystem (WSL2) for Linux. If you have the same issue, you can copy the firmware.bin file and use the Windows esptool.py to flash your device.

make USER_C_MODULES=../../../../st7789s3_mpy/st7789/micropython.cmake PORT=/dev/ttyUSB0 erase
make USER_C_MODULES=../../../../st7789s3_mpy/st7789/micropython.cmake PORT=/dev/ttyUSB0 deploy

The firmware.bin file will be in the build-GENERIC directory. To flash using the python esptool.py utility. Use pip3 to install the esptool if it's not already installed.

pip3 install esptool

Set PORT= to the ESP32's USB serial port

esptool.py --port COM3 erase_flash
esptool.py --chip esp32 --port COM3 write_flash -z 0x0 firmware.bin

CMake building instructions for MicroPython 1.14 and later

for ESP32:

$ cd micropython/ports/esp32

And then compile the module with specified USER_C_MODULES dir.

$ make USER_C_MODULES=../../../../st7789s3_mpy/st7789/micropython.cmake

Methods

Note: Optional Parameters are shown enclosed in '{' and '}' characters.

The module exposes predefined colors: BLACK, BLUE, RED, GREEN, CYAN, MAGENTA, YELLOW, and WHITE

Scrolling

The st7789 display controller contains a 240 by 320-pixel frame buffer used to store the pixels for the display. For scrolling, the frame buffer consists of three separate areas; The (tfa) top fixed area, the (height) scrolling area, and the (bfa) bottom fixed area. The tfa is the upper portion of the frame buffer in pixels not to scroll. The height is the center portion of the frame buffer in pixels to scroll. The bfa is the lower portion of the frame buffer in pixels not to scroll. These values control the ability to scroll the entire or a part of the display.

For displays that are 320 pixels high, setting the tfa to 0, height to 320, and bfa to 0 will allow scrolling of the entire display. You can set the tfa and bfa to a non-zero value to scroll a portion of the display. tfa + height + bfa = should equal 320, otherwise the scrolling mode is undefined.

Displays less than 320 pixels high, the tfa, height, and bfa will need to be adjusted to compensate for the smaller LCD panel. The actual values will vary depending on the configuration of the LCD panel. For example, scrolling the entire 135x240 TTGO T-Display requires a tfa value of 40, height value of 240, and bfa value of 40 (40+240+40=320) because the T-Display LCD shows 240 rows starting at the 40th row of the frame buffer, leaving the last 40 rows of the frame buffer undisplayed.

Other displays like the Waveshare Pico LCD 1.3 inch 240x240 display require the tfa set to 0, height set to 240, and bfa set to 80 (0+240+80=320) to scroll the entire display. The Pico LCD 1.3 shows 240 rows starting at the 0th row of the frame buffer, leaving the last 80 rows of the frame buffer undisplayed.

The vscsad method sets the (VSSA) Vertical Scroll Start Address. The VSSA sets the line in the frame buffer that will be the first line after the tfa.

The ST7789 datasheet warns:

The value of the vertical scrolling start address is absolute (with reference to the frame memory),
it must not enter the fixed area (defined by Vertical Scrolling Definition, otherwise undesirable
image will be displayed on the panel.

Helper functions