Add first pass of SPI LCD logic

This commit is contained in:
maddiebaka
2025-12-05 23:34:42 -05:00
parent 33314d2a4a
commit 1763303f3c
54 changed files with 3422 additions and 1 deletions

View File

@@ -0,0 +1 @@
476fe49688f95201cba03a9391142d48c20b26f8e937de9ece44770a24943829

View File

@@ -0,0 +1 @@
{"version": "1.0", "algorithm": "sha256", "created_at": "2025-07-31T09:02:22.887367+00:00", "files": [{"path": "CMakeLists.txt", "size": 258, "hash": "d00705da6437b218fc2e0bcfa025dc771c6505cc1633aa4a6514b00215f31c8c"}, {"path": "README.md", "size": 3539, "hash": "33e27d001fd8daf55c3a4759e23331e4f13148f7a86bcb9b8ff9a9587201d7ae"}, {"path": "esp_lcd_ili9341.c", "size": 14645, "hash": "775a38234f810ae2eb757a8a238bad2d1980f1f9525ea9e1b26abcc18b581cdd"}, {"path": "idf_component.yml", "size": 343, "hash": "4c005f325b816ad44f84487c4f7acb1dd52ecbbc6cc9069559477c7fad0913d2"}, {"path": "license.txt", "size": 11358, "hash": "cfc7749b96f63bd31c3c42b5c471bf756814053e847c10f3eb003417bc523d30"}, {"path": "include/esp_lcd_ili9341.h", "size": 3871, "hash": "c3e57d1ec8241d073df09a1acfedee6d92c8779cf0293705375f689122705e90"}, {"path": "test_apps/CMakeLists.txt", "size": 317, "hash": "29400ed5970d148f1133f03517ebbe94782e0ccfd7194b77725f4e7497643ff3"}, {"path": "test_apps/sdkconfig.defaults", "size": 49, "hash": "dccb276c3ef85f8f4d6c32edf7c8b582cf2d416f42950b778d7af95a9808e7d4"}, {"path": "test_apps/main/CMakeLists.txt", "size": 54, "hash": "5548ae67823a1c421d11b7365bbd105f6649e7b59f2746ca9613ff52eea84089"}, {"path": "test_apps/main/idf_component.yml", "size": 150, "hash": "98dd28f4f12dae586a0c57f39bb840a5e7315307f77ffd00d6e67b4b9f105404"}, {"path": "test_apps/main/test_esp_lcd_ili9341.c", "size": 6581, "hash": "6e2068aa8014d7c26952089beb4d8749b7a03d90268d8ce109741d0277559bfa"}]}

View File

@@ -0,0 +1,7 @@
idf_component_register(SRCS "esp_lcd_ili9341.c"
INCLUDE_DIRS "include"
REQUIRES "esp_lcd"
PRIV_REQUIRES "driver")
include(package_manager)
cu_pkg_define_version(${CMAKE_CURRENT_LIST_DIR})

View File

@@ -0,0 +1,66 @@
# ESP LCD ILI9341
[![Component Registry](https://components.espressif.com/components/espressif/esp_lcd_ili9341/badge.svg)](https://components.espressif.com/components/espressif/esp_lcd_ili9341)
Implementation of the ILI9341 LCD controller with esp_lcd component.
| LCD controller | Communication interface | Component name | Link to datasheet |
| :------------: | :---------------------: | :------------: | :---------------: |
| ILI9341 | SPI | esp_lcd_ili9341 | [Specification](https://cdn-shop.adafruit.com/datasheets/ILI9341.pdf) |
## Add to project
Packages from this repository are uploaded to [Espressif's component service](https://components.espressif.com/).
You can add them to your project via `idf.py add-dependency`, e.g.
```bash
idf.py add-dependency "espressif/esp_lcd_ili9341^2.0.0"
```
Alternatively, you can create `idf_component.yml`. More is in [Espressif's documentation](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/tools/idf-component-manager.html).
## Example use
```c
ESP_LOGI(TAG, "Initialize SPI bus");
const spi_bus_config_t bus_config = ILI9341_PANEL_BUS_SPI_CONFIG(EXAMPLE_PIN_NUM_LCD_PCLK, EXAMPLE_PIN_NUM_LCD_MOSI,
EXAMPLE_LCD_H_RES * 80 * sizeof(uint16_t));
ESP_ERROR_CHECK(spi_bus_initialize(EXAMPLE_LCD_HOST, &bus_config, SPI_DMA_CH_AUTO));
ESP_LOGI(TAG, "Install panel IO");
esp_lcd_panel_io_handle_t io_handle = NULL;
const esp_lcd_panel_io_spi_config_t io_config = ILI9341_PANEL_IO_SPI_CONFIG(EXAMPLE_PIN_NUM_LCD_CS, EXAMPLE_PIN_NUM_LCD_DC,
example_callback, &example_callback_ctx);
ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)EXAMPLE_LCD_HOST, &io_config, &io_handle));
/**
* Uncomment these lines if use custom initialization commands.
* The array should be declared as "static const" and positioned outside the function.
*/
// static const ili9341_lcd_init_cmd_t lcd_init_cmds[] = {
// // {cmd, { data }, data_size, delay_ms}
// {0xCF, (uint8_t []){0x00, 0xAA, 0XE0}, 3, 0},
// {0xED, (uint8_t []){0x67, 0x03, 0X12, 0X81}, 4, 0},
// {0xE8, (uint8_t []){0x8A, 0x01, 0x78}, 3, 0},
// ...
// };
ESP_LOGI(TAG, "Install ILI9341 panel driver");
esp_lcd_panel_handle_t panel_handle = NULL;
// ili9341_vendor_config_t vendor_config = { // Uncomment these lines if use custom initialization commands
// .init_cmds = lcd_init_cmds,
// .init_cmds_size = sizeof(lcd_init_cmds) / sizeof(ili9341_lcd_init_cmd_t),
// };
const esp_lcd_panel_dev_config_t panel_config = {
.reset_gpio_num = EXAMPLE_PIN_NUM_LCD_RST, // Set to -1 if not use
.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB, // RGB element order: R-G-B
.bits_per_pixel = 16, // Implemented by LCD command `3Ah` (16/18)
// .vendor_config = &vendor_config, // Uncomment this line if use custom initialization commands
};
ESP_ERROR_CHECK(esp_lcd_new_panel_ili9341(io_handle, &panel_config, &panel_handle));
ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle));
ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle));
ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_handle, true));
```
There is an example in ESP-IDF with this LCD controller. Please follow this [link](https://github.com/espressif/esp-idf/tree/master/examples/peripherals/lcd/spi_lcd_touch).

View File

@@ -0,0 +1,393 @@
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdlib.h>
#include <sys/cdefs.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_lcd_panel_interface.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 "driver/gpio.h"
#include "esp_log.h"
#include "esp_check.h"
#include "esp_lcd_ili9341.h"
static const char *TAG = "ili9341";
static esp_err_t panel_ili9341_del(esp_lcd_panel_t *panel);
static esp_err_t panel_ili9341_reset(esp_lcd_panel_t *panel);
static esp_err_t panel_ili9341_init(esp_lcd_panel_t *panel);
static esp_err_t panel_ili9341_draw_bitmap(esp_lcd_panel_t *panel, int x_start, int y_start, int x_end, int y_end, const void *color_data);
static esp_err_t panel_ili9341_invert_color(esp_lcd_panel_t *panel, bool invert_color_data);
static esp_err_t panel_ili9341_mirror(esp_lcd_panel_t *panel, bool mirror_x, bool mirror_y);
static esp_err_t panel_ili9341_swap_xy(esp_lcd_panel_t *panel, bool swap_axes);
static esp_err_t panel_ili9341_set_gap(esp_lcd_panel_t *panel, int x_gap, int y_gap);
static esp_err_t panel_ili9341_disp_on_off(esp_lcd_panel_t *panel, bool off);
typedef struct {
esp_lcd_panel_t base;
esp_lcd_panel_io_handle_t io;
int reset_gpio_num;
bool reset_level;
int x_gap;
int y_gap;
uint8_t fb_bits_per_pixel;
uint8_t madctl_val; // save current value of LCD_CMD_MADCTL register
uint8_t colmod_val; // save current value of LCD_CMD_COLMOD register
const ili9341_lcd_init_cmd_t *init_cmds;
uint16_t init_cmds_size;
} ili9341_panel_t;
esp_err_t esp_lcd_new_panel_ili9341(const esp_lcd_panel_io_handle_t io, const esp_lcd_panel_dev_config_t *panel_dev_config, esp_lcd_panel_handle_t *ret_panel)
{
esp_err_t ret = ESP_OK;
ili9341_panel_t *ili9341 = NULL;
gpio_config_t io_conf = { 0 };
ESP_GOTO_ON_FALSE(io && panel_dev_config && ret_panel, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
ili9341 = (ili9341_panel_t *)calloc(1, sizeof(ili9341_panel_t));
ESP_GOTO_ON_FALSE(ili9341, ESP_ERR_NO_MEM, err, TAG, "no mem for ili9341 panel");
if (panel_dev_config->reset_gpio_num >= 0) {
io_conf.mode = GPIO_MODE_OUTPUT;
io_conf.pin_bit_mask = 1ULL << panel_dev_config->reset_gpio_num;
ESP_GOTO_ON_ERROR(gpio_config(&io_conf), err, TAG, "configure GPIO for RST line failed");
}
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)
switch (panel_dev_config->color_space) {
case ESP_LCD_COLOR_SPACE_RGB:
ili9341->madctl_val = 0;
break;
case ESP_LCD_COLOR_SPACE_BGR:
ili9341->madctl_val |= LCD_CMD_BGR_BIT;
break;
default:
ESP_GOTO_ON_FALSE(false, ESP_ERR_NOT_SUPPORTED, err, TAG, "unsupported color space");
break;
}
#elif ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(6, 0, 0)
switch (panel_dev_config->rgb_endian) {
case LCD_RGB_ENDIAN_RGB:
ili9341->madctl_val = 0;
break;
case LCD_RGB_ENDIAN_BGR:
ili9341->madctl_val |= LCD_CMD_BGR_BIT;
break;
default:
ESP_GOTO_ON_FALSE(false, ESP_ERR_NOT_SUPPORTED, err, TAG, "unsupported rgb endian");
break;
}
#else
switch (panel_dev_config->rgb_ele_order) {
case LCD_RGB_ELEMENT_ORDER_RGB:
ili9341->madctl_val = 0;
break;
case LCD_RGB_ELEMENT_ORDER_BGR:
ili9341->madctl_val |= LCD_CMD_BGR_BIT;
break;
default:
ESP_GOTO_ON_FALSE(false, ESP_ERR_NOT_SUPPORTED, err, TAG, "unsupported rgb element order");
break;
}
#endif
switch (panel_dev_config->bits_per_pixel) {
case 16: // RGB565
ili9341->colmod_val = 0x55;
ili9341->fb_bits_per_pixel = 16;
break;
case 18: // RGB666
ili9341->colmod_val = 0x66;
// each color component (R/G/B) should occupy the 6 high bits of a byte, which means 3 full bytes are required for a pixel
ili9341->fb_bits_per_pixel = 24;
break;
default:
ESP_GOTO_ON_FALSE(false, ESP_ERR_NOT_SUPPORTED, err, TAG, "unsupported pixel width");
break;
}
ili9341->io = io;
ili9341->reset_gpio_num = panel_dev_config->reset_gpio_num;
ili9341->reset_level = panel_dev_config->flags.reset_active_high;
if (panel_dev_config->vendor_config) {
ili9341->init_cmds = ((ili9341_vendor_config_t *)panel_dev_config->vendor_config)->init_cmds;
ili9341->init_cmds_size = ((ili9341_vendor_config_t *)panel_dev_config->vendor_config)->init_cmds_size;
}
ili9341->base.del = panel_ili9341_del;
ili9341->base.reset = panel_ili9341_reset;
ili9341->base.init = panel_ili9341_init;
ili9341->base.draw_bitmap = panel_ili9341_draw_bitmap;
ili9341->base.invert_color = panel_ili9341_invert_color;
ili9341->base.set_gap = panel_ili9341_set_gap;
ili9341->base.mirror = panel_ili9341_mirror;
ili9341->base.swap_xy = panel_ili9341_swap_xy;
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)
ili9341->base.disp_off = panel_ili9341_disp_on_off;
#else
ili9341->base.disp_on_off = panel_ili9341_disp_on_off;
#endif
*ret_panel = &(ili9341->base);
ESP_LOGD(TAG, "new ili9341 panel @%p", ili9341);
ESP_LOGI(TAG, "LCD panel create success, version: %d.%d.%d", ESP_LCD_ILI9341_VER_MAJOR, ESP_LCD_ILI9341_VER_MINOR,
ESP_LCD_ILI9341_VER_PATCH);
return ESP_OK;
err:
if (ili9341) {
if (panel_dev_config->reset_gpio_num >= 0) {
gpio_reset_pin(panel_dev_config->reset_gpio_num);
}
free(ili9341);
}
return ret;
}
static esp_err_t panel_ili9341_del(esp_lcd_panel_t *panel)
{
ili9341_panel_t *ili9341 = __containerof(panel, ili9341_panel_t, base);
if (ili9341->reset_gpio_num >= 0) {
gpio_reset_pin(ili9341->reset_gpio_num);
}
ESP_LOGD(TAG, "del ili9341 panel @%p", ili9341);
free(ili9341);
return ESP_OK;
}
static esp_err_t panel_ili9341_reset(esp_lcd_panel_t *panel)
{
ili9341_panel_t *ili9341 = __containerof(panel, ili9341_panel_t, base);
esp_lcd_panel_io_handle_t io = ili9341->io;
// perform hardware reset
if (ili9341->reset_gpio_num >= 0) {
gpio_set_level(ili9341->reset_gpio_num, ili9341->reset_level);
vTaskDelay(pdMS_TO_TICKS(10));
gpio_set_level(ili9341->reset_gpio_num, !ili9341->reset_level);
vTaskDelay(pdMS_TO_TICKS(10));
} else { // perform software reset
ESP_RETURN_ON_ERROR(esp_lcd_panel_io_tx_param(io, LCD_CMD_SWRESET, NULL, 0), TAG, "send command failed");
vTaskDelay(pdMS_TO_TICKS(20)); // spec, wait at least 5ms before sending new command
}
return ESP_OK;
}
static const ili9341_lcd_init_cmd_t vendor_specific_init_default[] = {
// {cmd, { data }, data_size, delay_ms}
/* Power contorl B, power control = 0, DC_ENA = 1 */
{0xCF, (uint8_t []){0x00, 0xAA, 0XE0}, 3, 0},
/* Power on sequence control,
* cp1 keeps 1 frame, 1st frame enable
* vcl = 0, ddvdh=3, vgh=1, vgl=2
* DDVDH_ENH=1
*/
{0xED, (uint8_t []){0x67, 0x03, 0X12, 0X81}, 4, 0},
/* Driver timing control A,
* non-overlap=default +1
* EQ=default - 1, CR=default
* pre-charge=default - 1
*/
{0xE8, (uint8_t []){0x8A, 0x01, 0x78}, 3, 0},
/* Power control A, Vcore=1.6V, DDVDH=5.6V */
{0xCB, (uint8_t []){0x39, 0x2C, 0x00, 0x34, 0x02}, 5, 0},
/* Pump ratio control, DDVDH=2xVCl */
{0xF7, (uint8_t []){0x20}, 1, 0},
{0xF7, (uint8_t []){0x20}, 1, 0},
/* Driver timing control, all=0 unit */
{0xEA, (uint8_t []){0x00, 0x00}, 2, 0},
/* Power control 1, GVDD=4.75V */
{0xC0, (uint8_t []){0x23}, 1, 0},
/* Power control 2, DDVDH=VCl*2, VGH=VCl*7, VGL=-VCl*3 */
{0xC1, (uint8_t []){0x11}, 1, 0},
/* VCOM control 1, VCOMH=4.025V, VCOML=-0.950V */
{0xC5, (uint8_t []){0x43, 0x4C}, 2, 0},
/* VCOM control 2, VCOMH=VMH-2, VCOML=VML-2 */
{0xC7, (uint8_t []){0xA0}, 1, 0},
/* Frame rate control, f=fosc, 70Hz fps */
{0xB1, (uint8_t []){0x00, 0x1B}, 2, 0},
/* Enable 3G, disabled */
{0xF2, (uint8_t []){0x00}, 1, 0},
/* Gamma set, curve 1 */
{0x26, (uint8_t []){0x01}, 1, 0},
/* Positive gamma correction */
{0xE0, (uint8_t []){0x1F, 0x36, 0x36, 0x3A, 0x0C, 0x05, 0x4F, 0X87, 0x3C, 0x08, 0x11, 0x35, 0x19, 0x13, 0x00}, 15, 0},
/* Negative gamma correction */
{0xE1, (uint8_t []){0x00, 0x09, 0x09, 0x05, 0x13, 0x0A, 0x30, 0x78, 0x43, 0x07, 0x0E, 0x0A, 0x26, 0x2C, 0x1F}, 15, 0},
/* Entry mode set, Low vol detect disabled, normal display */
{0xB7, (uint8_t []){0x07}, 1, 0},
/* Display function control */
{0xB6, (uint8_t []){0x08, 0x82, 0x27}, 3, 0},
};
static esp_err_t panel_ili9341_init(esp_lcd_panel_t *panel)
{
ili9341_panel_t *ili9341 = __containerof(panel, ili9341_panel_t, base);
esp_lcd_panel_io_handle_t io = ili9341->io;
// LCD goes into sleep mode and display will be turned off after power on reset, exit sleep mode first
ESP_RETURN_ON_ERROR(esp_lcd_panel_io_tx_param(io, LCD_CMD_SLPOUT, NULL, 0), TAG, "send command failed");
vTaskDelay(pdMS_TO_TICKS(100));
ESP_RETURN_ON_ERROR(esp_lcd_panel_io_tx_param(io, LCD_CMD_MADCTL, (uint8_t[]) {
ili9341->madctl_val,
}, 1), TAG, "send command failed");
ESP_RETURN_ON_ERROR(esp_lcd_panel_io_tx_param(io, LCD_CMD_COLMOD, (uint8_t[]) {
ili9341->colmod_val,
}, 1), TAG, "send command failed");
const ili9341_lcd_init_cmd_t *init_cmds = NULL;
uint16_t init_cmds_size = 0;
if (ili9341->init_cmds) {
init_cmds = ili9341->init_cmds;
init_cmds_size = ili9341->init_cmds_size;
} else {
init_cmds = vendor_specific_init_default;
init_cmds_size = sizeof(vendor_specific_init_default) / sizeof(ili9341_lcd_init_cmd_t);
}
bool is_cmd_overwritten = false;
for (int i = 0; i < init_cmds_size; i++) {
// Check if the command has been used or conflicts with the internal
switch (init_cmds[i].cmd) {
case LCD_CMD_MADCTL:
is_cmd_overwritten = true;
ili9341->madctl_val = ((uint8_t *)init_cmds[i].data)[0];
break;
case LCD_CMD_COLMOD:
is_cmd_overwritten = true;
ili9341->colmod_val = ((uint8_t *)init_cmds[i].data)[0];
break;
default:
is_cmd_overwritten = false;
break;
}
if (is_cmd_overwritten) {
ESP_LOGW(TAG, "The %02Xh command has been used and will be overwritten by external initialization sequence", init_cmds[i].cmd);
}
ESP_RETURN_ON_ERROR(esp_lcd_panel_io_tx_param(io, init_cmds[i].cmd, init_cmds[i].data, init_cmds[i].data_bytes), TAG, "send command failed");
vTaskDelay(pdMS_TO_TICKS(init_cmds[i].delay_ms));
}
ESP_LOGD(TAG, "send init commands success");
return ESP_OK;
}
static esp_err_t panel_ili9341_draw_bitmap(esp_lcd_panel_t *panel, int x_start, int y_start, int x_end, int y_end, const void *color_data)
{
ili9341_panel_t *ili9341 = __containerof(panel, ili9341_panel_t, base);
assert((x_start < x_end) && (y_start < y_end) && "start position must be smaller than end position");
esp_lcd_panel_io_handle_t io = ili9341->io;
x_start += ili9341->x_gap;
x_end += ili9341->x_gap;
y_start += ili9341->y_gap;
y_end += ili9341->y_gap;
// define an area of frame memory where MCU can access
ESP_RETURN_ON_ERROR(esp_lcd_panel_io_tx_param(io, LCD_CMD_CASET, (uint8_t[]) {
(x_start >> 8) & 0xFF,
x_start & 0xFF,
((x_end - 1) >> 8) & 0xFF,
(x_end - 1) & 0xFF,
}, 4), TAG, "send command failed");
ESP_RETURN_ON_ERROR(esp_lcd_panel_io_tx_param(io, LCD_CMD_RASET, (uint8_t[]) {
(y_start >> 8) & 0xFF,
y_start & 0xFF,
((y_end - 1) >> 8) & 0xFF,
(y_end - 1) & 0xFF,
}, 4), TAG, "send command failed");
// transfer frame buffer
size_t len = (x_end - x_start) * (y_end - y_start) * ili9341->fb_bits_per_pixel / 8;
esp_lcd_panel_io_tx_color(io, LCD_CMD_RAMWR, color_data, len);
return ESP_OK;
}
static esp_err_t panel_ili9341_invert_color(esp_lcd_panel_t *panel, bool invert_color_data)
{
ili9341_panel_t *ili9341 = __containerof(panel, ili9341_panel_t, base);
esp_lcd_panel_io_handle_t io = ili9341->io;
int command = 0;
if (invert_color_data) {
command = LCD_CMD_INVON;
} else {
command = LCD_CMD_INVOFF;
}
ESP_RETURN_ON_ERROR(esp_lcd_panel_io_tx_param(io, command, NULL, 0), TAG, "send command failed");
return ESP_OK;
}
static esp_err_t panel_ili9341_mirror(esp_lcd_panel_t *panel, bool mirror_x, bool mirror_y)
{
ili9341_panel_t *ili9341 = __containerof(panel, ili9341_panel_t, base);
esp_lcd_panel_io_handle_t io = ili9341->io;
if (mirror_x) {
ili9341->madctl_val |= LCD_CMD_MX_BIT;
} else {
ili9341->madctl_val &= ~LCD_CMD_MX_BIT;
}
if (mirror_y) {
ili9341->madctl_val |= LCD_CMD_MY_BIT;
} else {
ili9341->madctl_val &= ~LCD_CMD_MY_BIT;
}
ESP_RETURN_ON_ERROR(esp_lcd_panel_io_tx_param(io, LCD_CMD_MADCTL, (uint8_t[]) {
ili9341->madctl_val
}, 1), TAG, "send command failed");
return ESP_OK;
}
static esp_err_t panel_ili9341_swap_xy(esp_lcd_panel_t *panel, bool swap_axes)
{
ili9341_panel_t *ili9341 = __containerof(panel, ili9341_panel_t, base);
esp_lcd_panel_io_handle_t io = ili9341->io;
if (swap_axes) {
ili9341->madctl_val |= LCD_CMD_MV_BIT;
} else {
ili9341->madctl_val &= ~LCD_CMD_MV_BIT;
}
ESP_RETURN_ON_ERROR(esp_lcd_panel_io_tx_param(io, LCD_CMD_MADCTL, (uint8_t[]) {
ili9341->madctl_val
}, 1), TAG, "send command failed");
return ESP_OK;
}
static esp_err_t panel_ili9341_set_gap(esp_lcd_panel_t *panel, int x_gap, int y_gap)
{
ili9341_panel_t *ili9341 = __containerof(panel, ili9341_panel_t, base);
ili9341->x_gap = x_gap;
ili9341->y_gap = y_gap;
return ESP_OK;
}
static esp_err_t panel_ili9341_disp_on_off(esp_lcd_panel_t *panel, bool on_off)
{
ili9341_panel_t *ili9341 = __containerof(panel, ili9341_panel_t, base);
esp_lcd_panel_io_handle_t io = ili9341->io;
int command = 0;
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)
on_off = !on_off;
#endif
if (on_off) {
command = LCD_CMD_DISPON;
} else {
command = LCD_CMD_DISPOFF;
}
ESP_RETURN_ON_ERROR(esp_lcd_panel_io_tx_param(io, command, NULL, 0), TAG, "send command failed");
return ESP_OK;
}

View File

@@ -0,0 +1,10 @@
dependencies:
cmake_utilities: 0.*
idf: '>=4.4'
description: ESP LCD ILI9341
repository: git://github.com/espressif/esp-bsp.git
repository_info:
commit_sha: 5bd2a04aa9c3eb7a05a98d2c526a26bdb763c054
path: components/lcd/esp_lcd_ili9341
url: https://github.com/espressif/esp-bsp/tree/master/components/lcd/esp_lcd_ili9341
version: 2.0.1

View File

@@ -0,0 +1,101 @@
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file
* @brief ESP LCD: ILI9341
*/
#pragma once
#include "esp_lcd_panel_vendor.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief LCD panel initialization commands.
*
*/
typedef struct {
int cmd; /*<! The specific LCD command */
const void *data; /*<! Buffer that holds the command specific data */
size_t data_bytes; /*<! Size of `data` in memory, in bytes */
unsigned int delay_ms; /*<! Delay in milliseconds after this command */
} ili9341_lcd_init_cmd_t;
/**
* @brief LCD panel vendor configuration.
*
* @note This structure needs to be passed to the `vendor_config` field in `esp_lcd_panel_dev_config_t`.
*
*/
typedef struct {
const ili9341_lcd_init_cmd_t *init_cmds; /*!< Pointer to initialization commands array. Set to NULL if using default commands.
* The array should be declared as `static const` and positioned outside the function.
* Please refer to `vendor_specific_init_default` in source file.
*/
uint16_t init_cmds_size; /*<! Number of commands in above array */
} ili9341_vendor_config_t;
/**
* @brief Create LCD panel for model ILI9341
*
* @note Vendor specific initialization can be different between manufacturers, should consult the LCD supplier for initialization sequence code.
*
* @param[in] io LCD panel IO handle
* @param[in] panel_dev_config general panel device configuration
* @param[out] ret_panel Returned LCD panel handle
* @return
* - ESP_ERR_INVALID_ARG if parameter is invalid
* - ESP_ERR_NO_MEM if out of memory
* - ESP_OK on success
*/
esp_err_t esp_lcd_new_panel_ili9341(const esp_lcd_panel_io_handle_t io, const esp_lcd_panel_dev_config_t *panel_dev_config, esp_lcd_panel_handle_t *ret_panel);
/**
* @brief LCD panel bus configuration structure
*
* @param[in] sclk SPI clock pin number
* @param[in] mosi SPI MOSI pin number
* @param[in] max_trans_sz Maximum transfer size in bytes
*
*/
#define ILI9341_PANEL_BUS_SPI_CONFIG(sclk, mosi, max_trans_sz) \
{ \
.sclk_io_num = sclk, \
.mosi_io_num = mosi, \
.miso_io_num = -1, \
.quadhd_io_num = -1, \
.quadwp_io_num = -1, \
.max_transfer_sz = max_trans_sz, \
}
/**
* @brief LCD panel IO configuration structure
*
* @param[in] cs SPI chip select pin number
* @param[in] dc SPI data/command pin number
* @param[in] cb Callback function when SPI transfer is done
* @param[in] cb_ctx Callback function context
*
*/
#define ILI9341_PANEL_IO_SPI_CONFIG(cs, dc, callback, callback_ctx) \
{ \
.cs_gpio_num = cs, \
.dc_gpio_num = dc, \
.spi_mode = 0, \
.pclk_hz = 40 * 1000 * 1000, \
.trans_queue_depth = 10, \
.on_color_trans_done = callback, \
.user_ctx = callback_ctx, \
.lcd_cmd_bits = 8, \
.lcd_param_bits = 8, \
}
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,202 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

View File

@@ -0,0 +1,6 @@
# The following lines of boilerplate have to be in your project's CMakeLists
# in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/unit-test-app/components")
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(test_esp_lcd_ili9341)

View File

@@ -0,0 +1 @@
idf_component_register(SRCS "test_esp_lcd_ili9341.c")

View File

@@ -0,0 +1,6 @@
## IDF Component Manager Manifest File
dependencies:
idf: ">=4.4"
esp_lcd_ili9341:
version: "*"
override_path: "../../../esp_lcd_ili9341"

View File

@@ -0,0 +1,179 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*/
#include <inttypes.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "driver/spi_master.h"
#include "driver/gpio.h"
#include "esp_heap_caps.h"
#include "esp_log.h"
#include "esp_lcd_panel_io_interface.h"
#include "esp_lcd_panel_io.h"
#include "esp_lcd_panel_ops.h"
#include "unity.h"
#include "unity_test_runner.h"
#include "esp_lcd_ili9341.h"
#define TEST_LCD_HOST SPI2_HOST
#define TEST_LCD_H_RES (320)
#define TEST_LCD_V_RES (240)
#define TEST_LCD_BIT_PER_PIXEL (16)
#define TEST_PIN_NUM_LCD_CS (GPIO_NUM_5)
#define TEST_PIN_NUM_LCD_PCLK (GPIO_NUM_7)
#define TEST_PIN_NUM_LCD_DATA0 (GPIO_NUM_6)
#define TEST_PIN_NUM_LCD_DC (GPIO_NUM_4)
#if CONFIG_IDF_TARGET_ESP32S3
#define TEST_PIN_NUM_LCD_RST (GPIO_NUM_48)
#else
#define TEST_PIN_NUM_LCD_RST (GPIO_NUM_1)
#endif
#if CONFIG_IDF_TARGET_ESP32S3
#define TEST_PIN_NUM_LCD_BL (GPIO_NUM_45)
#else
#define TEST_PIN_NUM_LCD_BL (GPIO_NUM_0)
#endif
#define TEST_DELAY_TIME_MS (3000)
static char *TAG = "ili9341_test";
static SemaphoreHandle_t refresh_finish = NULL;
IRAM_ATTR static bool test_notify_refresh_ready(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_io_event_data_t *edata, void *user_ctx)
{
BaseType_t need_yield = pdFALSE;
xSemaphoreGiveFromISR(refresh_finish, &need_yield);
return (need_yield == pdTRUE);
}
static void test_draw_bitmap(esp_lcd_panel_handle_t panel_handle)
{
refresh_finish = xSemaphoreCreateBinary();
TEST_ASSERT_NOT_NULL(refresh_finish);
uint16_t row_line = TEST_LCD_V_RES / TEST_LCD_BIT_PER_PIXEL;
uint8_t byte_per_pixel = TEST_LCD_BIT_PER_PIXEL / 8;
uint8_t *color = (uint8_t *)heap_caps_calloc(1, row_line * TEST_LCD_H_RES * byte_per_pixel, MALLOC_CAP_DMA);
TEST_ASSERT_NOT_NULL(color);
for (int j = 0; j < TEST_LCD_BIT_PER_PIXEL; j++) {
for (int i = 0; i < row_line * TEST_LCD_H_RES; i++) {
for (int k = 0; k < byte_per_pixel; k++) {
color[i * byte_per_pixel + k] = (SPI_SWAP_DATA_TX(BIT(j), TEST_LCD_BIT_PER_PIXEL) >> (k * 8)) & 0xff;
}
}
TEST_ESP_OK(esp_lcd_panel_draw_bitmap(panel_handle, 0, j * row_line, TEST_LCD_H_RES, (j + 1) * row_line, color));
xSemaphoreTake(refresh_finish, portMAX_DELAY);
}
free(color);
vSemaphoreDelete(refresh_finish);
}
TEST_CASE("test ili9341 to draw color bar with SPI interface", "[ili9341][spi]")
{
ESP_LOGI(TAG, "Turn on the backlight");
gpio_config_t io_conf = {
.pin_bit_mask = BIT64(TEST_PIN_NUM_LCD_BL),
.mode = GPIO_MODE_OUTPUT,
.pull_up_en = GPIO_PULLUP_DISABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_DISABLE,
};
gpio_config(&io_conf);
gpio_set_level(TEST_PIN_NUM_LCD_BL, 1);
ESP_LOGI(TAG, "Initialize SPI bus");
const spi_bus_config_t buscfg = ILI9341_PANEL_BUS_SPI_CONFIG(TEST_PIN_NUM_LCD_PCLK, TEST_PIN_NUM_LCD_DATA0,
TEST_LCD_H_RES * 80 * TEST_LCD_BIT_PER_PIXEL / 8);
TEST_ESP_OK(spi_bus_initialize(TEST_LCD_HOST, &buscfg, SPI_DMA_CH_AUTO));
ESP_LOGI(TAG, "Install panel IO");
esp_lcd_panel_io_handle_t io_handle = NULL;
const esp_lcd_panel_io_spi_config_t io_config = ILI9341_PANEL_IO_SPI_CONFIG(TEST_PIN_NUM_LCD_CS, TEST_PIN_NUM_LCD_DC,
test_notify_refresh_ready, NULL);
// Attach the LCD to the SPI bus
TEST_ESP_OK(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)TEST_LCD_HOST, &io_config, &io_handle));
ESP_LOGI(TAG, "Install ili9341 panel driver");
esp_lcd_panel_handle_t panel_handle = NULL;
const esp_lcd_panel_dev_config_t panel_config = {
.reset_gpio_num = TEST_PIN_NUM_LCD_RST, // Shared with Touch reset
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)
.color_space = ESP_LCD_COLOR_SPACE_BGR,
#elif ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(6, 0, 0)
.rgb_endian = LCD_RGB_ENDIAN_BGR,
#else
.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_BGR,
#endif
.bits_per_pixel = TEST_LCD_BIT_PER_PIXEL,
};
TEST_ESP_OK(esp_lcd_new_panel_ili9341(io_handle, &panel_config, &panel_handle));
TEST_ESP_OK(esp_lcd_panel_reset(panel_handle));
TEST_ESP_OK(esp_lcd_panel_init(panel_handle));
TEST_ESP_OK(esp_lcd_panel_mirror(panel_handle, true, true));
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)
TEST_ESP_OK(esp_lcd_panel_disp_off(panel_handle, false));
#else
TEST_ESP_OK(esp_lcd_panel_disp_on_off(panel_handle, true));
#endif
test_draw_bitmap(panel_handle);
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_TIME_MS));
gpio_reset_pin(TEST_PIN_NUM_LCD_BL);
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_LCD_HOST));
}
// Some resources are lazy allocated in the LCD driver, the threadhold is left for that case
#define TEST_MEMORY_LEAK_THRESHOLD (-300)
static size_t before_free_8bit;
static size_t before_free_32bit;
static void check_leak(size_t before_free, size_t after_free, const char *type)
{
ssize_t delta = after_free - before_free;
printf("MALLOC_CAP_%s: Before %u bytes free, After %u bytes free (delta %d)\n", type, before_free, after_free, delta);
TEST_ASSERT_MESSAGE(delta >= TEST_MEMORY_LEAK_THRESHOLD, "memory leak");
}
void setUp(void)
{
before_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
before_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
}
void tearDown(void)
{
size_t after_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
size_t after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
check_leak(before_free_8bit, after_free_8bit, "8BIT");
check_leak(before_free_32bit, after_free_32bit, "32BIT");
}
void app_main(void)
{
/**
* _____ __ _____ ___ _____ _ _ _
* \_ \/ / \_ \/ _ \___ /| || | / |
* / /\/ / / /\/ (_) ||_ \| || |_| |
* /\/ /_/ /___/\/ /_ \__, |__) |__ _| |
* \____/\____/\____/ /_/____/ |_| |_|
*/
printf(" _____ __ _____ ___ _____ _ _ _\r\n");
printf(" \\_ \\/ / \\_ \\/ _ \\___ /| || | / |\r\n");
printf(" / /\\/ / / /\\/ (_) ||_ \\| || |_| |\r\n");
printf("/\\/ /_/ /___/\\/ /_ \\__, |__) |__ _| |\r\n");
printf("\\____/\\____/\\____/ /_/____/ |_| |_|\r\n");
unity_run_menu();
}

View File

@@ -0,0 +1,2 @@
CONFIG_FREERTOS_HZ=1000
CONFIG_ESP_TASK_WDT_EN=n