diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 8603bd1b..e9952679 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -61,6 +61,8 @@ elseif(CONFIG_BOARD_TYPE_MAGICLICK_C3) set(BOARD_TYPE "magiclick-c3") elseif(CONFIG_BOARD_TYPE_M5STACK_CORE_S3) set(BOARD_TYPE "m5stack-core-s3") +elseif(CONFIG_BOARD_TYPE_ATOMS3_ECHO_BASE) + set(BOARD_TYPE "atoms3-echo-base") elseif(CONFIG_BOARD_TYPE_ATOMS3R_ECHO_BASE) set(BOARD_TYPE "atoms3r-echo-base") elseif(CONFIG_BOARD_TYPE_ATOMMATRIX_ECHO_BASE) diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild index 0a4421de..03225190 100644 --- a/main/Kconfig.projbuild +++ b/main/Kconfig.projbuild @@ -62,6 +62,8 @@ choice BOARD_TYPE bool "神奇按钮 Magiclick_C3" config BOARD_TYPE_M5STACK_CORE_S3 bool "M5Stack CoreS3" + config BOARD_TYPE_ATOMS3_ECHO_BASE + bool "AtomS3 + Echo Base" config BOARD_TYPE_ATOMS3R_ECHO_BASE bool "AtomS3R + Echo Base" config BOARD_TYPE_ATOMMATRIX_ECHO_BASE diff --git a/main/boards/atoms3-echo-base/README.md b/main/boards/atoms3-echo-base/README.md new file mode 100644 index 00000000..dfd9b0c7 --- /dev/null +++ b/main/boards/atoms3-echo-base/README.md @@ -0,0 +1,49 @@ +# 编译配置命令 + +**配置编译目标为 ESP32S3:** + +```bash +idf.py set-target esp32s3 +``` + +**打开 menuconfig:** + +```bash +idf.py menuconfig +``` + +**选择板子:** + +``` +Xiaozhi Assistant -> Board Type -> AtomS3 + Echo Base +``` + +**关闭语音唤醒:** + +``` +Xiaozhi Assistant -> [ ] 启用语音唤醒与音频处理 -> Unselect +``` + +**修改 flash 大小:** + +``` +Serial flasher config -> Flash size -> 8 MB +``` + +**修改分区表:** + +``` +Partition Table -> Custom partition CSV file -> partitions_8M.csv +``` + +**关闭片外 PSRAM:** + +``` +Component config -> ESP PSRAM -> [ ] Support for external, SPI-connected RAM -> Unselect +``` + +**编译:** + +```bash +idf.py build +``` \ No newline at end of file diff --git a/main/boards/atoms3-echo-base/atoms3_echo_base.cc b/main/boards/atoms3-echo-base/atoms3_echo_base.cc new file mode 100644 index 00000000..1c964663 --- /dev/null +++ b/main/boards/atoms3-echo-base/atoms3_echo_base.cc @@ -0,0 +1,238 @@ +#include "wifi_board.h" +#include "audio_codecs/es8311_audio_codec.h" +#include "display/lcd_display.h" +#include "display/no_display.h" +#include "application.h" +#include "button.h" +#include "config.h" +#include "i2c_device.h" +#include "iot/thing_manager.h" + +#include +#include +#include +#include +#include +#include + +#define TAG "AtomS3+EchoBase" + +LV_FONT_DECLARE(font_puhui_16_4); +LV_FONT_DECLARE(font_awesome_16_4); + +static const gc9a01_lcd_init_cmd_t gc9107_lcd_init_cmds[] = { + // {cmd, { data }, data_size, delay_ms} + {0xfe, (uint8_t[]){0x00}, 0, 0}, + {0xef, (uint8_t[]){0x00}, 0, 0}, + {0xb0, (uint8_t[]){0xc0}, 1, 0}, + {0xb2, (uint8_t[]){0x2f}, 1, 0}, + {0xb3, (uint8_t[]){0x03}, 1, 0}, + {0xb6, (uint8_t[]){0x19}, 1, 0}, + {0xb7, (uint8_t[]){0x01}, 1, 0}, + {0xac, (uint8_t[]){0xcb}, 1, 0}, + {0xab, (uint8_t[]){0x0e}, 1, 0}, + {0xb4, (uint8_t[]){0x04}, 1, 0}, + {0xa8, (uint8_t[]){0x19}, 1, 0}, + {0xb8, (uint8_t[]){0x08}, 1, 0}, + {0xe8, (uint8_t[]){0x24}, 1, 0}, + {0xe9, (uint8_t[]){0x48}, 1, 0}, + {0xea, (uint8_t[]){0x22}, 1, 0}, + {0xc6, (uint8_t[]){0x30}, 1, 0}, + {0xc7, (uint8_t[]){0x18}, 1, 0}, + {0xf0, + (uint8_t[]){0x1f, 0x28, 0x04, 0x3e, 0x2a, 0x2e, 0x20, 0x00, 0x0c, 0x06, + 0x00, 0x1c, 0x1f, 0x0f}, + 14, 0}, + {0xf1, + (uint8_t[]){0x00, 0x2d, 0x2f, 0x3c, 0x6f, 0x1c, 0x0b, 0x00, 0x00, 0x00, + 0x07, 0x0d, 0x11, 0x0f}, + 14, 0}, +}; + +class AtomS3EchoBaseBoard : public WifiBoard { +private: + i2c_master_bus_handle_t i2c_bus_; + Display* display_; + Button boot_button_; + bool is_echo_base_connected_ = false; + void InitializeI2c() { + // Initialize I2C peripheral + i2c_master_bus_config_t i2c_bus_cfg = { + .i2c_port = I2C_NUM_1, + .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 I2cDetect() { + is_echo_base_connected_ = false; + uint8_t echo_base_connected_flag = 0x00; + uint8_t address; + printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n"); + for (int i = 0; i < 128; i += 16) { + printf("%02x: ", i); + for (int j = 0; j < 16; j++) { + fflush(stdout); + address = i + j; + esp_err_t ret = i2c_master_probe(i2c_bus_, address, pdMS_TO_TICKS(200)); + if (ret == ESP_OK) { + printf("%02x ", address); + if (address == 0x18) { + echo_base_connected_flag |= 0xF0; + } else if (address == 0x43) { + echo_base_connected_flag |= 0x0F; + } + } else if (ret == ESP_ERR_TIMEOUT) { + printf("UU "); + } else { + printf("-- "); + } + } + printf("\r\n"); + } + is_echo_base_connected_ = (echo_base_connected_flag == 0xFF); + } + + void CheckEchoBaseConnection() { + if (is_echo_base_connected_) { + return; + } + + // Pop error page + InitializeSpi(); + InitializeGc9107Display(); + InitializeButtons(); + display_->SetStatus("错误"); + display_->SetEmotion("sad"); + display_->SetChatMessage("", "Echo Base\n未连接"); + + while (1) { + ESP_LOGE(TAG, "Atomic Echo Base is disconnected"); + vTaskDelay(pdMS_TO_TICKS(1000)); + + // Rerun detection + I2cDetect(); + if (is_echo_base_connected_) { + vTaskDelay(pdMS_TO_TICKS(500)); + I2cDetect(); + if (is_echo_base_connected_) { + ESP_LOGI(TAG, "Atomic Echo Base is reconnected"); + vTaskDelay(pdMS_TO_TICKS(200)); + esp_restart(); + } + } + } + } + + void InitializeSpi() { + ESP_LOGI(TAG, "Initialize SPI bus"); + spi_bus_config_t buscfg = {}; + buscfg.mosi_io_num = GPIO_NUM_21; + buscfg.miso_io_num = GPIO_NUM_NC; + buscfg.sclk_io_num = GPIO_NUM_17; + buscfg.quadwp_io_num = GPIO_NUM_NC; + buscfg.quadhd_io_num = GPIO_NUM_NC; + buscfg.max_transfer_sz = DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t); + ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO)); + } + + void InitializeGc9107Display() { + ESP_LOGI(TAG, "Init GC9107 display"); + + ESP_LOGI(TAG, "Install panel IO"); + esp_lcd_panel_io_handle_t io_handle = NULL; + esp_lcd_panel_io_spi_config_t io_config = {}; + io_config.cs_gpio_num = GPIO_NUM_15; + io_config.dc_gpio_num = GPIO_NUM_33; + io_config.spi_mode = 0; + io_config.pclk_hz = 40 * 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(SPI3_HOST, &io_config, &io_handle)); + + ESP_LOGI(TAG, "Install GC9A01 panel driver"); + esp_lcd_panel_handle_t panel_handle = NULL; + gc9a01_vendor_config_t gc9107_vendor_config = { + .init_cmds = gc9107_lcd_init_cmds, + .init_cmds_size = sizeof(gc9107_lcd_init_cmds) / sizeof(gc9a01_lcd_init_cmd_t), + }; + esp_lcd_panel_dev_config_t panel_config = {}; + panel_config.reset_gpio_num = GPIO_NUM_34; // Set to -1 if not use + panel_config.rgb_endian = LCD_RGB_ENDIAN_RGB; + panel_config.bits_per_pixel = 16; // Implemented by LCD command `3Ah` (16/18) + panel_config.vendor_config = &gc9107_vendor_config; + + ESP_ERROR_CHECK(esp_lcd_new_panel_gc9a01(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)); + + + display_ = new LcdDisplay(io_handle, panel_handle, DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT, + DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY, + { + .text_font = &font_puhui_16_4, + .icon_font = &font_awesome_16_4, + .emoji_font = font_emoji_32_init(), + }); + } + + void InitializeButtons() { + boot_button_.OnClick([this]() { + auto& app = Application::GetInstance(); + if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) { + ResetWifiConfiguration(); + } + app.ToggleChatState(); + }); + } + + // 物联网初始化,添加对 AI 可见设备 + void InitializeIot() { + auto& thing_manager = iot::ThingManager::GetInstance(); + thing_manager.AddThing(iot::CreateThing("Speaker")); + } + +public: + AtomS3EchoBaseBoard() : boot_button_(BOOT_BUTTON_GPIO) { + InitializeI2c(); + I2cDetect(); + CheckEchoBaseConnection(); + InitializeSpi(); + InitializeGc9107Display(); + InitializeButtons(); + InitializeIot(); + } + + virtual AudioCodec* GetAudioCodec() override { + static Es8311AudioCodec audio_codec( + i2c_bus_, + I2C_NUM_1, + 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_GPIO_PA, + AUDIO_CODEC_ES8311_ADDR, + false); + return &audio_codec; + } + + virtual Display* GetDisplay() override { + return display_; + } +}; + +DECLARE_BOARD(AtomS3EchoBaseBoard); \ No newline at end of file diff --git a/main/boards/atoms3-echo-base/config.h b/main/boards/atoms3-echo-base/config.h new file mode 100644 index 00000000..6454e3e4 --- /dev/null +++ b/main/boards/atoms3-echo-base/config.h @@ -0,0 +1,44 @@ +#ifndef _BOARD_CONFIG_H_ +#define _BOARD_CONFIG_H_ + +// AtomS3+EchoBase Board configuration + +#include + +#define AUDIO_INPUT_REFERENCE true +#define AUDIO_INPUT_SAMPLE_RATE 24000 +#define AUDIO_OUTPUT_SAMPLE_RATE 24000 +#define AUDIO_DEFAULT_OUTPUT_VOLUME 80 + +#define AUDIO_I2S_GPIO_MCLK GPIO_NUM_NC +#define AUDIO_I2S_GPIO_WS GPIO_NUM_6 +#define AUDIO_I2S_GPIO_BCLK GPIO_NUM_8 +#define AUDIO_I2S_GPIO_DIN GPIO_NUM_7 +#define AUDIO_I2S_GPIO_DOUT GPIO_NUM_5 + +#define AUDIO_CODEC_I2C_SDA_PIN GPIO_NUM_38 +#define AUDIO_CODEC_I2C_SCL_PIN GPIO_NUM_39 +#define AUDIO_CODEC_ES8311_ADDR ES8311_CODEC_DEFAULT_ADDR +#define AUDIO_CODEC_GPIO_PA GPIO_NUM_NC + +#define BUILTIN_LED_GPIO GPIO_NUM_NC +#define BOOT_BUTTON_GPIO GPIO_NUM_41 +#define VOLUME_UP_BUTTON_GPIO GPIO_NUM_NC +#define VOLUME_DOWN_BUTTON_GPIO GPIO_NUM_NC + +#define DISPLAY_SDA_PIN GPIO_NUM_NC +#define DISPLAY_SCL_PIN GPIO_NUM_NC +#define DISPLAY_WIDTH 128 +#define DISPLAY_HEIGHT 128 +#define DISPLAY_MIRROR_X false +#define DISPLAY_MIRROR_Y false +#define DISPLAY_SWAP_XY false + +#define DISPLAY_OFFSET_X 0 +#define DISPLAY_OFFSET_Y 32 + +#define DISPLAY_BACKLIGHT_PIN GPIO_NUM_16 +#define DISPLAY_BACKLIGHT_OUTPUT_INVERT false + + +#endif // _BOARD_CONFIG_H_ \ No newline at end of file