From 7a716377a326ba3d14f5c6a0186991f04268b08c Mon Sep 17 00:00:00 2001 From: morris Date: Tue, 11 Apr 2023 19:01:09 +0800 Subject: [PATCH 1/2] spi_lcd: maximum transfer size should respect bus configuration Also this commit added the SPI_TRANS_CS_KEEP_ACTIVE flag for io_tx_color --- components/esp_lcd/src/esp_lcd_panel_io_spi.c | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/components/esp_lcd/src/esp_lcd_panel_io_spi.c b/components/esp_lcd/src/esp_lcd_panel_io_spi.c index 2ed72b179a69..945bd2326b13 100644 --- a/components/esp_lcd/src/esp_lcd_panel_io_spi.c +++ b/components/esp_lcd/src/esp_lcd_panel_io_spi.c @@ -21,8 +21,7 @@ #include "esp_log.h" #include "esp_check.h" #include "esp_lcd_common.h" - -#define LCD_SPI_MAX_DATA_SIZE (SPI_LL_DATA_MAX_BIT_LEN / 8) +#include "esp_private/spi_common_internal.h" static const char *TAG = "lcd_panel.io.spi"; @@ -45,6 +44,7 @@ typedef struct { typedef struct { esp_lcd_panel_io_t base; // Base class of generic lcd panel io spi_device_handle_t spi_dev; // SPI device handle + size_t spi_trans_max_bytes; // Maximum bytes that can be transmitted in one spi transaction int dc_gpio_num; // D/C line GPIO number esp_lcd_panel_io_color_trans_done_cb_t on_color_trans_done; // User register's callback, invoked when color data trans done void *user_ctx; // User's private data, passed directly to callback on_color_trans_done @@ -107,6 +107,7 @@ esp_err_t esp_lcd_new_panel_io_spi(esp_lcd_spi_bus_handle_t bus, const esp_lcd_p spi_panel_io->base.tx_color = panel_io_spi_tx_color; spi_panel_io->base.del = panel_io_spi_del; spi_panel_io->base.register_event_callbacks = panel_io_spi_register_event_callbacks; + spi_panel_io->spi_trans_max_bytes = spi_bus_get_attr((spi_host_device_t)bus)->max_transfer_sz; *ret_io = &(spi_panel_io->base); ESP_LOGD(TAG, "new spi lcd panel io @%p", spi_panel_io); @@ -305,19 +306,21 @@ static esp_err_t panel_io_spi_tx_color(esp_lcd_panel_io_t *io, int lcd_cmd, cons spi_transaction_t *spi_trans = NULL; lcd_spi_trans_descriptor_t *lcd_trans = NULL; esp_lcd_panel_io_spi_t *spi_panel_io = __containerof(io, esp_lcd_panel_io_spi_t, base); - bool send_cmd = (lcd_cmd >= 0); - // before issue a polling transaction, need to wait queued transactions finished - size_t num_trans_inflight = spi_panel_io->num_trans_inflight; - for (size_t i = 0; i < num_trans_inflight; i++) { - ret = spi_device_get_trans_result(spi_panel_io->spi_dev, &spi_trans, portMAX_DELAY); - ESP_GOTO_ON_ERROR(ret, err, TAG, "recycle spi transactions failed"); - spi_panel_io->num_trans_inflight--; - } - lcd_trans = &spi_panel_io->trans_pool[0]; - memset(lcd_trans, 0, sizeof(lcd_spi_trans_descriptor_t)); + ESP_RETURN_ON_ERROR(spi_device_acquire_bus(spi_panel_io->spi_dev, portMAX_DELAY), TAG, "acquire spi bus failed"); + bool send_cmd = (lcd_cmd >= 0); if (send_cmd) { + // before issue a polling transaction, need to wait queued transactions finished + size_t num_trans_inflight = spi_panel_io->num_trans_inflight; + for (size_t i = 0; i < num_trans_inflight; i++) { + ret = spi_device_get_trans_result(spi_panel_io->spi_dev, &spi_trans, portMAX_DELAY); + ESP_GOTO_ON_ERROR(ret, err, TAG, "recycle spi transactions failed"); + spi_panel_io->num_trans_inflight--; + } + lcd_trans = &spi_panel_io->trans_pool[0]; + memset(lcd_trans, 0, sizeof(lcd_spi_trans_descriptor_t)); + spi_lcd_prepare_cmd_buffer(spi_panel_io, &lcd_cmd); lcd_trans->base.user = spi_panel_io; lcd_trans->flags.dc_gpio_level = !spi_panel_io->flags.dc_data_level; // set D/C line to command mode @@ -332,8 +335,7 @@ static esp_err_t panel_io_spi_tx_color(esp_lcd_panel_io_t *io, int lcd_cmd, cons ESP_GOTO_ON_ERROR(ret, err, TAG, "spi transmit (polling) command failed"); } - // split to chunks if required: - // the SPI bus has a maximum transaction size determined by SPI_LL_DATA_MAX_BIT_LEN + // if the color buffer is big, we want to split it into chunks, and queue the chunks one by one do { size_t chunk_size = color_size; @@ -349,13 +351,15 @@ static esp_err_t panel_io_spi_tx_color(esp_lcd_panel_io_t *io, int lcd_cmd, cons } memset(lcd_trans, 0, sizeof(lcd_spi_trans_descriptor_t)); - // SPI per-transfer size has its limitation, if the color buffer is too big, we need to split it into multiple trunks - if (chunk_size > LCD_SPI_MAX_DATA_SIZE) { + // SPI per-transfer size has its limitation, if the color buffer is too big, we need to split it into multiple chunks + if (chunk_size > spi_panel_io->spi_trans_max_bytes) { // cap the transfer size to the maximum supported by the bus - chunk_size = LCD_SPI_MAX_DATA_SIZE; + chunk_size = spi_panel_io->spi_trans_max_bytes; + lcd_trans->base.flags |= SPI_TRANS_CS_KEEP_ACTIVE; } else { // mark en_trans_done_cb only at the last round to avoid premature completion callback lcd_trans->flags.en_trans_done_cb = 1; + lcd_trans->base.flags &= ~SPI_TRANS_CS_KEEP_ACTIVE; } lcd_trans->base.user = spi_panel_io; @@ -378,6 +382,7 @@ static esp_err_t panel_io_spi_tx_color(esp_lcd_panel_io_t *io, int lcd_cmd, cons } while (color_size > 0); // continue while we have remaining data to transmit err: + spi_device_release_bus(spi_panel_io->spi_dev); return ret; } From 2b461df8a821918d2b7dcc127e3b58d26125277d Mon Sep 17 00:00:00 2001 From: morris Date: Wed, 12 Apr 2023 15:48:18 +0800 Subject: [PATCH 2/2] spi_lcd: test spi lcd io can transfer color data to a fixed window region also test the io tx_param and tx_color can skip the command phase --- .../spi_lcd/main/test_spi_lcd_panel.c | 73 ++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/components/esp_lcd/test_apps/spi_lcd/main/test_spi_lcd_panel.c b/components/esp_lcd/test_apps/spi_lcd/main/test_spi_lcd_panel.c index 9b575e8c7be1..39aab7bb734a 100644 --- a/components/esp_lcd/test_apps/spi_lcd/main/test_spi_lcd_panel.c +++ b/components/esp_lcd/test_apps/spi_lcd/main/test_spi_lcd_panel.c @@ -6,12 +6,15 @@ #include #include #include "sdkconfig.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" #include "unity.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_lcd_panel_commands.h" #include "esp_random.h" #include "soc/soc_caps.h" #include "test_spi_board.h" @@ -35,7 +38,7 @@ void test_spi_lcd_common_initialize(esp_lcd_panel_io_handle_t *io_handle, esp_lc .miso_io_num = -1, .quadwp_io_num = -1, .quadhd_io_num = -1, - .max_transfer_sz = TEST_LCD_H_RES * TEST_LCD_V_RES * sizeof(uint16_t) + .max_transfer_sz = 100 * 100 * sizeof(uint16_t), }; #if SOC_SPI_SUPPORT_OCT if (oct_mode) { @@ -189,3 +192,71 @@ TEST_CASE("lcd_panel_with_1-line_spi_interface_(st7789)", "[lcd]") TEST_ESP_OK(esp_lcd_new_panel_st7789(io_handle, &panel_config, &panel_handle)); lcd_panel_test(io_handle, panel_handle); } + +TEST_CASE("spi_lcd_send_colors_to_fixed_region", "[lcd]") +{ + int x_start = 100; + int y_start = 100; + int x_end = 200; + int y_end = 200; + size_t color_size = (x_end - x_start) * (y_end - y_start) * 2; + void *color_data = malloc(color_size); + TEST_ASSERT_NOT_NULL(color_data); + uint8_t color_byte = esp_random() & 0xFF; + memset(color_data, color_byte, color_size); + + esp_lcd_panel_io_handle_t io_handle = NULL; + esp_lcd_panel_handle_t panel_handle = NULL; + test_spi_lcd_common_initialize(&io_handle, NULL, NULL, 8, 8, false); + + // we don't use the panel handle in this test, creating the panel just for a quick initialization + esp_lcd_panel_dev_config_t panel_config = { + .reset_gpio_num = TEST_LCD_RST_GPIO, + .rgb_endian = LCD_RGB_ENDIAN_RGB, + .bits_per_pixel = 16, + }; + TEST_ESP_OK(esp_lcd_new_panel_st7789(io_handle, &panel_config, &panel_handle)); + 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 display + esp_lcd_panel_disp_on_off(panel_handle, true); + // turn on backlight + gpio_set_level(TEST_LCD_BK_LIGHT_GPIO, 1); + + printf("set the flush window for only once\r\n"); + esp_lcd_panel_io_tx_param(io_handle, LCD_CMD_CASET, (uint8_t[]) { + (x_start >> 8) & 0xFF, + x_start & 0xFF, + ((x_end - 1) >> 8) & 0xFF, + (x_end - 1) & 0xFF, + }, 4); + esp_lcd_panel_io_tx_param(io_handle, LCD_CMD_RASET, (uint8_t[]) { + (y_start >> 8) & 0xFF, + y_start & 0xFF, + ((y_end - 1) >> 8) & 0xFF, + (y_end - 1) & 0xFF, + }, 4); + esp_lcd_panel_io_tx_param(io_handle, LCD_CMD_RAMWR, NULL, 0); + + printf("send colors to the fixed region in multiple steps\r\n"); + const int steps = 10; + int color_size_per_step = color_size / steps; + for (int i = 0; i < steps; i++) { + TEST_ESP_OK(esp_lcd_panel_io_tx_color(io_handle, -1, color_data + i * color_size_per_step, color_size_per_step)); + } + vTaskDelay(pdMS_TO_TICKS(1000)); + // change to another color + color_byte = esp_random() & 0xFF; + memset(color_data, color_byte, color_size); + for (int i = 0; i < steps; i++) { + TEST_ESP_OK(esp_lcd_panel_io_tx_color(io_handle, -1, color_data + i * color_size_per_step, color_size_per_step)); + } + + 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)); + free(color_data); +}