ESP32S3+HX8347+RGB运行LVGL例程

        之前用3线SPI驱动的HX8347屏其实是一个RGB屏,SPI只是用来给RGB屏幕的做配置的,当然也可以用来驱动屏幕,但是3线SPI驱动能力终究有限。本文谈一下用RGB方式来驱动。

        RGB接线比较多,为此做了个转接板:

ESP32S3+HX8347+RGB运行LVGL例程_第1张图片 ESP32S3+HX8347+RGB运行LVGL例程_第2张图片

一、源码

1、screen_rgb_spi.c用于SPI初始化屏幕

screen_rgb_spi.h

#ifndef SCREEN_RGB_SPI_H_
#define SCREEN_RGB_SPI_H_

void rgb_spi_init(void) ;

#endif

 screen_rgb_spi.c

#include 
#include 
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "soc/soc_caps.h"
#include "driver/gpio.h"
#include "driver/spi_master.h"
#include "esp_log.h"
#include "screen_rgb_spi.h"

static const char *TAG = "SCREN_REG_SPI";

#define LCD_HOST    SPI2_HOST

#define PIN_NUM_MISO 4 //37
#define PIN_NUM_MOSI 5 //36
#define PIN_NUM_CLK  3 //38
#define PIN_NUM_CS   6 //35

#define PIN_NUM_DC   -1
#define PIN_NUM_RST  2 //39
#define PIN_NUM_BCKL 1


static spi_device_handle_t g_screen_spi;

static int lcd_cs_port(int status)
{
   if (status) {
       gpio_set_level(PIN_NUM_CS, 1);
   } else {
       gpio_set_level(PIN_NUM_CS, 0);
   }
    return 0;
}

static void spi_writeData(spi_device_handle_t spi,uint8_t data)
{
    esp_err_t ret;
    spi_transaction_t t;

    memset(&t, 0, sizeof(t));
    t.length = 8;
    t.tx_buffer = &data;
    ret = spi_device_polling_transmit(spi, &t);  //Transmit!
    assert(ret==ESP_OK);
}


static void lcd_cmd(spi_device_handle_t spi, const uint8_t data) {
    lcd_cs_port(0);
    spi_writeData(spi,0x70);
    spi_writeData(spi,data);
    lcd_cs_port(1);
}

static void lcd_data(spi_device_handle_t spi, const uint8_t data) {
    lcd_cs_port(0);
    spi_writeData(spi,0x72);
    spi_writeData(spi,data);
    lcd_cs_port(1);
}


static void LCD_WriteComm(uint8_t cmd) {
    lcd_cmd(g_screen_spi, cmd);
}

static void LCD_WriteData(uint8_t data) {
    lcd_data(g_screen_spi, data);
}

void LCD_WriteData_16Bit(uint16_t Data)
{
   lcd_cs_port(0);
   spi_writeData(g_screen_spi,0x72);
   spi_writeData(g_screen_spi,Data>>8);
   spi_writeData(g_screen_spi,Data);
   lcd_cs_port(1);
}
void Lcd_Write_REG(uint8_t Index,uint8_t Data)
{
    LCD_WriteComm(Index);
    LCD_WriteData(Data);
}





void LCD_Init(void)
{
    gpio_set_level(PIN_NUM_BCKL,1);

    gpio_set_level(PIN_NUM_RST, 0);
    vTaskDelay(100/portTICK_PERIOD_MS);
    gpio_set_level(PIN_NUM_RST, 1);
    vTaskDelay(100/portTICK_PERIOD_MS);

    Lcd_Write_REG(0x18,0xff);        //UADJ 75Hz
    Lcd_Write_REG(0x19,0x01);        //OSC_EN='1', start Osc
    //Power Voltage Setting
    Lcd_Write_REG(0x1B,0x1E); // 1e VRH=4.60V
    Lcd_Write_REG(0x1C,0x07); //AP Crosstalk    04
    Lcd_Write_REG(0x1A,0x01); //BT (VGH~15V,VGL~-10V,DDVDH~5V)
    Lcd_Write_REG(0x24,0x38); //VMH 27
    Lcd_Write_REG(0x25,0x5F); //VML
    //VCOM offset
    Lcd_Write_REG(0x23,0x8C); //for Flicker adjust
    Lcd_Write_REG(0x1F,0x88);// GAS=1, VOMG=00, PON=0, DK=1, XDK=0, DVDH_TRI=0, STB=0
    vTaskDelay(5/portTICK_PERIOD_MS);
    Lcd_Write_REG(0x1F,0x80);// GAS=1, VOMG=00, PON=0, DK=0, XDK=0, DVDH_TRI=0, STB=0
    vTaskDelay(5/portTICK_PERIOD_MS);
    Lcd_Write_REG(0x1F,0x90);// GAS=1, VOMG=00, PON=1, DK=0, XDK=0, DVDH_TRI=0, STB=0
    vTaskDelay(5/portTICK_PERIOD_MS);
    Lcd_Write_REG(0x1F,0xD0);// GAS=1, VOMG=10, PON=1, DK=0, XDK=0, DDVDH_TRI=0, STB=0
    vTaskDelay(5/portTICK_PERIOD_MS);
    //Display ON Setting
    Lcd_Write_REG(0x28,0x38);   //GON=1, DTE=1, D=1000
    vTaskDelay(40/portTICK_PERIOD_MS);
    Lcd_Write_REG(0x28,0x3C);   //GON=1, DTE=1, D=1100
    Lcd_Write_REG(0x36,0x09);   //09 REV, BGR 翻转 RGB控制
    Lcd_Write_REG(0x17,0x50);  //16BIT/PIXEL
// //Gamma 2.2 Setting
Lcd_Write_REG(0x40,0x01); //
Lcd_Write_REG(0x41,0x00); //
Lcd_Write_REG(0x42,0x00); //
Lcd_Write_REG(0x43,0x10); //
Lcd_Write_REG(0x44,0x0E); //
Lcd_Write_REG(0x45,0x24); //
Lcd_Write_REG(0x46,0x04); //
Lcd_Write_REG(0x47,0x50); //
Lcd_Write_REG(0x48,0x02); //
Lcd_Write_REG(0x49,0x13); //
Lcd_Write_REG(0x4A,0x19); //
Lcd_Write_REG(0x4B,0x19); //
Lcd_Write_REG(0x4C,0x16); //
Lcd_Write_REG(0x50,0x1B); //
Lcd_Write_REG(0x51,0x31); //
Lcd_Write_REG(0x52,0x2F); //
Lcd_Write_REG(0x53,0x3F); //
Lcd_Write_REG(0x54,0x3F); //
Lcd_Write_REG(0x55,0x3E); //
Lcd_Write_REG(0x56,0x2F); //
Lcd_Write_REG(0x57,0x7B); //
Lcd_Write_REG(0x58,0x09); //
Lcd_Write_REG(0x59,0x06); //
Lcd_Write_REG(0x5A,0x06); //
Lcd_Write_REG(0x5B,0x0C); //
Lcd_Write_REG(0x5C,0x1D); //
Lcd_Write_REG(0x5D,0xCC); //

//Set Window Area
Lcd_Write_REG(0x02,0x00);
Lcd_Write_REG(0x03,0x00); //Column Start
Lcd_Write_REG(0x04,0x00);
Lcd_Write_REG(0x05,0xEF); //Column End
Lcd_Write_REG(0x06,0x00);
Lcd_Write_REG(0x07,0x00); //Row Start
Lcd_Write_REG(0x08,0x01);
Lcd_Write_REG(0x09,0x3F); //Row End


Lcd_Write_REG(0x31,0x02); //RGB Interface(1) (VS+HS+DE)
Lcd_Write_REG(0x32,0x00);  //DPL:(08) HSPL(04)VSPL(02) EPL (01)
Lcd_Write_REG(0x33,0x02);
Lcd_Write_REG(0x34,0x02);

}

void rgb_spi_init(void)
{
    esp_err_t ret;
    gpio_config_t io_conf = {};

    spi_bus_config_t buscfg={
        .miso_io_num=PIN_NUM_MISO,
        .mosi_io_num=PIN_NUM_MOSI,
        .sclk_io_num=PIN_NUM_CLK,
        .quadwp_io_num=-1,
        .quadhd_io_num=-1,
        .max_transfer_sz=16*320
    };

    spi_device_interface_config_t devcfg={
        .clock_speed_hz=40*1000*1000,           //Clock out at 10 MHz
        .mode=0,                                //SPI mode 0
        //.spics_io_num=PIN_NUM_CS,               //CS pin
        .queue_size=1,                          //We want to be able to queue 7 transactions at a time
    };

    //Initialize the lcd gpio

    io_conf.pin_bit_mask = ( (1ULL<

2、rgb_lcd_example_main.c主程序

/*
 * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
 *
 * SPDX-License-Identifier: CC0-1.0
 */

#include 
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "esp_timer.h"
#include "esp_lcd_panel_ops.h"
#include "esp_lcd_panel_rgb.h"
#include "driver/gpio.h"
#include "esp_err.h"
#include "esp_log.h"
#include "lvgl.h"
#include "screen_rgb_spi.h"


#include "demos/lv_demos.h"

static const char *TAG = "example";


 Please update the following configuration according to your LCD spec //

#define EXAMPLE_LCD_PIXEL_CLOCK_HZ     (15 * 1000 * 1000)
#define EXAMPLE_LCD_BK_LIGHT_ON_LEVEL  1
#define EXAMPLE_LCD_BK_LIGHT_OFF_LEVEL !EXAMPLE_LCD_BK_LIGHT_ON_LEVEL
#define EXAMPLE_PIN_NUM_BK_LIGHT       1
#define EXAMPLE_PIN_NUM_HSYNC          47
#define EXAMPLE_PIN_NUM_VSYNC          48
#define EXAMPLE_PIN_NUM_DE             45
#define EXAMPLE_PIN_NUM_PCLK           21

//D1-D11,D13-D17
//D0x  D12x

#define EXAMPLE_PIN_NUM_DATA0          36 // B0
#define EXAMPLE_PIN_NUM_DATA1          37 // B1
#define EXAMPLE_PIN_NUM_DATA2          38 // B2
#define EXAMPLE_PIN_NUM_DATA3          39// B3
#define EXAMPLE_PIN_NUM_DATA4          40// B4
#define EXAMPLE_PIN_NUM_DATA5          41// G0
#define EXAMPLE_PIN_NUM_DATA6          42// G1
#define EXAMPLE_PIN_NUM_DATA7          15// G2
#define EXAMPLE_PIN_NUM_DATA8          16// G3
#define EXAMPLE_PIN_NUM_DATA9          17// G4
#define EXAMPLE_PIN_NUM_DATA10         18 // G5

#define EXAMPLE_PIN_NUM_DATA11         10 // R0
#define EXAMPLE_PIN_NUM_DATA12         11 // R1
#define EXAMPLE_PIN_NUM_DATA13         12 // R2
#define EXAMPLE_PIN_NUM_DATA14         13 // R3
#define EXAMPLE_PIN_NUM_DATA15         14 // R4
#define EXAMPLE_PIN_NUM_DISP_EN        -1

// The pixel number in horizontal and vertical
#define EXAMPLE_LCD_H_RES              240
#define EXAMPLE_LCD_V_RES              320

#if CONFIG_EXAMPLE_DOUBLE_FB
#define EXAMPLE_LCD_NUM_FB             2
#else
#define EXAMPLE_LCD_NUM_FB             1
#endif // CONFIG_EXAMPLE_DOUBLE_FB

#define EXAMPLE_LVGL_TICK_PERIOD_MS    1

// we use two semaphores to sync the VSYNC event and the LVGL task, to avoid potential tearing effect
#if CONFIG_EXAMPLE_AVOID_TEAR_EFFECT_WITH_SEM
SemaphoreHandle_t sem_vsync_end;
SemaphoreHandle_t sem_gui_ready;
#endif

extern void example_lvgl_demo_ui(lv_disp_t *disp);

static bool example_on_vsync_event(esp_lcd_panel_handle_t panel, const esp_lcd_rgb_panel_event_data_t *event_data, void *user_data)
{
    BaseType_t high_task_awoken = pdFALSE;
#if CONFIG_EXAMPLE_AVOID_TEAR_EFFECT_WITH_SEM
    if (xSemaphoreTakeFromISR(sem_gui_ready, &high_task_awoken) == pdTRUE) {
        xSemaphoreGiveFromISR(sem_vsync_end, &high_task_awoken);
    }
#endif
    return high_task_awoken == pdTRUE;
}

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;
    ESP_LOGI(TAG, "example_lvgl_flush_cb:%d,%d,%d,%d",offsetx1,offsetx2,offsety1,offsety2);
#if CONFIG_EXAMPLE_AVOID_TEAR_EFFECT_WITH_SEM
    xSemaphoreGive(sem_gui_ready);
    xSemaphoreTake(sem_vsync_end, portMAX_DELAY);
#endif
    // pass the draw buffer to the driver
    esp_lcd_panel_draw_bitmap(panel_handle, offsetx1, offsety1, offsetx2 + 1, offsety2 + 1, color_map);
    lv_disp_flush_ready(drv);
}

static void example_increase_lvgl_tick(void *arg)
{
    /* Tell LVGL how many milliseconds has elapsed */
    lv_tick_inc(EXAMPLE_LVGL_TICK_PERIOD_MS);
}

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


    //
    rgb_spi_init();


    //
#if CONFIG_EXAMPLE_AVOID_TEAR_EFFECT_WITH_SEM
    ESP_LOGI(TAG, "Create semaphores");
    sem_vsync_end = xSemaphoreCreateBinary();
    assert(sem_vsync_end);
    sem_gui_ready = xSemaphoreCreateBinary();
    assert(sem_gui_ready);
#endif

#if EXAMPLE_PIN_NUM_BK_LIGHT >= 0
    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
    };
    ESP_ERROR_CHECK(gpio_config(&bk_gpio_config));
#endif

    ESP_LOGI(TAG, "Install RGB LCD panel driver");
    esp_lcd_panel_handle_t panel_handle = NULL;
    esp_lcd_rgb_panel_config_t panel_config = {
        .data_width = 16, // RGB565 in parallel mode, thus 16bit in width
        .psram_trans_align = 64,
        .num_fbs = EXAMPLE_LCD_NUM_FB,
#if CONFIG_EXAMPLE_USE_BOUNCE_BUFFER
        .bounce_buffer_size_px = 10 * EXAMPLE_LCD_H_RES,
#endif
        .clk_src = LCD_CLK_SRC_DEFAULT,
        .disp_gpio_num = EXAMPLE_PIN_NUM_DISP_EN,
        .pclk_gpio_num = EXAMPLE_PIN_NUM_PCLK,
        .vsync_gpio_num = EXAMPLE_PIN_NUM_VSYNC,
        .hsync_gpio_num = EXAMPLE_PIN_NUM_HSYNC,
        .de_gpio_num = EXAMPLE_PIN_NUM_DE,
        .data_gpio_nums = {
            EXAMPLE_PIN_NUM_DATA0,
            EXAMPLE_PIN_NUM_DATA1,
            EXAMPLE_PIN_NUM_DATA2,
            EXAMPLE_PIN_NUM_DATA3,
            EXAMPLE_PIN_NUM_DATA4,
            EXAMPLE_PIN_NUM_DATA5,
            EXAMPLE_PIN_NUM_DATA6,
            EXAMPLE_PIN_NUM_DATA7,
            EXAMPLE_PIN_NUM_DATA8,
            EXAMPLE_PIN_NUM_DATA9,
            EXAMPLE_PIN_NUM_DATA10,
            EXAMPLE_PIN_NUM_DATA11,
            EXAMPLE_PIN_NUM_DATA12,
            EXAMPLE_PIN_NUM_DATA13,
            EXAMPLE_PIN_NUM_DATA14,
            EXAMPLE_PIN_NUM_DATA15,
        },
        .timings = {
            .pclk_hz = EXAMPLE_LCD_PIXEL_CLOCK_HZ,
            .h_res = EXAMPLE_LCD_H_RES,
            .v_res = EXAMPLE_LCD_V_RES,
            // The following parameters should refer to LCD spec
            .hsync_back_porch = 40,
            .hsync_front_porch = 20,
            .hsync_pulse_width = 1,
            .vsync_back_porch = 8,
            .vsync_front_porch = 4,
            .vsync_pulse_width = 1,
            .flags.pclk_active_neg = true,
        },
        .flags.fb_in_psram = true, // allocate frame buffer in PSRAM
    };
    ESP_ERROR_CHECK(esp_lcd_new_rgb_panel(&panel_config, &panel_handle));

    ESP_LOGI(TAG, "Register event callbacks");
    esp_lcd_rgb_panel_event_callbacks_t cbs = {
        .on_vsync = example_on_vsync_event,
    };
    ESP_ERROR_CHECK(esp_lcd_rgb_panel_register_event_callbacks(panel_handle, &cbs, &disp_drv));

    ESP_LOGI(TAG, "Initialize RGB LCD panel");
    ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle));
    ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle));

#if EXAMPLE_PIN_NUM_BK_LIGHT >= 0
    ESP_LOGI(TAG, "Turn on LCD backlight");
    gpio_set_level(EXAMPLE_PIN_NUM_BK_LIGHT, EXAMPLE_LCD_BK_LIGHT_ON_LEVEL);
#endif

    ESP_LOGI(TAG, "Initialize LVGL library");
    lv_init();
    lv_color_t *buf1 = NULL;
    lv_color_t *buf2 = NULL;
#if CONFIG_EXAMPLE_DOUBLE_FB
    ESP_LOGI(TAG, "Use frame buffers as LVGL draw buffers");
    ESP_ERROR_CHECK(esp_lcd_rgb_panel_get_frame_buffer(panel_handle, 2, &buf1, &buf2));
    // initialize LVGL draw buffers
    lv_disp_draw_buf_init(&disp_buf, buf1, buf2, EXAMPLE_LCD_H_RES * EXAMPLE_LCD_V_RES);
#else
    ESP_LOGI(TAG, "Allocate separate LVGL draw buffers from PSRAM");
    buf1 = heap_caps_malloc(EXAMPLE_LCD_H_RES *50*  sizeof(lv_color_t), MALLOC_CAP_INTERNAL);
    assert(buf1);
    buf2 = heap_caps_malloc(EXAMPLE_LCD_H_RES *50*  sizeof(lv_color_t), MALLOC_CAP_INTERNAL);
    assert(buf2);
    // initialize LVGL draw buffers
    lv_disp_draw_buf_init(&disp_buf, buf1, buf2, EXAMPLE_LCD_H_RES * 50);
#endif // CONFIG_EXAMPLE_DOUBLE_FB

    ESP_LOGI(TAG, "Register display driver to LVGL");
    lv_disp_drv_init(&disp_drv);
    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;
#if CONFIG_EXAMPLE_DOUBLE_FB
    disp_drv.full_refresh = true; // the full_refresh mode can maintain the synchronization between the two frame buffers
#endif
    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 Scatter Chart");
    //example_lvgl_demo_ui(disp);
    //lv_demo_widgets();
    lv_demo_benchmark();

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

二、说明

1、ESP32S3只支持RGB565,因此HX8347的17H寄存器必须设置为50h。

需要特别注意的是连线不是连续的,要跳过D12和D0连接剩余的16根线。

ESP32S3+HX8347+RGB运行LVGL例程_第3张图片

2、HX8347关于RGB的配置涉及4个寄存器31h,32h,33h,34h

Lcd_Write_REG(0x31,0x02); //RGB Interface(1) (VS+HS+DE) 进入RGB模式1

Lcd_Write_REG(0x32,0x00);  //DPL:(08) HSPL(04)VSPL(02) EPL (01)好像必须设置0x00

Lcd_Write_REG(0x33,0x02);        //瞎设的

Lcd_Write_REG(0x34,0x02);        //瞎设的

ESP32S3+HX8347+RGB运行LVGL例程_第4张图片

ESP32S3+HX8347+RGB运行LVGL例程_第5张图片

3、这几个参数应该好好设一下的,现在用的是DEMO中缺省的

 .hsync_back_porch = 40,
 .hsync_front_porch = 20,
 .hsync_pulse_width = 1,
 .vsync_back_porch = 8,
 .vsync_front_porch = 4,
 .vsync_pulse_width = 1,
 .flags.pclk_active_neg = true,

但是好像需要看懂下面这个图才能设,以后再研究吧

ESP32S3+HX8347+RGB运行LVGL例程_第6张图片

4、所用程序改自官方RGB demo程序。

ESP32S3+HX8347+RGB运行LVGL例程_第7张图片

你可能感兴趣的:(HX8347,ESP32S3,LVGL,RGB)