Esp32 IDF ST7789 Impossible to control 4/5 off the screen ( picture)

Hi ,

I’m trying to use my new custom hardware to display on a 240*320 ST7789V via 4wire SPI.

Hardware connexion are ok, but i’m not able to print more than 4/5 of the screen in height.

I have tried almost all configuration wit all the parametre, but even by rotating the screen, i have the same effet. I can properly display on the full width, but not on the full height.

Then I go back on a really simple example code from esp-idf framework for lvgl just adapted for spi, but same issue. My Version 1 with lvgl and liligo T display 170*320 works perfectly well.

Here is the picture of the problem:


And here is the code :
lvgl_example_main.c (6.8 KB)

Thanks a lot all for your help, i have no more idea for the moment :slight_smile:

Some progress today. I was able to display properly with the project of nopnop2002 ( GitHub - nopnop2002/esp-idf-st7789: ST7789 Driver for esp-idf) so all the hardware is ok.

I tried to implement the lcdInit function of nopnop2002 with the esp_lcd_panel_io_tx_param function of esp-idf. It has an action but doesn’t allow to print properly so i guess the problem comes also from the espidf lvlg librarie .

If you have some tips may be or see smething obvious to do during init sequence don’t hesitate :slight_smile:

Thanks a lot !

1 Like
 * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
 * SPDX-License-Identifier: CC0-1.0

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_timer.h"
#include "esp_lcd_panel_io.h"
#include "esp_lcd_panel_vendor.h"
#include "esp_lcd_panel_ops.h"
#include "driver/gpio.h"
#include "esp_err.h"
#include "esp_log.h"
#include "lvgl.h"

#include "driver/spi_master.h"

static const char *TAG = "example";

//////////////////// Please update the following configuration according to your LCD spec //////////////////////////////
#define EXAMPLE_LCD_PIXEL_CLOCK_HZ     (80 * 1000 * 1000)

// LCD
#define EXAMPLE_PIN_NUM_BK_LIGHT    GPIO_NUM_9         //IO9, PIN17
#define EXAMPLE_PIN_RD              GPIO_NUM_47        //IO10, PIN18
#define EXAMPLE_PIN_NUM_RST         GPIO_NUM_11


// The pixel number in horizontal and vertical
#define EXAMPLE_LCD_H_RES              320
#define EXAMPLE_LCD_V_RES              240
// Bit number used to represent command and parameter
#define EXAMPLE_LCD_CMD_BITS           8
#define EXAMPLE_LCD_PARAM_BITS         8


extern void example_lvgl_demo_ui(lv_obj_t *scr);

static bool example_notify_lvgl_flush_ready(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_io_event_data_t *edata, void *user_ctx)
    lv_disp_drv_t *disp_driver = (lv_disp_drv_t *)user_ctx;
    return false;

static void example_lvgl_flush_cb(lv_disp_drv_t *drv, const lv_area_t *area, lv_color_t *color_map)
    esp_lcd_panel_handle_t panel_handle = (esp_lcd_panel_handle_t) drv->user_data;
    int offsetx1 = area->x1;
    int offsetx2 = area->x2;
    int offsety1 = area->y1;
    int offsety2 = area->y2;
    // copy a buffer's content to a specific area of the display
    esp_lcd_panel_draw_bitmap(panel_handle, offsetx1, offsety1, offsetx2 + 1, offsety2 + 1, color_map);

static void example_increase_lvgl_tick(void *arg)
    /* Tell LVGL how many milliseconds has elapsed */

void app_main(void)
    static lv_disp_draw_buf_t disp_buf; // contains internal graphic buffer(s) called draw buffer(s)
    static lv_disp_drv_t disp_drv;      // contains callback functions

    ESP_LOGI(TAG, "Turn off LCD backlight");
    gpio_config_t bk_gpio_config = {
        .mode = GPIO_MODE_OUTPUT,
        .pin_bit_mask = 1ULL << EXAMPLE_PIN_NUM_BK_LIGHT

// 1.Create an SPI bus

    ESP_LOGI(TAG, "Initialize LCD SPI bus");

    spi_bus_config_t buscfg = {
        .sclk_io_num = EXAMPLE_PIN_NUM_SCLK,
        .mosi_io_num = EXAMPLE_PIN_NUM_MOSI,
        .miso_io_num = EXAMPLE_PIN_NUM_MISO,
        .quadwp_io_num = -1, // Quad SPI LCD driver is not yet supported
        .quadhd_io_num = -1, // Quad SPI LCD driver is not yet supported
        .data4_io_num = -1,
        .data5_io_num = -1,
        .data6_io_num = -1,
        .data7_io_num = -1,
        .max_transfer_sz = EXAMPLE_LCD_H_RES * EXAMPLE_LCD_V_RES * 2 / 10, // transfer 80 lines of pixels (assume pixel is RGB565) at most in one SPI transaction
    ESP_ERROR_CHECK(spi_bus_initialize(LCD_HOST, &buscfg, SPI_DMA_CH_AUTO)); // Enable the DMA feature

    // 2. LCD IO device handle 
    esp_lcd_panel_io_handle_t io_handle = NULL;
    esp_lcd_panel_io_spi_config_t io_config = {
        .dc_gpio_num = 47,//EXAMPLE_PIN_NUM_LCD_DC,
        .cs_gpio_num = EXAMPLE_PIN_NUM_CS,
        .pclk_hz = EXAMPLE_LCD_PIXEL_CLOCK_HZ,
        .lcd_cmd_bits = EXAMPLE_LCD_CMD_BITS,
        .lcd_param_bits = EXAMPLE_LCD_PARAM_BITS,
        .spi_mode = 0,
        .trans_queue_depth = 10,
    // Attach the LCD to the SPI bus
    ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)LCD_HOST, &io_config, &io_handle));

    ESP_LOGI(TAG, "Install LCD driver of st7789");
    esp_lcd_panel_handle_t panel_handle = NULL;
    esp_lcd_panel_dev_config_t panel_config = {
        .reset_gpio_num = EXAMPLE_PIN_NUM_RST,
        .color_space = ESP_LCD_COLOR_SPACE_RGB,
        .bits_per_pixel = 16,
    ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(io_handle, &panel_config, &panel_handle));

    esp_lcd_panel_invert_color(panel_handle, true);
    // the gap is LCD panel specific, even panels with the same driver IC, can have different gap value
    esp_lcd_panel_set_gap(panel_handle, 0, 0);

    ESP_LOGI(TAG, "Turn on LCD backlight");

    ESP_LOGI(TAG, "Initialize LVGL library");
    // alloc draw buffers used by LVGL
    // it's recommended to choose the size of the draw buffer(s) to be at least 1/10 screen sized
    lv_color_t *buf1 = heap_caps_malloc(EXAMPLE_LCD_H_RES * EXAMPLE_LCD_V_RES * 2 / 10, MALLOC_CAP_DMA);
    lv_color_t *buf2 = heap_caps_malloc(EXAMPLE_LCD_H_RES * EXAMPLE_LCD_V_RES * 2 / 10, MALLOC_CAP_DMA);
    // initialize LVGL draw buffers
    lv_disp_draw_buf_init(&disp_buf, buf1, buf2, EXAMPLE_LCD_H_RES * EXAMPLE_LCD_V_RES * 2 / 10);

    ESP_LOGI(TAG, "Register display driver to LVGL");
    disp_drv.hor_res = EXAMPLE_LCD_H_RES;
    disp_drv.ver_res = EXAMPLE_LCD_V_RES;
    disp_drv.flush_cb = example_lvgl_flush_cb;
    disp_drv.draw_buf = &disp_buf;
    disp_drv.user_data = panel_handle;
    lv_disp_t *disp = lv_disp_drv_register(&disp_drv);

    ESP_LOGI(TAG, "Install LVGL tick timer");
    // Tick interface for LVGL (using esp_timer to generate 2ms periodic event)
    const esp_timer_create_args_t lvgl_tick_timer_args = {
        .callback = &example_increase_lvgl_tick,
        .name = "lvgl_tick"
    esp_timer_handle_t lvgl_tick_timer = NULL;
    ESP_ERROR_CHECK(esp_timer_create(&lvgl_tick_timer_args, &lvgl_tick_timer));
    ESP_ERROR_CHECK(esp_timer_start_periodic(lvgl_tick_timer, EXAMPLE_LVGL_TICK_PERIOD_MS * 1000));

    ESP_LOGI(TAG, "Display LVGL animation");
    lv_obj_t *scr = lv_disp_get_scr_act(disp);

    while (1) {
        // raise the task priority of LVGL and/or reduce the handler period can improve the performance
        // The task running lv_timer_handler should have lower priority than that running `lv_tick_inc`

1 Like

I tried running my display at 80MHz but it wouldn’t work. Work only at a frequency of 40 MHz and below.

1 Like

Thanks hdschlosser, it was the part that was missing. After adapting from 8bit to spi i forgot to copy this part of the config.

Thanks a lot.

The screen is a bit slower than 8bit version, i will try to improve it further may be…

I believe with the SPI version and that display you are able to set sio_mode where mosi and miso are able to both run at transfer lines. this would effectively double the output speed. You have to lookup what the cmd and parameter are to set the display IC into that mode.

also if possible I highly recommend using the hardware SPI pins. This is so the pins don’t have to be routed through the GPIO matrix. according to the documentation for the ESP32 if the SPI pins have to get routed through the matrix the maximum speed is going to be 26.6mhz where as if they don’t you can turn the speed up to 80mhz. in your code I believe you had it set to 20mhz.