Skip to content

Commit 9512eef

Browse files
committed
Merge branch 'bugfix/fix_capture_err_when_change_sink' into 'main'
Fix capture fail if change sink config See merge request adf/esp-webrtc-solution!32
2 parents 2ac8737 + 44fdb0d commit 9512eef

File tree

11 files changed

+268
-33
lines changed

11 files changed

+268
-33
lines changed

components/codec_board/CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Changelog
2+
3+
## v0.5.4
4+
5+
- Add PDM support
6+
- Add play and record support for `XD_AIOT_C3` and `ESP_SPOT`
7+
8+
## v0.5.3
9+
10+
- Add support for mount SDCard on esp32
11+
- Add support for I2C manually control use `init_i2c` and `deinit_i2c`
12+
- Fixed I2S driver failed to initialized if more than one
13+
14+
## v0.5.0
15+
16+
- Initial version of `codec_board`

components/codec_board/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
set(COMPONENT_PRIV_REQUIRE driver esp_codec_dev fatfs media_lib_sal esp_lcd)
2+
set(COMPONENT_PRIV_REQUIRE driver esp_codec_dev fatfs esp_lcd)
33

44
if("${IDF_VERSION_MAJOR}.${IDF_VERSION_MINOR}" VERSION_GREATER_EQUAL "5.3")
55
list(APPEND COMPONENT_PRIV_REQUIRE esp_driver_i2c)

components/codec_board/LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Espressif Modified MIT License
2+
3+
Copyright (c) 2025 Espressif Systems (Shanghai) CO., LTD
4+
5+
Permission is hereby granted for use EXCLUSIVELY with Espressif Systems products.
6+
This includes the right to use, copy, modify, merge, publish, distribute, and sublicense
7+
the Software, subject to the following conditions:
8+
9+
1. This Software MUST BE USED IN CONJUNCTION WITH ESPRESSIF SYSTEMS PRODUCTS.
10+
2. The above copyright notice and this permission notice shall be included in all copies
11+
or substantial portions of the Software.
12+
3. Redistribution of the Software in source or binary form FOR USE WITH NON-ESPRESSIF PRODUCTS
13+
is strictly prohibited.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16+
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17+
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
18+
FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20+
DEALINGS IN THE SOFTWARE.
21+
22+
SPDX-License-Identifier: LicenseRef-Espressif-Modified-MIT

components/codec_board/board_cfg.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ i2c: {sda: 18, scl: 23}
3434
i2s: {mclk: 0, bclk: 5, ws: 25, dout: 26, din: 35}
3535
i2s: {mclk: -1, bclk: 32, ws: 33, din: 36}
3636
out: {codec: ES8311, pa: 21, use_mclk: 1, pa_gain:20}
37-
in: {codec: ES7243, i2s_port: 0}
37+
in: {codec: ES7243, i2s_port: 1}
3838
sdcard: {power: 13, clk: 14, cmd: 15, d0: 2}
3939

4040
Board: ESP32_KORVO_V1
@@ -84,3 +84,13 @@ i2s: {mclk: -1, bclk: 41, ws: 42, dout: -1, din: 2}
8484
in: {codec: DUMMY, i2c_port: -1}
8585
sdcard: {clk: 39, cmd: 38, d0: 40}
8686
camera: {type: dvp, xclk: 15, pclk: 13, vsync: 6, href: 7, d0: 11, d1: 9, d2: 8, d3: 10, d4: 12, d5: 18, d6: 17, d7: 16}
87+
88+
Board: XD_AIOT_C3
89+
i2c: {sda: 0, scl: 1}
90+
i2s: {mclk: 10, bclk: 8, ws: 12, dout: 11, din: 7}
91+
in_out: {codec: ES8311, pa: 13, pa_gain:6}
92+
93+
Board: ESP_SPOT
94+
i2c: {sda: 25, scl: 26, i2c_addr: 0x18}
95+
i2s: {bclk: 7, ws: 8, dout: 9, din: 10}
96+
in_out: {codec: ES8311, pa: 23, pa_gain:6}

components/codec_board/codec_board.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ static board_section_t *codec;
1313

1414
void set_codec_board_type(const char *codec_type)
1515
{
16+
if (codec) {
17+
return;
18+
}
1619
codec = get_codec_section(codec_type);
1720
}
1821

components/codec_board/codec_init.c

Lines changed: 118 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
66
#include "driver/i2s_std.h"
77
#include "driver/i2s_tdm.h"
8+
#include "driver/i2s_pdm.h"
89
#include "soc/soc_caps.h"
910
#else
1011
#include "driver/i2s.h"
@@ -102,7 +103,12 @@ static int _i2c_init(uint8_t port)
102103

103104
void *get_i2c_bus_handle(uint8_t port)
104105
{
105-
ESP_LOGI(TAG, "Get mater handle %d %p", port, i2c_bus_handle[port]);
106+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 4, 0)
107+
// Try to get port from I2C driver directly
108+
i2c_master_bus_handle_t bus_handle = NULL;
109+
i2c_master_get_bus_handle(port, &bus_handle);
110+
return bus_handle;
111+
#endif
106112
return i2c_bus_handle[port];
107113
}
108114

@@ -182,6 +188,7 @@ static int _i2s_init(uint8_t port, esp_codec_dev_type_t dev_type, codec_init_cfg
182188
};
183189
tdm_cfg.slot_cfg.total_slot = 4;
184190
#endif
191+
chan_cfg.id = I2S_NUM_AUTO; // Use auto ID
185192
int ret = i2s_new_channel(&chan_cfg, output == false ? NULL : &i2s_keep[port]->tx_handle,
186193
input == false ? NULL : &i2s_keep[port]->rx_handle);
187194
ESP_LOGI(TAG, "tx:%p rx:%p", i2s_keep[port]->tx_handle, i2s_keep[port]->rx_handle);
@@ -195,6 +202,23 @@ static int _i2s_init(uint8_t port, esp_codec_dev_type_t dev_type, codec_init_cfg
195202
ret = i2s_channel_init_tdm_mode(i2s_keep[port]->tx_handle, &tdm_cfg);
196203
ESP_LOGI(TAG, "output init tdm ret %d", ret);
197204
}
205+
#endif
206+
#ifdef SOC_I2S_SUPPORTS_PDM_TX
207+
else if (init_cfg->out_mode == CODEC_I2S_MODE_PDM) {
208+
i2s_pdm_tx_config_t pdm_cfg = {
209+
.clk_cfg = I2S_PDM_TX_CLK_DEFAULT_CONFIG(16000),
210+
.slot_cfg = I2S_PDM_TX_SLOT_DEFAULT_CONFIG(16, I2S_SLOT_MODE_STEREO),
211+
.gpio_cfg = {
212+
.dout = i2s_cfg.dout,
213+
.clk = i2s_cfg.bclk,
214+
.invert_flags = {
215+
.clk_inv = false,
216+
},
217+
},
218+
};
219+
ret = i2s_channel_init_pdm_tx_mode(i2s_keep[port]->tx_handle, &pdm_cfg);
220+
ESP_LOGI(TAG, "output init pdm ret %d", ret);
221+
}
198222
#endif
199223
}
200224
if (i2s_keep[port]->rx_handle) {
@@ -207,6 +231,23 @@ static int _i2s_init(uint8_t port, esp_codec_dev_type_t dev_type, codec_init_cfg
207231
ret = i2s_channel_init_tdm_mode(i2s_keep[port]->rx_handle, &tdm_cfg);
208232
ESP_LOGI(TAG, "Input init tdm ret %d", ret);
209233
}
234+
#endif
235+
#ifdef SOC_I2S_SUPPORTS_PDM_RX
236+
else if (init_cfg->in_mode == CODEC_I2S_MODE_PDM) {
237+
i2s_pdm_rx_config_t pdm_cfg = {
238+
.clk_cfg = I2S_PDM_RX_CLK_DEFAULT_CONFIG(16000),
239+
.slot_cfg = I2S_PDM_RX_SLOT_DEFAULT_CONFIG(16, I2S_SLOT_MODE_STEREO),
240+
.gpio_cfg = {
241+
.din = i2s_cfg.din,
242+
.clk = i2s_cfg.bclk,
243+
.invert_flags = {
244+
.clk_inv = false,
245+
},
246+
},
247+
};
248+
ret = i2s_channel_init_pdm_rx_mode(i2s_keep[port]->rx_handle, &pdm_cfg);
249+
ESP_LOGI(TAG, "Input init pdm ret %d", ret);
250+
}
210251
#endif
211252
}
212253
// Enable I2S here for maybe some codec need I2S clock to set register correctly
@@ -275,6 +316,28 @@ static int _i2s_deinit(uint8_t port)
275316
return 0;
276317
}
277318

319+
int init_i2c(uint8_t port)
320+
{
321+
return _i2c_init(port);
322+
}
323+
324+
int deinit_i2c(uint8_t port)
325+
{
326+
return _i2c_deinit(port);
327+
}
328+
329+
static int check_i2c_inited(int8_t port)
330+
{
331+
if (port < 0) {
332+
return 0;
333+
}
334+
// Already installed
335+
if (get_i2c_bus_handle(port)) {
336+
return 0;
337+
}
338+
return _i2c_init(port);
339+
}
340+
278341
int init_codec(codec_init_cfg_t *cfg)
279342
{
280343
if (cfg == NULL) {
@@ -299,13 +362,13 @@ int init_codec(codec_init_cfg_t *cfg)
299362
ESP_LOGE(TAG, "No codec device found");
300363
return -1;
301364
}
302-
// Force to init I2C firstly
303-
_i2c_init(0);
365+
// Try to get I2C handle
366+
check_i2c_inited(0);
304367
// Init i2c and i2s
305368
bool same_i2s = (has_in && has_out && out_cfg.i2s_port == in_cfg.i2s_port);
306369
ESP_LOGI(TAG, "in:%d out:%d port: %d", has_in, has_out, out_cfg.i2s_port == in_cfg.i2s_port);
307370
if (has_out) {
308-
if (out_cfg.i2c_port >= 0 && _i2c_init(out_cfg.i2c_port)) {
371+
if (check_i2c_inited(out_cfg.i2c_port) < 0) {
309372
ESP_LOGE(TAG, "Fail to int i2c: %d", out_cfg.i2c_port);
310373
return -1;
311374
}
@@ -317,7 +380,7 @@ int init_codec(codec_init_cfg_t *cfg)
317380
ESP_LOGI(TAG, "Success to init i2s: %d", in_cfg.i2s_port);
318381
}
319382
if (has_in) {
320-
if (in_cfg.i2c_port >= 0 && _i2c_init(in_cfg.i2c_port)) {
383+
if (check_i2c_inited(in_cfg.i2c_port) < 0) {
321384
ESP_LOGE(TAG, "Fail to int i2c: %d", in_cfg.i2c_port);
322385
return -1;
323386
}
@@ -346,7 +409,7 @@ int init_codec(codec_init_cfg_t *cfg)
346409
audio_codec_i2c_cfg_t i2c_cfg = {
347410
.port = out_cfg.i2c_port,
348411
#ifdef USE_I2C_MASTER
349-
.bus_handle = i2c_bus_handle[out_cfg.i2c_port],
412+
.bus_handle = get_i2c_bus_handle(out_cfg.i2c_port),
350413
#endif
351414
};
352415
// TODO add other codec support
@@ -420,7 +483,7 @@ int init_codec(codec_init_cfg_t *cfg)
420483
audio_codec_i2c_cfg_t i2c_cfg = {
421484
.port = in_cfg.i2c_port,
422485
#ifdef USE_I2C_MASTER
423-
.bus_handle = i2c_bus_handle[in_cfg.i2c_port],
486+
.bus_handle = get_i2c_bus_handle(in_cfg.i2c_port),
424487
#endif
425488
};
426489
// TODO add other codec support
@@ -464,12 +527,18 @@ int init_codec(codec_init_cfg_t *cfg)
464527
};
465528
codec_res.record_dev = esp_codec_dev_new(&dev_cfg);
466529
}
467-
int ret = esp_codec_dev_set_out_vol(codec_res.play_dev, 60.0);
468-
ret = esp_codec_dev_set_in_gain(codec_res.record_dev, 30.0);
469-
if (ret == 0) {
530+
// Set default volume and gain for play and record
531+
if (codec_res.play_dev) {
532+
esp_codec_dev_set_out_vol(codec_res.play_dev, 60.0);
533+
}
534+
if (codec_res.record_dev) {
535+
esp_codec_dev_set_in_gain(codec_res.record_dev, 30.0);
536+
}
537+
if ((codec_res.play_dev != NULL) || (codec_res.record_dev != NULL)) {
470538
codec_res.inited = true;
539+
return 0;
471540
}
472-
return ret;
541+
return -1;
473542
}
474543

475544
esp_codec_dev_handle_t get_playback_handle(void)
@@ -554,6 +623,17 @@ static void enable_mmc_phy_power(void)
554623
#endif
555624
}
556625

626+
#if CONFIG_IDF_TARGET_ESP32P4
627+
static void sdmmc_get_slot(const int slot, sdmmc_slot_config_t *config)
628+
{
629+
memset(config, 0, sizeof(sdmmc_slot_config_t));
630+
config->cd = SDMMC_SLOT_NO_CD;
631+
config->wp = SDMMC_SLOT_NO_WP;
632+
config->width = 4;
633+
config->flags = 0;
634+
}
635+
#endif
636+
557637
int mount_sdcard(void)
558638
{
559639
sdcard_cfg_t cfg = { 0 };
@@ -562,17 +642,33 @@ int mount_sdcard(void)
562642
if (ret != 0) {
563643
return ret;
564644
}
645+
646+
#if defined CONFIG_IDF_TARGET_ESP32
647+
gpio_config_t sdcard_pwr_pin_cfg = {
648+
.pin_bit_mask = 1UL << GPIO_NUM_13,
649+
.mode = GPIO_MODE_OUTPUT,
650+
.pull_up_en = GPIO_PULLUP_DISABLE,
651+
.pull_down_en = GPIO_PULLDOWN_DISABLE,
652+
.intr_type = GPIO_INTR_DISABLE,
653+
};
654+
gpio_config(&sdcard_pwr_pin_cfg);
655+
gpio_set_level(GPIO_NUM_13, 0);
656+
#endif
657+
565658
#if SOC_SDMMC_HOST_SUPPORTED
566659
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
567660
.format_if_mount_failed = false,
568661
.max_files = 5,
569662
};
570663
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
571664
#if CONFIG_IDF_TARGET_ESP32P4
665+
host.slot = 0;
572666
host.max_freq_khz = SDMMC_FREQ_HIGHSPEED;
573667
#endif
574668
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
575669
slot_config.width = cfg.d3 ? 4 : 1;
670+
#if SOC_SDMMC_USE_GPIO_MATRIX
671+
slot_config.width = cfg.d3 ? 4 : 1;
576672
slot_config.flags |= SDMMC_SLOT_FLAG_INTERNAL_PULLUP;
577673
slot_config.d4 = -1;
578674
slot_config.d5 = -1;
@@ -586,14 +682,24 @@ int mount_sdcard(void)
586682
slot_config.d1 = cfg.d1 ? cfg.d1 : -1;
587683
slot_config.d2 = cfg.d2 ? cfg.d2 : -1;
588684
slot_config.d3 = cfg.d3 ? cfg.d3 : -1;
685+
#endif /* SOC_SDMMC_USE_GPIO_MATRIX */
686+
#if CONFIG_IDF_TARGET_ESP32P4
687+
sdmmc_get_slot(0, &slot_config);
688+
#endif
689+
589690
printf("use %d %d %d %d\n", cfg.d0, cfg.d1, cfg.d2, cfg.d3);
590691
return esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card);
591692
#else
592693
return -1;
593694
#endif
594695
}
595696

596-
void unmount_sdcard()
697+
void *get_sdcard_handle(void)
698+
{
699+
return card;
700+
}
701+
702+
void unmount_sdcard(void)
597703
{
598704
if (card) {
599705
esp_vfs_fat_sdcard_unmount("/sdcard", card);

components/codec_board/idf_component.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
## IDF Component Manager Manifest File
2+
version: "0.5.4"
3+
description: Simple ESP32 series codec board realization
24
dependencies:
35
espressif/esp_codec_dev: "~1.3.4"
46
esp_lcd_ek79007:

0 commit comments

Comments
 (0)