From ebb804bab9beb3e9ef6d1f3a1923aca2b94ae0b7 Mon Sep 17 00:00:00 2001 From: Gao Wei Date: Fri, 9 Jun 2023 13:13:20 +0800 Subject: [PATCH] audio_hal: Update es7210 driver --- .../esp32_s2_kaluga_1_v1_2/board_def.h | 19 +- components/audio_board/esp32_s3_box/board.c | 1 + .../audio_board/esp32_s3_box/board_def.h | 20 +- components/audio_board/esp32_s3_box_3/board.c | 1 + .../audio_board/esp32_s3_box_3/board_def.h | 19 +- .../audio_board/esp32_s3_box_lite/board.c | 1 + .../esp32_s3_korvo2_v3/board_def.h | 23 +- .../audio_board/lyrat_mini_v1_1/board.c | 1 + components/audio_hal/audio_hal.c | 8 +- components/audio_hal/driver/es7210/es7210.c | 177 +++++++++------- components/audio_hal/driver/es7210/es7210.h | 197 +++++++++--------- .../algorithm/main/algorithm_examples.c | 10 +- 12 files changed, 260 insertions(+), 217 deletions(-) diff --git a/components/audio_board/esp32_s2_kaluga_1_v1_2/board_def.h b/components/audio_board/esp32_s2_kaluga_1_v1_2/board_def.h index 94bb61ecb..b82545e86 100644 --- a/components/audio_board/esp32_s2_kaluga_1_v1_2/board_def.h +++ b/components/audio_board/esp32_s2_kaluga_1_v1_2/board_def.h @@ -67,18 +67,17 @@ #define ESP_SD_PIN_D0 -1 #define ESP_SD_PIN_D3 -1 - /** - * @brief Audio Codec Chip Function Definition + * @brief Audio Codec Chip Function Definition */ -#define FUNC_AUDIO_CODEC_EN (1) -#define PA_ENABLE_GPIO 10 -#define ES8311_MCLK_SOURCE 1 /* 0 From MCLK, 1 From BCLK */ -#define CODEC_ADC_I2S_PORT (0) -#define CODEC_ADC_BITS_PER_SAMPLE (16) /* 16bit */ -#define CODEC_ADC_SAMPLE_RATE (48000) -#define RECORD_HARDWARE_AEC (false) -#define BOARD_PA_GAIN (0) /* Power amplifier gain defined by board (dB) */ +#define FUNC_AUDIO_CODEC_EN (1) +#define PA_ENABLE_GPIO (10) +#define ES8311_MCLK_SOURCE (1) /* 0 From MCLK, 1 From BCLK */ +#define CODEC_ADC_I2S_PORT (0) +#define CODEC_ADC_BITS_PER_SAMPLE (16) /* 16bit */ +#define CODEC_ADC_SAMPLE_RATE (48000) +#define RECORD_HARDWARE_AEC (false) +#define BOARD_PA_GAIN (0) /* Power amplifier gain defined by board (dB) */ extern audio_hal_func_t AUDIO_CODEC_ES8311_DEFAULT_HANDLE; #define AUDIO_CODEC_DEFAULT_CONFIG(){ \ diff --git a/components/audio_board/esp32_s3_box/board.c b/components/audio_board/esp32_s3_box/board.c index 3ae259423..0825e3cbc 100644 --- a/components/audio_board/esp32_s3_box/board.c +++ b/components/audio_board/esp32_s3_box/board.c @@ -51,6 +51,7 @@ audio_board_handle_t audio_board_init(void) audio_hal_handle_t audio_board_adc_init(void) { audio_hal_codec_config_t audio_codec_cfg = AUDIO_CODEC_DEFAULT_CONFIG(); + audio_codec_cfg.codec_mode = AUDIO_HAL_CODEC_MODE_ENCODE; audio_hal_handle_t adc_hal = NULL; adc_hal = audio_hal_init(&audio_codec_cfg, &AUDIO_CODEC_ES7210_DEFAULT_HANDLE); AUDIO_NULL_CHECK(TAG, adc_hal, return NULL); diff --git a/components/audio_board/esp32_s3_box/board_def.h b/components/audio_board/esp32_s3_box/board_def.h index 080691280..9c2f329e4 100644 --- a/components/audio_board/esp32_s3_box/board_def.h +++ b/components/audio_board/esp32_s3_box/board_def.h @@ -75,18 +75,18 @@ #define TOUCH_PANEL_INVERSE_X (1) #define TOUCH_PANEL_INVERSE_Y (0) - /** - * @brief Audio Codec Chip Function Definition + * @brief Audio Codec Chip Function Definition */ -#define FUNC_AUDIO_CODEC_EN (1) -#define PA_ENABLE_GPIO GPIO_NUM_46 -#define HEADPHONE_DETECT -1 -#define CODEC_ADC_I2S_PORT (0) -#define CODEC_ADC_BITS_PER_SAMPLE (32) /* 32bit */ -#define CODEC_ADC_SAMPLE_RATE (48000) -#define RECORD_HARDWARE_AEC (true) -#define BOARD_PA_GAIN (0) /* Power amplifier gain defined by board (dB) */ +#define FUNC_AUDIO_CODEC_EN (1) +#define CODEC_ADC_I2S_PORT (0) +#define CODEC_ADC_BITS_PER_SAMPLE (32) /* 32bit */ +#define CODEC_ADC_SAMPLE_RATE (48000) +#define RECORD_HARDWARE_AEC (true) +#define BOARD_PA_GAIN (0) /* Power amplifier gain defined by board (dB) */ +#define PA_ENABLE_GPIO GPIO_NUM_46 +#define HEADPHONE_DETECT -1 +#define ES7210_MIC_SELECT (ES7210_INPUT_MIC1 | ES7210_INPUT_MIC2 | ES7210_INPUT_MIC3) extern audio_hal_func_t AUDIO_CODEC_ES8311_DEFAULT_HANDLE; extern audio_hal_func_t AUDIO_CODEC_ES7210_DEFAULT_HANDLE; diff --git a/components/audio_board/esp32_s3_box_3/board.c b/components/audio_board/esp32_s3_box_3/board.c index 535a6a4c9..03503b007 100644 --- a/components/audio_board/esp32_s3_box_3/board.c +++ b/components/audio_board/esp32_s3_box_3/board.c @@ -52,6 +52,7 @@ audio_board_handle_t audio_board_init(void) audio_hal_handle_t audio_board_adc_init(void) { audio_hal_codec_config_t audio_codec_cfg = AUDIO_CODEC_DEFAULT_CONFIG(); + audio_codec_cfg.codec_mode = AUDIO_HAL_CODEC_MODE_ENCODE; audio_hal_handle_t adc_hal = NULL; adc_hal = audio_hal_init(&audio_codec_cfg, &AUDIO_CODEC_ES7210_DEFAULT_HANDLE); AUDIO_NULL_CHECK(TAG, adc_hal, return NULL); diff --git a/components/audio_board/esp32_s3_box_3/board_def.h b/components/audio_board/esp32_s3_box_3/board_def.h index c47899a93..52fb89be8 100644 --- a/components/audio_board/esp32_s3_box_3/board_def.h +++ b/components/audio_board/esp32_s3_box_3/board_def.h @@ -73,16 +73,17 @@ #define TOUCH_PANEL_INVERSE_Y (0) /** - * @brief Audio Codec Chip Function Definition + * @brief Audio Codec Chip Function Definition */ -#define FUNC_AUDIO_CODEC_EN (1) -#define PA_ENABLE_GPIO GPIO_NUM_46 -#define HEADPHONE_DETECT (-1) -#define CODEC_ADC_I2S_PORT (0) -#define CODEC_ADC_BITS_PER_SAMPLE (32) /* 32bit */ -#define CODEC_ADC_SAMPLE_RATE (48000) -#define RECORD_HARDWARE_AEC (true) -#define BOARD_PA_GAIN (0) /* Power amplifier gain defined by board (dB) */ +#define FUNC_AUDIO_CODEC_EN (1) +#define CODEC_ADC_I2S_PORT (0) +#define CODEC_ADC_BITS_PER_SAMPLE (32) /* 32bit */ +#define CODEC_ADC_SAMPLE_RATE (48000) +#define RECORD_HARDWARE_AEC (true) +#define BOARD_PA_GAIN (0) /* Power amplifier gain defined by board (dB) */ +#define PA_ENABLE_GPIO GPIO_NUM_46 +#define HEADPHONE_DETECT (-1) +#define ES7210_MIC_SELECT (ES7210_INPUT_MIC1 | ES7210_INPUT_MIC2 | ES7210_INPUT_MIC3) extern audio_hal_func_t AUDIO_CODEC_ES8311_DEFAULT_HANDLE; extern audio_hal_func_t AUDIO_CODEC_ES7210_DEFAULT_HANDLE; diff --git a/components/audio_board/esp32_s3_box_lite/board.c b/components/audio_board/esp32_s3_box_lite/board.c index 21ee49dfb..69ff7d6e9 100644 --- a/components/audio_board/esp32_s3_box_lite/board.c +++ b/components/audio_board/esp32_s3_box_lite/board.c @@ -51,6 +51,7 @@ audio_board_handle_t audio_board_init(void) audio_hal_handle_t audio_board_adc_init(void) { audio_hal_codec_config_t audio_codec_cfg = AUDIO_CODEC_DEFAULT_CONFIG(); + audio_codec_cfg.codec_mode = AUDIO_HAL_CODEC_MODE_ENCODE; audio_hal_handle_t adc_hal = NULL; adc_hal = audio_hal_init(&audio_codec_cfg, &AUDIO_CODEC_ES7243E_DEFAULT_HANDLE); AUDIO_NULL_CHECK(TAG, adc_hal, return NULL); diff --git a/components/audio_board/esp32_s3_korvo2_v3/board_def.h b/components/audio_board/esp32_s3_korvo2_v3/board_def.h index cae2678d9..bbb891da4 100644 --- a/components/audio_board/esp32_s3_korvo2_v3/board_def.h +++ b/components/audio_board/esp32_s3_korvo2_v3/board_def.h @@ -106,19 +106,19 @@ #define TOUCH_PANEL_INVERSE_X (1) #define TOUCH_PANEL_INVERSE_Y (0) - /** - * @brief Audio Codec Chip Function Definition + * @brief Audio Codec Chip Function Definition */ -#define FUNC_AUDIO_CODEC_EN (1) -#define ES8311_MCLK_SOURCE (0) /* 0 From MCLK of esp32 1 From BCLK */ -#define HEADPHONE_DETECT (-1) -#define PA_ENABLE_GPIO GPIO_NUM_48 -#define CODEC_ADC_I2S_PORT (0) -#define CODEC_ADC_BITS_PER_SAMPLE (32) /* 32bit */ -#define CODEC_ADC_SAMPLE_RATE (48000) -#define RECORD_HARDWARE_AEC (true) -#define BOARD_PA_GAIN (6) /* Power amplifier gain defined by board (dB) */ +#define FUNC_AUDIO_CODEC_EN (1) +#define CODEC_ADC_I2S_PORT (0) +#define CODEC_ADC_BITS_PER_SAMPLE (32) /* 32bit */ +#define CODEC_ADC_SAMPLE_RATE (48000) +#define RECORD_HARDWARE_AEC (true) +#define BOARD_PA_GAIN (6) /* Power amplifier gain defined by board (dB) */ +#define HEADPHONE_DETECT (-1) +#define PA_ENABLE_GPIO GPIO_NUM_48 +#define ES8311_MCLK_SOURCE (0) /* 0 From MCLK of esp32 1 From BCLK */ +#define ES7210_MIC_SELECT (ES7210_INPUT_MIC1 | ES7210_INPUT_MIC2 | ES7210_INPUT_MIC3) extern audio_hal_func_t AUDIO_CODEC_ES8311_DEFAULT_HANDLE; extern audio_hal_func_t AUDIO_CODEC_ES7210_DEFAULT_HANDLE; @@ -135,7 +135,6 @@ extern audio_hal_func_t AUDIO_CODEC_ES7210_DEFAULT_HANDLE; }, \ }; - /** * @brief Button Function Definition */ diff --git a/components/audio_board/lyrat_mini_v1_1/board.c b/components/audio_board/lyrat_mini_v1_1/board.c index e42a701ca..5ed3f0ca3 100644 --- a/components/audio_board/lyrat_mini_v1_1/board.c +++ b/components/audio_board/lyrat_mini_v1_1/board.c @@ -50,6 +50,7 @@ audio_board_handle_t audio_board_init(void) audio_hal_handle_t audio_board_adc_init(void) { audio_hal_codec_config_t audio_codec_cfg = AUDIO_CODEC_DEFAULT_CONFIG(); + audio_codec_cfg.codec_mode = AUDIO_HAL_CODEC_MODE_ENCODE; audio_hal_handle_t adc_hal = audio_hal_init(&audio_codec_cfg, &AUDIO_CODEC_ES7243_DEFAULT_HANDLE); AUDIO_NULL_CHECK(TAG, adc_hal, return NULL); return adc_hal; diff --git a/components/audio_hal/audio_hal.c b/components/audio_hal/audio_hal.c index 0241b7b4f..ab9337d04 100755 --- a/components/audio_hal/audio_hal.c +++ b/components/audio_hal/audio_hal.c @@ -41,7 +41,7 @@ static const char *TAG = "AUDIO_HAL"; audio_hal_handle_t audio_hal_init(audio_hal_codec_config_t *audio_hal_conf, audio_hal_func_t *audio_hal_func) { esp_err_t ret = 0; - audio_hal_handle_t audio_hal = (audio_hal_handle_t) audio_calloc(1, sizeof(audio_hal_func_t)); + audio_hal_handle_t audio_hal = (audio_hal_handle_t)audio_calloc(1, sizeof(audio_hal_func_t)); AUDIO_MEM_CHECK(TAG, audio_hal, return NULL); memcpy(audio_hal, audio_hal_func, sizeof(audio_hal_func_t)); audio_hal->audio_hal_lock = mutex_create(); @@ -51,7 +51,7 @@ audio_hal_handle_t audio_hal_init(audio_hal_codec_config_t *audio_hal_conf, audi return NULL; }); mutex_lock(audio_hal->audio_hal_lock); - ret = audio_hal->audio_codec_initialize(audio_hal_conf); + ret = audio_hal->audio_codec_initialize(audio_hal_conf); if (ret == ESP_FAIL) { audio_free(audio_hal); if (audio_hal_func->handle) { @@ -62,7 +62,9 @@ audio_hal_handle_t audio_hal_init(audio_hal_codec_config_t *audio_hal_conf, audi } } ret |= audio_hal->audio_codec_config_iface(audio_hal_conf->codec_mode, &audio_hal_conf->i2s_iface); - ret |= audio_hal->audio_codec_set_volume(AUDIO_HAL_VOL_DEFAULT); + if ((audio_hal_conf->codec_mode == AUDIO_HAL_CODEC_MODE_DECODE) || (audio_hal_conf->codec_mode == AUDIO_HAL_CODEC_MODE_BOTH)) { + ret |= audio_hal->audio_codec_set_volume(AUDIO_HAL_VOL_DEFAULT); + } AUDIO_RET_ON_FALSE(TAG, ret, return NULL, "audio_hal_init failed"); audio_hal->handle = audio_hal; audio_hal_func->handle = audio_hal; diff --git a/components/audio_hal/driver/es7210/es7210.c b/components/audio_hal/driver/es7210/es7210.c index 89260682b..73bd08f4a 100644 --- a/components/audio_hal/driver/es7210/es7210.c +++ b/components/audio_hal/driver/es7210/es7210.c @@ -28,22 +28,19 @@ #include "i2c_bus.h" #include "es7210.h" -#define I2S_DSP_MODE 0 -#define MCLK_DIV_FRE 256 - /* ES7210 address */ -#define ES7210_ADDR ES7210_AD1_AD0_00 -#define ES7210_MCLK_SOURCE FROM_CLOCK_DOUBLE_PIN /* In master mode, 0 : MCLK from pad 1 : MCLK from clock doubler */ -#define FROM_PAD_PIN 0 -#define FROM_CLOCK_DOUBLE_PIN 1 - -/* ES7210 mic select */ -#if CONFIG_ESP32_S3_KORVO2_V3_BOARD -#define ES7210_MIC_SELECT ES7210_INPUT_MIC1 | ES7210_INPUT_MIC2 | ES7210_INPUT_MIC3 -#else -#define ES7210_MIC_SELECT ES7210_INPUT_MIC1 | ES7210_INPUT_MIC2 -#endif -#define ENABLE_TDM_MAX_NUM 3 +#define ES7210_ADDR (ES7210_AD1_AD0_00) + +#define I2S_DSP_MODE (0) +#define MCLK_DIV_FRE (256) +#define FROM_PAD_PIN (0) +#define FROM_CLOCK_DOUBLE_PIN (1) +#define ENABLE_TDM_MAX_NUM (3) +#define ES7210_MCLK_SOURCE (FROM_CLOCK_DOUBLE_PIN) /* In master mode. 0 : MCLK from pad; 1 : MCLK from clock doubler */ + +#ifndef ES7210_MIC_SELECT +#define ES7210_MIC_SELECT (ES7210_INPUT_MIC1 | ES7210_INPUT_MIC2) +#endif /* ES7210_MIC_SELECT */ static char *TAG = "ES7210"; @@ -53,19 +50,20 @@ static struct { es7210_gain_value_t gain; } es7210_handle; -/* +/** * Operate function of ADC */ audio_hal_func_t AUDIO_CODEC_ES7210_DEFAULT_HANDLE = { - .audio_codec_initialize = es7210_adc_init, + .audio_codec_initialize = es7210_adc_init, .audio_codec_deinitialize = es7210_adc_deinit, - .audio_codec_ctrl = es7210_adc_ctrl_state, + .audio_codec_ctrl = es7210_adc_ctrl_state, .audio_codec_config_iface = es7210_adc_config_i2s, - .audio_codec_set_mute = es7210_set_mute, - .audio_codec_set_volume = es7210_adc_set_volume, - .audio_codec_enable_pa = NULL, - .audio_hal_lock = NULL, - .handle = NULL, + .audio_codec_set_mute = es7210_set_mute, + .audio_codec_set_volume = es7210_adc_set_volume, + .audio_codec_get_volume = NULL, + .audio_codec_enable_pa = NULL, + .audio_hal_lock = NULL, + .handle = NULL, }; /* @@ -149,6 +147,16 @@ static const struct _coeff_div coeff_div[] = { {19200000, 96000, 0x01, 0x05, 0x00, 0x01, 0x28, 0x00, 0x00, 0xc8}, }; +static int es7210_read_reg(uint8_t reg_addr) +{ + uint8_t data; + esp_err_t ret = i2c_bus_read_bytes(es7210_handle.i2c_handle, ES7210_ADDR, ®_addr, sizeof(reg_addr), &data, sizeof(data)); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to es7210 read 0x%x reg", reg_addr); + } + return (int)data; +} + static esp_err_t es7210_write_reg(uint8_t reg_addr, uint8_t data) { return i2c_bus_write_bytes(es7210_handle.i2c_handle, ES7210_ADDR, ®_addr, sizeof(reg_addr), &data, sizeof(data)); @@ -191,13 +199,6 @@ int8_t get_es7210_mclk_src(void) return ES7210_MCLK_SOURCE; } -int es7210_read_reg(uint8_t reg_addr) -{ - uint8_t data; - i2c_bus_read_bytes(es7210_handle.i2c_handle, ES7210_ADDR, ®_addr, sizeof(reg_addr), &data, sizeof(data)); - return (int)data; -} - esp_err_t es7210_config_sample(audio_hal_iface_samples_t sample) { int coeff; @@ -260,11 +261,11 @@ esp_err_t es7210_config_sample(audio_hal_iface_samples_t sample) return ret; } -esp_err_t es7210_mic_select(es7210_input_mics_t mic) +esp_err_t es7210_mic_select(es7210_input_mics_t mic_select) { esp_err_t ret = ESP_OK; uint16_t mic_num = 0; - es7210_handle.mic_select = mic; + es7210_handle.mic_select = mic_select; if (es7210_handle.mic_select & (ES7210_INPUT_MIC1 | ES7210_INPUT_MIC2 | ES7210_INPUT_MIC3 | ES7210_INPUT_MIC4)) { for (int i = 0; i < 4; i++) { ret |= es7210_update_reg_bit(ES7210_MIC1_GAIN_REG43 + i, 0x10, 0x00); @@ -326,16 +327,16 @@ esp_err_t es7210_adc_init(audio_hal_codec_config_t *codec_cfg) ret |= es7210_write_reg(ES7210_RESET_REG00, 0xff); ret |= es7210_write_reg(ES7210_RESET_REG00, 0x41); ret |= es7210_write_reg(ES7210_CLOCK_OFF_REG01, 0x3f); - ret |= es7210_write_reg(ES7210_TIME_CONTROL0_REG09, 0x30); /* Set chip state cycle */ - ret |= es7210_write_reg(ES7210_TIME_CONTROL1_REG0A, 0x30); /* Set power on state cycle */ - ret |= es7210_write_reg(ES7210_ADC12_HPF2_REG23, 0x2a); /* Quick setup */ + ret |= es7210_write_reg(ES7210_TIME_CONTROL0_REG09, 0x30); /* Set chip state cycle */ + ret |= es7210_write_reg(ES7210_TIME_CONTROL1_REG0A, 0x30); /* Set power on state cycle */ + ret |= es7210_write_reg(ES7210_ADC12_HPF2_REG23, 0x2a); /* Quick setup */ ret |= es7210_write_reg(ES7210_ADC12_HPF1_REG22, 0x0a); ret |= es7210_write_reg(ES7210_ADC34_HPF2_REG20, 0x0a); ret |= es7210_write_reg(ES7210_ADC34_HPF1_REG21, 0x2a); /* Set master/slave audio interface */ - audio_hal_codec_i2s_iface_t *i2s_cfg = & (codec_cfg->i2s_iface); + audio_hal_codec_i2s_iface_t *i2s_cfg = &(codec_cfg->i2s_iface); switch (i2s_cfg->mode) { - case AUDIO_HAL_MODE_MASTER: /* MASTER MODE */ + case AUDIO_HAL_MODE_MASTER: /* MASTER MODE */ ESP_LOGI(TAG, "ES7210 in Master mode"); ret |= es7210_update_reg_bit(ES7210_MODE_CONFIG_REG08, 0x01, 0x01); /* Select clock source for internal mclk */ @@ -351,21 +352,21 @@ esp_err_t es7210_adc_init(audio_hal_codec_config_t *codec_cfg) break; } break; - case AUDIO_HAL_MODE_SLAVE: /* SLAVE MODE */ + case AUDIO_HAL_MODE_SLAVE: /* SLAVE MODE */ ESP_LOGI(TAG, "ES7210 in Slave mode"); ret |= es7210_update_reg_bit(ES7210_MODE_CONFIG_REG08, 0x01, 0x00); break; default: ret |= es7210_update_reg_bit(ES7210_MODE_CONFIG_REG08, 0x01, 0x00); } - ret |= es7210_write_reg(ES7210_ANALOG_REG40, 0x43); /* Select power off analog, vdda = 3.3V, close vx20ff, VMID select 5KΩ start */ - ret |= es7210_write_reg(ES7210_MIC12_BIAS_REG41, 0x70); /* Select 2.87v */ - ret |= es7210_write_reg(ES7210_MIC34_BIAS_REG42, 0x70); /* Select 2.87v */ + ret |= es7210_write_reg(ES7210_ANALOG_REG40, 0x43); /* Select power off analog, vdda = 3.3V, close vx20ff, VMID select 5KΩ start */ + ret |= es7210_write_reg(ES7210_MIC12_BIAS_REG41, 0x70); /* Select 2.87v */ + ret |= es7210_write_reg(ES7210_MIC34_BIAS_REG42, 0x70); /* Select 2.87v */ ret |= es7210_write_reg(ES7210_OSR_REG07, 0x20); - ret |= es7210_write_reg(ES7210_MAINCLK_REG02, 0xc1); /* Set the frequency division coefficient and use dll except clock doubler, and need to set 0xc1 to clear the state */ + ret |= es7210_write_reg(ES7210_MAINCLK_REG02, 0xc1); /* Set the frequency division coefficient and use dll except clock doubler, and need to set 0xc1 to clear the state */ ret |= es7210_config_sample(i2s_cfg->samples); ret |= es7210_mic_select(ES7210_MIC_SELECT); - ret |= es7210_adc_set_gain(GAIN_24DB); + ret |= es7210_adc_set_gain(ES7210_MIC_SELECT, GAIN_24DB); return ESP_OK; } @@ -491,54 +492,84 @@ esp_err_t es7210_adc_ctrl_state(audio_hal_codec_mode_t mode, audio_hal_ctrl_t ct return ret; } -esp_err_t es7210_adc_set_gain(es7210_gain_value_t gain) +esp_err_t es7210_adc_set_gain(es7210_input_mics_t mic_select, es7210_gain_value_t gain) { esp_err_t ret = ESP_OK; - uint32_t max_gain_vaule = 14; + uint32_t max_gain_vaule = 14; if (gain < 0) { gain = 0; } else if (gain > max_gain_vaule) { gain = max_gain_vaule; } - ESP_LOGD(TAG, "SET: gain:%d", gain); - if (es7210_handle.mic_select & ES7210_INPUT_MIC1) { + ESP_LOGD(TAG, "Set gain %d", gain); + if (mic_select & ES7210_INPUT_MIC1) { ret |= es7210_update_reg_bit(ES7210_MIC1_GAIN_REG43, 0x0f, gain); } - if (es7210_handle.mic_select & ES7210_INPUT_MIC2) { + if (mic_select & ES7210_INPUT_MIC2) { ret |= es7210_update_reg_bit(ES7210_MIC2_GAIN_REG44, 0x0f, gain); } - if (es7210_handle.mic_select & ES7210_INPUT_MIC3) { + if (mic_select & ES7210_INPUT_MIC3) { ret |= es7210_update_reg_bit(ES7210_MIC3_GAIN_REG45, 0x0f, gain); } - if (es7210_handle.mic_select & ES7210_INPUT_MIC4) { + if (mic_select & ES7210_INPUT_MIC4) { ret |= es7210_update_reg_bit(ES7210_MIC4_GAIN_REG46, 0x0f, gain); } es7210_handle.gain = gain; return ret; } -esp_err_t es7210_adc_get_gain(void) +esp_err_t es7210_adc_get_gain(es7210_input_mics_t mic_select, int *gain) +{ + AUDIO_NULL_CHECK(TAG, gain, return ESP_FAIL); + switch (mic_select) { + case ES7210_INPUT_MIC1: + *gain = es7210_read_reg(ES7210_MIC1_GAIN_REG43); + break; + case ES7210_INPUT_MIC2: + *gain = es7210_read_reg(ES7210_MIC2_GAIN_REG44); + break; + case ES7210_INPUT_MIC3: + *gain = es7210_read_reg(ES7210_MIC3_GAIN_REG45); + break; + case ES7210_INPUT_MIC4: + *gain = es7210_read_reg(ES7210_MIC4_GAIN_REG46); + break; + default: + ESP_LOGE(TAG, "Unable to obtain multiple or zero microphone gains at the same time"); + return ESP_FAIL; + } + return ESP_OK; +} + +esp_err_t es7210_adc_print_gains(void) { int regv = 0; - uint8_t gain_value; if (es7210_handle.mic_select & ES7210_INPUT_MIC1) { - regv = es7210_read_reg(ES7210_MIC1_GAIN_REG43); - } else if (es7210_handle.mic_select & ES7210_INPUT_MIC2) { - regv = es7210_read_reg(ES7210_MIC2_GAIN_REG44); - } else if (es7210_handle.mic_select & ES7210_INPUT_MIC3) { - regv = es7210_read_reg(ES7210_MIC3_GAIN_REG45); - } else if (es7210_handle.mic_select & ES7210_INPUT_MIC4) { - regv = es7210_read_reg(ES7210_MIC4_GAIN_REG46); - } else { - ESP_LOGE(TAG, "No MIC selected"); - return ESP_FAIL; + es7210_adc_get_gain(ES7210_INPUT_MIC1, ®v); + /* Print the last four bits for gain */ + if (regv != ESP_FAIL) { + ESP_LOGI(TAG, "Get ES7210_MIC1_GAIN_REG43 (gain_value & 0xF) 0x0%x", (regv & 0x0f)); + } } - if (regv == ESP_FAIL) { - return regv; + if (es7210_handle.mic_select & ES7210_INPUT_MIC2) { + es7210_adc_get_gain(ES7210_INPUT_MIC2, ®v); + if (regv != ESP_FAIL) { + ESP_LOGI(TAG, "Get ES7210_MIC2_GAIN_REG44 (gain_value & 0xF) 0x0%x", (regv & 0x0f)); + } + } + if (es7210_handle.mic_select & ES7210_INPUT_MIC3) { + es7210_adc_get_gain(ES7210_INPUT_MIC3, ®v); + if (regv != ESP_FAIL) { + ESP_LOGI(TAG, "Get ES7210_MIC3_GAIN_REG45 (gain_value & 0xF) 0x0%x", (regv & 0x0f)); + } } - gain_value = (regv & 0x0f); /* Retain the last four bits for gain */ - ESP_LOGI(TAG, "GET: gain_value:%d", gain_value); - return gain_value; + if (es7210_handle.mic_select & ES7210_INPUT_MIC4) { + es7210_adc_get_gain(ES7210_INPUT_MIC4, ®v); + if (regv != ESP_FAIL) { + ESP_LOGI(TAG, "Get ES7210_MIC4_GAIN_REG46 (gain_value & 0xF) 0x0%x", (regv & 0x0f)); + } + } + return ESP_OK; } esp_err_t es7210_adc_set_volume(int volume) @@ -547,19 +578,19 @@ esp_err_t es7210_adc_set_volume(int volume) if (volume > GAIN_37_5DB) { volume = GAIN_37_5DB; } - esp_err_t ret = es7210_adc_set_gain(volume); + esp_err_t ret = es7210_adc_set_gain(es7210_handle.mic_select, volume); return ret; } esp_err_t es7210_set_mute(bool enable) { - int ret = 0; + esp_err_t ret = 0; if (enable) { - ret |= es7210_update_reg_bit(0x14, 0x03, 0x03); - ret |= es7210_update_reg_bit(0x15, 0x03, 0x03); + ret |= es7210_update_reg_bit(ES7210_ADC34_MUTERANGE_REG14, 0x03, 0x03); + ret |= es7210_update_reg_bit(ES7210_ADC12_MUTERANGE_REG15, 0x03, 0x03); } else { - ret |= es7210_update_reg_bit(0x14, 0x03, 0x00); - ret |= es7210_update_reg_bit(0x15, 0x03, 0x00); + ret |= es7210_update_reg_bit(ES7210_ADC34_MUTERANGE_REG14, 0x03, 0x00); + ret |= es7210_update_reg_bit(ES7210_ADC12_MUTERANGE_REG15, 0x03, 0x00); } ESP_LOGI(TAG, "%s", enable ? "Muted" : "Unmuted"); return ret == 0 ? ESP_OK : ESP_FAIL; diff --git a/components/audio_hal/driver/es7210/es7210.h b/components/audio_hal/driver/es7210/es7210.h index 2f9463531..52899c9e4 100644 --- a/components/audio_hal/driver/es7210/es7210.h +++ b/components/audio_hal/driver/es7210/es7210.h @@ -29,40 +29,41 @@ #ifdef __cplusplus extern "C" { -#endif - -#define ES7210_RESET_REG00 0x00 /* Reset control */ -#define ES7210_CLOCK_OFF_REG01 0x01 /* Used to turn off the ADC clock */ -#define ES7210_MAINCLK_REG02 0x02 /* Set ADC clock frequency division */ -#define ES7210_MASTER_CLK_REG03 0x03 /* MCLK source $ SCLK division */ -#define ES7210_LRCK_DIVH_REG04 0x04 /* lrck_divh */ -#define ES7210_LRCK_DIVL_REG05 0x05 /* lrck_divl */ -#define ES7210_POWER_DOWN_REG06 0x06 /* power down */ -#define ES7210_OSR_REG07 0x07 -#define ES7210_MODE_CONFIG_REG08 0x08 /* Set master/slave & channels */ -#define ES7210_TIME_CONTROL0_REG09 0x09 /* Set Chip intial state period*/ -#define ES7210_TIME_CONTROL1_REG0A 0x0A /* Set Power up state period */ -#define ES7210_SDP_INTERFACE1_REG11 0x11 /* Set sample & fmt */ -#define ES7210_SDP_INTERFACE2_REG12 0x12 /* Pins state */ -#define ES7210_ADC_AUTOMUTE_REG13 0x13 /* Set mute */ -#define ES7210_ADC34_MUTERANGE_REG14 0x14 /* Set mute range */ -#define ES7210_ADC34_HPF2_REG20 0x20 /* HPF */ -#define ES7210_ADC34_HPF1_REG21 0x21 -#define ES7210_ADC12_HPF1_REG22 0x22 -#define ES7210_ADC12_HPF2_REG23 0x23 -#define ES7210_ANALOG_REG40 0x40 /* ANALOG Power */ -#define ES7210_MIC12_BIAS_REG41 0x41 -#define ES7210_MIC34_BIAS_REG42 0x42 -#define ES7210_MIC1_GAIN_REG43 0x43 -#define ES7210_MIC2_GAIN_REG44 0x44 -#define ES7210_MIC3_GAIN_REG45 0x45 -#define ES7210_MIC4_GAIN_REG46 0x46 -#define ES7210_MIC1_POWER_REG47 0x47 -#define ES7210_MIC2_POWER_REG48 0x48 -#define ES7210_MIC3_POWER_REG49 0x49 -#define ES7210_MIC4_POWER_REG4A 0x4A -#define ES7210_MIC12_POWER_REG4B 0x4B /* MICBias & ADC & PGA Power */ -#define ES7210_MIC34_POWER_REG4C 0x4C +#endif /* __cplusplus */ + +#define ES7210_RESET_REG00 0x00 /* Reset control */ +#define ES7210_CLOCK_OFF_REG01 0x01 /* Used to turn off the ADC clock */ +#define ES7210_MAINCLK_REG02 0x02 /* Set ADC clock frequency division */ +#define ES7210_MASTER_CLK_REG03 0x03 /* MCLK source $ SCLK division */ +#define ES7210_LRCK_DIVH_REG04 0x04 /* lrck_divh */ +#define ES7210_LRCK_DIVL_REG05 0x05 /* lrck_divl */ +#define ES7210_POWER_DOWN_REG06 0x06 /* power down */ +#define ES7210_OSR_REG07 0x07 +#define ES7210_MODE_CONFIG_REG08 0x08 /* Set master/slave & channels */ +#define ES7210_TIME_CONTROL0_REG09 0x09 /* Set Chip intial state period*/ +#define ES7210_TIME_CONTROL1_REG0A 0x0A /* Set Power up state period */ +#define ES7210_SDP_INTERFACE1_REG11 0x11 /* Set sample & fmt */ +#define ES7210_SDP_INTERFACE2_REG12 0x12 /* Pins state */ +#define ES7210_ADC_AUTOMUTE_REG13 0x13 /* Set mute */ +#define ES7210_ADC34_MUTERANGE_REG14 0x14 /* Set mute range */ +#define ES7210_ADC12_MUTERANGE_REG15 0x15 /* Set mute range */ +#define ES7210_ADC34_HPF2_REG20 0x20 /* HPF */ +#define ES7210_ADC34_HPF1_REG21 0x21 +#define ES7210_ADC12_HPF1_REG22 0x22 +#define ES7210_ADC12_HPF2_REG23 0x23 +#define ES7210_ANALOG_REG40 0x40 /* ANALOG Power */ +#define ES7210_MIC12_BIAS_REG41 0x41 +#define ES7210_MIC34_BIAS_REG42 0x42 +#define ES7210_MIC1_GAIN_REG43 0x43 +#define ES7210_MIC2_GAIN_REG44 0x44 +#define ES7210_MIC3_GAIN_REG45 0x45 +#define ES7210_MIC4_GAIN_REG46 0x46 +#define ES7210_MIC1_POWER_REG47 0x47 +#define ES7210_MIC2_POWER_REG48 0x48 +#define ES7210_MIC3_POWER_REG49 0x49 +#define ES7210_MIC4_POWER_REG4A 0x4A +#define ES7210_MIC12_POWER_REG4B 0x4B /* MICBias & ADC & PGA Power */ +#define ES7210_MIC34_POWER_REG4C 0x4C typedef enum { ES7210_AD1_AD0_00 = 0x80, @@ -78,7 +79,7 @@ typedef enum { ES7210_INPUT_MIC4 = 0x08 } es7210_input_mics_t; -typedef enum gain_value{ +typedef enum gain_value { GAIN_0DB = 0, GAIN_3DB, GAIN_6DB, @@ -96,131 +97,131 @@ typedef enum gain_value{ GAIN_37_5DB, } es7210_gain_value_t; -/* - * @brief Initialize ES7210 ADC chip +/** + * @brief Initialize ES7210 ADC chip * - * @param[in] codec_cfg: configuration of ES7210 + * @param[in] codec_cfg configuration of ES7210 * * @return - * - ESP_OK - * - ESP_FAIL + * - ESP_OK + * - ESP_FAIL */ esp_err_t es7210_adc_init(audio_hal_codec_config_t *codec_cfg); /** - * @brief Deinitialize ES7210 ADC chip + * @brief Deinitialize ES7210 ADC chip * * @return - * - ESP_OK - * - ESP_FAIL + * - ESP_OK + * - ESP_FAIL */ esp_err_t es7210_adc_deinit(); /** - * @brief Configure ES7210 ADC mode and I2S interface + * @brief Configure ES7210 ADC mode and I2S interface * - * @param[in] mode: codec mode - * @param[in] iface: I2S config + * @param[in] mode codec mode + * @param[in] iface I2S config * * @return - * - ESP_FAIL Parameter error - * - ESP_OK Success + * - ESP_FAIL Parameter error + * - ESP_OK Success */ esp_err_t es7210_adc_config_i2s(audio_hal_codec_mode_t mode, audio_hal_codec_i2s_iface_t *iface); /** - * @brief Control ES7210 ADC chip + * @brief Control ES7210 ADC chip * - * @param[in] mode: codec mode - * @param[in] ctrl_state: start or stop progress + * @param[in] mode codec mode + * @param[in] ctrl_state start or stop progress * * @return - * - ESP_FAIL Parameter error - * - ESP_OK Success + * - ESP_FAIL Parameter error + * - ESP_OK Success */ esp_err_t es7210_adc_ctrl_state(audio_hal_codec_mode_t mode, audio_hal_ctrl_t ctrl_state); /** * @brief Set gain (Note: the enabled microphone sets the same gain) * - * @param[in] gain: gain - * - * gain : value - * GAIN_0DB : 1 - * GAIN_3DB : 2 - * GAIN_6DB : 3 - * · - * · - * · - * GAIN_30DB : 10 - * GAIN_33DB : 11 - * GAIN_34_5DB : 12 - * GAIN_36DB : 13 - * GAIN_37_5DB : 14 + * @param[in] mic_select mic select. eg: ES7210_INPUT_MIC1 | ES7210_INPUT_MIC2 + * @param[in] gain gain + * + * gain : value + * GAIN_0DB : 1 + * GAIN_3DB : 2 + * GAIN_6DB : 3 + * · + * · + * · + * GAIN_30DB : 10 + * GAIN_33DB : 11 + * GAIN_34_5DB : 12 + * GAIN_36DB : 13 + * GAIN_37_5DB : 14 * * @return - * - ESP_OK - * - ESP_FAIL + * - ESP_OK + * - ESP_FAIL */ -esp_err_t es7210_adc_set_gain(es7210_gain_value_t gain); +esp_err_t es7210_adc_set_gain(es7210_input_mics_t mic_select, es7210_gain_value_t gain); /** - * @brief Get gain + * @brief Get adcn gain + * + * @param[in] mic_select Selected mic + * @param[in] gain gain value * * @return - * - ESP_OK - * - ESP_FAIL + * - ESP_OK */ -esp_err_t es7210_adc_get_gain(void); +esp_err_t es7210_adc_get_gain(es7210_input_mics_t mic_select, int *gain); /** - * @brief Set volume - * - * @param[in] volume: volume + * @brief Print all adc gain * * @return - * - ESP_OK + * - ESP_OK */ -esp_err_t es7210_adc_set_volume(int volume); +esp_err_t es7210_adc_print_gains(void); /** - * @brief Set ES7210 ADC mute status + * @brief Set volume + * + * @param[in] volume volume * * @return - * - ESP_FAIL - * - ESP_OK + * - ESP_OK */ -esp_err_t es7210_set_mute(bool enable); +esp_err_t es7210_adc_set_volume(int volume); /** - * @brief Select ES7210 mic + * @brief Set ES7210 ADC mute status * - * @param[in] mic: mics - * * @return - * - ESP_FAIL - * - ESP_OK + * - ESP_FAIL + * - ESP_OK */ -esp_err_t es7210_mic_select(es7210_input_mics_t mic); +esp_err_t es7210_set_mute(bool enable); /** - * @brief Read regs of ES7210 + * @brief Select ES7210 mic + * + * @param[in] mic_select mics * - * @param[in] reg_addr: reg_addr - * * @return - * - ESP_FAIL - * - ESP_OK + * - ESP_FAIL + * - ESP_OK */ -int es7210_read_reg(uint8_t reg_addr); +esp_err_t es7210_mic_select(es7210_input_mics_t mic_select); /** - * @brief Read all regs of ES7210 + * @brief Read all regs of ES7210 */ void es7210_read_all(void); #ifdef __cplusplus } -#endif +#endif /* __cplusplus */ -#endif /* _ES7210_H_ */ +#endif /* _ES7210_H_ */ diff --git a/examples/advanced_examples/algorithm/main/algorithm_examples.c b/examples/advanced_examples/algorithm/main/algorithm_examples.c index 8eb1d0aff..fc4f8a993 100644 --- a/examples/advanced_examples/algorithm/main/algorithm_examples.c +++ b/examples/advanced_examples/algorithm/main/algorithm_examples.c @@ -16,6 +16,9 @@ #include "audio_pipeline.h" #include "board.h" #include "es8311.h" +#ifdef CONFIG_ESP32_S3_KORVO2_V3_BOARD +#include "es7210.h" +#endif /* CONFIG_ESP32_S3_KORVO2_V3_BOARD */ #include "fatfs_stream.h" #include "i2s_stream.h" #include "algorithm_stream.h" @@ -124,6 +127,9 @@ void app_main() audio_board_handle_t board_handle = audio_board_init(); audio_hal_ctrl_codec(board_handle->audio_hal, AUDIO_HAL_CODEC_MODE_BOTH, AUDIO_HAL_CTRL_START); audio_hal_set_volume(board_handle->audio_hal, 60); +#if CONFIG_ESP32_S3_KORVO2_V3_BOARD + es7210_adc_set_gain(ES7210_INPUT_MIC3, GAIN_0DB); +#endif /* CONFIG_ESP32_S3_KORVO2_V3_BOARD */ i2s_stream_cfg_t i2s_r_cfg = I2S_STREAM_CFG_DEFAULT_WITH_PARA(CODEC_ADC_I2S_PORT, I2S_SAMPLE_RATE, I2S_BITS, AUDIO_STREAM_READER); i2s_r_cfg.task_stack = -1; @@ -258,7 +264,7 @@ void app_main() continue; } - if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT && msg.source == (void *) mp3_decoder + if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT && msg.source == (void *)mp3_decoder && msg.cmd == AEL_MSG_CMD_REPORT_MUSIC_INFO) { audio_element_info_t music_info = {0}; audio_element_getinfo(mp3_decoder, &music_info); @@ -269,7 +275,7 @@ void app_main() } /* Stop when the last pipeline element receives stop event */ - if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT && msg.source == (void *) filter_w + if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT && msg.source == (void *)filter_w && msg.cmd == AEL_MSG_CMD_REPORT_STATUS && (((int)msg.data == AEL_STATUS_STATE_STOPPED) || ((int)msg.data == AEL_STATUS_STATE_FINISHED))) { ESP_LOGW(TAG, "[ * ] Stop event received");