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");
71 if (!i2s.setSlave()) {
72 LOGE(
"Could not set slave mode");
77 if (cfg.pin_ws == cfg.pin_bck + 1) {
78 if (!i2s.setBCLK(cfg.pin_bck)) {
79 LOGE(
"Could not set bck pin: %d", cfg.pin_bck);
82 }
else if (cfg.pin_ws == cfg.pin_bck - 1) {
83 if (!i2s.swapClocks() ||
86 LOGE(
"Could not set bck pin: %d", cfg.pin_bck);
90 LOGE(
"pins bck: '%d' and ws: '%d' must be next to each other",
91 cfg.pin_bck, cfg.pin_ws);
94 if (!i2s.setDATA(cfg.pin_data)) {
95 LOGE(
"Could not set data pin: %d", cfg.pin_data);
98 if (cfg.pin_mck != -1) {
99 LOGI(
"Using MCK pin: %d with multiplier %d", cfg.pin_mck,
101 i2s.setMCLKmult(cfg.mck_multiplier);
102 if (!i2s.setMCLK(cfg.pin_mck)) {
103 LOGE(
"Could not set data pin: %d", cfg.pin_mck);
108 if (cfg.bits_per_sample == 8 ||
109 !i2s.setBitsPerSample(cfg.bits_per_sample)) {
110 LOGE(
"Could not set bits per sample: %d", cfg.bits_per_sample);
114 if (!i2s.setBuffers(cfg.buffer_count, cfg.buffer_size)) {
115 LOGE(
"Could not set buffers: Count: '%d', size: '%d'", cfg.buffer_count,
121 if (cfg.i2s_format == I2S_STD_FORMAT ||
122 cfg.i2s_format == I2S_PHILIPS_FORMAT) {
124 }
else if (cfg.i2s_format == I2S_LEFT_JUSTIFIED_FORMAT ||
125 cfg.i2s_format == I2S_LSB_FORMAT) {
126 if (!i2s.setLSBJFormat()) {
127 LOGE(
"Could not set LSB Format")
131 LOGE(
"Unsupported I2S format");
135 if (cfg.signal_type != TDM && (cfg.channels < 1 || cfg.channels > 2)) {
136 LOGE(
"Unsupported channels: '%d'", cfg.channels);
140 if (cfg.signal_type == TDM) {
142 i2s.setTDMChannels(cfg.channels);
145 if (!i2s.begin(cfg.sample_rate)) {
146 LOGE(
"Could not start I2S");
161 I2SConfigStd config() {
return cfg; }
164 size_t writeBytes(
const void *src,
size_t size_bytes) {
165 LOGD(
"writeBytes(%d)", size_bytes);
168 if (cfg.channels == 1) {
169 result = writeExpandChannel(src, size_bytes);
170 }
else if (cfg.channels == 2) {
171 const uint8_t *p = (
const uint8_t *)src;
172 while (size_bytes >=
sizeof(int32_t)) {
173 bool justWritten = i2s.write(
177 size_bytes -=
sizeof(int32_t);
178 p +=
sizeof(int32_t);
179 result +=
sizeof(int32_t);
187 size_t readBytes(
void *dest,
size_t size_bytes) {
189 switch (cfg.channels) {
191 return read1Channel(dest, size_bytes);
193 return read2Channels(dest, size_bytes);
198 int availableForWrite() {
199 if (cfg.channels == 1) {
200 return cfg.buffer_size;
202 return i2s.availableForWrite();
206 int available() {
return min(i2s.available(), cfg.buffer_size); }
208 void flush() { i2s.flush(); }
210 bool getOverUnderflow() {
211 return i2s.getOverUnderflow() ;
218 bool is_active =
false;
222 size_t writeExpandChannel(
const void *src,
size_t size_bytes) {
223 switch (cfg.bits_per_sample) {
234 int16_t *pt16 = (int16_t *)src;
235 for (
int j = 0; j < size_bytes /
sizeof(int16_t); j++) {
236 i2s.write16(pt16[j], pt16[j]);
240 int32_t *pt24 = (int32_t *)src;
241 for (
int j = 0; j < size_bytes /
sizeof(int32_t); j++) {
242 i2s.write24(pt24[j], pt24[j]);
246 int32_t *pt32 = (int32_t *)src;
247 for (
int j = 0; j < size_bytes /
sizeof(int32_t); j++) {
248 i2s.write32(pt32[j], pt32[j]);
256 size_t read2Channels(
void *dest,
size_t size_bytes) {
259 switch (cfg.bits_per_sample) {
272 int16_t *data = (int16_t *)dest;
273 for (
int j = 0; j < size_bytes /
sizeof(int16_t); j += 2) {
274 if (i2s.read16(data + j, data + j + 1)) {
283 int32_t *data = (int32_t *)dest;
284 for (
int j = 0; j < size_bytes /
sizeof(int32_t); j += 2) {
285 if (i2s.read24(data + j, data + j + 1)) {
294 int32_t *data = (int32_t *)dest;
295 for (
int j = 0; j < size_bytes /
sizeof(int32_t); j += 2) {
296 if (i2s.read32(data + j, data + j + 1)) {
309 size_t read1Channel(
void *dest,
size_t size_bytes) {
312 switch (cfg.bits_per_sample) {
328 int16_t *data = (int16_t *)dest;
329 for (
int j = 0; j < size_bytes /
sizeof(int16_t); j++) {
330 if (i2s.read16(tmp, tmp + 1)) {
331 data[j] = mix(tmp[0], tmp[1]);
341 int32_t *data = (int32_t *)dest;
342 for (
int j = 0; j < size_bytes /
sizeof(int32_t); j++) {
343 if (i2s.read24(tmp, tmp + 1)) {
344 data[j] = mix(tmp[0], tmp[1]);
354 int32_t *data = (int32_t *)dest;
355 for (
int j = 0; j < size_bytes /
sizeof(int32_t); j++) {
356 if (i2s.read32(tmp, tmp + 1)) {
357 data[j] = mix(tmp[0], tmp[1]);
370 T mix(T left, T right) {
371 if (left != 0) has_input[0] =
true;
372 if (right != 0) has_input[1] =
true;
375 if (has_input[0] && !has_input[1]) {
380 if (!has_input[0] && has_input[1]) {
384 return (left / 2) + (right / 2);
388using 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