最近刚更换了IDF的5.0版本,SDK集成了LVGL8.0版本,开发上方便了许多,但是乐鑫组件平台上的驱动还不够丰富,很多LCD的驱动没有,比如我使用的是以st7735s芯片驱动的0.96寸tft屏,就得通过自己编写驱动组件点亮。
以例程spi_lcd_touch为例(具体位置examples\peripherals\lcd\spi_lcd_touch),首先乐鑫提供的LCD驱动组件(在managed_components文件夹中)格式都一样的,直接复制一份,改成相应名称:
只保留需要的文件就可以了。注意CMakeLists.txt文件中的内容:
idf_component_register(SRCS "esp_lcd_st7735.c"
INCLUDE_DIRS "include"
REQUIRES "esp_lcd"
PRIV_REQUIRES "driver")
然后编辑C文件内容,建议直接将原文件内的LCD名称更改替换为st7735,每个LCD驱动组件主要的不同是vendor_specific_init,也就是初始化指令,st7735s的初始化指令如下:
static const lcd_init_cmd_t vendor_specific_init[] = {
{ST7735_SWRESET, {0}, 0x80}, // Software reset, 0 args, w/delay 150
{ST7735_SLPOUT, {0}, 0x80}, // Out of sleep mode, 0 args, w/delay 500
{ST7735_FRMCTR1, {0x05, 0x3A, 0x3A}, 3}, // Frame rate ctrl - normal mode, 3 args: Rate = fosc/(1x2+40) * (LINE+2C+2D)
{ST7735_FRMCTR2, {0x05, 0x3A, 0x3A}, 3}, // Frame rate control - idle mode, 3 args:Rate = fosc/(1x2+40) * (LINE+2C+2D)
{ST7735_FRMCTR3, {0x05, 0x3A, 0x3A, 0x05, 0x3A, 0x3A}, 6}, //Frame rate ctrl - partial mode, 6 args:Dot inversion mode. Line inversion mode
{ST7735_INVCTR, {0x03}, 1}, // Display inversion ctrl, 1 arg, no delay:No inversion
{ST7735_PWCTR1, {0x62, 0x02, 0x04}, 3}, // Power control, 3 args, no delay:-4.6V AUTO mode
{ST7735_PWCTR2, {0xC0}, 1}, // Power control, 1 arg, no delay:VGH25 = 2.4C VGSEL = -10 VGH = 3 * AVDD
{ST7735_PWCTR3, {0x0D, 0x00}, 2}, // Power control, 2 args, no delay: Opamp current small, Boost frequency
{ST7735_PWCTR4, {0x8D, 0x6A}, 2}, // Power control, 2 args, no delay: BCLK/2, Opamp current small & Medium low
{ST7735_PWCTR5, {0x8D, 0xEE}, 2}, // Power control, 2 args, no delay:
{ST7735_VMCTR1, {0x0E}, 1}, // Power control, 1 arg, no delay:
{ST7735_INVON, {0}, 0}, // set inverted mode
//{ST7735_INVOFF, {0}, 0}, // set non-inverted mode
{ST7735_COLMOD, {0x05}, 1}, // set color mode, 1 arg, no delay: 16-bit color
{ST7735_GMCTRP1, {0x10, 0x0E, 0x02, 0x03, 0x0E, 0x07, 0x02, 0x07, 0x0A, 0x12, 0x27, 0x37, 0x00, 0x0D, 0x0E, 0x10}, 16}, // 16 args, no delay:
{ST7735_GMCTRN1, {0x10, 0x0E, 0x03, 0x03, 0x0F, 0x06, 0x02, 0x08, 0x0A, 0x13, 0x26, 0x36, 0x00, 0x0D, 0x0E, 0x10}, 16}, // 16 args, no delay:
{ST7735_NORON, {0}, TFT_INIT_DELAY}, // Normal display on, no args, w/delay 10 ms delay
{ST7735_DISPON, {0}, TFT_INIT_DELAY}, // Main screen turn on, no args w/delay 100 ms delay
{0, {0}, 0xff}
};
指令定义项如下:
#define ST7735_GREENTAB160x80 // For 160 x 80 display (BGR, inverted, 26 / 1 offset) 0.96 tft
#define COLSTART 26
#define ROWSTART 1
// Delay between some initialisation commands
#define TFT_INIT_DELAY 0x80
#define TFT_INVOFF 0x20
#define TFT_INVON 0x21
// ST7735 specific commands used in init
#define ST7735_NOP 0x00
#define ST7735_SWRESET 0x01
#define ST7735_RDDID 0x04
#define ST7735_RDDST 0x09
#define ST7735_SLPIN 0x10
#define ST7735_SLPOUT 0x11
#define ST7735_PTLON 0x12
#define ST7735_NORON 0x13
#define ST7735_INVOFF 0x20
#define ST7735_INVON 0x21
#define ST7735_DISPOFF 0x28
#define ST7735_DISPON 0x29
#define ST7735_CASET 0x2A
#define ST7735_RASET 0x2B
#define ST7735_RAMWR 0x2C
#define ST7735_RAMRD 0x2E
#define ST7735_PTLAR 0x30
#define ST7735_VSCRDEF 0x33
#define ST7735_COLMOD 0x3A
#define ST7735_MADCTL 0x36
#define ST7735_VSCRSADD 0x37
#define ST7735_FRMCTR1 0xB1
#define ST7735_FRMCTR2 0xB2
#define ST7735_FRMCTR3 0xB3
#define ST7735_INVCTR 0xB4
#define ST7735_DISSET5 0xB6
#define ST7735_PWCTR1 0xC0
#define ST7735_PWCTR2 0xC1
#define ST7735_PWCTR3 0xC2
#define ST7735_PWCTR4 0xC3
#define ST7735_PWCTR5 0xC4
#define ST7735_VMCTR1 0xC5
#define ST7735_RDID1 0xDA
#define ST7735_RDID2 0xDB
#define ST7735_RDID3 0xDC
#define ST7735_RDID4 0xDD
#define ST7735_PWCTR6 0xFC
#define ST7735_GMCTRP1 0xE0
#define ST7735_GMCTRN1 0xE1
0.96 tft屏还需要更改刷屏的偏移量,在函数panel_st7735_draw_bitmap中设置:
esp_lcd_panel_io_tx_param(io, LCD_CMD_CASET, (uint8_t[]) {
(x_start >> 8) & 0xFF,
(x_start + COLSTART) & 0xFF,
((x_end - 1) >> 8) & 0xFF,
(x_end - 1 + COLSTART) & 0xFF,
}, 4);
esp_lcd_panel_io_tx_param(io, LCD_CMD_RASET, (uint8_t[]) {
(y_start >> 8) & 0xFF,
(y_start + ROWSTART) & 0xFF,
((y_end - 1) >> 8) & 0xFF,
(y_end -1 + ROWSTART) & 0xFF,
}, 4);
另外屏幕镜像函数panel_st7735_mirror根据自己需要修改。
至此组件主要编辑完成,下一步需要加入工程才能进行编译使用,打开工程主CMakeLists.txt,加入set语句:
cmake_minimum_required(VERSION 3.16)
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/peripherals/lcd/spi_lcd_touch/managed_components/espressif__esp_lcd_st7735)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(spi_lcd_touch)
这样驱动组件就可以通过引用头文件的方式在工程中使用了。