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) {
56 switch (cfg.rx_tx_mode) {
62 has_input[0] = has_input[1] =
false;
65 LOGE(
"Unsupported mode: only TX_MODE is supported");
70 if (cfg.pin_ws == cfg.pin_bck + 1) {
71 if (!i2s.setBCLK(cfg.pin_bck)) {
72 LOGE(
"Could not set bck pin: %d", cfg.pin_bck);
75 }
else if (cfg.pin_ws == cfg.pin_bck - 1) {
76 if (!i2s.swapClocks() ||
79 LOGE(
"Could not set bck pin: %d", cfg.pin_bck);
83 LOGE(
"pins bck: '%d' and ws: '%d' must be next to each other",
84 cfg.pin_bck, cfg.pin_ws);
87 if (!i2s.setDATA(cfg.pin_data)) {
88 LOGE(
"Could not set data pin: %d", cfg.pin_data);
91 if (cfg.pin_mck != -1) {
92 i2s.setMCLKmult(cfg.mck_multiplier);
93 if (!i2s.setMCLK(cfg.pin_mck)) {
94 LOGE(
"Could not set data pin: %d", cfg.pin_mck);
99 if (cfg.bits_per_sample == 8 ||
100 !i2s.setBitsPerSample(cfg.bits_per_sample)) {
101 LOGE(
"Could not set bits per sample: %d", cfg.bits_per_sample);
105 if (!i2s.setBuffers(cfg.buffer_count, cfg.buffer_size)) {
106 LOGE(
"Could not set buffers: Count: '%d', size: '%d'", cfg.buffer_count,
112 if (cfg.i2s_format == I2S_STD_FORMAT ||
113 cfg.i2s_format == I2S_PHILIPS_FORMAT) {
115 }
else if (cfg.i2s_format == I2S_LEFT_JUSTIFIED_FORMAT ||
116 cfg.i2s_format == I2S_LSB_FORMAT) {
117 if (!i2s.setLSBJFormat()) {
118 LOGE(
"Could not set LSB Format")
122 LOGE(
"Unsupported I2S format");
126#if defined(RP2040_HOWER)
128 if (cfg.signal_type != TDM && (cfg.channels < 1 || cfg.channels > 2)) {
129 LOGE(
"Unsupported channels: '%d'", cfg.channels);
133 if (cfg.signal_type == TDM) {
135 i2s.setTDMChannels(cfg.channels);
140 if (cfg.channels < 1 || cfg.channels > 2) {
141 LOGE(
"Unsupported channels: '%d'", cfg.channels);
145 if (cfg.signal_type != Digital) {
146 LOGE(
"Unsupported signal_type: '%d'", cfg.signal_type);
151 if (!i2s.begin(cfg.sample_rate)) {
152 LOGE(
"Could not start I2S");
167 I2SConfigStd config() {
return cfg; }
170 size_t writeBytes(
const void *src,
size_t size_bytes) {
171 LOGD(
"writeBytes(%d)", size_bytes);
174 if (cfg.channels == 1) {
175 result = writeExpandChannel(src, size_bytes);
176 }
else if (cfg.channels == 2) {
177 const uint8_t *p = (
const uint8_t *)src;
178 while (size_bytes >=
sizeof(int32_t)) {
179 bool justWritten = i2s.write(
183 size_bytes -=
sizeof(int32_t);
184 p +=
sizeof(int32_t);
185 result +=
sizeof(int32_t);
193 size_t readBytes(
void *dest,
size_t size_bytes) {
195 switch (cfg.channels) {
197 return read1Channel(dest, size_bytes);
199 return read2Channels(dest, size_bytes);
204 int availableForWrite() {
205 if (cfg.channels == 1) {
206 return cfg.buffer_size;
208 return i2s.availableForWrite();
212 int available() {
return min(i2s.available(), cfg.buffer_size); }
214 void flush() { i2s.flush(); }
216 bool getOverUnderflow() {
217 return i2s.getOverUnderflow() ;
224 bool is_active =
false;
228 size_t writeExpandChannel(
const void *src,
size_t size_bytes) {
229 switch (cfg.bits_per_sample) {
240 int16_t *pt16 = (int16_t *)src;
241 for (
int j = 0; j < size_bytes /
sizeof(int16_t); j++) {
242 i2s.write16(pt16[j], pt16[j]);
246 int32_t *pt24 = (int32_t *)src;
247 for (
int j = 0; j < size_bytes /
sizeof(int32_t); j++) {
248 i2s.write24(pt24[j], pt24[j]);
252 int32_t *pt32 = (int32_t *)src;
253 for (
int j = 0; j < size_bytes /
sizeof(int32_t); j++) {
254 i2s.write32(pt32[j], pt32[j]);
262 size_t read2Channels(
void *dest,
size_t size_bytes) {
265 switch (cfg.bits_per_sample) {
278 int16_t *data = (int16_t *)dest;
279 for (
int j = 0; j < size_bytes /
sizeof(int16_t); j += 2) {
280 if (i2s.read16(data + j, data + j + 1)) {
289 int32_t *data = (int32_t *)dest;
290 for (
int j = 0; j < size_bytes /
sizeof(int32_t); j += 2) {
291 if (i2s.read24(data + j, data + j + 1)) {
300 int32_t *data = (int32_t *)dest;
301 for (
int j = 0; j < size_bytes /
sizeof(int32_t); j += 2) {
302 if (i2s.read32(data + j, data + j + 1)) {
315 size_t read1Channel(
void *dest,
size_t size_bytes) {
318 switch (cfg.bits_per_sample) {
334 int16_t *data = (int16_t *)dest;
335 for (
int j = 0; j < size_bytes /
sizeof(int16_t); j++) {
336 if (i2s.read16(tmp, tmp + 1)) {
337 data[j] = mix(tmp[0], tmp[1]);
347 int32_t *data = (int32_t *)dest;
348 for (
int j = 0; j < size_bytes /
sizeof(int32_t); j++) {
349 if (i2s.read24(tmp, tmp + 1)) {
350 data[j] = mix(tmp[0], tmp[1]);
360 int32_t *data = (int32_t *)dest;
361 for (
int j = 0; j < size_bytes /
sizeof(int32_t); j++) {
362 if (i2s.read32(tmp, tmp + 1)) {
363 data[j] = mix(tmp[0], tmp[1]);
376 T mix(T left, T right) {
377 if (left != 0) has_input[0] =
true;
378 if (right != 0) has_input[1] =
true;
381 if (has_input[0] && !has_input[1]) {
386 if (!has_input[0] && has_input[1]) {
390 return (left / 2) + (right / 2);
394using 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