Skip to content

Commit 63304e2

Browse files
committed
Merge branch 'refactor/migrate_i2c_driver_in_es7210_example' into 'master'
refactor(i2s_es7210): refactor es7210 example Closes IDF-10769 and IDF-13148 See merge request espressif/esp-idf!40058
2 parents 92fa23b + aad133a commit 63304e2

File tree

5 files changed

+91
-70
lines changed

5 files changed

+91
-70
lines changed

examples/peripherals/i2s/i2s_codec/i2s_es7210_tdm/README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ This example demonstrates how to use I2S TDM mode to record 4 MICs connected to
1616
| ES7210_I2S_FMT_DSP_A | PCM short format |
1717
| ES7210_I2S_FMT_DSP_B | PCM long format |
1818

19+
In this example, we use `esp_codec_dev` dependency which contains the `es7210` driver. This driver uses `ES7210_I2S_FMT_I2S` format by default.
20+
1921
Recorded voice will be saved to SD card in `wav` format, and can be played or processed on PC.
2022

2123
## How to Use Example
@@ -31,11 +33,11 @@ All the GPIO used in this example can be changed according to your board, by mac
3133

3234
### Dependency
3335

34-
This example is based on [es7210 component](https://components.espressif.com/component/espressif/es7210)
36+
This example is based on [esp_codec_dev component](https://components.espressif.com/components/espressif/esp_codec_dev)
3537

36-
The component can be installed by esp component manager. Since this example already installed it, no need to re-installed it again, but if you want to install this component in your own project, you can input the following command:
38+
The component can be installed by [IDF Component Manager](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/tools/idf-component-manager.html). This example already includes it. If you want to install [esp_codec_dev component](https://components.espressif.com/components/espressif/esp_codec_dev) separately in your project, you can input the following command:
3739
```
38-
idf.py add-dependency espressif/es7210^1.0.0
40+
idf.py add-dependency "espressif/esp_codec_dev^1.3.4"
3941
```
4042

4143
If the dependency is added, you can check `idf_component.yml` for more detail. When building this example or other projects with managed components, the component manager will search for the required components online and download them into the `managed_components` folder.
@@ -46,11 +48,8 @@ If the dependency is added, you can check `idf_component.yml` for more detail. W
4648
```
4749
idf.py set-target TARGET
4850
```
49-
* Change value of `EXAMPLE_I2S_FORMAT` to check I2S driver's functionality on different I2S formats.
50-
* Change `EXAMPLE_ES7210_MIC_GAIN` and `EXAMPLE_ES7210_MIC_BIAS` according your MIC's specs if needed.
51-
* Change `EXAMPLE_ES7210_ADC_VOLUME` if recorded voice is too loud or too quite.
5251

53-
Note: it's better to adjust `EXAMPLE_ES7210_MIC_GAIN` first. If adjusting MIC gain doesn't meet your demand, you can then adjust `EXAMPLE_ES7210_ADC_VOLUME`. That is to say, it's better to adjust analog gain than digital gain.
52+
The configuration macros are defined at the beginning of `main/i2s_es7210_record_example.c`, you can change the configuration macros according to your needs.
5453

5554
### Build and Flash
5655

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
idf_component_register(SRCS "i2s_es7210_record_example.c"
2-
PRIV_REQUIRES esp_driver_i2s esp_driver_gpio fatfs i2s_examples_common
2+
PRIV_REQUIRES esp_driver_i2s esp_driver_i2c esp_driver_gpio fatfs i2s_examples_common
33
INCLUDE_DIRS
44
)

examples/peripherals/i2s/i2s_codec/i2s_es7210_tdm/main/i2s_es7210_record_example.c

Lines changed: 80 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Unlicense OR CC0-1.0
55
*/
@@ -8,8 +8,10 @@
88
#include "esp_check.h"
99
#include "esp_vfs_fat.h"
1010
#include "driver/i2s_tdm.h"
11-
#include "driver/i2c.h"
12-
#include "es7210.h"
11+
#include "driver/i2c_master.h"
12+
#include "esp_codec_dev_defaults.h"
13+
#include "esp_codec_dev.h"
14+
#include "esp_codec_dev_vol.h"
1315
#include "format_wav.h"
1416

1517
#if CONFIG_IDF_TARGET_ESP32S3 // ESP32-S3-Korvo-1 pin out
@@ -67,7 +69,6 @@
6769
#endif
6870

6971
/* I2S configurations */
70-
#define EXAMPLE_I2S_TDM_FORMAT (ES7210_I2S_FMT_I2S)
7172
#define EXAMPLE_I2S_CHAN_NUM (4)
7273
#define EXAMPLE_I2S_SAMPLE_RATE (48000)
7374
#define EXAMPLE_I2S_MCLK_MULTIPLE (I2S_MCLK_MULTIPLE_256)
@@ -76,10 +77,8 @@
7677

7778
/* ES7210 configurations */
7879
#define EXAMPLE_ES7210_I2C_ADDR (0x40)
79-
#define EXAMPLE_ES7210_I2C_CLK (100000)
80-
#define EXAMPLE_ES7210_MIC_GAIN (ES7210_MIC_GAIN_30DB)
81-
#define EXAMPLE_ES7210_MIC_BIAS (ES7210_MIC_BIAS_2V87)
82-
#define EXAMPLE_ES7210_ADC_VOLUME (0)
80+
#define EXAMPLE_ES7210_MIC_GAIN (30) // 30db
81+
#define EXAMPLE_ES7210_MIC_SELECTED (ES7120_SEL_MIC1 | ES7120_SEL_MIC2 | ES7120_SEL_MIC3 | ES7120_SEL_MIC4)
8382

8483
/* SD card & recording configurations */
8584
#define EXAMPLE_RECORD_TIME_SEC (10)
@@ -97,15 +96,11 @@ static i2s_chan_handle_t es7210_i2s_init(void)
9796

9897
ESP_LOGI(TAG, "Configure I2S receive channel to TDM mode");
9998
i2s_tdm_config_t i2s_tdm_rx_conf = {
100-
#if EXAMPLE_I2S_FORMAT == ES7210_I2S_FMT_I2S
101-
.slot_cfg = I2S_TDM_PHILIPS_SLOT_DEFAULT_CONFIG(EXAMPLE_I2S_SAMPLE_BITS, I2S_SLOT_MODE_STEREO, EXAMPLE_I2S_TDM_SLOT_MASK),
102-
#elif EXAMPLE_I2S_FORMAT == ES7210_I2S_FMT_LJ
103-
.slot_cfg = I2S_TDM_MSB_SLOT_DEFAULT_CONFIG(EXAMPLE_I2S_SAMPLE_BITS, I2S_SLOT_MODE_STEREO, EXAMPLE_I2S_TDM_SLOT_MASK),
104-
#elif EXAMPLE_I2S_FORMAT == ES7210_I2S_FMT_DSP_A
105-
.slot_cfg = I2S_TDM_PCM_SHORT_SLOT_DEFAULT_CONFIG(EXAMPLE_I2S_SAMPLE_BITS, I2S_SLOT_MODE_STEREO, EXAMPLE_I2S_TDM_SLOT_MASK),
106-
#elif EXAMPLE_I2S_FORMAT == ES7210_I2S_FMT_DSP_B
107-
.slot_cfg = I2S_TDM_PCM_LONG_SLOT_DEFAULT_CONFIG(EXAMPLE_I2S_SAMPLE_BITS, I2S_SLOT_MODE_STEREO, EXAMPLE_I2S_TDM_SLOT_MASK),
108-
#endif
99+
// es7210 driver is default to use philips format in esp_codec_dev component
100+
.slot_cfg = I2S_TDM_PHILIPS_SLOT_DEFAULT_CONFIG(
101+
EXAMPLE_I2S_SAMPLE_BITS,
102+
I2S_SLOT_MODE_STEREO,
103+
EXAMPLE_I2S_TDM_SLOT_MASK),
109104
.clk_cfg = {
110105
.clk_src = I2S_CLK_SRC_DEFAULT,
111106
.sample_rate_hz = EXAMPLE_I2S_SAMPLE_RATE,
@@ -173,40 +168,81 @@ sdmmc_card_t * mount_sdcard(void)
173168
return sdmmc_card;
174169
}
175170

176-
static void es7210_codec_init(void)
171+
static esp_codec_dev_handle_t es7210_codec_init(i2s_chan_handle_t i2s_rx_chan)
177172
{
178173
ESP_LOGI(TAG, "Init I2C used to configure ES7210");
179-
i2c_config_t i2c_conf = {
174+
/* Initialize I2C peripheral */
175+
i2c_master_bus_handle_t i2c_bus_handle = NULL;
176+
i2c_master_bus_config_t i2c_mst_cfg = {
177+
.i2c_port = EXAMPLE_I2C_NUM,
180178
.sda_io_num = EXAMPLE_I2C_SDA_IO,
181179
.scl_io_num = EXAMPLE_I2C_SCL_IO,
182-
.mode = I2C_MODE_MASTER,
183-
.sda_pullup_en = GPIO_PULLUP_ENABLE,
184-
.scl_pullup_en = GPIO_PULLUP_ENABLE,
185-
.master.clk_speed = EXAMPLE_ES7210_I2C_CLK,
180+
.clk_source = I2C_CLK_SRC_DEFAULT,
181+
.glitch_ignore_cnt = 7,
182+
/* Pull-up internally for no external pull-up case.
183+
Suggest to use external pull-up to ensure a strong enough pull-up. */
184+
.flags.enable_internal_pullup = true,
186185
};
187-
ESP_ERROR_CHECK(i2c_param_config(EXAMPLE_I2C_NUM, &i2c_conf));
188-
ESP_ERROR_CHECK(i2c_driver_install(EXAMPLE_I2C_NUM, i2c_conf.mode, 0, 0, 0));
186+
ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_mst_cfg, &i2c_bus_handle));
189187

190-
/* Create ES7210 device handle */
191-
es7210_dev_handle_t es7210_handle = NULL;
192-
es7210_i2c_config_t es7210_i2c_conf = {
193-
.i2c_port = EXAMPLE_I2C_NUM,
194-
.i2c_addr = EXAMPLE_ES7210_I2C_ADDR
188+
/* Create control interface with I2C bus handle */
189+
audio_codec_i2c_cfg_t i2c_cfg = {
190+
.port = EXAMPLE_I2C_NUM,
191+
.addr = ES7210_CODEC_DEFAULT_ADDR,
192+
.bus_handle = i2c_bus_handle,
195193
};
196-
ESP_ERROR_CHECK(es7210_new_codec(&es7210_i2c_conf, &es7210_handle));
194+
const audio_codec_ctrl_if_t *ctrl_if = audio_codec_new_i2c_ctrl(&i2c_cfg);
195+
assert(ctrl_if);
196+
197+
/* Create data interface with I2S bus handle */
198+
audio_codec_i2s_cfg_t i2s_cfg = {
199+
.port = EXAMPLE_I2S_NUM,
200+
.rx_handle = i2s_rx_chan,
201+
.tx_handle = NULL,
202+
};
203+
const audio_codec_data_if_t *data_if = audio_codec_new_i2s_data(&i2s_cfg);
204+
assert(data_if);
197205

206+
/* Create ES7210 interface handle */
198207
ESP_LOGI(TAG, "Configure ES7210 codec parameters");
199-
es7210_codec_config_t codec_conf = {
200-
.i2s_format = EXAMPLE_I2S_TDM_FORMAT,
201-
.mclk_ratio = EXAMPLE_I2S_MCLK_MULTIPLE,
202-
.sample_rate_hz = EXAMPLE_I2S_SAMPLE_RATE,
203-
.bit_width = (es7210_i2s_bits_t)EXAMPLE_I2S_SAMPLE_BITS,
204-
.mic_bias = EXAMPLE_ES7210_MIC_BIAS,
205-
.mic_gain = EXAMPLE_ES7210_MIC_GAIN,
206-
.flags.tdm_enable = true
208+
es7210_codec_cfg_t es7210_cfg = {
209+
.ctrl_if = ctrl_if,
210+
.master_mode = false,
211+
.mic_selected = EXAMPLE_ES7210_MIC_SELECTED,
212+
.mclk_src = ES7210_MCLK_FROM_PAD,
213+
.mclk_div = EXAMPLE_I2S_MCLK_MULTIPLE,
214+
};
215+
const audio_codec_if_t *es7210_if = es7210_codec_new(&es7210_cfg);
216+
assert(es7210_if);
217+
218+
/* Create the top codec handle with ES7210 interface handle and data interface */
219+
esp_codec_dev_cfg_t dev_cfg = {
220+
.dev_type = ESP_CODEC_DEV_TYPE_IN,
221+
.codec_if = es7210_if,
222+
.data_if = data_if,
207223
};
208-
ESP_ERROR_CHECK(es7210_config_codec(es7210_handle, &codec_conf));
209-
ESP_ERROR_CHECK(es7210_config_volume(es7210_handle, EXAMPLE_ES7210_ADC_VOLUME));
224+
esp_codec_dev_handle_t codec_handle = esp_codec_dev_new(&dev_cfg);
225+
assert(codec_handle);
226+
227+
/* Specify the sample configurations and open the device */
228+
esp_codec_dev_sample_info_t sample_cfg = {
229+
.bits_per_sample = EXAMPLE_I2S_SAMPLE_BITS,
230+
.channel = EXAMPLE_I2S_CHAN_NUM,
231+
.channel_mask = EXAMPLE_ES7210_MIC_SELECTED,
232+
.sample_rate = EXAMPLE_I2S_SAMPLE_RATE,
233+
};
234+
if (esp_codec_dev_open(codec_handle, &sample_cfg) != ESP_CODEC_DEV_OK) {
235+
ESP_LOGE(TAG, "Open codec device failed");
236+
assert(false);
237+
}
238+
239+
/* Set the initial gain */
240+
if (esp_codec_dev_set_in_gain(codec_handle, EXAMPLE_ES7210_MIC_GAIN) != ESP_CODEC_DEV_OK) {
241+
ESP_LOGE(TAG, "set input gain failed");
242+
assert(false);
243+
}
244+
245+
return codec_handle;
210246
}
211247

212248
static esp_err_t record_wav(i2s_chan_handle_t i2s_rx_chan)
@@ -231,7 +267,6 @@ static esp_err_t record_wav(i2s_chan_handle_t i2s_rx_chan)
231267
/* Start recording */
232268
size_t wav_written = 0;
233269
static int16_t i2s_readraw_buff[4096];
234-
ESP_GOTO_ON_ERROR(i2s_channel_enable(i2s_rx_chan), err, TAG, "error while starting i2s rx channel");
235270
while (wav_written < wav_size) {
236271
if (wav_written % byte_rate < sizeof(i2s_readraw_buff)) {
237272
ESP_LOGI(TAG, "Recording: %"PRIu32"/%ds", wav_written / byte_rate + 1, (int)EXAMPLE_RECORD_TIME_SEC);
@@ -247,7 +282,6 @@ static esp_err_t record_wav(i2s_chan_handle_t i2s_rx_chan)
247282
}
248283

249284
err:
250-
i2s_channel_disable(i2s_rx_chan);
251285
ESP_LOGI(TAG, "Recording done! Flushing file buffer");
252286
fclose(f);
253287

@@ -259,13 +293,15 @@ void app_main(void)
259293
/* Init I2C bus to configure ES7210 and I2S bus to receive audio data from ES7210 */
260294
i2s_chan_handle_t i2s_rx_chan = es7210_i2s_init();
261295
/* Create ES7210 device handle and configure codec parameters */
262-
es7210_codec_init();
296+
esp_codec_dev_handle_t codec_handle = es7210_codec_init(i2s_rx_chan);
263297
/* Mount SD card, the recorded audio file will be saved into it */
264298
sdmmc_card_t *sdmmc_card = mount_sdcard();
265299
/* Start to record wav audio */
266300
esp_err_t err = record_wav(i2s_rx_chan);
267301
/* Unmount SD card */
268302
esp_vfs_fat_sdcard_unmount(EXAMPLE_SD_MOUNT_POINT, sdmmc_card);
303+
/* Close codec device */
304+
esp_codec_dev_close(codec_handle);
269305
if (err == ESP_OK) {
270306
ESP_LOGI(TAG, "Audio was successfully recorded into "EXAMPLE_RECORD_FILE_PATH
271307
". You can now remove the SD card safely");
Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,5 @@
11
## IDF Component Manager Manifest File
22
dependencies:
3-
espressif/es7210: "^1.0.0"
4-
## Required IDF version
5-
idf:
6-
version: ">=4.4.0"
7-
# # Put list of dependencies here
8-
# # For components maintained by Espressif:
9-
# component: "~1.0.0"
10-
# # For 3rd party components:
11-
# username/component: ">=1.0.0,<2.0.0"
12-
# username2/component2:
13-
# version: "~1.0.0"
14-
# # For transient dependencies `public` flag can be set.
15-
# # `public` flag doesn't have an effect dependencies of the `main` component.
16-
# # All dependencies of `main` are public by default.
17-
# public: true
3+
espressif/esp_codec_dev: ^1.3.4
184
i2s_examples_common:
195
path: ${IDF_PATH}/examples/peripherals/i2s/i2s_examples_common

examples/peripherals/i2s/i2s_codec/i2s_es8311/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ Note: Since ESP32-C3 & ESP32-H2 board does not have GPIO 16/17, you can use othe
5959

6060
### Dependency
6161

62-
This example is based on [es8311 component](https://components.espressif.com/component/espressif/es8311)
62+
This example is based on [esp_codec_dev component](https://components.espressif.com/components/espressif/esp_codec_dev)
6363

64-
The component can be installed by [IDF Component Manager](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/tools/idf-component-manager.html). This example already includes it. If you want to install [es8311 component](https://components.espressif.com/components/espressif/es8311) separately in your project, you can input the following command:
64+
The component can be installed by [IDF Component Manager](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/tools/idf-component-manager.html). This example already includes it. If you want to install [esp_codec_dev component](https://components.espressif.com/components/espressif/esp_codec_dev) separately in your project, you can input the following command:
6565
```
66-
idf.py add-dependency "espressif/es8311^1.0.0"
66+
idf.py add-dependency "espressif/esp_codec_dev^1.3.4"
6767
```
6868

6969
If the dependency is added, you can check `idf_component.yml` for more detail. When building this example or other projects with managed components, the component manager will search for the required components online and download them into the `managed_components` folder.

0 commit comments

Comments
 (0)