#include #include #include "sdkconfig.h" #include "unity.h" #include "test_utils.h" #include "driver/spi_master.h" #include "driver/gpio.h" #include "esp_lcd_panel_io.h" #include "esp_lcd_panel_vendor.h" #include "esp_lcd_panel_ops.h" #include "esp_system.h" #define TEST_LCD_H_RES (240) #define TEST_LCD_V_RES (280) #define TEST_SPI_CLK_GPIO (2) #define TEST_SPI_MOSI_GPIO (4) #define TEST_LCD_RST_GPIO (5) #define TEST_LCD_DC_GPIO (18) #define TEST_LCD_BK_LIGHT_GPIO (19) #define TEST_SPI_CS_GPIO (0) #define TEST_SPI_HOST_ID (1) #define TEST_LCD_PIXEL_CLOCK_HZ (20 * 1000 * 1000) TEST_CASE("lcd panel with spi interface (st7789)", "[lcd]") { #define TEST_IMG_SIZE (100 * 100 * sizeof(uint16_t)) uint8_t *img = heap_caps_malloc(TEST_IMG_SIZE, MALLOC_CAP_DMA); TEST_ASSERT_NOT_NULL(img); gpio_config_t bk_gpio_config = { .mode = GPIO_MODE_OUTPUT, .pin_bit_mask = 1ULL << TEST_LCD_BK_LIGHT_GPIO }; TEST_ESP_OK(gpio_config(&bk_gpio_config)); spi_bus_config_t buscfg = { .miso_io_num = -1, .mosi_io_num = TEST_SPI_MOSI_GPIO, .sclk_io_num = TEST_SPI_CLK_GPIO, .quadwp_io_num = -1, .quadhd_io_num = -1, .max_transfer_sz = TEST_LCD_H_RES * TEST_LCD_V_RES * sizeof(uint16_t) }; TEST_ESP_OK(spi_bus_initialize(TEST_SPI_HOST_ID, &buscfg, SPI_DMA_CH_AUTO)); esp_lcd_panel_io_handle_t io_handle = NULL; esp_lcd_panel_io_spi_config_t io_config = { .dc_gpio_num = TEST_LCD_DC_GPIO, .cs_gpio_num = TEST_SPI_CS_GPIO, .pclk_hz = TEST_LCD_PIXEL_CLOCK_HZ, .spi_mode = 0, .trans_queue_depth = 10, .lcd_cmd_bits = 8, .lcd_param_bits = 8, }; TEST_ESP_OK(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)TEST_SPI_HOST_ID, &io_config, &io_handle)); esp_lcd_panel_handle_t panel_handle = NULL; esp_lcd_panel_dev_config_t panel_config = { .reset_gpio_num = TEST_LCD_RST_GPIO, .color_space = ESP_LCD_COLOR_SPACE_RGB, .bits_per_pixel = 16, }; TEST_ESP_OK(esp_lcd_new_panel_st7789(io_handle, &panel_config, &panel_handle)); // turn off backlight gpio_set_level(TEST_LCD_BK_LIGHT_GPIO, 0); esp_lcd_panel_reset(panel_handle); esp_lcd_panel_init(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, 20); // turn on backlight gpio_set_level(TEST_LCD_BK_LIGHT_GPIO, 1); for (int i = 0; i < 200; i++) { uint8_t color_byte = esp_random() & 0xFF; int x_start = esp_random() % (TEST_LCD_H_RES - 100); int y_start = esp_random() % (TEST_LCD_V_RES - 100); memset(img, color_byte, TEST_IMG_SIZE); esp_lcd_panel_draw_bitmap(panel_handle, x_start, y_start, x_start + 100, y_start + 100, img); } esp_lcd_panel_disp_off(panel_handle, true); // turn off screen TEST_ESP_OK(esp_lcd_panel_del(panel_handle)); TEST_ESP_OK(esp_lcd_panel_io_del(io_handle)); TEST_ESP_OK(spi_bus_free(TEST_SPI_HOST_ID)); TEST_ESP_OK(gpio_reset_pin(TEST_LCD_BK_LIGHT_GPIO)); free(img); #undef TEST_IMG_SIZE } // The following test shows a porting example of LVGL GUI library // To run the LVGL tests, you need to clone the LVGL library into components directory firstly #if CONFIG_LV_USE_USER_DATA #include "test_lvgl_port.h" static bool notify_lvgl_ready_to_flush(esp_lcd_panel_io_handle_t panel_io, void *user_data, void *event_data) { lv_disp_t *disp = *(lv_disp_t **)user_data; lv_disp_flush_ready(&disp->driver); return false; } TEST_CASE("lvgl gui with spi interface (st7789)", "[lcd][lvgl][ignore]") { // initialize LVGL graphics library lv_disp_t *disp = NULL; lv_init(); gpio_config_t bk_gpio_config = { .mode = GPIO_MODE_OUTPUT, .pin_bit_mask = 1ULL << TEST_LCD_BK_LIGHT_GPIO }; TEST_ESP_OK(gpio_config(&bk_gpio_config)); spi_bus_config_t buscfg = { .miso_io_num = -1, .mosi_io_num = TEST_SPI_MOSI_GPIO, .sclk_io_num = TEST_SPI_CLK_GPIO, .quadwp_io_num = -1, .quadhd_io_num = -1, .max_transfer_sz = TEST_LCD_H_RES * TEST_LCD_V_RES * 2 }; TEST_ESP_OK(spi_bus_initialize(TEST_SPI_HOST_ID, &buscfg, SPI_DMA_CH_AUTO)); esp_lcd_panel_io_handle_t io_handle = NULL; esp_lcd_panel_io_spi_config_t io_config = { .dc_gpio_num = TEST_LCD_DC_GPIO, .cs_gpio_num = TEST_SPI_CS_GPIO, .pclk_hz = TEST_LCD_PIXEL_CLOCK_HZ, .spi_mode = 0, .trans_queue_depth = 10, .on_color_trans_done = notify_lvgl_ready_to_flush, .user_data = &disp, // we must use "address of disp" here, since the disp object has not been allocated .lcd_cmd_bits = 8, .lcd_param_bits = 8, }; TEST_ESP_OK(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)TEST_SPI_HOST_ID, &io_config, &io_handle)); esp_lcd_panel_handle_t panel_handle = NULL; esp_lcd_panel_dev_config_t panel_config = { .reset_gpio_num = TEST_LCD_RST_GPIO, .color_space = ESP_LCD_COLOR_SPACE_RGB, .bits_per_pixel = 16, }; TEST_ESP_OK(esp_lcd_new_panel_st7789(io_handle, &panel_config, &panel_handle)); // turn off backlight gpio_set_level(TEST_LCD_BK_LIGHT_GPIO, 0); esp_lcd_panel_reset(panel_handle); esp_lcd_panel_init(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, 20); // turn on backlight gpio_set_level(TEST_LCD_BK_LIGHT_GPIO, 1); test_lvgl_task_loop(panel_handle, TEST_LCD_H_RES, TEST_LCD_V_RES, &disp); } #endif // CONFIG_LV_USE_USER_DATA