Skip to content

Commit e23b919

Browse files
committed
Optimize RP2040 I2S
1 parent 212996f commit e23b919

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/AudioTools/CoreAudio/AudioI2S/I2SRP2040.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,21 @@ class I2SDriverRP2040 {
2424
I2SConfigStd c(mode);
2525
return c;
2626
}
27-
/// Potentially updates the sample rate (if supported)
28-
bool setAudioInfo(AudioInfo) { return false; }
27+
/// Potentially updates values dynamically
28+
bool setAudioInfo(AudioInfo info) {
29+
if (info.sample_rate != cfg.sample_rate && !i2s.setFrequency(info.sample_rate)) {
30+
LOGI("i2s.setFrequency %d failed", info.sample_rate);
31+
return false;
32+
}
33+
if (info.bits_per_sample != cfg.bits_per_sample && !i2s.setBitsPerSample(info.bits_per_sample)) {
34+
LOGI("i2s.setBitsPerSample %d failed", info.bits_per_sample);
35+
return false;
36+
}
37+
cfg.sample_rate = info.sample_rate;
38+
cfg.bits_per_sample = info.bits_per_sample;
39+
cfg.channels = info.channels;
40+
return true;
41+
}
2942

3043
/// starts the DAC with the default config in TX Mode
3144
bool begin(RxTxMode mode = TX_MODE) {
@@ -36,6 +49,8 @@ class I2SDriverRP2040 {
3649
/// starts the DAC
3750
bool begin(I2SConfigStd cfg) {
3851
TRACEI();
52+
// prevent multiple begins w/o calling end
53+
if (is_active) end();
3954
this->cfg = cfg;
4055
cfg.logInfo();
4156
switch (cfg.rx_tx_mode) {
@@ -137,13 +152,15 @@ class I2SDriverRP2040 {
137152
LOGE("Could not start I2S");
138153
return false;
139154
}
155+
is_active = true;
140156
return true;
141157
}
142158

143159
/// stops the I2C and uninstalls the driver
144160
void end() {
145161
flush();
146162
i2s.end();
163+
is_active = false;
147164
}
148165

149166
/// provides the actual configuration
@@ -204,6 +221,7 @@ class I2SDriverRP2040 {
204221
I2SConfigStd cfg;
205222
I2S i2s;
206223
bool has_input[2];
224+
bool is_active = false;
207225

208226
/// writes 1 channel to I2S while expanding it to 2 channels
209227
// returns amount of bytes written from src to i2s

0 commit comments

Comments
 (0)