关于esp_codec_dev组件录音问题
Posted: Thu Sep 05, 2024 2:09 am
我的板子麦克风连接的ES7243,然后由I2S1连接到ESP32,程序下载之后,读取录音文件,发现数值为FF或者00,求大神解答,代码如下:
#include <stdio.h>
#include "driver/i2c_master.h"
#include "driver/i2s_std.h"
#include "esp_codec_dev.h"
#include "esp_codec_dev_defaults.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "es7243_adc.h"
#define I2S_MAX_KEEP SOC_I2S_NUM
static i2c_master_bus_handle_t i2c_bus_handle;
typedef struct {
i2s_chan_handle_t tx_handle;
i2s_chan_handle_t rx_handle;
} i2s_keep_t;
static i2s_keep_t *i2s_keep[I2S_MAX_KEEP];
static int ut_i2c_init(uint8_t port)
{
i2c_master_bus_config_t i2c_bus_config = {0};
i2c_bus_config.clk_source = I2C_CLK_SRC_DEFAULT;
i2c_bus_config.i2c_port = port;
i2c_bus_config.sda_io_num = GPIO_NUM_18;
i2c_bus_config.scl_io_num = GPIO_NUM_23;
i2c_bus_config.glitch_ignore_cnt = 7;
i2c_bus_config.flags.enable_internal_pullup = true;
return i2c_new_master_bus(&i2c_bus_config, &i2c_bus_handle);
}
static int ut_i2c_deinit(uint8_t port)
{
if(i2c_bus_handle)
{
i2c_del_master_bus(i2c_bus_handle);
}
i2c_bus_handle = NULL;
return 0;
}
static int ut_i2s_init(uint8_t port)
{
if (port >= I2S_MAX_KEEP) {
return -1;
}
// Already installed
if (i2s_keep[port]) {
return 0;
}
i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_1, I2S_ROLE_MASTER);
i2s_std_config_t std_cfg = {
.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(16000),
.slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(16, I2S_SLOT_MODE_MONO),
.gpio_cfg = {
.mclk = GPIO_NUM_0,
.bclk = GPIO_NUM_32,
.ws = GPIO_NUM_33,
.din = GPIO_NUM_36,
},
};
i2s_keep[port] = (i2s_keep_t *) calloc(1, sizeof(i2s_keep_t));
if (i2s_keep[port] == NULL) {
return -1;
}
i2s_new_channel(&chan_cfg, NULL, &i2s_keep[port]->rx_handle);
i2s_channel_init_std_mode(i2s_keep[port]->rx_handle, &std_cfg);
i2s_channel_enable(i2s_keep[port]->rx_handle);
return 0;
}
static int ut_i2s_deinit(uint8_t port)
{
if (port >= I2S_MAX_KEEP) {
return -1;
}
// already installed
if (i2s_keep[port] == NULL) {
return 0;
}
i2s_channel_disable(i2s_keep[port]->rx_handle);
i2s_del_channel(i2s_keep[port]->rx_handle);
free(i2s_keep[port]);
i2s_keep[port] = NULL;
return 0;
}
void app_main(void)
{
ut_i2c_init(0);
ut_i2s_init(0);
audio_codec_i2s_cfg_t i2s_cfg = {
.rx_handle = i2s_keep[0]->rx_handle,
.port = 0,
};
audio_codec_data_if_t *data_if = audio_codec_new_i2s_data(&i2s_cfg);
audio_codec_i2c_cfg_t i2c_cfg = {
.addr = ES7243_CODEC_DEFAULT_ADDR,
.bus_handle = i2c_bus_handle,
.port = 0,
};
const audio_codec_ctrl_if_t *in_ctrl_if = audio_codec_new_i2c_ctrl(&i2c_cfg);
const audio_codec_gpio_if_t *gpio_if = audio_codec_new_gpio();
es7243_codec_cfg_t codec_cfg = {
.ctrl_if = in_ctrl_if,
};
const audio_codec_if_t *in_codec_if = es7243_codec_new(&codec_cfg);
esp_codec_dev_cfg_t codec_dev_cfg = {
.codec_if = in_codec_if,
.dev_type = ESP_CODEC_DEV_TYPE_IN,
.data_if = data_if,
};
esp_codec_dev_handle_t record_dev = esp_codec_dev_new(&codec_dev_cfg);
esp_codec_dev_set_in_gain(record_dev, 0.0);
esp_codec_dev_sample_info_t fs = {
.channel = 1,
.sample_rate = 16000,
.bits_per_sample = 16,
.channel_mask = ESP_CODEC_DEV_MAKE_CHANNEL_MASK(1),
};
esp_codec_dev_open(record_dev, &fs);
uint8_t *data = (uint8_t *) malloc(2048);
while(1)
{
esp_codec_dev_read(record_dev, data, 2048);
for (int i = 0; i < 2048; i++)
{
printf("%02X ", data);
}
printf("\n");
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
#include <stdio.h>
#include "driver/i2c_master.h"
#include "driver/i2s_std.h"
#include "esp_codec_dev.h"
#include "esp_codec_dev_defaults.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "es7243_adc.h"
#define I2S_MAX_KEEP SOC_I2S_NUM
static i2c_master_bus_handle_t i2c_bus_handle;
typedef struct {
i2s_chan_handle_t tx_handle;
i2s_chan_handle_t rx_handle;
} i2s_keep_t;
static i2s_keep_t *i2s_keep[I2S_MAX_KEEP];
static int ut_i2c_init(uint8_t port)
{
i2c_master_bus_config_t i2c_bus_config = {0};
i2c_bus_config.clk_source = I2C_CLK_SRC_DEFAULT;
i2c_bus_config.i2c_port = port;
i2c_bus_config.sda_io_num = GPIO_NUM_18;
i2c_bus_config.scl_io_num = GPIO_NUM_23;
i2c_bus_config.glitch_ignore_cnt = 7;
i2c_bus_config.flags.enable_internal_pullup = true;
return i2c_new_master_bus(&i2c_bus_config, &i2c_bus_handle);
}
static int ut_i2c_deinit(uint8_t port)
{
if(i2c_bus_handle)
{
i2c_del_master_bus(i2c_bus_handle);
}
i2c_bus_handle = NULL;
return 0;
}
static int ut_i2s_init(uint8_t port)
{
if (port >= I2S_MAX_KEEP) {
return -1;
}
// Already installed
if (i2s_keep[port]) {
return 0;
}
i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_1, I2S_ROLE_MASTER);
i2s_std_config_t std_cfg = {
.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(16000),
.slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(16, I2S_SLOT_MODE_MONO),
.gpio_cfg = {
.mclk = GPIO_NUM_0,
.bclk = GPIO_NUM_32,
.ws = GPIO_NUM_33,
.din = GPIO_NUM_36,
},
};
i2s_keep[port] = (i2s_keep_t *) calloc(1, sizeof(i2s_keep_t));
if (i2s_keep[port] == NULL) {
return -1;
}
i2s_new_channel(&chan_cfg, NULL, &i2s_keep[port]->rx_handle);
i2s_channel_init_std_mode(i2s_keep[port]->rx_handle, &std_cfg);
i2s_channel_enable(i2s_keep[port]->rx_handle);
return 0;
}
static int ut_i2s_deinit(uint8_t port)
{
if (port >= I2S_MAX_KEEP) {
return -1;
}
// already installed
if (i2s_keep[port] == NULL) {
return 0;
}
i2s_channel_disable(i2s_keep[port]->rx_handle);
i2s_del_channel(i2s_keep[port]->rx_handle);
free(i2s_keep[port]);
i2s_keep[port] = NULL;
return 0;
}
void app_main(void)
{
ut_i2c_init(0);
ut_i2s_init(0);
audio_codec_i2s_cfg_t i2s_cfg = {
.rx_handle = i2s_keep[0]->rx_handle,
.port = 0,
};
audio_codec_data_if_t *data_if = audio_codec_new_i2s_data(&i2s_cfg);
audio_codec_i2c_cfg_t i2c_cfg = {
.addr = ES7243_CODEC_DEFAULT_ADDR,
.bus_handle = i2c_bus_handle,
.port = 0,
};
const audio_codec_ctrl_if_t *in_ctrl_if = audio_codec_new_i2c_ctrl(&i2c_cfg);
const audio_codec_gpio_if_t *gpio_if = audio_codec_new_gpio();
es7243_codec_cfg_t codec_cfg = {
.ctrl_if = in_ctrl_if,
};
const audio_codec_if_t *in_codec_if = es7243_codec_new(&codec_cfg);
esp_codec_dev_cfg_t codec_dev_cfg = {
.codec_if = in_codec_if,
.dev_type = ESP_CODEC_DEV_TYPE_IN,
.data_if = data_if,
};
esp_codec_dev_handle_t record_dev = esp_codec_dev_new(&codec_dev_cfg);
esp_codec_dev_set_in_gain(record_dev, 0.0);
esp_codec_dev_sample_info_t fs = {
.channel = 1,
.sample_rate = 16000,
.bits_per_sample = 16,
.channel_mask = ESP_CODEC_DEV_MAKE_CHANNEL_MASK(1),
};
esp_codec_dev_open(record_dev, &fs);
uint8_t *data = (uint8_t *) malloc(2048);
while(1)
{
esp_codec_dev_read(record_dev, data, 2048);
for (int i = 0; i < 2048; i++)
{
printf("%02X ", data);
}
printf("\n");
vTaskDelay(pdMS_TO_TICKS(1000));
}
}