3 #include "AudioTools/CoreAudio/AudioI2S/I2SConfig.h"
4 #if defined(RP2040_HOWER)
7 #define IS_I2S_IMPLEMENTED
18 class I2SDriverRP2040 {
19 friend class I2SStream;
23 I2SConfigStd defaultConfig(
RxTxMode mode) {
28 bool setAudioInfo(AudioInfo) {
return false; }
31 bool begin(
RxTxMode mode = TX_MODE) {
33 return begin(defaultConfig(mode));
37 bool begin(I2SConfigStd cfg) {
41 switch (cfg.rx_tx_mode) {
47 has_input[0] = has_input[1] =
false;
50 LOGE(
"Unsupported mode: only TX_MODE is supported");
55 if (cfg.pin_ws == cfg.pin_bck + 1) {
56 if (!i2s.setBCLK(cfg.pin_bck)) {
57 LOGE(
"Could not set bck pin: %d", cfg.pin_bck);
60 }
else if (cfg.pin_ws == cfg.pin_bck - 1) {
61 if (!i2s.swapClocks() ||
64 LOGE(
"Could not set bck pin: %d", cfg.pin_bck);
68 LOGE(
"pins bck: '%d' and ws: '%d' must be next to each other",
69 cfg.pin_bck, cfg.pin_ws);
72 if (!i2s.setDATA(cfg.pin_data)) {
73 LOGE(
"Could not set data pin: %d", cfg.pin_data);
76 if (cfg.pin_mck != -1) {
77 i2s.setMCLKmult(cfg.mck_multiplier);
78 if (!i2s.setMCLK(cfg.pin_mck)) {
79 LOGE(
"Could not set data pin: %d", cfg.pin_mck);
84 if (cfg.bits_per_sample == 8 ||
85 !i2s.setBitsPerSample(cfg.bits_per_sample)) {
86 LOGE(
"Could not set bits per sample: %d", cfg.bits_per_sample);
90 if (!i2s.setBuffers(cfg.buffer_count, cfg.buffer_size)) {
91 LOGE(
"Could not set buffers: Count: '%d', size: '%d'", cfg.buffer_count,
97 if (cfg.i2s_format == I2S_STD_FORMAT ||
98 cfg.i2s_format == I2S_PHILIPS_FORMAT) {
100 }
else if (cfg.i2s_format == I2S_LEFT_JUSTIFIED_FORMAT ||
101 cfg.i2s_format == I2S_LSB_FORMAT) {
102 if (!i2s.setLSBJFormat()) {
103 LOGE(
"Could not set LSB Format")
107 LOGE(
"Unsupported I2S format");
111 #if defined(RP2040_HOWER)
113 if (cfg.signal_type != TDM && (cfg.channels < 1 || cfg.channels > 2)) {
114 LOGE(
"Unsupported channels: '%d'", cfg.channels);
118 if (cfg.signal_type == TDM) {
120 i2s.setTDMChannels(cfg.channels);
125 if (cfg.channels < 1 || cfg.channels > 2) {
126 LOGE(
"Unsupported channels: '%d'", cfg.channels);
130 if (cfg.signal_type != Digital) {
131 LOGE(
"Unsupported signal_type: '%d'", cfg.signal_type);
136 if (!i2s.begin(cfg.sample_rate)) {
137 LOGE(
"Could not start I2S");
150 I2SConfigStd config() {
return cfg; }
153 size_t writeBytes(
const void *src,
size_t size_bytes) {
154 LOGD(
"writeBytes(%d)", size_bytes);
157 if (cfg.channels == 1) {
158 result = writeExpandChannel(src, size_bytes);
159 }
else if (cfg.channels == 2) {
160 const uint8_t *p = (
const uint8_t *)src;
161 while (size_bytes >=
sizeof(int32_t)) {
162 bool justWritten = i2s.write(
166 size_bytes -=
sizeof(int32_t);
167 p +=
sizeof(int32_t);
168 result +=
sizeof(int32_t);
176 size_t readBytes(
void *dest,
size_t size_bytes) {
178 switch (cfg.channels) {
180 return read1Channel(dest, size_bytes);
182 return read2Channels(dest, size_bytes);
187 int availableForWrite() {
188 if (cfg.channels == 1) {
189 return cfg.buffer_size;
191 return i2s.availableForWrite();
195 int available() {
return min(i2s.available(), cfg.buffer_size); }
197 void flush() { i2s.flush(); }
199 bool getOverUnderflow() {
200 return i2s.getOverUnderflow() ;
210 size_t writeExpandChannel(
const void *src,
size_t size_bytes) {
211 switch (cfg.bits_per_sample) {
222 int16_t *pt16 = (int16_t *)src;
223 for (
int j = 0; j < size_bytes /
sizeof(int16_t); j++) {
224 i2s.write16(pt16[j], pt16[j]);
228 int32_t *pt24 = (int32_t *)src;
229 for (
int j = 0; j < size_bytes /
sizeof(int32_t); j++) {
230 i2s.write24(pt24[j], pt24[j]);
234 int32_t *pt32 = (int32_t *)src;
235 for (
int j = 0; j < size_bytes /
sizeof(int32_t); j++) {
236 i2s.write32(pt32[j], pt32[j]);
244 size_t read2Channels(
void *dest,
size_t size_bytes) {
247 switch (cfg.bits_per_sample) {
260 int16_t *data = (int16_t *)dest;
261 for (
int j = 0; j < size_bytes /
sizeof(int16_t); j += 2) {
262 if (i2s.read16(data + j, data + j + 1)) {
271 int32_t *data = (int32_t *)dest;
272 for (
int j = 0; j < size_bytes /
sizeof(int32_t); j += 2) {
273 if (i2s.read24(data + j, data + j + 1)) {
282 int32_t *data = (int32_t *)dest;
283 for (
int j = 0; j < size_bytes /
sizeof(int32_t); j += 2) {
284 if (i2s.read32(data + j, data + j + 1)) {
297 size_t read1Channel(
void *dest,
size_t size_bytes) {
300 switch (cfg.bits_per_sample) {
316 int16_t *data = (int16_t *)dest;
317 for (
int j = 0; j < size_bytes /
sizeof(int16_t); j++) {
318 if (i2s.read16(tmp, tmp + 1)) {
319 data[j] = mix(tmp[0], tmp[1]);
329 int32_t *data = (int32_t *)dest;
330 for (
int j = 0; j < size_bytes /
sizeof(int32_t); j++) {
331 if (i2s.read24(tmp, tmp + 1)) {
332 data[j] = mix(tmp[0], tmp[1]);
342 int32_t *data = (int32_t *)dest;
343 for (
int j = 0; j < size_bytes /
sizeof(int32_t); j++) {
344 if (i2s.read32(tmp, tmp + 1)) {
345 data[j] = mix(tmp[0], tmp[1]);
358 T mix(T left, T right) {
359 if (left != 0) has_input[0] =
true;
360 if (right != 0) has_input[1] =
true;
363 if (has_input[0] && !has_input[1]) {
368 if (!has_input[0] && has_input[1]) {
372 return (left / 2) + (right / 2);
376 using I2SDriver = I2SDriverRP2040;
RxTxMode
The Microcontroller is the Audio Source (TX_MODE) or Audio Sink (RX_MODE). RXTX_MODE is Source and Si...
Definition: AudioTypes.h:28