From f590e49f2d3de8fb006055d10f20ee09312a4509 Mon Sep 17 00:00:00 2001 From: ourkix <54349656+ourkix@users.noreply.github.com> Date: Wed, 30 Apr 2025 18:21:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B9=90=E9=91=AB=E5=BC=80?= =?UTF-8?q?=E5=8F=91=E6=9D=BFesp32-s3-lcd-ev-board=E6=94=AF=E6=8C=81=20(#5?= =?UTF-8?q?43)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 添加乐鑫开发版esp32-s3-lcd-ev-board支持 * 新增乐鑫开发版esp32-s3-lcd-ev-board支持 * 新增乐鑫开发板esp32-s3-lcd-ev-board支持,修复led和背光bug * Delete main/boards/ESP32-S3-LCD-EV-Board directory * 新增乐鑫开发板esp32-s3-lcd-ev-board支持,修复led和背光bug * 去除冗余代码 * 添加esp-s3-lcd-ev-board的硬件版本选择配置 * Delete main/boards/esp-s3-lcd-ev-board-1.4 directory * 添加esp-s3-lcd-ev-board的硬件版本选择配置 * 新增不同版本的名称标识,使ota更新时能识别 --- main/CMakeLists.txt | 2 + main/Kconfig.projbuild | 14 + main/boards/esp-s3-lcd-ev-board/README.md | 11 + main/boards/esp-s3-lcd-ev-board/config.h | 53 ++ main/boards/esp-s3-lcd-ev-board/config.json | 17 + .../esp-s3-lcd-ev-board.cc | 226 ++++++++ .../esp_io_expander_tca9554.c | 154 ++++++ .../esp_io_expander_tca9554.h | 90 ++++ .../esp-s3-lcd-ev-board/esp_lcd_gc9503.c | 504 ++++++++++++++++++ .../esp-s3-lcd-ev-board/esp_lcd_gc9503.h | 167 ++++++ main/boards/esp-s3-lcd-ev-board/pin_config.h | 62 +++ 11 files changed, 1300 insertions(+) create mode 100644 main/boards/esp-s3-lcd-ev-board/README.md create mode 100644 main/boards/esp-s3-lcd-ev-board/config.h create mode 100644 main/boards/esp-s3-lcd-ev-board/config.json create mode 100644 main/boards/esp-s3-lcd-ev-board/esp-s3-lcd-ev-board.cc create mode 100644 main/boards/esp-s3-lcd-ev-board/esp_io_expander_tca9554.c create mode 100644 main/boards/esp-s3-lcd-ev-board/esp_io_expander_tca9554.h create mode 100644 main/boards/esp-s3-lcd-ev-board/esp_lcd_gc9503.c create mode 100644 main/boards/esp-s3-lcd-ev-board/esp_lcd_gc9503.h create mode 100644 main/boards/esp-s3-lcd-ev-board/pin_config.h diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index b96be016..8fe25ebb 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -153,6 +153,8 @@ elseif(CONFIG_BOARD_TYPE_MIXGO_NOVA) set(BOARD_TYPE "mixgo-nova") elseif(CONFIG_BOARD_TYPE_ESP32_CGC) set(BOARD_TYPE "esp32-cgc") +elseif(CONFIG_BOARD_TYPE_ESP_S3_LCD_EV_Board) + set(BOARD_TYPE "esp-s3-lcd-ev-board") endif() file(GLOB BOARD_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/boards/${BOARD_TYPE}/*.cc diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild index c490e4c5..96567005 100644 --- a/main/Kconfig.projbuild +++ b/main/Kconfig.projbuild @@ -146,6 +146,20 @@ choice BOARD_TYPE bool "四博智联AI陪伴盒子" config BOARD_TYPE_MIXGO_NOVA bool "元控·青春" + config BOARD_TYPE_ESP_S3_LCD_EV_Board + bool "乐鑫ESP S3 LCD EV Board开发板" +endchoice + +choice ESP_S3_LCD_EV_Board_Version_TYPE + depends on BOARD_TYPE_ESP_S3_LCD_EV_Board + prompt "EV_BOARD Type" + default ESP_S3_LCD_EV_Board_1p4 + help + 开发板硬件版本型号选择 + config ESP_S3_LCD_EV_Board_1p4 + bool "乐鑫ESP32_S3_LCD_EV_Board-MB_V1.4" + config ESP_S3_LCD_EV_Board_1p5 + bool "乐鑫ESP32_S3_LCD_EV_Board-MB_V1.5" endchoice choice DISPLAY_OLED_TYPE diff --git a/main/boards/esp-s3-lcd-ev-board/README.md b/main/boards/esp-s3-lcd-ev-board/README.md new file mode 100644 index 00000000..8696f059 --- /dev/null +++ b/main/boards/esp-s3-lcd-ev-board/README.md @@ -0,0 +1,11 @@ +请确认自己的开发板硬件版本,如果硬件版本,在配置中进行ev_board type进行选择 +1.4与1.5只有io进行变更 +可以查看官方文档,确认具体细节https://docs.espressif.com/projects/esp-dev-kits/en/latest/esp32s3/esp32-s3-lcd-ev-board/user_guide.html +具体调整为: +I2C_SCL IO18 -> IO48 +I2C_SDA IO8 -> IO47 +LCD_DATA6 IO47 -> IO8 +LCD_DATA7 IO48 -> IO18 + + +本版本只支持了480x480的屏幕 \ No newline at end of file diff --git a/main/boards/esp-s3-lcd-ev-board/config.h b/main/boards/esp-s3-lcd-ev-board/config.h new file mode 100644 index 00000000..4db17752 --- /dev/null +++ b/main/boards/esp-s3-lcd-ev-board/config.h @@ -0,0 +1,53 @@ +#ifndef _BOARD_CONFIG_H_ +#define _BOARD_CONFIG_H_ + +#include + + +#define AUDIO_INPUT_SAMPLE_RATE 16000 +#define AUDIO_OUTPUT_SAMPLE_RATE 16000 + +#define AUDIO_I2S_GPIO_MCLK GPIO_NUM_5 +#define AUDIO_I2S_GPIO_WS GPIO_NUM_7 +#define AUDIO_I2S_GPIO_BCLK GPIO_NUM_16 +#define AUDIO_I2S_GPIO_DIN GPIO_NUM_15 +#define AUDIO_I2S_GPIO_DOUT GPIO_NUM_6 + +#define BSP_POWER_AMP_IO (IO_EXPANDER_PIN_NUM_0) +#define AUDIO_CODEC_PA_PIN GPIO_NUM_NC + +//如果开发板是V1.4 IO 定义为 +#ifdef CONFIG_ESP_S3_LCD_EV_Board_1p4 + #define AUDIO_CODEC_I2C_SDA_PIN GPIO_NUM_8 + #define AUDIO_CODEC_I2C_SCL_PIN GPIO_NUM_18 +#endif +//如果开发板是V1.5 IO 定义为 +#ifdef CONFIG_ESP_S3_LCD_EV_Board_1p5 + #define AUDIO_CODEC_I2C_SDA_PIN GPIO_NUM_47 + #define AUDIO_CODEC_I2C_SCL_PIN GPIO_NUM_48 +#endif + +#define AUDIO_CODEC_ES8311_ADDR ES8311_CODEC_DEFAULT_ADDR +#define AUDIO_CODEC_ES7210_ADDR 0x82 + + +#define BUILTIN_LED_GPIO GPIO_NUM_4 +#define BOOT_BUTTON_GPIO GPIO_NUM_0 + +#define DISPLAY_WIDTH 480 +#define DISPLAY_HEIGHT 480 +#define DISPLAY_MIRROR_X false +#define DISPLAY_MIRROR_Y false +#define DISPLAY_SWAP_XY false + +#define DISPLAY_OFFSET_X 0 +#define DISPLAY_OFFSET_Y 0 + +#define DISPLAY_BACKLIGHT_PIN GPIO_NUM_19 +#define DISPLAY_BACKLIGHT_OUTPUT_INVERT false + + + + + +#endif // _BOARD_CONFIG_H_ diff --git a/main/boards/esp-s3-lcd-ev-board/config.json b/main/boards/esp-s3-lcd-ev-board/config.json new file mode 100644 index 00000000..55d16023 --- /dev/null +++ b/main/boards/esp-s3-lcd-ev-board/config.json @@ -0,0 +1,17 @@ +{ + "target": "esp32s3", + "builds": [ + { + "name": "esp-s3-lcd-ev-board-1p4", + "sdkconfig_append": [ + "CONFIG_ESP_S3_LCD_EV_Board_1p4=y" + ] + }, + { + "name": "esp-s3-lcd-ev-board-1p5", + "sdkconfig_append": [ + "CONFIG_ESP_S3_LCD_EV_Board_1p5=y" + ] + } + ] +} diff --git a/main/boards/esp-s3-lcd-ev-board/esp-s3-lcd-ev-board.cc b/main/boards/esp-s3-lcd-ev-board/esp-s3-lcd-ev-board.cc new file mode 100644 index 00000000..208b6059 --- /dev/null +++ b/main/boards/esp-s3-lcd-ev-board/esp-s3-lcd-ev-board.cc @@ -0,0 +1,226 @@ +#include "wifi_board.h" +#include "audio_codecs/es8311_audio_codec.h" +#include "display/lcd_display.h" +#include "application.h" +#include "button.h" +#include "led/single_led.h" +#include "pin_config.h" + +#include "config.h" +#include "iot/thing_manager.h" + +#include +#include +#include +#include "esp_lcd_gc9503.h" +#include +#include +#include + +#include "audio_codecs/box_audio_codec.h" +#include "esp_io_expander_tca9554.h" + +#define TAG "ESP_S3_LCD_EV_Board" + +LV_FONT_DECLARE(font_puhui_30_4); +LV_FONT_DECLARE(font_awesome_30_4); + +class ESP_S3_LCD_EV_Board : public WifiBoard { +private: + i2c_master_bus_handle_t codec_i2c_bus_; + Button boot_button_; + LcdDisplay* display_; + + //add support ev board lcd + esp_io_expander_handle_t expander = NULL; + + void InitializeRGB_GC9503V_Display() { + ESP_LOGI(TAG, "Init GC9503V"); + + esp_lcd_panel_io_handle_t panel_io = nullptr; + + //add support ev board lcd + gpio_config_t io_conf = { + .pin_bit_mask = BIT64(GC9503V_PIN_NUM_VSYNC), + .mode = GPIO_MODE_OUTPUT, + .pull_up_en = GPIO_PULLUP_ENABLE, + }; + + gpio_config(&io_conf); + gpio_set_level(GC9503V_PIN_NUM_VSYNC, 1); + + ESP_LOGI(TAG, "Install 3-wire SPI panel IO"); + spi_line_config_t line_config = { + .cs_io_type = IO_TYPE_EXPANDER, + .cs_expander_pin = GC9503V_LCD_IO_SPI_CS_1, + .scl_io_type = IO_TYPE_EXPANDER, + .scl_expander_pin = GC9503V_LCD_IO_SPI_SCL_1, + .sda_io_type = IO_TYPE_EXPANDER, + .sda_expander_pin = GC9503V_LCD_IO_SPI_SDO_1, + .io_expander = expander, + }; + + esp_lcd_panel_io_3wire_spi_config_t io_config = GC9503_PANEL_IO_3WIRE_SPI_CONFIG(line_config, 0); + int espok = esp_lcd_new_panel_io_3wire_spi(&io_config, &panel_io); + ESP_LOGI(TAG, "Install 3-wire SPI panel IO:%d",espok); + + + ESP_LOGI(TAG, "Install RGB LCD panel driver"); + esp_lcd_panel_handle_t panel_handle = NULL; + esp_lcd_rgb_panel_config_t rgb_config = { + .clk_src = LCD_CLK_SRC_PLL160M, + //.timings = GC9503_376_960_PANEL_60HZ_RGB_TIMING(), + //add support ev board + .timings = GC9503_480_480_PANEL_60HZ_RGB_TIMING(), + .data_width = 16, // RGB565 in parallel mode, thus 16bit in width + .bits_per_pixel = 16, + .num_fbs = GC9503V_LCD_RGB_BUFFER_NUMS, + .bounce_buffer_size_px = GC9503V_LCD_H_RES * GC9503V_LCD_RGB_BOUNCE_BUFFER_HEIGHT, + .dma_burst_size = 64, + .hsync_gpio_num = GC9503V_PIN_NUM_HSYNC, + .vsync_gpio_num = GC9503V_PIN_NUM_VSYNC, + .de_gpio_num = GC9503V_PIN_NUM_DE, + .pclk_gpio_num = GC9503V_PIN_NUM_PCLK, + .disp_gpio_num = GC9503V_PIN_NUM_DISP_EN, + .data_gpio_nums = { + GC9503V_PIN_NUM_DATA0, + GC9503V_PIN_NUM_DATA1, + GC9503V_PIN_NUM_DATA2, + GC9503V_PIN_NUM_DATA3, + GC9503V_PIN_NUM_DATA4, + GC9503V_PIN_NUM_DATA5, + GC9503V_PIN_NUM_DATA6, + GC9503V_PIN_NUM_DATA7, + GC9503V_PIN_NUM_DATA8, + GC9503V_PIN_NUM_DATA9, + GC9503V_PIN_NUM_DATA10, + GC9503V_PIN_NUM_DATA11, + GC9503V_PIN_NUM_DATA12, + GC9503V_PIN_NUM_DATA13, + GC9503V_PIN_NUM_DATA14, + GC9503V_PIN_NUM_DATA15, + }, + .flags= { + .fb_in_psram = true, // allocate frame buffer in PSRAM + } + }; + + ESP_LOGI(TAG, "Initialize RGB LCD panel"); + + gc9503_vendor_config_t vendor_config = { + .rgb_config = &rgb_config, + .flags = { + .mirror_by_cmd = 0, + .auto_del_panel_io = 1, + }, + }; + const esp_lcd_panel_dev_config_t panel_config = { + .reset_gpio_num = -1, + .rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB, + // .bits_per_pixel = 16, + //add surpport ev board + .bits_per_pixel = 18, + .vendor_config = &vendor_config, + }; + (esp_lcd_new_panel_gc9503(panel_io, &panel_config, &panel_handle)); + (esp_lcd_panel_reset(panel_handle)); + (esp_lcd_panel_init(panel_handle)); + + display_ = new RgbLcdDisplay(panel_io, panel_handle, + DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, + DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY, + { + .text_font = &font_puhui_30_4, + .icon_font = &font_awesome_30_4, + .emoji_font = font_emoji_64_init(), + }); + } + void InitializeCodecI2c() { + // Initialize I2C peripheral + i2c_master_bus_config_t i2c_bus_cfg = { + .i2c_port = I2C_NUM_0, + .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN, + .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN, + .clk_source = I2C_CLK_SRC_DEFAULT, + .glitch_ignore_cnt = 7, + .intr_priority = 0, + .trans_queue_depth = 0, + .flags = { + .enable_internal_pullup = 1, + }, + }; + ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &codec_i2c_bus_)); + + //add support ev board lcd amp + //初始化扩展io口 + esp_io_expander_new_i2c_tca9554(codec_i2c_bus_, 0x20, &expander); + /* Setup power amplifier pin, set default to enable */ + esp_io_expander_set_dir(expander, BSP_POWER_AMP_IO, IO_EXPANDER_OUTPUT); + esp_io_expander_set_level(expander, BSP_POWER_AMP_IO, true); + + } + + void InitializeButtons() { + boot_button_.OnClick([this]() { + auto& app = Application::GetInstance(); + if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) { + ResetWifiConfiguration(); + } + }); + boot_button_.OnPressDown([this]() { + Application::GetInstance().StartListening(); + }); + boot_button_.OnPressUp([this]() { + Application::GetInstance().StopListening(); + }); + } + + // 物联网初始化,添加对 AI 可见设备 + void InitializeIot() { + auto& thing_manager = iot::ThingManager::GetInstance(); + thing_manager.AddThing(iot::CreateThing("Speaker")); + } + +public: + ESP_S3_LCD_EV_Board() : boot_button_(BOOT_BUTTON_GPIO) { + InitializeCodecI2c(); + InitializeButtons(); + InitializeIot(); + InitializeRGB_GC9503V_Display(); + } + + + //es7210用作音频采集 + virtual AudioCodec* GetAudioCodec() override { + static BoxAudioCodec audio_codec( + codec_i2c_bus_, + AUDIO_INPUT_SAMPLE_RATE, + AUDIO_OUTPUT_SAMPLE_RATE, + AUDIO_I2S_GPIO_MCLK, + AUDIO_I2S_GPIO_BCLK, + AUDIO_I2S_GPIO_WS, + AUDIO_I2S_GPIO_DOUT, + AUDIO_I2S_GPIO_DIN, + GPIO_NUM_NC, + AUDIO_CODEC_ES8311_ADDR, + AUDIO_CODEC_ES7210_ADDR, + true); + return &audio_codec; + } + + + + virtual Display* GetDisplay() override { + return display_; + } + + //添加彩灯显示状态,如果亮度太暗可以去更改默认亮度值 DEFAULT_BRIGHTNESS 在led的sigle_led.cc中 + virtual Led* GetLed() override { + static SingleLed led(BUILTIN_LED_GPIO); + return &led; + } + + +}; + +DECLARE_BOARD(ESP_S3_LCD_EV_Board); diff --git a/main/boards/esp-s3-lcd-ev-board/esp_io_expander_tca9554.c b/main/boards/esp-s3-lcd-ev-board/esp_io_expander_tca9554.c new file mode 100644 index 00000000..321472f3 --- /dev/null +++ b/main/boards/esp-s3-lcd-ev-board/esp_io_expander_tca9554.c @@ -0,0 +1,154 @@ +/* + * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include "esp_bit_defs.h" +#include "esp_check.h" +#include "esp_log.h" +#include "esp_io_expander.h" +#include "esp_io_expander_tca9554.h" + +/* I2C communication related */ +#define I2C_TIMEOUT_MS (1000) +#define I2C_CLK_SPEED (400000) + +#define IO_COUNT (8) + +/* Register address */ +#define INPUT_REG_ADDR (0x00) +#define OUTPUT_REG_ADDR (0x01) +#define DIRECTION_REG_ADDR (0x03) + +/* Default register value on power-up */ +#define DIR_REG_DEFAULT_VAL (0xff) +#define OUT_REG_DEFAULT_VAL (0xff) + +/** + * @brief Device Structure Type + * + */ +typedef struct { + esp_io_expander_t base; + i2c_master_dev_handle_t i2c_handle; + struct { + uint8_t direction; + uint8_t output; + } regs; +} esp_io_expander_tca9554_t; + +static char *TAG = "tca9554"; + +static esp_err_t read_input_reg(esp_io_expander_handle_t handle, uint32_t *value); +static esp_err_t write_output_reg(esp_io_expander_handle_t handle, uint32_t value); +static esp_err_t read_output_reg(esp_io_expander_handle_t handle, uint32_t *value); +static esp_err_t write_direction_reg(esp_io_expander_handle_t handle, uint32_t value); +static esp_err_t read_direction_reg(esp_io_expander_handle_t handle, uint32_t *value); +static esp_err_t reset(esp_io_expander_t *handle); +static esp_err_t del(esp_io_expander_t *handle); + +esp_err_t esp_io_expander_new_i2c_tca9554(i2c_master_bus_handle_t i2c_bus, uint32_t dev_addr, esp_io_expander_handle_t *handle_ret) +{ + ESP_RETURN_ON_FALSE(handle_ret != NULL, ESP_ERR_INVALID_ARG, TAG, "Invalid handle_ret"); + + // Allocate memory for driver object + esp_io_expander_tca9554_t *tca9554 = (esp_io_expander_tca9554_t *)calloc(1, sizeof(esp_io_expander_tca9554_t)); + ESP_RETURN_ON_FALSE(tca9554 != NULL, ESP_ERR_NO_MEM, TAG, "Malloc failed"); + + // Add new I2C device + esp_err_t ret = ESP_OK; + const i2c_device_config_t i2c_dev_cfg = { + .device_address = dev_addr, + .scl_speed_hz = I2C_CLK_SPEED, + }; + ESP_GOTO_ON_ERROR(i2c_master_bus_add_device(i2c_bus, &i2c_dev_cfg, &tca9554->i2c_handle), err, TAG, "Add new I2C device failed"); + + tca9554->base.config.io_count = IO_COUNT; + tca9554->base.config.flags.dir_out_bit_zero = 1; + tca9554->base.read_input_reg = read_input_reg; + tca9554->base.write_output_reg = write_output_reg; + tca9554->base.read_output_reg = read_output_reg; + tca9554->base.write_direction_reg = write_direction_reg; + tca9554->base.read_direction_reg = read_direction_reg; + tca9554->base.del = del; + tca9554->base.reset = reset; + + /* Reset configuration and register status */ + ESP_GOTO_ON_ERROR(reset(&tca9554->base), err, TAG, "Reset failed"); + + *handle_ret = &tca9554->base; + return ESP_OK; +err: + free(tca9554); + return ret; +} + +static esp_err_t read_input_reg(esp_io_expander_handle_t handle, uint32_t *value) +{ + esp_io_expander_tca9554_t *tca9554 = (esp_io_expander_tca9554_t *)__containerof(handle, esp_io_expander_tca9554_t, base); + + uint8_t temp = 0; + ESP_RETURN_ON_ERROR(i2c_master_transmit_receive(tca9554->i2c_handle, (uint8_t[]) { + INPUT_REG_ADDR + }, 1, &temp, sizeof(temp), I2C_TIMEOUT_MS), TAG, "Read input reg failed"); + *value = temp; + return ESP_OK; +} + +static esp_err_t write_output_reg(esp_io_expander_handle_t handle, uint32_t value) +{ + esp_io_expander_tca9554_t *tca9554 = (esp_io_expander_tca9554_t *)__containerof(handle, esp_io_expander_tca9554_t, base); + value &= 0xff; + + uint8_t data[] = {OUTPUT_REG_ADDR, value}; + ESP_RETURN_ON_ERROR(i2c_master_transmit(tca9554->i2c_handle, data, sizeof(data), I2C_TIMEOUT_MS), TAG, "Write output reg failed"); + tca9554->regs.output = value; + return ESP_OK; +} + +static esp_err_t read_output_reg(esp_io_expander_handle_t handle, uint32_t *value) +{ + esp_io_expander_tca9554_t *tca9554 = (esp_io_expander_tca9554_t *)__containerof(handle, esp_io_expander_tca9554_t, base); + + *value = tca9554->regs.output; + return ESP_OK; +} + +static esp_err_t write_direction_reg(esp_io_expander_handle_t handle, uint32_t value) +{ + esp_io_expander_tca9554_t *tca9554 = (esp_io_expander_tca9554_t *)__containerof(handle, esp_io_expander_tca9554_t, base); + value &= 0xff; + + uint8_t data[] = {DIRECTION_REG_ADDR, value}; + ESP_RETURN_ON_ERROR(i2c_master_transmit(tca9554->i2c_handle, data, sizeof(data), I2C_TIMEOUT_MS), TAG, "Write direction reg failed"); + tca9554->regs.direction = value; + return ESP_OK; +} + +static esp_err_t read_direction_reg(esp_io_expander_handle_t handle, uint32_t *value) +{ + esp_io_expander_tca9554_t *tca9554 = (esp_io_expander_tca9554_t *)__containerof(handle, esp_io_expander_tca9554_t, base); + + *value = tca9554->regs.direction; + return ESP_OK; +} + +static esp_err_t reset(esp_io_expander_t *handle) +{ + ESP_RETURN_ON_ERROR(write_direction_reg(handle, DIR_REG_DEFAULT_VAL), TAG, "Write dir reg failed"); + ESP_RETURN_ON_ERROR(write_output_reg(handle, OUT_REG_DEFAULT_VAL), TAG, "Write output reg failed"); + return ESP_OK; +} + +static esp_err_t del(esp_io_expander_t *handle) +{ + esp_io_expander_tca9554_t *tca9554 = (esp_io_expander_tca9554_t *)__containerof(handle, esp_io_expander_tca9554_t, base); + + ESP_RETURN_ON_ERROR(i2c_master_bus_rm_device(tca9554->i2c_handle), TAG, "Remove I2C device failed"); + free(tca9554); + return ESP_OK; +} diff --git a/main/boards/esp-s3-lcd-ev-board/esp_io_expander_tca9554.h b/main/boards/esp-s3-lcd-ev-board/esp_io_expander_tca9554.h new file mode 100644 index 00000000..192107ce --- /dev/null +++ b/main/boards/esp-s3-lcd-ev-board/esp_io_expander_tca9554.h @@ -0,0 +1,90 @@ +/* + * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * @file + * @brief ESP IO expander: TCA9554 + */ + +#pragma once + +#include +#include "esp_err.h" +#include "driver/i2c_master.h" +#include "esp_io_expander.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Create a TCA9554(A) IO expander object + * + * @param[in] i2c_bus I2C bus handle. Obtained from `i2c_new_master_bus()` + * @param[in] dev_addr I2C device address of chip. Can be `ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_XXX` or `ESP_IO_EXPANDER_I2C_TCA9554A_ADDRESS_XXX`. + * @param[out] handle_ret Handle to created IO expander object + * + * @return + * - ESP_OK: Success, otherwise returns ESP_ERR_xxx + */ +esp_err_t esp_io_expander_new_i2c_tca9554(i2c_master_bus_handle_t i2c_bus, uint32_t dev_addr, esp_io_expander_handle_t *handle_ret); + +/** + * @brief I2C address of the TCA9554 + * + * The 8-bit address format is as follows: + * + * (Slave Address) + * ┌─────────────────┷─────────────────┐ + * ┌─────┐─────┐─────┐─────┐─────┐─────┐─────┐─────┐ + * | 0 | 1 | 0 | 0 | A2 | A1 | A0 | R/W | + * └─────┘─────┘─────┘─────┘─────┘─────┘─────┘─────┘ + * └────────┯────────┘ └─────┯──────┘ + * (Fixed) (Hareware Selectable) + * + * And the 7-bit slave address is the most important data for users. + * For example, if a chip's A0,A1,A2 are connected to GND, it's 7-bit slave address is 0100000b(0x20). + * Then users can use `ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_000` to init it. + */ +#define ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_000 (0x20) +#define ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_001 (0x21) +#define ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_010 (0x22) +#define ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_011 (0x23) +#define ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_100 (0x24) +#define ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_101 (0x25) +#define ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_110 (0x26) +#define ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_111 (0x27) + + +/** + * @brief I2C address of the TCA9554A + * + * The 8-bit address format is as follows: + * + * (Slave Address) + * ┌─────────────────┷─────────────────┐ + * ┌─────┐─────┐─────┐─────┐─────┐─────┐─────┐─────┐ + * | 0 | 1 | 1 | 1 | A2 | A1 | A0 | R/W | + * └─────┘─────┘─────┘─────┘─────┘─────┘─────┘─────┘ + * └────────┯────────┘ └─────┯──────┘ + * (Fixed) (Hareware Selectable) + * + * And the 7-bit slave address is the most important data for users. + * For example, if a chip's A0,A1,A2 are connected to GND, it's 7-bit slave address is 0111000b(0x38). + * Then users can use `ESP_IO_EXPANDER_I2C_TCA9554A_ADDRESS_000` to init it. + */ +#define ESP_IO_EXPANDER_I2C_TCA9554A_ADDRESS_000 (0x38) +#define ESP_IO_EXPANDER_I2C_TCA9554A_ADDRESS_001 (0x39) +#define ESP_IO_EXPANDER_I2C_TCA9554A_ADDRESS_010 (0x3A) +#define ESP_IO_EXPANDER_I2C_TCA9554A_ADDRESS_011 (0x3B) +#define ESP_IO_EXPANDER_I2C_TCA9554A_ADDRESS_100 (0x3C) +#define ESP_IO_EXPANDER_I2C_TCA9554A_ADDRESS_101 (0x3D) +#define ESP_IO_EXPANDER_I2C_TCA9554A_ADDRESS_110 (0x3E) +#define ESP_IO_EXPANDER_I2C_TCA9554A_ADDRESS_111 (0x3F) + +#ifdef __cplusplus +} +#endif diff --git a/main/boards/esp-s3-lcd-ev-board/esp_lcd_gc9503.c b/main/boards/esp-s3-lcd-ev-board/esp_lcd_gc9503.c new file mode 100644 index 00000000..4db550e2 --- /dev/null +++ b/main/boards/esp-s3-lcd-ev-board/esp_lcd_gc9503.c @@ -0,0 +1,504 @@ +/* + * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "esp_lcd_gc9503.h" + +#define GC9503_CMD_MADCTL (0xB1) // Memory data access control +#define GC9503_CMD_MADCTL_DEFAULT (0x10) // Default value of Memory data access control +#define GC9503_CMD_SS_BIT (1 << 0) // Source driver scan direction, 0: top to bottom, 1: bottom to top +#define GC9503_CMD_GS_BIT (1 << 1) // Gate driver scan direction, 0: left to right, 1: right to left +#define GC9503_CMD_BGR_BIT (1 << 5) // RGB/BGR order, 0: RGB, 1: BGR + +typedef struct +{ + esp_lcd_panel_io_handle_t io; + int reset_gpio_num; + uint8_t madctl_val; // Save current value of GC9503_CMD_MADCTL register + uint8_t colmod_val; // Save current value of LCD_CMD_COLMOD register + const gc9503_lcd_init_cmd_t *init_cmds; + uint16_t init_cmds_size; + struct + { + unsigned int mirror_by_cmd : 1; + unsigned int auto_del_panel_io : 1; + unsigned int display_on_off_use_cmd : 1; + unsigned int reset_level : 1; + } flags; + // To save the original functions of RGB panel + esp_err_t (*init)(esp_lcd_panel_t *panel); + esp_err_t (*del)(esp_lcd_panel_t *panel); + esp_err_t (*reset)(esp_lcd_panel_t *panel); + esp_err_t (*mirror)(esp_lcd_panel_t *panel, bool x_axis, bool y_axis); + esp_err_t (*disp_on_off)(esp_lcd_panel_t *panel, bool on_off); +} gc9503_panel_t; + +static const char *TAG = "gc9503"; + +static esp_err_t panel_gc9503_send_init_cmds(gc9503_panel_t *gc9503); + +static esp_err_t panel_gc9503_init(esp_lcd_panel_t *panel); +static esp_err_t panel_gc9503_del(esp_lcd_panel_t *panel); +static esp_err_t panel_gc9503_reset(esp_lcd_panel_t *panel); +static esp_err_t panel_gc9503_mirror(esp_lcd_panel_t *panel, bool mirror_x, bool mirror_y); +static esp_err_t panel_gc9503_disp_on_off(esp_lcd_panel_t *panel, bool off); + +esp_err_t esp_lcd_new_panel_gc9503(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_RETURN_ON_FALSE(io && panel_dev_config && ret_panel, ESP_ERR_INVALID_ARG, TAG, "invalid arguments"); + gc9503_vendor_config_t *vendor_config = (gc9503_vendor_config_t *)panel_dev_config->vendor_config; + ESP_RETURN_ON_FALSE(vendor_config && vendor_config->rgb_config, ESP_ERR_INVALID_ARG, TAG, "`verndor_config` and `rgb_config` are necessary"); + ESP_RETURN_ON_FALSE(!vendor_config->flags.auto_del_panel_io || !vendor_config->flags.mirror_by_cmd, + ESP_ERR_INVALID_ARG, TAG, "`mirror_by_cmd` and `auto_del_panel_io` cannot work together"); + + esp_err_t ret = ESP_OK; + gpio_config_t io_conf = {0}; + + gc9503_panel_t *gc9503 = (gc9503_panel_t *)calloc(1, sizeof(gc9503_panel_t)); + ESP_RETURN_ON_FALSE(gc9503, ESP_ERR_NO_MEM, TAG, "no mem for gc9503 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"); + } + + gc9503->madctl_val = GC9503_CMD_MADCTL_DEFAULT; + switch (panel_dev_config->rgb_ele_order) + { + case LCD_RGB_ELEMENT_ORDER_RGB: + gc9503->madctl_val &= ~GC9503_CMD_BGR_BIT; + break; + case LCD_RGB_ELEMENT_ORDER_BGR: + gc9503->madctl_val |= GC9503_CMD_BGR_BIT; + break; + default: + ESP_GOTO_ON_FALSE(false, ESP_ERR_NOT_SUPPORTED, err, TAG, "unsupported color element order"); + break; + } + + gc9503->colmod_val = 0; + switch (panel_dev_config->bits_per_pixel) + { + case 16: // RGB565 + gc9503->colmod_val = 0x50; + break; + case 18: // RGB666 + gc9503->colmod_val = 0x60; + break; + case 24: // RGB888 + gc9503->colmod_val = 0x70; + break; + default: + ESP_GOTO_ON_FALSE(false, ESP_ERR_NOT_SUPPORTED, err, TAG, "unsupported pixel width"); + break; + } + + gc9503->io = io; + gc9503->init_cmds = vendor_config->init_cmds; + gc9503->init_cmds_size = vendor_config->init_cmds_size; + gc9503->reset_gpio_num = panel_dev_config->reset_gpio_num; + gc9503->flags.reset_level = panel_dev_config->flags.reset_active_high; + gc9503->flags.auto_del_panel_io = vendor_config->flags.auto_del_panel_io; + gc9503->flags.mirror_by_cmd = vendor_config->flags.mirror_by_cmd; + gc9503->flags.display_on_off_use_cmd = (vendor_config->rgb_config->disp_gpio_num >= 0) ? 0 : 1; + + if (gc9503->flags.auto_del_panel_io) + { + if (gc9503->reset_gpio_num >= 0) + { // Perform hardware reset + gpio_set_level(gc9503->reset_gpio_num, gc9503->flags.reset_level); + vTaskDelay(pdMS_TO_TICKS(10)); + gpio_set_level(gc9503->reset_gpio_num, !gc9503->flags.reset_level); + } + else + { // Perform software reset + ESP_GOTO_ON_ERROR(esp_lcd_panel_io_tx_param(io, LCD_CMD_SWRESET, NULL, 0), err, TAG, "send command failed"); + } + vTaskDelay(pdMS_TO_TICKS(120)); + + /** + * In order to enable the 3-wire SPI interface pins (such as SDA and SCK) to share other pins of the RGB interface + * (such as HSYNC) and save GPIOs, we need to send LCD initialization commands via the 3-wire SPI interface before + * `esp_lcd_new_rgb_panel()` is called. + */ + ESP_GOTO_ON_ERROR(panel_gc9503_send_init_cmds(gc9503), err, TAG, "send init commands failed"); + // After sending the initialization commands, the 3-wire SPI interface can be deleted + ESP_GOTO_ON_ERROR(esp_lcd_panel_io_del(io), err, TAG, "delete panel IO failed"); + gc9503->io = NULL; + ESP_LOGD(TAG, "delete panel IO"); + } + + // Create RGB panel + ESP_GOTO_ON_ERROR(esp_lcd_new_rgb_panel(vendor_config->rgb_config, ret_panel), err, TAG, "create RGB panel failed"); + ESP_LOGD(TAG, "new RGB panel @%p", ret_panel); + + // Save the original functions of RGB panel + gc9503->init = (*ret_panel)->init; + gc9503->del = (*ret_panel)->del; + gc9503->reset = (*ret_panel)->reset; + gc9503->mirror = (*ret_panel)->mirror; + gc9503->disp_on_off = (*ret_panel)->disp_on_off; + // Overwrite the functions of RGB panel + (*ret_panel)->init = panel_gc9503_init; + (*ret_panel)->del = panel_gc9503_del; + (*ret_panel)->reset = panel_gc9503_reset; + (*ret_panel)->mirror = panel_gc9503_mirror; + (*ret_panel)->disp_on_off = panel_gc9503_disp_on_off; + (*ret_panel)->user_data = gc9503; + ESP_LOGD(TAG, "new gc9503 panel @%p", gc9503); + + // ESP_LOGI(TAG, "LCD panel create success, version: %d.%d.%d", ESP_LCD_GC9503_VER_MAJOR, ESP_LCD_GC9503_VER_MINOR, + // ESP_LCD_GC9503_VER_PATCH); + return ESP_OK; + +err: + if (gc9503) + { + if (panel_dev_config->reset_gpio_num >= 0) + { + gpio_reset_pin(panel_dev_config->reset_gpio_num); + } + free(gc9503); + } + return ret; +} + +// *INDENT-OFF* +// static const gc9503_lcd_init_cmd_t vendor_specific_init_default[] = { +// // {cmd, { data }, data_size, delay_ms} +// {0x11, (uint8_t []){0x00}, 0, 120}, + +// {0xf0, (uint8_t []){0x55, 0xaa, 0x52, 0x08, 0x00}, 5, 0}, +// {0xf6, (uint8_t []){0x5a, 0x87}, 2, 0}, +// {0xc1, (uint8_t []){0x3f}, 1, 0}, +// {0xc2, (uint8_t []){0x0e}, 1, 0}, +// {0xc6, (uint8_t []){0xf8}, 1, 0}, +// {0xc9, (uint8_t []){0x10}, 1, 0}, +// {0xcd, (uint8_t []){0x25}, 1, 0}, +// {0xf8, (uint8_t []){0x8a}, 1, 0}, +// {0xac, (uint8_t []){0x45}, 1, 0}, +// {0xa0, (uint8_t []){0xdd}, 1, 0}, +// {0xa7, (uint8_t []){0x47}, 1, 0}, +// {0xfa, (uint8_t []){0x00, 0x00, 0x00, 0x04}, 4, 0}, +// {0x86, (uint8_t []){0x99, 0xa3, 0xa3, 0x51}, 4, 0}, +// {0xa3, (uint8_t []){0xee}, 1, 0}, +// {0xfd, (uint8_t []){0x3c, 0x3c, 0x00}, 3, 0}, +// {0x71, (uint8_t []){0x48}, 1, 0}, +// {0x72, (uint8_t []){0x48}, 1, 0}, +// {0x73, (uint8_t []){0x00, 0x44}, 2, 0}, +// {0x97, (uint8_t []){0xee}, 1, 0}, +// {0x83, (uint8_t []){0x93}, 1, 0}, +// {0x9a, (uint8_t []){0x72}, 1, 0}, +// {0x9b, (uint8_t []){0x5a}, 1, 0}, +// {0x82, (uint8_t []){0x2c, 0x2c}, 2, 0}, +// {0x6d, (uint8_t []){0x00, 0x1f, 0x19, 0x1a, 0x10, 0x0e, 0x0c, 0x0a, 0x02, 0x07, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, +// 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x08, 0x01, 0x09, 0x0b, 0x0d, 0x0f, 0x1a, 0x19, 0x1f, 0x00}, 32, 0}, +// {0x64, (uint8_t []){0x38, 0x05, 0x01, 0xdb, 0x03, 0x03, 0x38, 0x04, 0x01, 0xdc, 0x03, 0x03, 0x7a, 0x7a, 0x7a, 0x7a}, 16, 0}, +// {0x65, (uint8_t []){0x38, 0x03, 0x01, 0xdd, 0x03, 0x03, 0x38, 0x02, 0x01, 0xde, 0x03, 0x03, 0x7a, 0x7a, 0x7a, 0x7a}, 16, 0}, +// {0x66, (uint8_t []){0x38, 0x01, 0x01, 0xdf, 0x03, 0x03, 0x38, 0x00, 0x01, 0xe0, 0x03, 0x03, 0x7a, 0x7a, 0x7a, 0x7a}, 16, 0}, +// {0x67, (uint8_t []){0x30, 0x01, 0x01, 0xe1, 0x03, 0x03, 0x30, 0x02, 0x01, 0xe2, 0x03, 0x03, 0x7a, 0x7a, 0x7a, 0x7a}, 16, 0}, +// {0x68, (uint8_t []){0x00, 0x08, 0x15, 0x08, 0x15, 0x7a, 0x7a, 0x08, 0x15, 0x08, 0x15, 0x7a, 0x7a}, 13, 0}, +// {0x60, (uint8_t []){0x38, 0x08, 0x7a, 0x7a, 0x38, 0x09, 0x7a, 0x7a}, 8, 0}, +// {0x63, (uint8_t []){0x31, 0xe4, 0x7a, 0x7a, 0x31, 0xe5, 0x7a, 0x7a}, 8, 0}, +// {0x69, (uint8_t []){0x04, 0x22, 0x14, 0x22, 0x14, 0x22, 0x08}, 7, 0}, +// {0x6b, (uint8_t []){0x07}, 1, 0}, +// {0x7a, (uint8_t []){0x08, 0x13}, 2, 0}, +// {0x7b, (uint8_t []){0x08, 0x13}, 2, 0}, +// {0xd1, (uint8_t []){0x00, 0x00, 0x00, 0x04, 0x00, 0x12, 0x00, 0x18, 0x00, 0x21, 0x00, 0x2a, 0x00, 0x35, 0x00, 0x47, 0x00, +// 0x56, 0x00, 0x90, 0x00, 0xe5, 0x01, 0x68, 0x01, 0xd5, 0x01, 0xd7, 0x02, 0x36, 0x02, 0xa6, 0x02, 0xee, +// 0x03, 0x48, 0x03, 0xa0, 0x03, 0xba, 0x03, 0xc5, 0x03, 0xd0, 0x03, 0xe0, 0x03, 0xea, 0x03, 0xfa, 0x03, +// 0xff}, 52, 0}, +// {0xd2, (uint8_t []){0x00, 0x00, 0x00, 0x04, 0x00, 0x12, 0x00, 0x18, 0x00, 0x21, 0x00, 0x2a, 0x00, 0x35, 0x00, 0x47, 0x00, +// 0x56, 0x00, 0x90, 0x00, 0xe5, 0x01, 0x68, 0x01, 0xd5, 0x01, 0xd7, 0x02, 0x36, 0x02, 0xa6, 0x02, 0xee, +// 0x03, 0x48, 0x03, 0xa0, 0x03, 0xba, 0x03, 0xc5, 0x03, 0xd0, 0x03, 0xe0, 0x03, 0xea, 0x03, 0xfa, 0x03, +// 0xff}, 52, 0}, +// {0xd3, (uint8_t []){0x00, 0x00, 0x00, 0x04, 0x00, 0x12, 0x00, 0x18, 0x00, 0x21, 0x00, 0x2a, 0x00, 0x35, 0x00, 0x47, 0x00, +// 0x56, 0x00, 0x90, 0x00, 0xe5, 0x01, 0x68, 0x01, 0xd5, 0x01, 0xd7, 0x02, 0x36, 0x02, 0xa6, 0x02, 0xee, +// 0x03, 0x48, 0x03, 0xa0, 0x03, 0xba, 0x03, 0xc5, 0x03, 0xd0, 0x03, 0xe0, 0x03, 0xea, 0x03, 0xfa, 0x03, +// 0xff}, 52, 0}, +// {0xd4, (uint8_t []){0x00, 0x00, 0x00, 0x04, 0x00, 0x12, 0x00, 0x18, 0x00, 0x21, 0x00, 0x2a, 0x00, 0x35, 0x00, 0x47, 0x00, +// 0x56, 0x00, 0x90, 0x00, 0xe5, 0x01, 0x68, 0x01, 0xd5, 0x01, 0xd7, 0x02, 0x36, 0x02, 0xa6, 0x02, 0xee, +// 0x03, 0x48, 0x03, 0xa0, 0x03, 0xba, 0x03, 0xc5, 0x03, 0xd0, 0x03, 0xe0, 0x03, 0xea, 0x03, 0xfa, 0x03, +// 0xff}, 52, 0}, +// {0xd5, (uint8_t []){0x00, 0x00, 0x00, 0x04, 0x00, 0x12, 0x00, 0x18, 0x00, 0x21, 0x00, 0x2a, 0x00, 0x35, 0x00, 0x47, 0x00, +// 0x56, 0x00, 0x90, 0x00, 0xe5, 0x01, 0x68, 0x01, 0xd5, 0x01, 0xd7, 0x02, 0x36, 0x02, 0xa6, 0x02, 0xee, +// 0x03, 0x48, 0x03, 0xa0, 0x03, 0xba, 0x03, 0xc5, 0x03, 0xd0, 0x03, 0xe0, 0x03, 0xea, 0x03, 0xfa, 0x03, +// 0xff}, 52, 0}, +// {0xd6, (uint8_t []){0x00, 0x00, 0x00, 0x04, 0x00, 0x12, 0x00, 0x18, 0x00, 0x21, 0x00, 0x2a, 0x00, 0x35, 0x00, 0x47, 0x00, +// 0x56, 0x00, 0x90, 0x00, 0xe5, 0x01, 0x68, 0x01, 0xd5, 0x01, 0xd7, 0x02, 0x36, 0x02, 0xa6, 0x02, 0xee, +// 0x03, 0x48, 0x03, 0xa0, 0x03, 0xba, 0x03, 0xc5, 0x03, 0xd0, 0x03, 0xe0, 0x03, 0xea, 0x03, 0xfa, 0x03, +// 0xff}, 52, 0}, +// {0x11, (uint8_t []){0x00}, 0, 120}, +// {0x29, (uint8_t []){0x00}, 0, 20}, +// }; +static const gc9503_lcd_init_cmd_t vendor_specific_init_default[] = { + // {0x11, (uint8_t[]){}, 0, 20}, + + {0xf0, (uint8_t []){0x55, 0xaa, 0x52, 0x08, 0x00}, 5, 0}, + {0xf6, (uint8_t []){0x5a, 0x87}, 2, 0}, + {0xc1, (uint8_t []){0x3f}, 1, 0}, + {0xc2, (uint8_t []){0x0e}, 1, 0}, + {0xc6, (uint8_t []){0xf8}, 1, 0}, + {0xc9, (uint8_t []){0x10}, 1, 0}, + {0xcd, (uint8_t []){0x25}, 1, 0}, + {0xf8, (uint8_t []){0x8a}, 1, 0}, + {0xac, (uint8_t []){0x45}, 1, 0}, + {0xa0, (uint8_t []){0xdd}, 1, 0}, + {0xa7, (uint8_t []){0x47}, 1, 0}, + {0xfa, (uint8_t []){0x00, 0x00, 0x00, 0x04}, 4, 0}, + {0x86, (uint8_t []){0x99, 0xa3, 0xa3, 0x51}, 4, 0}, + {0xa3, (uint8_t []){0xee}, 1, 0}, + {0xfd, (uint8_t []){0x3c, 0x3c, 0x00}, 3, 0}, + {0x71, (uint8_t []){0x48}, 1, 0}, + {0x72, (uint8_t []){0x48}, 1, 0}, + {0x73, (uint8_t []){0x00, 0x44}, 2, 0}, + {0x97, (uint8_t []){0xee}, 1, 0}, + {0x83, (uint8_t []){0x93}, 1, 0}, + {0x9a, (uint8_t []){0x72}, 1, 0}, + {0x9b, (uint8_t []){0x5a}, 1, 0}, + {0x82, (uint8_t []){0x2c, 0x2c}, 2, 0}, + {0x6d, (uint8_t []){0x00, 0x1f, 0x19, 0x1a, 0x10, 0x0e, 0x0c, 0x0a, 0x02, 0x07, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, + 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x08, 0x01, 0x09, 0x0b, 0x0d, 0x0f, 0x1a, 0x19, 0x1f, 0x00}, 32, 0}, + {0x64, (uint8_t []){0x38, 0x05, 0x01, 0xdb, 0x03, 0x03, 0x38, 0x04, 0x01, 0xdc, 0x03, 0x03, 0x7a, 0x7a, 0x7a, 0x7a}, 16, 0}, + {0x65, (uint8_t []){0x38, 0x03, 0x01, 0xdd, 0x03, 0x03, 0x38, 0x02, 0x01, 0xde, 0x03, 0x03, 0x7a, 0x7a, 0x7a, 0x7a}, 16, 0}, + {0x66, (uint8_t []){0x38, 0x01, 0x01, 0xdf, 0x03, 0x03, 0x38, 0x00, 0x01, 0xe0, 0x03, 0x03, 0x7a, 0x7a, 0x7a, 0x7a}, 16, 0}, + {0x67, (uint8_t []){0x30, 0x01, 0x01, 0xe1, 0x03, 0x03, 0x30, 0x02, 0x01, 0xe2, 0x03, 0x03, 0x7a, 0x7a, 0x7a, 0x7a}, 16, 0}, + {0x68, (uint8_t []){0x00, 0x08, 0x15, 0x08, 0x15, 0x7a, 0x7a, 0x08, 0x15, 0x08, 0x15, 0x7a, 0x7a}, 13, 0}, + {0x60, (uint8_t []){0x38, 0x08, 0x7a, 0x7a, 0x38, 0x09, 0x7a, 0x7a}, 8, 0}, + {0x63, (uint8_t []){0x31, 0xe4, 0x7a, 0x7a, 0x31, 0xe5, 0x7a, 0x7a}, 8, 0}, + {0x69, (uint8_t []){0x04, 0x22, 0x14, 0x22, 0x14, 0x22, 0x08}, 7, 0}, + {0x6b, (uint8_t []){0x07}, 1, 0}, + {0x7a, (uint8_t []){0x08, 0x13}, 2, 0}, + {0x7b, (uint8_t []){0x08, 0x13}, 2, 0}, + {0xd1, (uint8_t []){0x00, 0x00, 0x00, 0x04, 0x00, 0x12, 0x00, 0x18, 0x00, 0x21, 0x00, 0x2a, 0x00, 0x35, 0x00, 0x47, 0x00, + 0x56, 0x00, 0x90, 0x00, 0xe5, 0x01, 0x68, 0x01, 0xd5, 0x01, 0xd7, 0x02, 0x36, 0x02, 0xa6, 0x02, 0xee, + 0x03, 0x48, 0x03, 0xa0, 0x03, 0xba, 0x03, 0xc5, 0x03, 0xd0, 0x03, 0xe0, 0x03, 0xea, 0x03, 0xfa, 0x03, + 0xff}, 52, 0}, + {0xd2, (uint8_t []){0x00, 0x00, 0x00, 0x04, 0x00, 0x12, 0x00, 0x18, 0x00, 0x21, 0x00, 0x2a, 0x00, 0x35, 0x00, 0x47, 0x00, + 0x56, 0x00, 0x90, 0x00, 0xe5, 0x01, 0x68, 0x01, 0xd5, 0x01, 0xd7, 0x02, 0x36, 0x02, 0xa6, 0x02, 0xee, + 0x03, 0x48, 0x03, 0xa0, 0x03, 0xba, 0x03, 0xc5, 0x03, 0xd0, 0x03, 0xe0, 0x03, 0xea, 0x03, 0xfa, 0x03, + 0xff}, 52, 0}, + {0xd3, (uint8_t []){0x00, 0x00, 0x00, 0x04, 0x00, 0x12, 0x00, 0x18, 0x00, 0x21, 0x00, 0x2a, 0x00, 0x35, 0x00, 0x47, 0x00, + 0x56, 0x00, 0x90, 0x00, 0xe5, 0x01, 0x68, 0x01, 0xd5, 0x01, 0xd7, 0x02, 0x36, 0x02, 0xa6, 0x02, 0xee, + 0x03, 0x48, 0x03, 0xa0, 0x03, 0xba, 0x03, 0xc5, 0x03, 0xd0, 0x03, 0xe0, 0x03, 0xea, 0x03, 0xfa, 0x03, + 0xff}, 52, 0}, + {0xd4, (uint8_t []){0x00, 0x00, 0x00, 0x04, 0x00, 0x12, 0x00, 0x18, 0x00, 0x21, 0x00, 0x2a, 0x00, 0x35, 0x00, 0x47, 0x00, + 0x56, 0x00, 0x90, 0x00, 0xe5, 0x01, 0x68, 0x01, 0xd5, 0x01, 0xd7, 0x02, 0x36, 0x02, 0xa6, 0x02, 0xee, + 0x03, 0x48, 0x03, 0xa0, 0x03, 0xba, 0x03, 0xc5, 0x03, 0xd0, 0x03, 0xe0, 0x03, 0xea, 0x03, 0xfa, 0x03, + 0xff}, 52, 0}, + {0xd5, (uint8_t []){0x00, 0x00, 0x00, 0x04, 0x00, 0x12, 0x00, 0x18, 0x00, 0x21, 0x00, 0x2a, 0x00, 0x35, 0x00, 0x47, 0x00, + 0x56, 0x00, 0x90, 0x00, 0xe5, 0x01, 0x68, 0x01, 0xd5, 0x01, 0xd7, 0x02, 0x36, 0x02, 0xa6, 0x02, 0xee, + 0x03, 0x48, 0x03, 0xa0, 0x03, 0xba, 0x03, 0xc5, 0x03, 0xd0, 0x03, 0xe0, 0x03, 0xea, 0x03, 0xfa, 0x03, + 0xff}, 52, 0}, + {0xd6, (uint8_t []){0x00, 0x00, 0x00, 0x04, 0x00, 0x12, 0x00, 0x18, 0x00, 0x21, 0x00, 0x2a, 0x00, 0x35, 0x00, 0x47, 0x00, + 0x56, 0x00, 0x90, 0x00, 0xe5, 0x01, 0x68, 0x01, 0xd5, 0x01, 0xd7, 0x02, 0x36, 0x02, 0xa6, 0x02, 0xee, + 0x03, 0x48, 0x03, 0xa0, 0x03, 0xba, 0x03, 0xc5, 0x03, 0xd0, 0x03, 0xe0, 0x03, 0xea, 0x03, 0xfa, 0x03, + 0xff}, 52, 0}, + {0x11, (uint8_t []){0x00}, 0, 120}, + {0x29, (uint8_t []){0x00}, 0, 20}, + + }; + +// *INDENT-OFF* + +static esp_err_t panel_gc9503_send_init_cmds(gc9503_panel_t *gc9503) +{ + esp_lcd_panel_io_handle_t io = gc9503->io; + + ESP_RETURN_ON_ERROR(esp_lcd_panel_io_tx_param(io, GC9503_CMD_MADCTL, (uint8_t[]){ + gc9503->madctl_val, + }, + 1), + TAG, "send command failed"); + ; + ESP_RETURN_ON_ERROR(esp_lcd_panel_io_tx_param(io, LCD_CMD_COLMOD, (uint8_t[]){ + gc9503->colmod_val, + }, + 1), + TAG, "send command failed"); + ; + + // Vendor specific initialization, it can be different between manufacturers + // should consult the LCD supplier for initialization sequence code + const gc9503_lcd_init_cmd_t *init_cmds = NULL; + uint16_t init_cmds_size = 0; + if (gc9503->init_cmds) + { + init_cmds = gc9503->init_cmds; + init_cmds_size = gc9503->init_cmds_size; + } + else + { + init_cmds = vendor_specific_init_default; + init_cmds_size = sizeof(vendor_specific_init_default) / sizeof(gc9503_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; + gc9503->madctl_val = ((uint8_t *)init_cmds[i].data)[0]; + break; + case LCD_CMD_COLMOD: + is_cmd_overwritten = true; + gc9503->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_gc9503_init(esp_lcd_panel_t *panel) +{ + gc9503_panel_t *gc9503 = (gc9503_panel_t *)panel->user_data; + + if (!gc9503->flags.auto_del_panel_io) + { + ESP_RETURN_ON_ERROR(panel_gc9503_send_init_cmds(gc9503), TAG, "send init commands failed"); + } + // Init RGB panel + ESP_RETURN_ON_ERROR(gc9503->init(panel), TAG, "init RGB panel failed"); + + return ESP_OK; +} + +static esp_err_t panel_gc9503_del(esp_lcd_panel_t *panel) +{ + gc9503_panel_t *gc9503 = (gc9503_panel_t *)panel->user_data; + + if (gc9503->reset_gpio_num >= 0) + { + gpio_reset_pin(gc9503->reset_gpio_num); + } + // Delete RGB panel + gc9503->del(panel); + free(gc9503); + ESP_LOGD(TAG, "del gc9503 panel @%p", gc9503); + return ESP_OK; +} + +static esp_err_t panel_gc9503_reset(esp_lcd_panel_t *panel) +{ + gc9503_panel_t *gc9503 = (gc9503_panel_t *)panel->user_data; + esp_lcd_panel_io_handle_t io = gc9503->io; + + // Perform hardware reset + if (gc9503->reset_gpio_num >= 0) + { + gpio_set_level(gc9503->reset_gpio_num, gc9503->flags.reset_level); + vTaskDelay(pdMS_TO_TICKS(10)); + gpio_set_level(gc9503->reset_gpio_num, !gc9503->flags.reset_level); + vTaskDelay(pdMS_TO_TICKS(120)); + } + else if (io) + { // 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(120)); + } + // Reset RGB panel + ESP_RETURN_ON_ERROR(gc9503->reset(panel), TAG, "reset RGB panel failed"); + + return ESP_OK; +} + +static esp_err_t panel_gc9503_mirror(esp_lcd_panel_t *panel, bool mirror_x, bool mirror_y) +{ + gc9503_panel_t *gc9503 = (gc9503_panel_t *)panel->user_data; + esp_lcd_panel_io_handle_t io = gc9503->io; + + if (gc9503->flags.mirror_by_cmd) + { + ESP_RETURN_ON_FALSE(io, ESP_FAIL, TAG, "Panel IO is deleted, cannot send command"); + // Control mirror through LCD command + if (mirror_x) + { + gc9503->madctl_val |= GC9503_CMD_GS_BIT; + } + else + { + gc9503->madctl_val &= ~GC9503_CMD_GS_BIT; + } + if (mirror_y) + { + gc9503->madctl_val |= GC9503_CMD_SS_BIT; + } + else + { + gc9503->madctl_val &= ~GC9503_CMD_SS_BIT; + } + ESP_RETURN_ON_ERROR(esp_lcd_panel_io_tx_param(io, GC9503_CMD_MADCTL, (uint8_t[]){ + gc9503->madctl_val, + }, + 1), + TAG, "send command failed"); + ; + } + else + { + // Control mirror through RGB panel + ESP_RETURN_ON_ERROR(gc9503->mirror(panel, mirror_x, mirror_y), TAG, "RGB panel mirror failed"); + } + return ESP_OK; +} + +static esp_err_t panel_gc9503_disp_on_off(esp_lcd_panel_t *panel, bool on_off) +{ + gc9503_panel_t *gc9503 = (gc9503_panel_t *)panel->user_data; + esp_lcd_panel_io_handle_t io = gc9503->io; + int command = 0; + + if (gc9503->flags.display_on_off_use_cmd) + { + ESP_RETURN_ON_FALSE(io, ESP_FAIL, TAG, "Panel IO is deleted, cannot send command"); + // Control display on/off through LCD command + 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"); + } + else + { + // Control display on/off through display control signal + ESP_RETURN_ON_ERROR(gc9503->disp_on_off(panel, on_off), TAG, "RGB panel disp_on_off failed"); + } + return ESP_OK; +} diff --git a/main/boards/esp-s3-lcd-ev-board/esp_lcd_gc9503.h b/main/boards/esp-s3-lcd-ev-board/esp_lcd_gc9503.h new file mode 100644 index 00000000..8feafd19 --- /dev/null +++ b/main/boards/esp-s3-lcd-ev-board/esp_lcd_gc9503.h @@ -0,0 +1,167 @@ +/* + * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +/** + * @file + * @brief ESP LCD: GC9503 + */ + +#pragma once + +#include + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief LCD panel initialization commands. + * + */ +typedef struct { + int cmd; /*