Skip to content

Commit f77945d

Browse files
committed
rg_audio: Do not build internal DAC support when speaker is disabled (for s2/s3 chips)
There's no need to (try to) build support for the built-in DAC when RG_AUDIO_USE_SPEAKER is set to 0 in the target definition.
1 parent c4578e5 commit f77945d

File tree

2 files changed

+44
-31
lines changed

2 files changed

+44
-31
lines changed

components/retro-go/rg_audio.c

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
#include <freertos/FreeRTOS.h>
22
#include <freertos/semphr.h>
3+
#include <driver/gpio.h>
34
#include <driver/i2s.h>
4-
#include <driver/dac.h>
55
#include <string.h>
66
#include <unistd.h>
77

88
#include "rg_system.h"
99
#include "rg_audio.h"
1010

11+
#if RG_AUDIO_USE_SPEAKER
12+
#include <driver/dac.h>
13+
#endif
14+
1115
static int audioSink = -1;
1216
static int audioSampleRate = 0;
1317
static int audioFilter = 0;
@@ -19,10 +23,10 @@ static int64_t dummyBusyUntil = 0;
1923
static const rg_sink_t sinks[] = {
2024
{RG_AUDIO_SINK_DUMMY, 0, "Dummy"},
2125
#if RG_AUDIO_USE_SPEAKER
22-
{RG_AUDIO_SINK_SPEAKER, 0, "Speaker"},
26+
{RG_AUDIO_SINK_I2S_DAC, 0, "Speaker"},
2327
#endif
2428
#if RG_AUDIO_USE_EXT_DAC
25-
{RG_AUDIO_SINK_EXT_DAC, 0, "Ext DAC"},
29+
{RG_AUDIO_SINK_I2S_EXT, 0, "Ext DAC"},
2630
#endif
2731
// {RG_AUDIO_SINK_BT_A2DP, 0, "Bluetooth"},
2832
};
@@ -50,19 +54,6 @@ void rg_audio_init(int sampleRate)
5054
{
5155
RG_ASSERT(audioSink == -1, "Audio sink already initialized!");
5256

53-
i2s_config_t i2s_config = {
54-
.mode = I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN,
55-
.sample_rate = sampleRate,
56-
.bits_per_sample = 16,
57-
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
58-
.communication_format = I2S_COMM_FORMAT_STAND_MSB,
59-
.dma_buf_count = 3, // Goal is to have ~800 samples over 2-8 buffers (3x270 or 5x180 are pretty good)
60-
.dma_buf_len = 270, // The unit is stereo samples (4 bytes) (optimize for 533 usage)
61-
.intr_alloc_flags = 0, // ESP_INTR_FLAG_LEVEL1
62-
.use_apll = 0
63-
};
64-
esp_err_t ret = ESP_FAIL;
65-
6657
if (audioDevLock == NULL)
6758
audioDevLock = xSemaphoreCreateMutex();
6859

@@ -74,18 +65,35 @@ void rg_audio_init(int sampleRate)
7465
audioVolume = (int)rg_settings_get_number(NS_GLOBAL, SETTING_VOLUME, audioVolume);
7566
// audioMuted = false;
7667

77-
if (audioSink == RG_AUDIO_SINK_SPEAKER)
68+
i2s_config_t i2s_config = {
69+
.mode = I2S_MODE_MASTER | I2S_MODE_TX,
70+
.sample_rate = sampleRate,
71+
.bits_per_sample = 16,
72+
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
73+
.communication_format = I2S_COMM_FORMAT_STAND_I2S,
74+
.intr_alloc_flags = 0, // ESP_INTR_FLAG_LEVEL1
75+
.dma_buf_count = 3, // Goal is to have ~800 samples over 2-8 buffers (3x270 or 5x180 are pretty good)
76+
.dma_buf_len = 270, // The unit is stereo samples (4 bytes) (optimize for 533 usage)
77+
.use_apll = true, // External DAC may care about accuracy
78+
};
79+
esp_err_t ret = ESP_FAIL;
80+
81+
if (audioSink == RG_AUDIO_SINK_I2S_DAC)
7882
{
83+
#if RG_AUDIO_USE_SPEAKER
84+
i2s_config.mode = I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN;
85+
i2s_config.communication_format = I2S_COMM_FORMAT_STAND_MSB;
86+
i2s_config.use_apll = false;
7987
if ((ret = i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL)) == ESP_OK)
8088
{
8189
ret = i2s_set_dac_mode(SPEAKER_DAC_MODE);
8290
}
91+
#else
92+
RG_LOGE("This device does not support internal DAC mode!\n");
93+
#endif
8394
}
84-
else if (audioSink == RG_AUDIO_SINK_EXT_DAC)
95+
else if (audioSink == RG_AUDIO_SINK_I2S_EXT)
8596
{
86-
i2s_config.mode = I2S_MODE_MASTER | I2S_MODE_TX;
87-
i2s_config.communication_format = I2S_COMM_FORMAT_STAND_I2S;
88-
i2s_config.use_apll = 1;
8997
if ((ret = i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL)) == ESP_OK)
9098
{
9199
ret = i2s_set_pin(I2S_NUM_0, &(i2s_pin_config_t) {
@@ -111,6 +119,7 @@ void rg_audio_init(int sampleRate)
111119
{
112120
RG_LOGI("Audio ready. sink='%s', samplerate=%d, volume=%d\n",
113121
rg_audio_get_sink()->name, audioSampleRate, audioVolume);
122+
// RG_LOGI("Real I2S samplerate: %f\n", i2s_get_clk(I2S_NUM_0));
114123
}
115124
else
116125
{
@@ -127,16 +136,20 @@ void rg_audio_deinit(void)
127136
// We'll go ahead even if we can't acquire the lock...
128137
ACQUIRE_DEVICE(1000);
129138

130-
if (audioSink == RG_AUDIO_SINK_SPEAKER)
139+
if (audioSink == RG_AUDIO_SINK_I2S_DAC)
131140
{
141+
#if RG_AUDIO_USE_SPEAKER
132142
i2s_driver_uninstall(I2S_NUM_0);
133143
if (SPEAKER_DAC_MODE & I2S_DAC_CHANNEL_RIGHT_EN)
134144
dac_output_disable(DAC_CHANNEL_1);
135145
if (SPEAKER_DAC_MODE & I2S_DAC_CHANNEL_LEFT_EN)
136146
dac_output_disable(DAC_CHANNEL_2);
137147
dac_i2s_disable();
148+
#else
149+
RG_LOGE("This device does not support internal DAC mode!\n");
150+
#endif
138151
}
139-
else if (audioSink == RG_AUDIO_SINK_EXT_DAC)
152+
else if (audioSink == RG_AUDIO_SINK_I2S_EXT)
140153
{
141154
i2s_driver_uninstall(I2S_NUM_0);
142155
gpio_reset_pin(RG_GPIO_SND_I2S_BCK);
@@ -190,7 +203,7 @@ void rg_audio_submit(int16_t *stereoAudioBuffer, size_t frameCount)
190203
dummyBusyUntil = rg_system_timer() + ((audioSampleRate * 1000) / sampleCount);
191204
written = bufferSize;
192205
}
193-
else if (audioSink == RG_AUDIO_SINK_SPEAKER)
206+
else if (audioSink == RG_AUDIO_SINK_I2S_DAC)
194207
{
195208
// In speaker mode we use dac left and right as a single channel
196209
// to increase resolution.
@@ -227,7 +240,7 @@ void rg_audio_submit(int16_t *stereoAudioBuffer, size_t frameCount)
227240
}
228241
i2s_write(I2S_NUM_0, stereoAudioBuffer, bufferSize, &written, 1000);
229242
}
230-
else if (audioSink == RG_AUDIO_SINK_EXT_DAC)
243+
else if (audioSink == RG_AUDIO_SINK_I2S_EXT)
231244
{
232245
for (size_t i = 0; i < sampleCount; ++i)
233246
{
@@ -299,8 +312,8 @@ void rg_audio_set_mute(bool mute)
299312
return;
300313

301314
if (RG_GPIO_SND_AMP_ENABLE != GPIO_NUM_NC)
302-
gpio_set_level(RG_GPIO_SND_AMP_ENABLE, mute ? 0 : 1);
303-
if (audioSink == RG_AUDIO_SINK_SPEAKER || audioSink == RG_AUDIO_SINK_EXT_DAC)
315+
gpio_set_level(RG_GPIO_SND_AMP_ENABLE, !mute);
316+
if (audioSink == RG_AUDIO_SINK_I2S_DAC || audioSink == RG_AUDIO_SINK_I2S_EXT)
304317
i2s_zero_dma_buffer(I2S_NUM_0);
305318

306319
audioMuted = mute;
@@ -320,12 +333,12 @@ void rg_audio_set_sample_rate(int sampleRate)
320333
if (!ACQUIRE_DEVICE(1000))
321334
return;
322335

323-
if (audioSink == RG_AUDIO_SINK_SPEAKER || audioSink == RG_AUDIO_SINK_EXT_DAC)
336+
if (audioSink == RG_AUDIO_SINK_I2S_DAC || audioSink == RG_AUDIO_SINK_I2S_EXT)
324337
{
325338
RG_LOGI("i2s_set_sample_rates(%d)\n", sampleRate);
326339
i2s_set_sample_rates(I2S_NUM_0, sampleRate);
327340
}
328-
audioSampleRate = sampleRate;
329341

342+
audioSampleRate = sampleRate;
330343
RELEASE_DEVICE();
331344
}

components/retro-go/rg_audio.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
typedef enum
77
{
8-
RG_AUDIO_SINK_SPEAKER = 0,
9-
RG_AUDIO_SINK_EXT_DAC,
8+
RG_AUDIO_SINK_I2S_DAC = 0,
9+
RG_AUDIO_SINK_I2S_EXT,
1010
RG_AUDIO_SINK_BT_A2DP,
1111
RG_AUDIO_SINK_DUMMY,
1212
} rg_sink_type_t;

0 commit comments

Comments
 (0)