1
6
7
8
9
10
11
12
15
16
24
25
26
27
28
29
30
31
32
33
34
35
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
136
137
138
139
140
141
142
143
144
145
146
147
152
153
156
157
158
159
160
161
162
163
164
165
166
167
170
171
172
173
174
175
176
177
188
189
190
191
192
193
194
195
196
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
222
223
224
225
226
229
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
278
279
280
281
282
283
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
334
335
336
337
338
339
340
341
342
345
349
350
351
352
353
354
355
356
357
358
368
369
370
371
380
381
382
383
384
385
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
417
418
/* ... */
#include <stdlib.h>
#include <string.h>
#include <sys/cdefs.h>
#include "sdkconfig.h"
#if CONFIG_LCD_ENABLE_DEBUG_LOG
#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG/* ... */
#endif
#include "esp_lcd_panel_io_interface.h"
#include "esp_lcd_panel_io.h"
#include "driver/spi_master.h"
#include "driver/gpio.h"
#include "hal/gpio_ll.h"
#include "esp_log.h"
#include "esp_check.h"
#include "esp_lcd_common.h"8 includes
static const char *TAG = "lcd_panel.io.spi";
static esp_err_t panel_io_spi_rx_param(esp_lcd_panel_io_t *io, int lcd_cmd, void *param, size_t param_size);
static esp_err_t panel_io_spi_tx_param(esp_lcd_panel_io_t *io, int lcd_cmd, const void *param, size_t param_size);
static esp_err_t panel_io_spi_tx_color(esp_lcd_panel_io_t *io, int lcd_cmd, const void *color, size_t color_size);
static esp_err_t panel_io_spi_del(esp_lcd_panel_io_t *io);
static void lcd_spi_pre_trans_cb(spi_transaction_t *trans);
static void lcd_spi_post_trans_color_cb(spi_transaction_t *trans);
static esp_err_t panel_io_spi_register_event_callbacks(esp_lcd_panel_io_handle_t io, const esp_lcd_panel_io_callbacks_t *cbs, void *user_ctx);
typedef struct {
spi_transaction_t base;
struct {
unsigned int dc_gpio_level: 1;
unsigned int en_trans_done_cb: 1;
}{ ... } flags;
}{ ... } lcd_spi_trans_descriptor_t;
typedef struct {
esp_lcd_panel_io_t base;
spi_device_handle_t spi_dev;
size_t spi_trans_max_bytes;
int dc_gpio_num;
esp_lcd_panel_io_color_trans_done_cb_t on_color_trans_done;
void *user_ctx;
size_t queue_size;
size_t num_trans_inflight;
int lcd_cmd_bits;
int lcd_param_bits;
uint8_t cs_ena_pretrans;
uint8_t cs_ena_posttrans;
struct {
unsigned int dc_cmd_level: 1;
unsigned int dc_data_level: 1;
unsigned int dc_param_level: 1;
unsigned int octal_mode: 1;
unsigned int quad_mode: 1;
}{ ... } flags;
lcd_spi_trans_descriptor_t trans_pool[];
}{ ... } esp_lcd_panel_io_spi_t;
esp_err_t esp_lcd_new_panel_io_spi(esp_lcd_spi_bus_handle_t bus, const esp_lcd_panel_io_spi_config_t *io_config, esp_lcd_panel_io_handle_t *ret_io)
{
#if CONFIG_LCD_ENABLE_DEBUG_LOG
esp_log_level_set(TAG, ESP_LOG_DEBUG);
#endif
esp_err_t ret = ESP_OK;
esp_lcd_panel_io_spi_t *spi_panel_io = NULL;
ESP_GOTO_ON_FALSE(bus && io_config && ret_io, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
spi_panel_io = calloc(1, sizeof(esp_lcd_panel_io_spi_t) + sizeof(lcd_spi_trans_descriptor_t) * io_config->trans_queue_depth);
ESP_GOTO_ON_FALSE(spi_panel_io, ESP_ERR_NO_MEM, err, TAG, "no mem for spi panel io");
spi_device_interface_config_t devcfg = {
.flags = SPI_DEVICE_HALFDUPLEX |
(io_config->flags.lsb_first ? SPI_DEVICE_TXBIT_LSBFIRST : 0) |
(io_config->flags.sio_mode ? SPI_DEVICE_3WIRE : 0) |
(io_config->flags.cs_high_active ? SPI_DEVICE_POSITIVE_CS : 0),
.clock_speed_hz = io_config->pclk_hz,
.mode = io_config->spi_mode,
.spics_io_num = io_config->cs_gpio_num,
.queue_size = io_config->trans_queue_depth,
.pre_cb = lcd_spi_pre_trans_cb,
.post_cb = lcd_spi_post_trans_color_cb,
.cs_ena_pretrans = io_config->cs_ena_pretrans,
.cs_ena_posttrans = io_config->cs_ena_posttrans,
}{...};
ret = spi_bus_add_device((spi_host_device_t)bus, &devcfg, &spi_panel_io->spi_dev);
ESP_GOTO_ON_ERROR(ret, err, TAG, "adding spi device to bus failed");
if (io_config->dc_gpio_num >= 0) {
gpio_config_t io_conf = {
.mode = GPIO_MODE_OUTPUT,
.pin_bit_mask = 1ULL << io_config->dc_gpio_num,
}{...};
ESP_GOTO_ON_ERROR(gpio_config(&io_conf), err, TAG, "configure GPIO for D/C line failed");
}{...}
spi_panel_io->flags.dc_cmd_level = io_config->flags.dc_high_on_cmd;
spi_panel_io->flags.dc_data_level = !io_config->flags.dc_low_on_data;
spi_panel_io->flags.dc_param_level = !io_config->flags.dc_low_on_param;
spi_panel_io->flags.octal_mode = io_config->flags.octal_mode;
spi_panel_io->flags.quad_mode = io_config->flags.quad_mode;
spi_panel_io->on_color_trans_done = io_config->on_color_trans_done;
spi_panel_io->user_ctx = io_config->user_ctx;
spi_panel_io->lcd_cmd_bits = io_config->lcd_cmd_bits;
spi_panel_io->lcd_param_bits = io_config->lcd_param_bits;
spi_panel_io->dc_gpio_num = io_config->dc_gpio_num;
spi_panel_io->queue_size = io_config->trans_queue_depth;
spi_panel_io->base.rx_param = panel_io_spi_rx_param;
spi_panel_io->base.tx_param = panel_io_spi_tx_param;
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;
size_t max_trans_bytes = 0;
ESP_GOTO_ON_ERROR(spi_bus_get_max_transaction_len((spi_host_device_t)bus, &max_trans_bytes), err, TAG, "get spi max transaction len failed");
spi_panel_io->spi_trans_max_bytes = max_trans_bytes;
*ret_io = &(spi_panel_io->base);
ESP_LOGD(TAG, "new spi lcd panel io @%p, max_trans_bytes: %d", spi_panel_io, (int)max_trans_bytes);
return ESP_OK;
err:
if (spi_panel_io) {
if (io_config->dc_gpio_num >= 0) {
gpio_reset_pin(io_config->dc_gpio_num);
}{...}
free(spi_panel_io);
}{...}
return ret;
}{ ... }
static esp_err_t panel_io_spi_del(esp_lcd_panel_io_t *io)
{
esp_err_t ret = ESP_OK;
spi_transaction_t *spi_trans = NULL;
esp_lcd_panel_io_spi_t *spi_panel_io = __containerof(io, esp_lcd_panel_io_spi_t, base);
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--;
}{...}
spi_bus_remove_device(spi_panel_io->spi_dev);
if (spi_panel_io->dc_gpio_num >= 0) {
gpio_reset_pin(spi_panel_io->dc_gpio_num);
}{...}
ESP_LOGD(TAG, "del lcd panel io spi @%p", spi_panel_io);
free(spi_panel_io);
err:
return ret;
}{ ... }
static esp_err_t panel_io_spi_register_event_callbacks(esp_lcd_panel_io_handle_t io, const esp_lcd_panel_io_callbacks_t *cbs, void *user_ctx)
{
esp_lcd_panel_io_spi_t *spi_panel_io = __containerof(io, esp_lcd_panel_io_spi_t, base);
if (spi_panel_io->on_color_trans_done != NULL) {
ESP_LOGW(TAG, "Callback on_color_trans_done was already set and now it was overwritten!");
}{...}
spi_panel_io->on_color_trans_done = cbs->on_color_trans_done;
spi_panel_io->user_ctx = user_ctx;
return ESP_OK;
}{ ... }
static void spi_lcd_prepare_cmd_buffer(esp_lcd_panel_io_spi_t *panel_io, const void *cmd)
{
uint8_t *from = (uint8_t *)cmd;
if (panel_io->lcd_cmd_bits > 8) {
int start = 0;
int end = panel_io->lcd_cmd_bits / 8 - 1;
lcd_com_reverse_buffer_bytes(from, start, end);
}{...}
}{ ... }
static void spi_lcd_prepare_param_buffer(esp_lcd_panel_io_spi_t *panel_io, const void *param, size_t param_size)
{
uint8_t *from = (uint8_t *)param;
int param_width = panel_io->lcd_param_bits / 8;
size_t param_num = param_size / param_width;
if (panel_io->lcd_param_bits > 8) {
for (size_t i = 0; i < param_num; i++) {
int start = i * param_width;
int end = start + param_width - 1;
lcd_com_reverse_buffer_bytes(from, start, end);
}{...}
}{...}
}{ ... }
static esp_err_t panel_io_spi_tx_param(esp_lcd_panel_io_t *io, int lcd_cmd, const void *param, size_t param_size)
{
esp_err_t ret = ESP_OK;
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);
ESP_RETURN_ON_ERROR(spi_device_acquire_bus(spi_panel_io->spi_dev, portMAX_DELAY), TAG, "acquire spi bus failed");
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));
lcd_trans->base.user = spi_panel_io;
if (param && param_size) {
lcd_trans->base.flags |= SPI_TRANS_CS_KEEP_ACTIVE;
}{...}
if (spi_panel_io->flags.octal_mode) {
lcd_trans->base.flags |= (SPI_TRANS_MULTILINE_CMD | SPI_TRANS_MULTILINE_ADDR | SPI_TRANS_MODE_OCT);
}{...}
if (send_cmd) {
spi_lcd_prepare_cmd_buffer(spi_panel_io, &lcd_cmd);
lcd_trans->flags.dc_gpio_level = spi_panel_io->flags.dc_cmd_level;
lcd_trans->base.length = spi_panel_io->lcd_cmd_bits;
lcd_trans->base.tx_buffer = &lcd_cmd;
ret = spi_device_polling_transmit(spi_panel_io->spi_dev, &lcd_trans->base);
ESP_GOTO_ON_ERROR(ret, err, TAG, "spi transmit (polling) command failed");
}{...}
if (param && param_size) {
spi_lcd_prepare_param_buffer(spi_panel_io, param, param_size);
lcd_trans->flags.dc_gpio_level = spi_panel_io->flags.dc_param_level;
lcd_trans->base.length = param_size * 8;
lcd_trans->base.tx_buffer = param;
lcd_trans->base.flags &= ~SPI_TRANS_CS_KEEP_ACTIVE;
ret = spi_device_polling_transmit(spi_panel_io->spi_dev, &lcd_trans->base);
ESP_GOTO_ON_ERROR(ret, err, TAG, "spi transmit (polling) param failed");
}{...}
err:
spi_device_release_bus(spi_panel_io->spi_dev);
return ret;
}{ ... }
static esp_err_t panel_io_spi_rx_param(esp_lcd_panel_io_t *io, int lcd_cmd, void *param, size_t param_size)
{
esp_err_t ret = ESP_OK;
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);
ESP_RETURN_ON_ERROR(spi_device_acquire_bus(spi_panel_io->spi_dev, portMAX_DELAY), TAG, "acquire spi bus failed");
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));
lcd_trans->base.user = spi_panel_io;
lcd_trans->base.flags |= SPI_TRANS_CS_KEEP_ACTIVE;
if (spi_panel_io->flags.octal_mode) {
lcd_trans->base.flags |= (SPI_TRANS_MULTILINE_CMD | SPI_TRANS_MULTILINE_ADDR | SPI_TRANS_MODE_OCT);
}{...}
if (send_cmd) {
spi_lcd_prepare_cmd_buffer(spi_panel_io, &lcd_cmd);
lcd_trans->flags.dc_gpio_level = spi_panel_io->flags.dc_cmd_level;
lcd_trans->base.length = spi_panel_io->lcd_cmd_bits;
lcd_trans->base.tx_buffer = &lcd_cmd;
ret = spi_device_polling_transmit(spi_panel_io->spi_dev, &lcd_trans->base);
ESP_GOTO_ON_ERROR(ret, err, TAG, "spi transmit (polling) command failed");
}{...}
if (param && param_size) {
lcd_trans->flags.dc_gpio_level = spi_panel_io->flags.dc_param_level;
lcd_trans->base.length = 0;
lcd_trans->base.tx_buffer = NULL;
lcd_trans->base.rxlength = param_size * 8;
lcd_trans->base.rx_buffer = param;
lcd_trans->base.flags &= ~SPI_TRANS_CS_KEEP_ACTIVE;
ret = spi_device_polling_transmit(spi_panel_io->spi_dev, &lcd_trans->base);
ESP_GOTO_ON_ERROR(ret, err, TAG, "spi transmit (polling) param failed");
}{...}
err:
spi_device_release_bus(spi_panel_io->spi_dev);
return ret;
}{ ... }
static esp_err_t panel_io_spi_tx_color(esp_lcd_panel_io_t *io, int lcd_cmd, const void *color, size_t color_size)
{
esp_err_t ret = ESP_OK;
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);
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) {
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_cmd_level;
lcd_trans->base.length = spi_panel_io->lcd_cmd_bits;
lcd_trans->base.tx_buffer = &lcd_cmd;
if (color && color_size) {
lcd_trans->base.flags |= SPI_TRANS_CS_KEEP_ACTIVE;
}{...}
if (spi_panel_io->flags.octal_mode) {
lcd_trans->base.flags |= (SPI_TRANS_MULTILINE_CMD | SPI_TRANS_MULTILINE_ADDR | SPI_TRANS_MODE_OCT);
}{...}
ret = spi_device_polling_transmit(spi_panel_io->spi_dev, &lcd_trans->base);
ESP_GOTO_ON_ERROR(ret, err, TAG, "spi transmit (polling) command failed");
}{...}
do {
size_t chunk_size = color_size;
if (spi_panel_io->num_trans_inflight < spi_panel_io->queue_size) {
lcd_trans = &spi_panel_io->trans_pool[spi_panel_io->num_trans_inflight];
}{...} else {
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");
lcd_trans = __containerof(spi_trans, lcd_spi_trans_descriptor_t, base);
spi_panel_io->num_trans_inflight--;
}{...}
memset(lcd_trans, 0, sizeof(lcd_spi_trans_descriptor_t));
if (chunk_size > spi_panel_io->spi_trans_max_bytes) {
chunk_size = spi_panel_io->spi_trans_max_bytes;
lcd_trans->base.flags |= SPI_TRANS_CS_KEEP_ACTIVE;
}{...} else {
lcd_trans->flags.en_trans_done_cb = 1;
lcd_trans->base.flags &= ~SPI_TRANS_CS_KEEP_ACTIVE;
}{...}
lcd_trans->base.user = spi_panel_io;
lcd_trans->flags.dc_gpio_level = spi_panel_io->flags.dc_data_level;
lcd_trans->base.length = chunk_size * 8;
lcd_trans->base.tx_buffer = color;
if (spi_panel_io->flags.octal_mode) {
lcd_trans->base.flags |= (SPI_TRANS_MULTILINE_CMD | SPI_TRANS_MULTILINE_ADDR | SPI_TRANS_MODE_OCT);
}{...} else if (spi_panel_io->flags.quad_mode) {
lcd_trans->base.flags |= SPI_TRANS_MODE_QIO;
}{...}
ret = spi_device_queue_trans(spi_panel_io->spi_dev, &lcd_trans->base, portMAX_DELAY);
ESP_GOTO_ON_ERROR(ret, err, TAG, "spi transmit (queue) color failed");
spi_panel_io->num_trans_inflight++;
color = (const uint8_t *)color + chunk_size;
color_size -= chunk_size;
}{...} while (color_size > 0);
err:
spi_device_release_bus(spi_panel_io->spi_dev);
return ret;
}{ ... }
IRAM_ATTR static void lcd_spi_pre_trans_cb(spi_transaction_t *trans)
{
esp_lcd_panel_io_spi_t *spi_panel_io = trans->user;
lcd_spi_trans_descriptor_t *lcd_trans = __containerof(trans, lcd_spi_trans_descriptor_t, base);
if (spi_panel_io->dc_gpio_num >= 0) {
gpio_ll_set_level(&GPIO, spi_panel_io->dc_gpio_num, lcd_trans->flags.dc_gpio_level);
}{...}
}{ ... }
static void lcd_spi_post_trans_color_cb(spi_transaction_t *trans)
{
esp_lcd_panel_io_spi_t *spi_panel_io = trans->user;
lcd_spi_trans_descriptor_t *lcd_trans = __containerof(trans, lcd_spi_trans_descriptor_t, base);
if (lcd_trans->flags.en_trans_done_cb) {
if (spi_panel_io->on_color_trans_done) {
spi_panel_io->on_color_trans_done(&spi_panel_io->base, NULL, spi_panel_io->user_ctx);
}{...}
}{...}
}{ ... }