3#include "AudioTools/CoreAudio/AudioI2S/I2SConfig.h"
4#if defined(RP2040_HOWER)
7#define IS_I2S_IMPLEMENTED
18class I2SDriverRP2040 {
19 friend class I2SStream;
23 I2SConfigStd defaultConfig(
RxTxMode mode) {
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);
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);
37 cfg.sample_rate = info.sample_rate;
38 cfg.bits_per_sample = info.bits_per_sample;
39 cfg.channels = info.channels;
44 bool begin(
RxTxMode mode = TX_MODE) {
46 return begin(defaultConfig(mode));
50 bool begin(I2SConfigStd cfg) {
55 has_input[0] = has_input[1] =
false;
59 if (!setupMode())
return false;
60 if (!setupSlaveMode())
return false;
61 if (!setupClockPins())
return false;
62 if (!setupDataPins())
return false;
63 if (!setupMCK())
return false;
64 if (!setupAudioParameters())
return false;
66 if (!i2s.begin(cfg.sample_rate)) {
67 LOGE(
"Could not start I2S");
82 I2SConfigStd config() {
return cfg; }
85 size_t writeBytes(
const void *src,
size_t size_bytes) {
86 LOGD(
"writeBytes(%d)", size_bytes);
89 if (cfg.channels == 1) {
90 result = writeExpandChannel(src, size_bytes);
91 }
else if (cfg.channels == 2) {
92 const uint8_t *p = (
const uint8_t *)src;
93 while (size_bytes >=
sizeof(int32_t)) {
94 bool justWritten = i2s.write(
98 size_bytes -=
sizeof(int32_t);
100 result +=
sizeof(int32_t);
108 size_t readBytes(
void *dest,
size_t size_bytes) {
110 switch (cfg.channels) {
112 return read1Channel(dest, size_bytes);
114 return read2Channels(dest, size_bytes);
119 int availableForWrite() {
120 if (cfg.channels == 1) {
121 return cfg.buffer_size;
123 return i2s.availableForWrite();
127 int available() {
return min(i2s.available(), cfg.buffer_size); }
129 void flush() { i2s.flush(); }
131 bool getOverUnderflow() {
132 return i2s.getOverUnderflow() ;
139 bool is_active =
false;
143 switch (cfg.rx_tx_mode) {
151 i2s = I2S(INPUT_PULLUP);
154 LOGE(
"Unsupported mode");
161 bool setupSlaveMode() {
162#if (ARDUINO_PICO_MAJOR >= 5 && ARDUINO_PICO_MINOR >= 3)
163 if (!cfg.is_master) {
164 if (!i2s.setSlave()) {
165 LOGE(
"Could not set slave mode");
174 bool setupClockPins() {
175 if (cfg.pin_ws == cfg.pin_bck + 1) {
176 if (!i2s.setBCLK(cfg.pin_bck)) {
177 LOGE(
"Could not set bck pin: %d", cfg.pin_bck);
180 }
else if (cfg.pin_ws == cfg.pin_bck - 1) {
181 if (!i2s.swapClocks() ||
184 LOGE(
"Could not set bck pin: %d", cfg.pin_bck);
188 LOGE(
"pins bck: '%d' and ws: '%d' must be next to each other",
189 cfg.pin_bck, cfg.pin_ws);
196 bool setupDataPins() {
197 if (cfg.rx_tx_mode == RXTX_MODE) {
198 if (!i2s.setDOUT(cfg.pin_data)) {
199 LOGE(
"Could not set pin_data: %d with setDOUT()", cfg.pin_data);
202 if (!i2s.setDIN(cfg.pin_data_rx)) {
203 LOGE(
"Could not set pin_data_rx: %d with setDIN()", cfg.pin_data);
208 int pin = cfg.pin_data;
209 if (pin == -1) pin = cfg.pin_data_rx;
210 if (!i2s.setDATA(pin)) {
211 LOGE(
"Could not set pin_data: %d with pin_data()", pin);
220 if (cfg.pin_mck != -1) {
221 LOGI(
"Using MCK pin: %d with multiplier %d", cfg.pin_mck,
223 i2s.setMCLKmult(cfg.mck_multiplier);
224 if (!i2s.setMCLK(cfg.pin_mck)) {
225 LOGE(
"Could not set data pin: %d", cfg.pin_mck);
233 bool setupAudioParameters() {
234 if (cfg.bits_per_sample == 8 ||
235 !i2s.setBitsPerSample(cfg.bits_per_sample)) {
236 LOGE(
"Could not set bits per sample: %d", cfg.bits_per_sample);
240 if (!i2s.setBuffers(cfg.buffer_count, cfg.buffer_size)) {
241 LOGE(
"Could not set buffers: Count: '%d', size: '%d'", cfg.buffer_count,
247 if (cfg.i2s_format == I2S_STD_FORMAT ||
248 cfg.i2s_format == I2S_PHILIPS_FORMAT) {
250 }
else if (cfg.i2s_format == I2S_LEFT_JUSTIFIED_FORMAT ||
251 cfg.i2s_format == I2S_LSB_FORMAT) {
252 if (!i2s.setLSBJFormat()) {
253 LOGE(
"Could not set LSB Format")
257 LOGE(
"Unsupported I2S format");
261 if (cfg.signal_type != TDM && (cfg.channels < 1 || cfg.channels > 2)) {
262 LOGE(
"Unsupported channels: '%d'", cfg.channels);
266 if (cfg.signal_type == TDM) {
268 i2s.setTDMChannels(cfg.channels);
275 size_t writeExpandChannel(
const void *src,
size_t size_bytes) {
276 switch (cfg.bits_per_sample) {
287 int16_t *pt16 = (int16_t *)src;
288 for (
int j = 0; j < size_bytes /
sizeof(int16_t); j++) {
289 i2s.write16(pt16[j], pt16[j]);
293 int32_t *pt24 = (int32_t *)src;
294 for (
int j = 0; j < size_bytes /
sizeof(int32_t); j++) {
295 i2s.write24(pt24[j], pt24[j]);
299 int32_t *pt32 = (int32_t *)src;
300 for (
int j = 0; j < size_bytes /
sizeof(int32_t); j++) {
301 i2s.write32(pt32[j], pt32[j]);
309 size_t read2Channels(
void *dest,
size_t size_bytes) {
312 switch (cfg.bits_per_sample) {
325 int16_t *data = (int16_t *)dest;
326 for (
int j = 0; j < size_bytes /
sizeof(int16_t); j += 2) {
327 if (i2s.read16(data + j, data + j + 1)) {
336 int32_t *data = (int32_t *)dest;
337 for (
int j = 0; j < size_bytes /
sizeof(int32_t); j += 2) {
338 if (i2s.read24(data + j, data + j + 1)) {
347 int32_t *data = (int32_t *)dest;
348 for (
int j = 0; j < size_bytes /
sizeof(int32_t); j += 2) {
349 if (i2s.read32(data + j, data + j + 1)) {
362 size_t read1Channel(
void *dest,
size_t size_bytes) {
365 switch (cfg.bits_per_sample) {
381 int16_t *data = (int16_t *)dest;
382 for (
int j = 0; j < size_bytes /
sizeof(int16_t); j++) {
383 if (i2s.read16(tmp, tmp + 1)) {
384 data[j] = mix(tmp[0], tmp[1]);
394 int32_t *data = (int32_t *)dest;
395 for (
int j = 0; j < size_bytes /
sizeof(int32_t); j++) {
396 if (i2s.read24(tmp, tmp + 1)) {
397 data[j] = mix(tmp[0], tmp[1]);
407 int32_t *data = (int32_t *)dest;
408 for (
int j = 0; j < size_bytes /
sizeof(int32_t); j++) {
409 if (i2s.read32(tmp, tmp + 1)) {
410 data[j] = mix(tmp[0], tmp[1]);
423 T mix(T left, T right) {
424 if (left != 0) has_input[0] =
true;
425 if (right != 0) has_input[1] =
true;
428 if (has_input[0] && !has_input[1]) {
433 if (!has_input[0] && has_input[1]) {
437 return (left / 2) + (right / 2);
441using 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:30