C3的I2S ES8311 Example例子没声音

llinzzi
Posts: 1
Joined: Mon Jul 31, 2023 9:14 am

C3的I2S ES8311 Example例子没声音

Postby llinzzi » Mon Jul 31, 2023 9:22 am

关于idf5下i2s_codec的例子I2S ES8311 Example的问题

同样的硬件,我用S3有声音,用C3没有声音。
代码如下,默认代码,只是修改了GPIO。

IDF 5.0和5.1 都测试过。编译和运行都没问题, C3就是没声音。 是IDF的bug么?


/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*/

#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/i2s_std.h"
#include "esp_system.h"
#include "esp_check.h"
#include "es8311.h"


#define I2C_NUM (0)
#define I2S_NUM (0)


#if CONFIG_IDF_TARGET_ESP32C3
#define I2C_SCL_IO (GPIO_NUM_3)
#define I2C_SDA_IO (GPIO_NUM_2)

/* I2S port and GPIOs */
#define I2S_MCK_IO (GPIO_NUM_10)
#define I2S_BCK_IO (GPIO_NUM_6)
#define I2S_WS_IO (GPIO_NUM_11)

#define I2S_DO_IO (GPIO_NUM_7)
#define I2S_DI_IO (GPIO_NUM_5)

#define PA_EN (GPIO_NUM_4)

#else

#define I2C_SCL_IO (GPIO_NUM_17)
#define I2C_SDA_IO (GPIO_NUM_18)

/* I2S port and GPIOs */
#define I2S_NUM (0)
#define I2S_MCK_IO (GPIO_NUM_16)
#define I2S_BCK_IO (GPIO_NUM_15)
#define I2S_WS_IO (GPIO_NUM_13)

#define I2S_DO_IO (GPIO_NUM_14)
#define I2S_DI_IO (GPIO_NUM_12)

#define PA_EN (GPIO_NUM_11)


#endif









/* Example configurations */
#define EXAMPLE_RECV_BUF_SIZE (2400)
#define EXAMPLE_SAMPLE_RATE (16000)
#define EXAMPLE_MCLK_MULTIPLE (256) // If not using 24-bit data width, 256 should be enough
#define EXAMPLE_MCLK_FREQ_HZ (EXAMPLE_SAMPLE_RATE * EXAMPLE_MCLK_MULTIPLE)
#define EXAMPLE_VOICE_VOLUME CONFIG_EXAMPLE_VOICE_VOLUME
#if CONFIG_EXAMPLE_MODE_ECHO
#define EXAMPLE_MIC_GAIN CONFIG_EXAMPLE_MIC_GAIN
#endif

static const char *TAG = "i2s_es8311";
static const char err_reason[][30] = {"input param is invalid",
"operation timeout"
};
static i2s_chan_handle_t tx_handle = NULL;
static i2s_chan_handle_t rx_handle = NULL;

/* Import music file as buffer */
#if CONFIG_EXAMPLE_MODE_MUSIC
extern const uint8_t music_pcm_start[] asm("_binary_canon_pcm_start");
extern const uint8_t music_pcm_end[] asm("_binary_canon_pcm_end");
#endif

static esp_err_t es8311_codec_init(void)
{
/* Initialize I2C peripheral */
i2c_config_t es_i2c_cfg = {
.sda_io_num = I2C_SDA_IO,
.scl_io_num = I2C_SCL_IO,
.mode = I2C_MODE_MASTER,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = 100000,
};
ESP_RETURN_ON_ERROR(i2c_param_config(I2C_NUM, &es_i2c_cfg), TAG, "config i2c failed");
ESP_RETURN_ON_ERROR(i2c_driver_install(I2C_NUM, I2C_MODE_MASTER, 0, 0, 0), TAG, "install i2c driver failed");

/* Initialize es8311 codec */
es8311_handle_t es_handle = es8311_create(I2C_NUM, ES8311_ADDRRES_0);
ESP_RETURN_ON_FALSE(es_handle, ESP_FAIL, TAG, "es8311 create failed");
es8311_clock_config_t es_clk = {
.mclk_inverted = false,
.sclk_inverted = false,
.mclk_from_mclk_pin = true,
.mclk_frequency = EXAMPLE_MCLK_FREQ_HZ,
.sample_frequency = EXAMPLE_SAMPLE_RATE
};

ESP_ERROR_CHECK(es8311_init(es_handle, &es_clk, ES8311_RESOLUTION_16, ES8311_RESOLUTION_16));
ESP_RETURN_ON_ERROR(es8311_sample_frequency_config(es_handle, EXAMPLE_SAMPLE_RATE * EXAMPLE_MCLK_MULTIPLE, EXAMPLE_SAMPLE_RATE), TAG, "set es8311 sample frequency failed");
ESP_RETURN_ON_ERROR(es8311_voice_volume_set(es_handle, EXAMPLE_VOICE_VOLUME, NULL), TAG, "set es8311 volume failed");
ESP_RETURN_ON_ERROR(es8311_microphone_config(es_handle, false), TAG, "set es8311 microphone failed");
#if CONFIG_EXAMPLE_MODE_ECHO
ESP_RETURN_ON_ERROR(es8311_microphone_gain_set(es_handle, EXAMPLE_MIC_GAIN), TAG, "set es8311 microphone gain failed");
#endif
return ESP_OK;
}

static esp_err_t i2s_driver_init(void)
{
i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM, I2S_ROLE_MASTER);
chan_cfg.auto_clear = true; // Auto clear the legacy data in the DMA buffer
ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg, &tx_handle, &rx_handle));
i2s_std_config_t std_cfg = {
.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(EXAMPLE_SAMPLE_RATE),
.slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO),
.gpio_cfg = {
.mclk = I2S_MCK_IO,
.bclk = I2S_BCK_IO,
.ws = I2S_WS_IO,
.dout = I2S_DI_IO,
.din = I2S_DO_IO,
.invert_flags = {
.mclk_inv = false,
.bclk_inv = false,
.ws_inv = false,
},
},
};
std_cfg.clk_cfg.mclk_multiple = EXAMPLE_MCLK_MULTIPLE;

ESP_ERROR_CHECK(i2s_channel_init_std_mode(tx_handle, &std_cfg));
ESP_ERROR_CHECK(i2s_channel_init_std_mode(rx_handle, &std_cfg));
ESP_ERROR_CHECK(i2s_channel_enable(tx_handle));
ESP_ERROR_CHECK(i2s_channel_enable(rx_handle));
return ESP_OK;
}

#if CONFIG_EXAMPLE_MODE_MUSIC
static void i2s_music(void *args)
{
esp_err_t ret = ESP_OK;
size_t bytes_write = 0;
while (1) {
/* Write music to earphone */
ret = i2s_channel_write(tx_handle, music_pcm_start, music_pcm_end - music_pcm_start, &bytes_write, portMAX_DELAY);
if (ret != ESP_OK) {
/* Since we set timeout to 'portMAX_DELAY' in 'i2s_channel_write'
so you won't reach here unless you set other timeout value,
if timeout detected, it means write operation failed. */
ESP_LOGE(TAG, "[music] i2s write failed, %s", err_reason[ret == ESP_ERR_TIMEOUT]);
abort();
}
if (bytes_write > 0) {
ESP_LOGI(TAG, "[music] i2s music played, %d bytes are written.", bytes_write);
} else {
ESP_LOGE(TAG, "[music] i2s music play failed.");
abort();
}
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
vTaskDelete(NULL);
}

#else
static void i2s_echo(void *args)
{
int *mic_data = malloc(EXAMPLE_RECV_BUF_SIZE);
if (!mic_data) {
ESP_LOGE(TAG, "[echo] No memory for read data buffer");
abort();
}
esp_err_t ret = ESP_OK;
size_t bytes_read = 0;
size_t bytes_write = 0;
ESP_LOGI(TAG, "[echo] Echo start");

while (1) {
memset(mic_data, 0, EXAMPLE_RECV_BUF_SIZE);
/* Read sample data from mic */
ret = i2s_channel_read(rx_handle, mic_data, EXAMPLE_RECV_BUF_SIZE, &bytes_read, 1000);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "[echo] i2s read failed, %s", err_reason[ret == ESP_ERR_TIMEOUT]);
abort();
}
/* Write sample data to earphone */
ret = i2s_channel_write(tx_handle, mic_data, EXAMPLE_RECV_BUF_SIZE, &bytes_write, 1000);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "[echo] i2s write failed, %s", err_reason[ret == ESP_ERR_TIMEOUT]);
abort();
}
if (bytes_read != bytes_write) {
ESP_LOGW(TAG, "[echo] %d bytes read but only %d bytes are written", bytes_read, bytes_write);
}
}
vTaskDelete(NULL);
}
#endif

void app_main(void)
{

gpio_config_t ioConfig = {
.pin_bit_mask = (1ull << PA_EN),
.mode = GPIO_MODE_OUTPUT,
.pull_up_en = GPIO_PULLUP_ENABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_DISABLE
};
gpio_config(&ioConfig);
gpio_set_level(PA_EN, 1);



printf("i2s es8311 codec example start\n-----------------------------\n");
/* Initialize i2s peripheral */
if (i2s_driver_init() != ESP_OK) {
ESP_LOGE(TAG, "i2s driver init failed");
abort();
} else {
ESP_LOGI(TAG, "i2s driver init success");
}
/* Initialize i2c peripheral and config es8311 codec by i2c */
if (es8311_codec_init() != ESP_OK) {
ESP_LOGE(TAG, "es8311 codec init failed");
abort();
} else {
ESP_LOGI(TAG, "es8311 codec init success");
}
#if CONFIG_EXAMPLE_MODE_MUSIC
/* Play a piece of music in music mode */
xTaskCreate(i2s_music, "i2s_music", 4096, NULL, 5, NULL);
#else
/* Echo the sound from MIC in echo mode */
xTaskCreate(i2s_echo, "i2s_echo", 8192, NULL, 5, NULL);
#endif
}

CCER_CCER
Posts: 17
Joined: Mon Oct 24, 2022 7:56 am

Re: C3的I2S ES8311 Example例子没声音

Postby CCER_CCER » Wed Aug 09, 2023 2:42 am

1. 用万用表测量 PA 使能引脚是否拉高
2. 使用逻辑分析仪测量 I2S 是否有时钟和数据
3. 若 i2s 正常,可播放正弦波音频测量 es8311 的 dac 输出引脚。并使用 es8311_read_all dump 出所有的寄存器值。与下方寄存器值进行对比
I (3977) DRV8311: 00 REG VAULE:80
I (3977) DRV8311: 01 REG VAULE:3f
I (3987) DRV8311: 02 REG VAULE:00
I (3987) DRV8311: 03 REG VAULE:10
I (3987) DRV8311: 04 REG VAULE:10
I (3997) DRV8311: 05 REG VAULE:00
I (3997) DRV8311: 06 REG VAULE:03
I (4007) DRV8311: 07 REG VAULE:00
I (4007) DRV8311: 08 REG VAULE:ff
I (4007) DRV8311: 09 REG VAULE:0c
I (4017) DRV8311: 0a REG VAULE:4c
I (4017) DRV8311: 0b REG VAULE:00
I (4027) DRV8311: 0c REG VAULE:00
I (4027) DRV8311: 0d REG VAULE:01
I (4027) DRV8311: 0e REG VAULE:02
I (4037) DRV8311: 0f REG VAULE:00
I (4037) DRV8311: 10 REG VAULE:1f
I (4047) DRV8311: 11 REG VAULE:7f
I (4047) DRV8311: 12 REG VAULE:00
I (4047) DRV8311: 13 REG VAULE:10
I (4057) DRV8311: 14 REG VAULE:1a
I (4057) DRV8311: 15 REG VAULE:40
I (4067) DRV8311: 16 REG VAULE:24
I (4067) DRV8311: 17 REG VAULE:bf
I (4067) DRV8311: 18 REG VAULE:00
I (4077) DRV8311: 19 REG VAULE:00
I (4077) DRV8311: 1a REG VAULE:00
I (4087) DRV8311: 1b REG VAULE:0a
I (4087) DRV8311: 1c REG VAULE:6a
I (4087) DRV8311: 1d REG VAULE:00
I (4097) DRV8311: 1e REG VAULE:00
I (4097) DRV8311: 1f REG VAULE:00
I (4107) DRV8311: 20 REG VAULE:00
I (4107) DRV8311: 21 REG VAULE:00
I (4107) DRV8311: 22 REG VAULE:00
I (4117) DRV8311: 23 REG VAULE:00
I (4117) DRV8311: 24 REG VAULE:00
I (4127) DRV8311: 25 REG VAULE:00
I (4127) DRV8311: 26 REG VAULE:00
I (4127) DRV8311: 27 REG VAULE:00
I (4137) DRV8311: 28 REG VAULE:00
I (4137) DRV8311: 29 REG VAULE:00
I (4147) DRV8311: 2a REG VAULE:00
I (4147) DRV8311: 2b REG VAULE:00
I (4147) DRV8311: 2c REG VAULE:00
I (4157) DRV8311: 2d REG VAULE:00
I (4157) DRV8311: 2e REG VAULE:00
I (4167) DRV8311: 2f REG VAULE:00
I (4167) DRV8311: 30 REG VAULE:00
I (4167) DRV8311: 31 REG VAULE:00
I (4177) DRV8311: 32 REG VAULE:9c
I (4177) DRV8311: 33 REG VAULE:00
I (4187) DRV8311: 34 REG VAULE:00
I (4187) DRV8311: 35 REG VAULE:00
I (4187) DRV8311: 36 REG VAULE:00
I (4197) DRV8311: 37 REG VAULE:48
I (4197) DRV8311: 38 REG VAULE:00
I (4207) DRV8311: 39 REG VAULE:00
I (4207) DRV8311: 3a REG VAULE:00
I (4207) DRV8311: 3b REG VAULE:00
I (4217) DRV8311: 3c REG VAULE:00
I (4217) DRV8311: 3d REG VAULE:00
I (4227) DRV8311: 3e REG VAULE:00
I (4227) DRV8311: 3f REG VAULE:00
I (4227) DRV8311: 40 REG VAULE:00
I (4237) DRV8311: 41 REG VAULE:00
I (4237) DRV8311: 42 REG VAULE:00
I (4247) DRV8311: 43 REG VAULE:00
I (4247) DRV8311: 44 REG VAULE:50
I (4247) DRV8311: 45 REG VAULE:00
I (4257) DRV8311: 46 REG VAULE:00
I (4257) DRV8311: 47 REG VAULE:00
I (4267) DRV8311: 48 REG VAULE:00
I (4267) DRV8311: 49 REG VAULE:00

xiesi0622
Posts: 3
Joined: Tue Oct 10, 2023 7:22 am

Re: C3的I2S ES8311 Example例子没声音

Postby xiesi0622 » Thu Oct 12, 2023 7:33 am

REG:00: 80
REG:01: 3f
REG:02: 48//x
REG:03: 10
REG:04: 10
REG:05: 00
REG:06: 03
REG:07: 00
REG:08: ff
REG:09: 0c
REG:0a: 0c//x
REG:0b: 00
REG:0c: 20//x
REG:0d: 01
REG:0e: 02
REG:0f: 00
REG:10: 13//x
REG:11: 7c//x
REG:12: 00
REG:13: 10
REG:14: 1a
REG:15: 00//x
REG:16: 04//x
REG:17: c8//x
REG:18: 00
REG:19: 00
REG:1a: 00
REG:1b: 0c//x
REG:1c: 6a
REG:1d: 00
REG:1e: 00
REG:1f: 00
REG:20: 00
REG:21: 00
REG:22: 00
REG:23: 00
REG:24: 00
REG:25: 00
REG:26: 00
REG:27: 00
REG:28: 00
REG:29: 00
REG:2a: 00
REG:2b: 00
REG:2c: 00
REG:2d: 00
REG:2e: 00
REG:2f: 00
REG:30: 00
REG:31: 00
REG:32: 98//x
REG:33: 00
REG:34: 00
REG:35: 00
REG:36: 00
REG:37: 08//x
REG:38: 00
REG:39: 00
REG:3a: 00
REG:3b: 00
REG:3c: 00
REG:3d: 00
REG:3e: 00
REG:3f: 00
REG:40: 00
REG:41: 00
REG:42: 00
REG:43: 00
REG:44: 00
REG:45: 00
REG:46: 00
REG:47: 00
REG:48: 00
REG:49: 00 这个是我从es8311寄存器里面读出来的值,和您给出来的略微有些区别,我这个是s3-wroom-1的开发板,也是没有声音,另外可用提供一份es8311寄存器的手册吗

im11926
Posts: 3
Joined: Mon Mar 18, 2024 3:01 am

Re: C3的I2S ES8311 Example例子没声音

Postby im11926 » Mon Mar 18, 2024 3:27 am

可以降低I2C通信速率试试

Who is online

Users browsing this forum: Google Adsense [Bot] and 26 guests