From 05f1a034ce92ab768c1eb99e32d9c01a64105a16 Mon Sep 17 00:00:00 2001 From: ggc121238 <64193327+ggc121238@users.noreply.github.com> Date: Mon, 2 Mar 2026 18:11:30 +0800 Subject: [PATCH] add waveshre ESP32-Touch-LCD-3.5 (#1794) Co-authored-by: ggc <1648371663@q.com> --- main/CMakeLists.txt | 6 + main/Kconfig.projbuild | 3 + .../waveshare/esp32-touch-lcd-3.5/README.md | 3 + .../waveshare/esp32-touch-lcd-3.5/config.h | 55 +++ .../waveshare/esp32-touch-lcd-3.5/config.json | 8 + .../esp32-touch-lcd-3.5.cc | 332 ++++++++++++++++++ 6 files changed, 407 insertions(+) create mode 100644 main/boards/waveshare/esp32-touch-lcd-3.5/README.md create mode 100644 main/boards/waveshare/esp32-touch-lcd-3.5/config.h create mode 100644 main/boards/waveshare/esp32-touch-lcd-3.5/config.json create mode 100644 main/boards/waveshare/esp32-touch-lcd-3.5/esp32-touch-lcd-3.5.cc diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 218bf2b..1abebde 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -267,6 +267,12 @@ elseif(CONFIG_BOARD_TYPE_ESP_SENSAIRSHUTTLE) set(BUILTIN_TEXT_FONT font_puhui_basic_16_4) set(BUILTIN_ICON_FONT font_awesome_16_4) set(DEFAULT_EMOJI_COLLECTION twemoji_32) +elseif(CONFIG_BOARD_TYPE_WAVESHARE_ESP32_TOUCH_LCD_3_5) + set(MANUFACTURER "waveshare") + set(BOARD_TYPE "esp32-touch-lcd-3.5") + set(BUILTIN_TEXT_FONT font_puhui_basic_20_4) + set(BUILTIN_ICON_FONT font_awesome_20_4) + set(DEFAULT_EMOJI_COLLECTION twemoji_64) elseif(CONFIG_BOARD_TYPE_WAVESHARE_ESP32_S3_AUDIO_BOARD) set(MANUFACTURER "waveshare") set(BOARD_TYPE "esp32-s3-audio-board") diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild index ebf30ae..d1616a8 100644 --- a/main/Kconfig.projbuild +++ b/main/Kconfig.projbuild @@ -266,6 +266,9 @@ choice BOARD_TYPE config BOARD_TYPE_M5STACK_ATOM_MATRIX_ECHO_BASE bool "M5Stack AtomMatrix + Echo Base" depends on IDF_TARGET_ESP32 + config BOARD_TYPE_WAVESHARE_ESP32_TOUCH_LCD_3_5 + bool "Waveshare ESP32-Touch-LCD-3.5" + depends on IDF_TARGET_ESP32 config BOARD_TYPE_WAVESHARE_ESP32_S3_AUDIO_BOARD bool "Waveshare ESP32-S3-Audio-Board" depends on IDF_TARGET_ESP32S3 diff --git a/main/boards/waveshare/esp32-touch-lcd-3.5/README.md b/main/boards/waveshare/esp32-touch-lcd-3.5/README.md new file mode 100644 index 0000000..6eb85df --- /dev/null +++ b/main/boards/waveshare/esp32-touch-lcd-3.5/README.md @@ -0,0 +1,3 @@ +新增 微雪 开发板: ESP32-Touch-LCD-3.5 +产品链接: +https://www.waveshare.net/shop/ESP32-Touch-LCD-3.5.htm \ No newline at end of file diff --git a/main/boards/waveshare/esp32-touch-lcd-3.5/config.h b/main/boards/waveshare/esp32-touch-lcd-3.5/config.h new file mode 100644 index 0000000..c800733 --- /dev/null +++ b/main/boards/waveshare/esp32-touch-lcd-3.5/config.h @@ -0,0 +1,55 @@ +#ifndef _BOARD_CONFIG_H_ +#define _BOARD_CONFIG_H_ + +#include +#include + +#define AUDIO_INPUT_SAMPLE_RATE 24000 +#define AUDIO_OUTPUT_SAMPLE_RATE 24000 + +#define AUDIO_I2S_GPIO_MCLK GPIO_NUM_3 +#define AUDIO_I2S_GPIO_WS GPIO_NUM_4 +#define AUDIO_I2S_GPIO_BCLK GPIO_NUM_2 +#define AUDIO_I2S_GPIO_DIN GPIO_NUM_34 +#define AUDIO_I2S_GPIO_DOUT GPIO_NUM_12 + +#define AUDIO_CODEC_PA_PIN GPIO_NUM_NC +#define AUDIO_CODEC_I2C_SDA_PIN GPIO_NUM_21 +#define AUDIO_CODEC_I2C_SCL_PIN GPIO_NUM_22 +#define AUDIO_CODEC_ES8311_ADDR ES8311_CODEC_DEFAULT_ADDR + +#define BUILTIN_LED_GPIO GPIO_NUM_NC +#define BOOT_BUTTON_GPIO GPIO_NUM_0 +#define PWR_BUTTON_GPIO GPIO_NUM_36 +#define VOLUME_UP_BUTTON_GPIO GPIO_NUM_NC +#define VOLUME_DOWN_BUTTON_GPIO GPIO_NUM_NC + +#define DISPLAY_SPI_MODE 0 +#define DISPLAY_CS_PIN GPIO_NUM_5 +#define DISPLAY_MOSI_PIN GPIO_NUM_23 +#define DISPLAY_MISO_PIN GPIO_NUM_19 +#define DISPLAY_CLK_PIN GPIO_NUM_18 +#define DISPLAY_DC_PIN GPIO_NUM_27 +#define DISPLAY_RST_PIN GPIO_NUM_NC + + + +#define DISPLAY_WIDTH 480 +#define DISPLAY_HEIGHT 320 +#define DISPLAY_MIRROR_X false +#define DISPLAY_MIRROR_Y false +#define DISPLAY_SWAP_XY true +#define DISPLAY_RGB_ORDER LCD_RGB_ELEMENT_ORDER_BGR +#define DISPLAY_INVERT_COLOR true + +#define DISPLAY_OFFSET_X 0 +#define DISPLAY_OFFSET_Y 0 + +#define DISPLAY_BACKLIGHT_PIN GPIO_NUM_25 +#define DISPLAY_BACKLIGHT_OUTPUT_INVERT false + + + + + +#endif // _BOARD_CONFIG_H_ diff --git a/main/boards/waveshare/esp32-touch-lcd-3.5/config.json b/main/boards/waveshare/esp32-touch-lcd-3.5/config.json new file mode 100644 index 0000000..0d0c1b9 --- /dev/null +++ b/main/boards/waveshare/esp32-touch-lcd-3.5/config.json @@ -0,0 +1,8 @@ +{ + "target": "esp32", + "builds": [ + { + "name": "esp32-touch-lcd-3.5" + } + ] +} \ No newline at end of file diff --git a/main/boards/waveshare/esp32-touch-lcd-3.5/esp32-touch-lcd-3.5.cc b/main/boards/waveshare/esp32-touch-lcd-3.5/esp32-touch-lcd-3.5.cc new file mode 100644 index 0000000..0154bd1 --- /dev/null +++ b/main/boards/waveshare/esp32-touch-lcd-3.5/esp32-touch-lcd-3.5.cc @@ -0,0 +1,332 @@ +#include "wifi_board.h" +#include "codecs/es8311_audio_codec.h" +#include "display/lcd_display.h" +#include "system_reset.h" +#include "application.h" +#include "button.h" +#include "config.h" +#include "mcp_server.h" + +#include +#include "i2c_device.h" +#include +#include +#include +#include +#include +#include + +#include +#include "esp_io_expander_tca9554.h" + +#include "axp2101.h" +#include "power_save_timer.h" + +#include +#include + +#define TAG "waveshare_lcd_3_5" + +class Pmic : public Axp2101 { + public: + Pmic(i2c_master_bus_handle_t i2c_bus, uint8_t addr) : Axp2101(i2c_bus, addr) { + WriteReg(0x22, 0b110); // PWRON > OFFLEVEL as POWEROFF Source enable + WriteReg(0x27, 0x10); // hold 4s to power off + + // Disable All DCs but DC1 + WriteReg(0x80, 0x01); + // Disable All LDOs + WriteReg(0x90, 0x00); + WriteReg(0x91, 0x00); + + // Set DC1 to 3.3V + WriteReg(0x82, (3300 - 1500) / 100); + + // Set ALDO1 to 3.3V + WriteReg(0x92, (3300 - 500) / 100); + + WriteReg(0x96, (1500 - 500) / 100); + WriteReg(0x97, (2800 - 500) / 100); + + // Enable ALDO1 BLDO1 BLDO2 + WriteReg(0x90, 0x31); + + WriteReg(0x64, 0x02); // CV charger voltage setting to 4.1V + + WriteReg(0x61, 0x02); // set Main battery precharge current to 50mA + WriteReg(0x62, 0x08); // set Main battery charger current to 400mA ( 0x08-200mA, 0x09-300mA, 0x0A-400mA ) + WriteReg(0x63, 0x01); // set Main battery term charge current to 25mA + } + }; + +typedef struct { + int cmd; /*OnEnterSleepMode([this]() { + GetDisplay()->SetPowerSaveMode(true); + GetBacklight()->SetBrightness(20); + }); + power_save_timer_->OnExitSleepMode([this]() { + GetDisplay()->SetPowerSaveMode(false); + GetBacklight()->RestoreBrightness(); + }); + power_save_timer_->OnShutdownRequest([this]() { + pmic_->PowerOff(); + }); + power_save_timer_->SetEnabled(true); + } + + void InitializeI2c() { + // Initialize I2C peripheral + i2c_master_bus_config_t i2c_bus_cfg = { + .i2c_port = (i2c_port_t)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, &i2c_bus_)); + } + + void InitializeTca9554(void) + { + esp_err_t ret = esp_io_expander_new_i2c_tca9554(i2c_bus_, ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_000, &io_expander); + if (ret != ESP_OK) + ESP_LOGE(TAG, "TCA9554 create returned error"); + ret = esp_io_expander_set_dir(io_expander, IO_EXPANDER_PIN_NUM_0 | IO_EXPANDER_PIN_NUM_1 | IO_EXPANDER_PIN_NUM_2, IO_EXPANDER_OUTPUT); + ESP_ERROR_CHECK(ret); + vTaskDelay(pdMS_TO_TICKS(100)); + ret = esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_1 | IO_EXPANDER_PIN_NUM_1, 0); + ESP_ERROR_CHECK(ret); + vTaskDelay(pdMS_TO_TICKS(100)); + ret = esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_0 | IO_EXPANDER_PIN_NUM_1 | IO_EXPANDER_PIN_NUM_2, 1); + ESP_ERROR_CHECK(ret); + } + + void InitializeAxp2101() { + ESP_LOGI(TAG, "Init AXP2101"); + pmic_ = new Pmic(i2c_bus_, 0x34); + } + + void InitializeSpi() { + ESP_LOGI(TAG, "Initialize QSPI bus"); + spi_bus_config_t buscfg = {}; + buscfg.mosi_io_num = DISPLAY_MOSI_PIN; + buscfg.miso_io_num = DISPLAY_MISO_PIN; + buscfg.sclk_io_num = DISPLAY_CLK_PIN; + buscfg.quadwp_io_num = GPIO_NUM_NC; + buscfg.quadhd_io_num = GPIO_NUM_NC; + buscfg.max_transfer_sz = 0; + ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO)); + } + void InitializeTouch() + { + esp_lcd_touch_handle_t tp; + esp_lcd_touch_config_t tp_cfg = { + .x_max = DISPLAY_WIDTH, + .y_max = DISPLAY_HEIGHT, + .rst_gpio_num = GPIO_NUM_NC, + .int_gpio_num = GPIO_NUM_NC, + .levels = { + .reset = 0, + .interrupt = 0, + }, + .flags = { + .swap_xy = 1, + .mirror_x = 1, + .mirror_y = 1, + }, + }; + esp_lcd_panel_io_handle_t tp_io_handle = NULL; + esp_lcd_panel_io_i2c_config_t tp_io_config = ESP_LCD_TOUCH_IO_I2C_FT5x06_CONFIG(); + tp_io_config.scl_speed_hz = 400 * 1000; + ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c(i2c_bus_, &tp_io_config, &tp_io_handle)); + ESP_LOGI(TAG, "Initialize touch controller"); + ESP_ERROR_CHECK(esp_lcd_touch_new_i2c_ft5x06(tp_io_handle, &tp_cfg, &tp)); + const lvgl_port_touch_cfg_t touch_cfg = { + .disp = lv_display_get_default(), + .handle = tp, + }; + lvgl_port_add_touch(&touch_cfg); + ESP_LOGI(TAG, "Touch panel initialized successfully"); + } + + void InitializeLcdDisplay() { + esp_lcd_panel_io_handle_t panel_io = nullptr; + esp_lcd_panel_handle_t panel = nullptr; + // 液晶屏控制IO初始化 + ESP_LOGI(TAG, "Install panel IO"); + esp_lcd_panel_io_spi_config_t io_config = {}; + io_config.cs_gpio_num = DISPLAY_CS_PIN; + io_config.dc_gpio_num = DISPLAY_DC_PIN; + io_config.spi_mode = DISPLAY_SPI_MODE; + io_config.pclk_hz = 24 * 1000 * 1000; + io_config.trans_queue_depth = 10; + io_config.lcd_cmd_bits = 8; + io_config.lcd_param_bits = 8; + ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI2_HOST, &io_config, &panel_io)); + + st7796_vendor_config_t st7796_vendor_config = { + .init_cmds = st7796_lcd_init_cmds, + .init_cmds_size = sizeof(st7796_lcd_init_cmds) / sizeof(st7796_lcd_init_cmd_t), + }; + + // 初始化液晶屏驱动芯片 + ESP_LOGI(TAG, "Install LCD driver"); + esp_lcd_panel_dev_config_t panel_config = {}; + panel_config.reset_gpio_num = DISPLAY_RST_PIN; + panel_config.rgb_ele_order = DISPLAY_RGB_ORDER; + panel_config.bits_per_pixel = 16; + panel_config.vendor_config = &st7796_vendor_config; + + ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(panel_io, &panel_config, &panel)); + + esp_lcd_panel_reset(panel); + + esp_lcd_panel_init(panel); + esp_lcd_panel_invert_color(panel, DISPLAY_INVERT_COLOR); + esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY); + esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y); + + display_ = new SpiLcdDisplay(panel_io, panel, + DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY); + } + + void InitializeButtons() { + boot_button_.OnClick([this]() { + auto& app = Application::GetInstance(); + // During startup (before connected), pressing BOOT button enters Wi-Fi config mode without reboot + if (app.GetDeviceState() == kDeviceStateStarting) { + EnterWifiConfigMode(); + return; + } + app.ToggleChatState(); + }); + +#if CONFIG_USE_DEVICE_AEC + boot_button_.OnDoubleClick([this]() { + auto& app = Application::GetInstance(); + if (app.GetDeviceState() == kDeviceStateIdle) { + app.SetAecMode(app.GetAecMode() == kAecOff ? kAecOnDeviceSide : kAecOff); + } + }); +#endif + } + + // 初始化工具 + void InitializeTools() { + auto &mcp_server = McpServer::GetInstance(); + mcp_server.AddTool("self.system.reconfigure_wifi", + "End this conversation and enter WiFi configuration mode.\n" + "**CAUTION** You must ask the user to confirm this action.", + PropertyList(), [this](const PropertyList& properties) { + EnterWifiConfigMode(); + return true; + }); + } + +public: + WaveshareEsp32TouchLCD3inch5() : + boot_button_(BOOT_BUTTON_GPIO),pwr_button_(PWR_BUTTON_GPIO, true) { + InitializePowerSaveTimer(); + InitializeI2c(); + InitializeTca9554(); + InitializeAxp2101(); + InitializeSpi(); + InitializeLcdDisplay(); + InitializeTouch(); + InitializeButtons(); + InitializeTools(); + GetBacklight()->RestoreBrightness(); + } + + virtual AudioCodec* GetAudioCodec() override { + static Es8311AudioCodec audio_codec(i2c_bus_, I2C_NUM_0, 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, + AUDIO_CODEC_PA_PIN, AUDIO_CODEC_ES8311_ADDR); + return &audio_codec; + } + + virtual Display* GetDisplay() override { + return display_; + } + + virtual Backlight* GetBacklight() override { + static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT); + return &backlight; + } + virtual bool GetBatteryLevel(int &level, bool& charging, bool& discharging) override { + static bool last_discharging = false; + charging = pmic_->IsCharging(); + discharging = pmic_->IsDischarging(); + if (discharging != last_discharging) { + power_save_timer_->SetEnabled(discharging); + last_discharging = discharging; + } + + level = pmic_->GetBatteryLevel(); + return true; + } + + virtual void SetPowerSaveLevel(PowerSaveLevel level) override { + if (level != PowerSaveLevel::LOW_POWER) { + power_save_timer_->WakeUp(); + } + WifiBoard::SetPowerSaveLevel(level); + } + +}; + +DECLARE_BOARD(WaveshareEsp32TouchLCD3inch5);