Skip to content

Commit 116f7bf

Browse files
committed
chip: update i2s driver
1 parent ab93e45 commit 116f7bf

File tree

6 files changed

+84
-7
lines changed

6 files changed

+84
-7
lines changed

main/Kconfig.projbuild

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,24 @@ config ENABLE_AUDIO_PROMPT
190190
help
191191
Select this to enable audio prompt.
192192

193+
choice AUDIO_OUTPUT
194+
prompt "Audio Output"
195+
default AUDIO_OUTPUT_I2S1
196+
depends on ENABLE_AUDIO_PROMPT
197+
help
198+
Select Audio Output Interface.
199+
200+
config AUDIO_OUTPUT_I2S0
201+
bool "I2S-0"
202+
config AUDIO_OUTPUT_I2S1
203+
bool "I2S-1"
204+
endchoice
205+
193206
config AUDIO_OUTPUT_I2S_NUM
194207
int
195-
default 0
208+
default 0 if AUDIO_OUTPUT_I2S0
209+
default 1 if AUDIO_OUTPUT_I2S1
210+
depends on ENABLE_AUDIO_PROMPT
196211

197212
config I2S_BCLK_PIN
198213
int "I2S BCLK Pin"

main/inc/chip/i2s.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
#ifndef INC_CHIP_I2S_H_
99
#define INC_CHIP_I2S_H_
1010

11-
extern void i2s0_init(void);
11+
extern void i2s_output_init(void);
12+
extern void i2s_output_deinit(void);
1213

1314
extern void i2s_output_set_sample_rate(int rate);
1415

main/src/app_main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static void chip_init(void)
4040
uart1_init();
4141

4242
#ifdef CONFIG_ENABLE_AUDIO_PROMPT
43-
i2s0_init();
43+
i2s_output_init();
4444
#endif
4545

4646
#ifdef CONFIG_ENABLE_GUI
@@ -83,6 +83,7 @@ int app_main(void)
8383
core_init();
8484

8585
chip_init();
86+
8687
board_init();
8788

8889
user_init();

main/src/chip/i2s.c

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "driver/i2s.h"
1212

1313
#define I2S0_TAG "i2s-0"
14+
#define I2S1_TAG "i2s-1"
1415

1516
#ifdef CONFIG_ENABLE_AUDIO_PROMPT
1617
static i2s_config_t i2s_output_config = {
@@ -25,8 +26,10 @@ static i2s_config_t i2s_output_config = {
2526
.dma_buf_len = 128,
2627
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, // 2-channels
2728
};
29+
#endif
2830

29-
void i2s0_init(void)
31+
#if (CONFIG_AUDIO_OUTPUT_I2S_NUM == 0)
32+
static void i2s0_init(void)
3033
{
3134
ESP_ERROR_CHECK(i2s_driver_install(I2S_NUM_0, &i2s_output_config, 0, NULL));
3235

@@ -46,12 +49,68 @@ void i2s0_init(void)
4649
);
4750
}
4851

52+
static void i2s0_deinit(void)
53+
{
54+
ESP_ERROR_CHECK(i2s_driver_uninstall(I2S_NUM_0));
55+
56+
ESP_LOGI(I2S0_TAG, "deinitialized.");
57+
}
58+
#endif
59+
60+
#if (CONFIG_AUDIO_OUTPUT_I2S_NUM == 1)
61+
static void i2s1_init(void)
62+
{
63+
ESP_ERROR_CHECK(i2s_driver_install(I2S_NUM_1, &i2s_output_config, 0, NULL));
64+
65+
i2s_pin_config_t pin_config = {
66+
.bck_io_num = CONFIG_I2S_BCLK_PIN,
67+
.ws_io_num = CONFIG_I2S_LRCK_PIN,
68+
.data_out_num = CONFIG_I2S_DOUT_PIN,
69+
.data_in_num = -1, // Not used
70+
};
71+
ESP_ERROR_CHECK(i2s_set_pin(I2S_NUM_1, &pin_config));
72+
73+
ESP_LOGI(I2S1_TAG, "initialized, bck: %d, ws: %d, dout: %d, din: %d",
74+
pin_config.bck_io_num,
75+
pin_config.ws_io_num,
76+
pin_config.data_out_num,
77+
pin_config.data_in_num
78+
);
79+
}
80+
81+
static void i2s1_deinit(void)
82+
{
83+
ESP_ERROR_CHECK(i2s_driver_uninstall(I2S_NUM_1));
84+
85+
ESP_LOGI(I2S1_TAG, "deinitialized.");
86+
}
87+
#endif
88+
89+
#ifdef CONFIG_ENABLE_AUDIO_PROMPT
90+
void i2s_output_init(void)
91+
{
92+
#if (CONFIG_AUDIO_OUTPUT_I2S_NUM == 0)
93+
i2s0_init();
94+
#else
95+
i2s1_init();
96+
#endif
97+
}
98+
99+
void i2s_output_deinit(void)
100+
{
101+
#if (CONFIG_AUDIO_OUTPUT_I2S_NUM == 0)
102+
i2s0_deinit();
103+
#else
104+
i2s1_deinit();
105+
#endif
106+
}
107+
49108
void i2s_output_set_sample_rate(int rate)
50109
{
51110
if (rate != i2s_output_config.sample_rate) {
52111
i2s_output_config.sample_rate = rate;
53-
i2s_zero_dma_buffer(0);
54-
i2s_set_sample_rates(0, i2s_output_config.sample_rate);
112+
i2s_zero_dma_buffer(CONFIG_AUDIO_OUTPUT_I2S_NUM);
113+
i2s_set_sample_rates(CONFIG_AUDIO_OUTPUT_I2S_NUM, i2s_output_config.sample_rate);
55114
}
56115
}
57116
#endif

main/src/user/audio_player.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ static const char *mp3_file_ptr[][2] = {
3131
{snd6_mp3_ptr, snd6_mp3_end}, // "系统故障"
3232
{snd7_mp3_ptr, snd7_mp3_end}, // "开始配网"
3333
};
34+
3435
static uint8_t mp3_file_index = 0;
3536
static uint8_t playback_pending = 0;
3637

main/src/user/audio_render.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void render_sample_block(short *sample_buff_ch0, short *sample_buff_ch1, int num
2828
for (int i = 0; i < num_samples; i++) {
2929
/* low - high / low - high */
3030
const char samp32[4] = {ptr_l[0], ptr_l[1], ptr_r[0], ptr_r[1]}; // ESP32 CPU is Little Endian
31-
i2s_write(0, (const char *)&samp32, sizeof(samp32), &bytes_written, portMAX_DELAY);
31+
i2s_write(CONFIG_AUDIO_OUTPUT_I2S_NUM, (const char *)&samp32, sizeof(samp32), &bytes_written, portMAX_DELAY);
3232

3333
// DMA buffer full - retry
3434
if (bytes_written == 0) {

0 commit comments

Comments
 (0)