ESP32 IDF5.0版本下添加个人组件(st7735s驱动)

最近刚更换了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)

这样驱动组件就可以通过引用头文件的方式在工程中使用了。
在这里插入图片描述

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
好的,针对您的问题,我可以提供以下步骤: 1. 准备工作 首先,您需要准备好以下设备和软件: - ESP32 开发板 - LED 屏幕 - LVGL 库 - ESP32 IDF 框架 2. 连接 LED 屏幕 将 LED 屏幕连接到 ESP32 开发板。您需要查看 LED 屏幕的数据手册以确定正确的引脚连接方式。建议使用 SPI 接口连接,因为这是最常见的连接方式。 3. 安装 LVGL 库 LVGL 是一个用于嵌入式系统的 GUI 库,它可以与多种平台和操作系统一起使用。在本例中,我们将使用 ESP32 IDF 框架集成的 LVGL 版本。 您需要在 ESP32 IDF 中启用 LVGL 库。打开 `sdkconfig.defaults` 文件,找到以下行: ``` # CONFIG_LVGL_ENABLED is not set ``` 将其更改为: ``` CONFIG_LVGL_ENABLED=y ``` 然后,运行以下命令安装 LVGL: ``` make menuconfig # 进入配置界面 make lvgl # 安装 LVGL 库 ``` 4. 编写代码 接下来,您需要编写代码来控制 LED 屏幕并使用 LVGL 绘制图形。以下是一个示例代码片段: ```c #include "driver/gpio.h" #include "lvgl/lvgl.h" #define LED_SCREEN_CS GPIO_NUM_5 void app_main() { // 初始化 SPI 总线和 GPIO 引脚 spi_bus_config_t bus_config = { .mosi_io_num = GPIO_NUM_23, .sclk_io_num = GPIO_NUM_18, .miso_io_num = -1, // 不使用 MISO 引脚 .quadwp_io_num = -1, .quadhd_io_num = -1, .max_transfer_sz = 320*240*2, // LED 屏幕尺寸 }; spi_device_interface_config_t dev_config = { .clock_speed_hz = 10 * 1000 * 1000, // SPI 时钟频率 .mode = 0, .spics_io_num = LED_SCREEN_CS, .queue_size = 1, }; spi_device_handle_t spi_handle; spi_bus_initialize(VSPI_HOST, &bus_config, 1); spi_bus_add_device(VSPI_HOST, &dev_config, &spi_handle); // 初始化 LVGL 库 lv_init(); // 创建屏幕对象 lv_disp_drv_t disp_drv; lv_disp_drv_init(&disp_drv); disp_drv.flush_cb = led_screen_flush; lv_disp_drv_register(&disp_drv); // 创建例程 lv_obj_t *label = lv_label_create(lv_scr_act(), NULL); lv_label_set_text(label, "Hello, world!"); lv_obj_align(label, NULL, LV_ALIGN_CENTER, 0, 0); // 主循环 while (1) { lv_task_handler(); vTaskDelay(10 / portTICK_RATE_MS); } } void led_screen_flush(lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p) { spi_transaction_t t; memset(&t, 0, sizeof(t)); t.length = area->size; t.tx_buffer = color_p; gpio_set_level(LED_SCREEN_CS, 0); spi_device_transmit(spi_handle, &t); gpio_set_level(LED_SCREEN_CS, 1); } ``` 这段代码使用 ESP32 IDF 的 SPI 驱动程序和 LVGL 库控制 LED 屏幕。它还创建了一个简单的例程,显示“Hello, world!”文本。 5. 编译和烧录 最后,您需要使用 ESP32 IDF 编译和烧录代码。运行以下命令: ``` make -j4 make flash ``` 在烧录完成后,您应该能够看到 LED 屏幕显示“Hello, world!”文本。 希望这些步骤对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值