我对I2S和ES7210做了驱动开发,但是麦克风的声音老是获取有问题
i2s_chan_handle_t es7210_i2s_init(void)
{
ESP_LOGI(TAG, "Initializing I2S");
i2s_chan_config_t chan_cfg = {
.id = I2S_NUM_0,
.role = I2S_ROLE_MASTER,
.dma_desc_num = 8, // 增加 DMA 描述符数量
.dma_frame_num = 64, // 增加每个描述符的帧数
.auto_clear = false, // 不自动清除 DMA buffer
};
ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg, NULL, &i2s_rx_chan));
i2s_std_config_t std_cfg = {
.clk_cfg = {
.sample_rate_hz = SAMPLE_RATE,
.clk_src = I2S_CLK_SRC_PLL_160M, // 明确指定时钟源
.mclk_multiple = I2S_MCLK_MULTIPLE_512, // 提高MCLK倍数以适应48kHz
},
.slot_cfg = {
.data_bit_width = I2S_DATA_BIT_WIDTH_16BIT,
.slot_bit_width = I2S_SLOT_BIT_WIDTH_16BIT,
.slot_mode = I2S_SLOT_MODE_STEREO,
.slot_mask = I2S_STD_SLOT_BOTH,
.ws_width = 16,
.ws_pol = false,
.bit_shift = true,
.left_align = false,
.big_endian = false,
.bit_order_lsb = false,
},
.gpio_cfg = {
.mclk = MCLK_PIN,
.bclk = ADC_BCLK_PIN,
.ws = ADC_LRCLK_PIN,
.dout = GPIO_NUM_NC,
.din = ADC_DATA_PIN,
.invert_flags = {
.mclk_inv = false,
.bclk_inv = false,
.ws_inv = false,
},
},
};
ESP_ERROR_CHECK(i2s_channel_init_std_mode(i2s_rx_chan, &std_cfg));
// 在启用通道之前添加延迟
vTaskDelay(pdMS_TO_TICKS(100));
ESP_ERROR_CHECK(i2s_channel_enable(i2s_rx_chan));
// 等待 I2S 稳定
vTaskDelay(pdMS_TO_TICKS(100));
ESP_LOGI(TAG, "I2S initialized successfully");
return i2s_rx_chan;
}
void es7210_codec_init(void)
{
ESP_LOGI(TAG, "Init I2C used to configure ES7210");
i2c_config_t i2c_conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_12,
.scl_io_num = GPIO_NUM_11,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master = {
.clk_speed = EXAMPLE_ES7210_I2C_CLK,
},
};
ESP_ERROR_CHECK(i2c_param_config(EXAMPLE_I2C_NUM, &i2c_conf));
ESP_ERROR_CHECK(i2c_driver_install(EXAMPLE_I2C_NUM, i2c_conf.mode, 0, 0, 0));
/* Create ES7210 device handle */
es7210_dev_handle_t es7210_handle = NULL;
es7210_i2c_config_t es7210_i2c_conf = {
.i2c_port = EXAMPLE_I2C_NUM,
.i2c_addr = EXAMPLE_ES7210_I2C_ADDR
};
ESP_ERROR_CHECK(es7210_new_codec(&es7210_i2c_conf, &es7210_handle));
ESP_LOGI(TAG, "Configure ES7210 codec parameters");
es7210_codec_config_t codec_conf = {
.sample_rate_hz = EXAMPLE_I2S_SAMPLE_RATE,
.mclk_ratio = EXAMPLE_I2S_MCLK_MULTIPLE,
.i2s_format = EXAMPLE_I2S_TDM_FORMAT,
.bit_width = (es7210_i2s_bits_t)EXAMPLE_I2S_SAMPLE_BITS,
.mic_bias = EXAMPLE_ES7210_MIC_BIAS,
.mic_gain = EXAMPLE_ES7210_MIC_GAIN,
.flags = {
.tdm_enable = 1
},
};
ESP_ERROR_CHECK(es7210_config_codec(es7210_handle, &codec_conf));
ESP_ERROR_CHECK(es7210_config_volume(es7210_handle, EXAMPLE_ES7210_ADC_VOLUME));
}