From 4787eb41ab58701edc13efcd7ac92a0543a87d74 Mon Sep 17 00:00:00 2001 From: Almost-LoverY <84276751+Almost-LoverY@users.noreply.github.com> Date: Sat, 12 Apr 2025 17:17:27 +0800 Subject: [PATCH] The atk-dnesp32s3-box is compatible with ES8311 devices and non-ES8311 devices (#477) * The atk-dnesp32s3-box is compatible with ES8311 devices and non-ES8311 devices * Update atk_dnesp32s3_box.cc format code --------- Co-authored-by: Xiaoxia --- .../atk-dnesp32s3-box/atk_dnesp32s3_box.cc | 73 ++++++++++++++++--- main/boards/atk-dnesp32s3-box/config.h | 1 + 2 files changed, 64 insertions(+), 10 deletions(-) diff --git a/main/boards/atk-dnesp32s3-box/atk_dnesp32s3_box.cc b/main/boards/atk-dnesp32s3-box/atk_dnesp32s3_box.cc index d3f9012d..e5f4d8a7 100644 --- a/main/boards/atk-dnesp32s3-box/atk_dnesp32s3_box.cc +++ b/main/boards/atk-dnesp32s3-box/atk_dnesp32s3_box.cc @@ -1,6 +1,7 @@ #include "wifi_board.h" #include "audio_codec.h" -#include "audio_codecs/no_audio_codec.h" +#include "es8311_audio_codec.h" +#include "no_audio_codec.h" #include "display/lcd_display.h" #include "application.h" #include "button.h" @@ -24,9 +25,14 @@ LV_FONT_DECLARE(font_puhui_20_4); LV_FONT_DECLARE(font_awesome_20_4); -class XL9555 : public I2cDevice { +class XL9555_IN : public I2cDevice { public: - XL9555(i2c_master_bus_handle_t i2c_bus, uint8_t addr) : I2cDevice(i2c_bus, addr) { + XL9555_IN(i2c_master_bus_handle_t i2c_bus, uint8_t addr) : I2cDevice(i2c_bus, addr) { + WriteReg(0x06, 0x3B); + WriteReg(0x07, 0xFE); + } + + void xl9555_cfg(void) { WriteReg(0x06, 0x1B); WriteReg(0x07, 0xFE); } @@ -48,6 +54,19 @@ public: WriteReg(0x03, data); } } + + int GetPingState(uint16_t pin) { + uint8_t data; + if (pin <= 0x0080) { + data = ReadReg(0x00); + return (data & (uint8_t)(pin & 0xFF)) ? 1 : 0; + } else { + data = ReadReg(0x01); + return (data & (uint8_t)((pin >> 8) & 0xFF )) ? 1 : 0; + } + + return 0; + } }; class atk_dnesp32s3_box : public WifiBoard { @@ -56,7 +75,8 @@ private: i2c_master_dev_handle_t xl9555_handle_; Button boot_button_; LcdDisplay* display_; - XL9555* xl9555_; + XL9555_IN* xl9555_in_; + bool es8311_detected_ = false; void InitializeI2c() { // Initialize I2C peripheral @@ -75,7 +95,15 @@ private: ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_bus_)); // Initialize XL9555 - xl9555_ = new XL9555(i2c_bus_, 0x20); + xl9555_in_ = new XL9555_IN(i2c_bus_, 0x20); + + if (xl9555_in_->GetPingState(0x0020) == 1) { + es8311_detected_ = true; /* 音频设备标志位,SPK_CTRL_IO为高电平时,该标志位置1,且判定为ES8311 */ + } else { + es8311_detected_ = false; /* 音频设备标志位,SPK_CTRL_IO为低电平时,该标志位置0,且判定为NS4168 */ + } + + xl9555_in_->xl9555_cfg(); } void InitializeATK_ST7789_80_Display() { @@ -186,16 +214,41 @@ public: atk_dnesp32s3_box() : boot_button_(BOOT_BUTTON_GPIO) { InitializeI2c(); InitializeATK_ST7789_80_Display(); - xl9555_->SetOutputState(5, 1); - xl9555_->SetOutputState(7, 1); + xl9555_in_->SetOutputState(5, 1); + xl9555_in_->SetOutputState(7, 1); InitializeButtons(); InitializeIot(); } virtual AudioCodec* GetAudioCodec() override { - static ATK_NoAudioCodecDuplex audio_codec(AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE, - AUDIO_I2S_GPIO_BCLK, AUDIO_I2S_GPIO_WS, AUDIO_I2S_GPIO_DOUT, AUDIO_I2S_GPIO_DIN); - return &audio_codec; + /* 根据探测结果初始化编解码器 */ + if (es8311_detected_) { + /* 使用ES8311 驱动 */ + static Es8311AudioCodec audio_codec( + i2c_bus_, + I2C_NUM_0, + AUDIO_INPUT_SAMPLE_RATE, + AUDIO_OUTPUT_SAMPLE_RATE, + GPIO_NUM_NC, + AUDIO_I2S_GPIO_BCLK, + AUDIO_I2S_GPIO_WS, + AUDIO_I2S_GPIO_DOUT, + AUDIO_I2S_GPIO_DIN, + GPIO_NUM_NC, + AUDIO_CODEC_ES8311_ADDR, + false); + return &audio_codec; + } else { + static ATK_NoAudioCodecDuplex audio_codec( + AUDIO_INPUT_SAMPLE_RATE, + AUDIO_OUTPUT_SAMPLE_RATE, + AUDIO_I2S_GPIO_BCLK, + AUDIO_I2S_GPIO_WS, + AUDIO_I2S_GPIO_DOUT, + AUDIO_I2S_GPIO_DIN); + return &audio_codec; + } + return NULL; } virtual Display* GetDisplay() override { diff --git a/main/boards/atk-dnesp32s3-box/config.h b/main/boards/atk-dnesp32s3-box/config.h index 6b27e95b..4b8e0022 100644 --- a/main/boards/atk-dnesp32s3-box/config.h +++ b/main/boards/atk-dnesp32s3-box/config.h @@ -10,6 +10,7 @@ #define AUDIO_I2S_GPIO_BCLK GPIO_NUM_21 #define AUDIO_I2S_GPIO_DIN GPIO_NUM_47 #define AUDIO_I2S_GPIO_DOUT GPIO_NUM_14 +#define AUDIO_CODEC_ES8311_ADDR ES8311_CODEC_DEFAULT_ADDR #define BUILTIN_LED_GPIO GPIO_NUM_4 #define BOOT_BUTTON_GPIO GPIO_NUM_0