Skip to content

Commit f608431

Browse files
committed
Merge branch 'bugfix/spi_lcd_max_trans_size_v5.1' into 'release/v5.1'
spi_lcd: maximum transfer size should respect bus configuration (v5.1) See merge request espressif/esp-idf!23229
2 parents 21c536c + 2b461df commit f608431

File tree

2 files changed

+94
-18
lines changed

2 files changed

+94
-18
lines changed

components/esp_lcd/src/esp_lcd_panel_io_spi.c

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
#include "esp_log.h"
2222
#include "esp_check.h"
2323
#include "esp_lcd_common.h"
24-
25-
#define LCD_SPI_MAX_DATA_SIZE (SPI_LL_DATA_MAX_BIT_LEN / 8)
24+
#include "esp_private/spi_common_internal.h"
2625

2726
static const char *TAG = "lcd_panel.io.spi";
2827

@@ -45,6 +44,7 @@ typedef struct {
4544
typedef struct {
4645
esp_lcd_panel_io_t base; // Base class of generic lcd panel io
4746
spi_device_handle_t spi_dev; // SPI device handle
47+
size_t spi_trans_max_bytes; // Maximum bytes that can be transmitted in one spi transaction
4848
int dc_gpio_num; // D/C line GPIO number
4949
esp_lcd_panel_io_color_trans_done_cb_t on_color_trans_done; // User register's callback, invoked when color data trans done
5050
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
107107
spi_panel_io->base.tx_color = panel_io_spi_tx_color;
108108
spi_panel_io->base.del = panel_io_spi_del;
109109
spi_panel_io->base.register_event_callbacks = panel_io_spi_register_event_callbacks;
110+
spi_panel_io->spi_trans_max_bytes = spi_bus_get_attr((spi_host_device_t)bus)->max_transfer_sz;
110111
*ret_io = &(spi_panel_io->base);
111112
ESP_LOGD(TAG, "new spi lcd panel io @%p", spi_panel_io);
112113

@@ -305,19 +306,21 @@ static esp_err_t panel_io_spi_tx_color(esp_lcd_panel_io_t *io, int lcd_cmd, cons
305306
spi_transaction_t *spi_trans = NULL;
306307
lcd_spi_trans_descriptor_t *lcd_trans = NULL;
307308
esp_lcd_panel_io_spi_t *spi_panel_io = __containerof(io, esp_lcd_panel_io_spi_t, base);
308-
bool send_cmd = (lcd_cmd >= 0);
309309

310-
// before issue a polling transaction, need to wait queued transactions finished
311-
size_t num_trans_inflight = spi_panel_io->num_trans_inflight;
312-
for (size_t i = 0; i < num_trans_inflight; i++) {
313-
ret = spi_device_get_trans_result(spi_panel_io->spi_dev, &spi_trans, portMAX_DELAY);
314-
ESP_GOTO_ON_ERROR(ret, err, TAG, "recycle spi transactions failed");
315-
spi_panel_io->num_trans_inflight--;
316-
}
317-
lcd_trans = &spi_panel_io->trans_pool[0];
318-
memset(lcd_trans, 0, sizeof(lcd_spi_trans_descriptor_t));
310+
ESP_RETURN_ON_ERROR(spi_device_acquire_bus(spi_panel_io->spi_dev, portMAX_DELAY), TAG, "acquire spi bus failed");
319311

312+
bool send_cmd = (lcd_cmd >= 0);
320313
if (send_cmd) {
314+
// before issue a polling transaction, need to wait queued transactions finished
315+
size_t num_trans_inflight = spi_panel_io->num_trans_inflight;
316+
for (size_t i = 0; i < num_trans_inflight; i++) {
317+
ret = spi_device_get_trans_result(spi_panel_io->spi_dev, &spi_trans, portMAX_DELAY);
318+
ESP_GOTO_ON_ERROR(ret, err, TAG, "recycle spi transactions failed");
319+
spi_panel_io->num_trans_inflight--;
320+
}
321+
lcd_trans = &spi_panel_io->trans_pool[0];
322+
memset(lcd_trans, 0, sizeof(lcd_spi_trans_descriptor_t));
323+
321324
spi_lcd_prepare_cmd_buffer(spi_panel_io, &lcd_cmd);
322325
lcd_trans->base.user = spi_panel_io;
323326
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
332335
ESP_GOTO_ON_ERROR(ret, err, TAG, "spi transmit (polling) command failed");
333336
}
334337

335-
// split to chunks if required:
336-
// the SPI bus has a maximum transaction size determined by SPI_LL_DATA_MAX_BIT_LEN
338+
// if the color buffer is big, we want to split it into chunks, and queue the chunks one by one
337339
do {
338340
size_t chunk_size = color_size;
339341

@@ -349,13 +351,15 @@ static esp_err_t panel_io_spi_tx_color(esp_lcd_panel_io_t *io, int lcd_cmd, cons
349351
}
350352
memset(lcd_trans, 0, sizeof(lcd_spi_trans_descriptor_t));
351353

352-
// SPI per-transfer size has its limitation, if the color buffer is too big, we need to split it into multiple trunks
353-
if (chunk_size > LCD_SPI_MAX_DATA_SIZE) {
354+
// SPI per-transfer size has its limitation, if the color buffer is too big, we need to split it into multiple chunks
355+
if (chunk_size > spi_panel_io->spi_trans_max_bytes) {
354356
// cap the transfer size to the maximum supported by the bus
355-
chunk_size = LCD_SPI_MAX_DATA_SIZE;
357+
chunk_size = spi_panel_io->spi_trans_max_bytes;
358+
lcd_trans->base.flags |= SPI_TRANS_CS_KEEP_ACTIVE;
356359
} else {
357360
// mark en_trans_done_cb only at the last round to avoid premature completion callback
358361
lcd_trans->flags.en_trans_done_cb = 1;
362+
lcd_trans->base.flags &= ~SPI_TRANS_CS_KEEP_ACTIVE;
359363
}
360364

361365
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
378382
} while (color_size > 0); // continue while we have remaining data to transmit
379383

380384
err:
385+
spi_device_release_bus(spi_panel_io->spi_dev);
381386
return ret;
382387
}
383388

components/esp_lcd/test_apps/spi_lcd/main/test_spi_lcd_panel.c

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
#include <stdio.h>
77
#include <string.h>
88
#include "sdkconfig.h"
9+
#include "freertos/FreeRTOS.h"
10+
#include "freertos/task.h"
911
#include "unity.h"
1012
#include "driver/spi_master.h"
1113
#include "driver/gpio.h"
1214
#include "esp_lcd_panel_io.h"
1315
#include "esp_lcd_panel_vendor.h"
1416
#include "esp_lcd_panel_ops.h"
17+
#include "esp_lcd_panel_commands.h"
1518
#include "esp_random.h"
1619
#include "soc/soc_caps.h"
1720
#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
3538
.miso_io_num = -1,
3639
.quadwp_io_num = -1,
3740
.quadhd_io_num = -1,
38-
.max_transfer_sz = TEST_LCD_H_RES * TEST_LCD_V_RES * sizeof(uint16_t)
41+
.max_transfer_sz = 100 * 100 * sizeof(uint16_t),
3942
};
4043
#if SOC_SPI_SUPPORT_OCT
4144
if (oct_mode) {
@@ -189,3 +192,71 @@ TEST_CASE("lcd_panel_with_1-line_spi_interface_(st7789)", "[lcd]")
189192
TEST_ESP_OK(esp_lcd_new_panel_st7789(io_handle, &panel_config, &panel_handle));
190193
lcd_panel_test(io_handle, panel_handle);
191194
}
195+
196+
TEST_CASE("spi_lcd_send_colors_to_fixed_region", "[lcd]")
197+
{
198+
int x_start = 100;
199+
int y_start = 100;
200+
int x_end = 200;
201+
int y_end = 200;
202+
size_t color_size = (x_end - x_start) * (y_end - y_start) * 2;
203+
void *color_data = malloc(color_size);
204+
TEST_ASSERT_NOT_NULL(color_data);
205+
uint8_t color_byte = esp_random() & 0xFF;
206+
memset(color_data, color_byte, color_size);
207+
208+
esp_lcd_panel_io_handle_t io_handle = NULL;
209+
esp_lcd_panel_handle_t panel_handle = NULL;
210+
test_spi_lcd_common_initialize(&io_handle, NULL, NULL, 8, 8, false);
211+
212+
// we don't use the panel handle in this test, creating the panel just for a quick initialization
213+
esp_lcd_panel_dev_config_t panel_config = {
214+
.reset_gpio_num = TEST_LCD_RST_GPIO,
215+
.rgb_endian = LCD_RGB_ENDIAN_RGB,
216+
.bits_per_pixel = 16,
217+
};
218+
TEST_ESP_OK(esp_lcd_new_panel_st7789(io_handle, &panel_config, &panel_handle));
219+
esp_lcd_panel_reset(panel_handle);
220+
esp_lcd_panel_init(panel_handle);
221+
esp_lcd_panel_invert_color(panel_handle, true);
222+
// the gap is LCD panel specific, even panels with the same driver IC, can have different gap value
223+
esp_lcd_panel_set_gap(panel_handle, 0, 20);
224+
// turn on display
225+
esp_lcd_panel_disp_on_off(panel_handle, true);
226+
// turn on backlight
227+
gpio_set_level(TEST_LCD_BK_LIGHT_GPIO, 1);
228+
229+
printf("set the flush window for only once\r\n");
230+
esp_lcd_panel_io_tx_param(io_handle, LCD_CMD_CASET, (uint8_t[]) {
231+
(x_start >> 8) & 0xFF,
232+
x_start & 0xFF,
233+
((x_end - 1) >> 8) & 0xFF,
234+
(x_end - 1) & 0xFF,
235+
}, 4);
236+
esp_lcd_panel_io_tx_param(io_handle, LCD_CMD_RASET, (uint8_t[]) {
237+
(y_start >> 8) & 0xFF,
238+
y_start & 0xFF,
239+
((y_end - 1) >> 8) & 0xFF,
240+
(y_end - 1) & 0xFF,
241+
}, 4);
242+
esp_lcd_panel_io_tx_param(io_handle, LCD_CMD_RAMWR, NULL, 0);
243+
244+
printf("send colors to the fixed region in multiple steps\r\n");
245+
const int steps = 10;
246+
int color_size_per_step = color_size / steps;
247+
for (int i = 0; i < steps; i++) {
248+
TEST_ESP_OK(esp_lcd_panel_io_tx_color(io_handle, -1, color_data + i * color_size_per_step, color_size_per_step));
249+
}
250+
vTaskDelay(pdMS_TO_TICKS(1000));
251+
// change to another color
252+
color_byte = esp_random() & 0xFF;
253+
memset(color_data, color_byte, color_size);
254+
for (int i = 0; i < steps; i++) {
255+
TEST_ESP_OK(esp_lcd_panel_io_tx_color(io_handle, -1, color_data + i * color_size_per_step, color_size_per_step));
256+
}
257+
258+
TEST_ESP_OK(esp_lcd_panel_del(panel_handle));
259+
TEST_ESP_OK(esp_lcd_panel_io_del(io_handle));
260+
TEST_ESP_OK(spi_bus_free(TEST_SPI_HOST_ID));
261+
free(color_data);
262+
}

0 commit comments

Comments
 (0)