Skip to content

Commit c01972e

Browse files
committed
STM32 I2S
1 parent 77eaf01 commit c01972e

File tree

3 files changed

+35
-37
lines changed

3 files changed

+35
-37
lines changed

src/AudioI2S/I2SConfigStd.h

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,7 @@ namespace audio_tools {
1717
class I2SConfigStd : public AudioInfo {
1818
public:
1919

20-
I2SConfigStd() {
21-
channels = DEFAULT_CHANNELS;
22-
sample_rate = DEFAULT_SAMPLE_RATE;
23-
bits_per_sample = DEFAULT_BITS_PER_SAMPLE;
24-
}
25-
20+
I2SConfigStd() = default;
2621
/// Default Copy Constructor
2722
I2SConfigStd(const I2SConfigStd &cfg) = default;
2823

@@ -32,6 +27,8 @@ class I2SConfigStd : public AudioInfo {
3227
sample_rate = DEFAULT_SAMPLE_RATE;
3328
bits_per_sample = DEFAULT_BITS_PER_SAMPLE;
3429
rx_tx_mode = mode;
30+
31+
#ifndef STM32
3532
switch(mode){
3633
case RX_MODE:
3734
pin_data = PIN_I2S_DATA_IN;
@@ -44,21 +41,23 @@ class I2SConfigStd : public AudioInfo {
4441
pin_data_rx = PIN_I2S_DATA_IN;
4542
break;
4643
}
44+
#endif
4745
}
4846

4947
/// public settings
5048
RxTxMode rx_tx_mode = TX_MODE;
5149
bool is_master = true;
52-
//int port_no = 0; // processor dependent port
50+
I2SFormat i2s_format = I2S_STD_FORMAT;
51+
int buffer_count = I2S_BUFFER_COUNT;
52+
int buffer_size = I2S_BUFFER_SIZE;
53+
54+
#ifndef STM32
5355
int pin_ws = PIN_I2S_WS;
5456
int pin_bck = PIN_I2S_BCK;
5557
int pin_data; // rx or tx pin dependent on mode: tx pin for RXTX_MODE
5658
int pin_data_rx; // rx pin for RXTX_MODE
5759
int pin_mck = PIN_I2S_MCK;
58-
I2SFormat i2s_format = I2S_STD_FORMAT;
59-
60-
int buffer_count = I2S_BUFFER_COUNT;
61-
int buffer_size = I2S_BUFFER_SIZE;
60+
#endif
6261

6362
#if defined(RP2040_HOWER)
6463
/// materclock multiplier for RP2040: must be multiple of 64
@@ -81,6 +80,8 @@ class I2SConfigStd : public AudioInfo {
8180
LOGI("i2s_format: %s", i2s_formats[i2s_format]);
8281
LOGI("buffer_count:%d",buffer_count);
8382
LOGI("buffer_size:%d",buffer_size);
83+
84+
#ifndef STM32
8485
if (pin_mck!=-1)
8586
LOGI("pin_mck: %d", pin_mck);
8687
if (pin_bck!=-1)
@@ -92,6 +93,7 @@ class I2SConfigStd : public AudioInfo {
9293
if (pin_data_rx!=-1 && rx_tx_mode==RXTX_MODE){
9394
LOGI("pin_data_rx: %d", pin_data_rx);
9495
}
96+
#endif
9597
}
9698

9799
};

src/AudioI2S/I2SSTM32.h

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ class I2SDriverSTM32 {
6868
/// we assume the data is already available in the buffer
6969
int available() {
7070
if (!active) return 0;
71-
if (use_dma) return p_rx_buffer == nullptr ? 0 : cfg.buffer_size;
71+
if (use_dma && p_rx_buffer == nullptr) return 0;
7272
return cfg.buffer_size;
7373
}
7474

7575
/// We limit the write size to the buffer size
7676
int availableForWrite() {
7777
if (!active) return 0;
78-
if (use_dma) return p_tx_buffer == nullptr ? 0 : cfg.buffer_size;
78+
if (use_dma && p_tx_buffer == nullptr) return 0;
7979
return cfg.buffer_size;
8080
}
8181

@@ -150,22 +150,15 @@ class I2SDriverSTM32 {
150150
} else {
151151
// get data from buffer
152152
if (self->stm32_write_active) {
153-
// if we risk an onderflow we force the execution of the loop
154-
if (self->p_tx_buffer->available() < byteCount) {
155-
loop();
156-
}
157153
read = self->p_tx_buffer->readArray(buffer, byteCount);
158-
} else {
159-
// force execution of loop to fill buffer;
160-
loop();
161-
}
154+
}
162155
}
163156

164157
// check for underflow
165-
count++;
166-
if (read != byteCount) {
167-
LOGW("Buffer undeflow at %lu: %d for %d", count, read, byteCount);
168-
}
158+
// count++;
159+
// if (read != byteCount) {
160+
// LOGW("Buffer undeflow at %lu: %d for %d", count, read, byteCount);
161+
// }
169162
}
170163

171164
/// Checks if timout has been activated and if so, if it is timed out
@@ -196,7 +189,7 @@ class I2SDriverSTM32 {
196189
bool result = true;
197190
BaseBuffer<uint8_t> *p_tx_buffer = nullptr;
198191
BaseBuffer<uint8_t> *p_rx_buffer = nullptr;
199-
bool stm32_write_active = false;
192+
volatile bool stm32_write_active = false;
200193
bool use_dma = true;
201194
Print *p_dma_out = nullptr;
202195
Stream *p_dma_in = nullptr;
@@ -211,7 +204,8 @@ class I2SDriverSTM32 {
211204
result += actual_written;
212205
open -= actual_written;
213206
if (open > 0) {
214-
delay(10);
207+
stm32_write_active = true;
208+
delay(1);
215209
}
216210
}
217211

@@ -220,6 +214,7 @@ class I2SDriverSTM32 {
220214
stm32_write_active = true;
221215
LOGI("Buffer is full->starting i2s output");
222216
}
217+
223218
return size_bytes;
224219
}
225220

@@ -259,31 +254,28 @@ class I2SDriverSTM32 {
259254
switch (cfg.rx_tx_mode) {
260255
case RX_MODE:
261256
if (use_dma && p_rx_buffer == nullptr)
262-
p_rx_buffer = new NBuffer<uint8_t>(cfg.buffer_size, cfg.buffer_count);
257+
p_rx_buffer = allocateBuffer();
263258
result = i2s.beginReadDMA(i2s_stm32, writeFromReceive);
264259
break;
265260
case TX_MODE:
266261
stm32_write_active = false;
267262
if (use_dma && p_tx_buffer == nullptr)
268-
p_tx_buffer = new NBuffer<uint8_t>(cfg.buffer_size, cfg.buffer_count);
263+
p_tx_buffer = allocateBuffer();
269264
result = i2s.beginWriteDMA(i2s_stm32, readToTransmit);
270265
break;
271266

272-
#ifdef IS_READWRITE
273267
case RXTX_MODE:
274268
if (use_dma) {
275269
stm32_write_active = false;
276270
if (p_rx_buffer == nullptr)
277-
p_rx_buffer =
278-
new NBuffer<uint8_t>(cfg.buffer_size, cfg.buffer_count);
271+
p_rx_buffer = allocateBuffer();
279272
if (p_tx_buffer == nullptr)
280-
p_tx_buffer =
281-
new NBuffer<uint8_t>(cfg.buffer_size, cfg.buffer_count);
273+
p_tx_buffer = allocateBuffer();
282274
}
283275
result = i2s.beginReadWriteDMA(
284276
i2s_stm32, readToTransmit, writeFromReceive);
285277
break;
286-
#endif
278+
287279
default:
288280
LOGE("Unsupported mode");
289281
return false;
@@ -319,11 +311,9 @@ class I2SDriverSTM32 {
319311
i2s_stm32.data_format = toDataFormat(cfg.bits_per_sample);
320312
i2s_stm32.mode = getMode(cfg);
321313
i2s_stm32.standard = getStandard(cfg);
322-
#ifdef I2S_FULLDUPLEXMODE_ENABLE
323314
i2s_stm32.fullduplexmode = cfg.rx_tx_mode == RXTX_MODE
324315
? I2S_FULLDUPLEXMODE_ENABLE
325316
: I2S_FULLDUPLEXMODE_DISABLE;
326-
#endif
327317
i2s_stm32.hardware_config.buffer_size = cfg.buffer_size;
328318
// provide ourself as parameter to callback
329319
i2s_stm32.ref = this;
@@ -412,6 +402,10 @@ class I2SDriverSTM32 {
412402
LOGD("writeBytesExt: %u", result)
413403
return result;
414404
}
405+
406+
BaseBuffer<uint8_t>* allocateBuffer() {
407+
return new RingBuffer<uint8_t>(cfg.buffer_size * cfg.buffer_count);
408+
}
415409
};
416410

417411
using I2SDriver = I2SDriverSTM32;

src/AudioTools/AudioSPDIF.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,11 @@ class SPDIFOutput : public AudioStream {
165165
I2SConfig i2s_cfg;
166166
i2s_cfg.sample_rate = sample_rate;
167167
i2s_cfg.channels = cfg.channels;
168+
#ifndef STM32
168169
i2s_cfg.pin_ws = -1;
169170
i2s_cfg.pin_bck = -1;
170171
i2s_cfg.pin_data = cfg.pin_data;
172+
#endif
171173
i2s_cfg.buffer_count = cfg.buffer_count;
172174
i2s_cfg.buffer_size = cfg.buffer_size;
173175
i2s_cfg.bits_per_sample = I2S_BITS_PER_SAMPLE;

0 commit comments

Comments
 (0)