3 #include "AudioTools/CoreAudio/AudioI2S/I2SConfig.h"
4 #if defined(RP2040_MBED)
5 #include "RP2040-I2S.h"
7 #define IS_I2S_IMPLEMENTED
21 class I2SDriverRP2040MBED {
22 friend class I2SStream;
26 I2SConfigStd defaultConfig(
RxTxMode mode) {
32 bool setAudioInfo(AudioInfo) {
return false; }
35 bool begin(
RxTxMode mode = TX_MODE) {
37 return begin(defaultConfig(mode));
41 bool begin(I2SConfigStd cfg) {
45 if (cfg.rx_tx_mode != TX_MODE) {
46 LOGE(
"Unsupported mode: only TX_MODE is supported");
49 if (!I2S.setBCLK(cfg.pin_bck)) {
50 LOGE(
"Could not set bck pin: %d", cfg.pin_bck);
53 if (!I2S.setDATA(cfg.pin_data)) {
54 LOGE(
"Could not set data pin: %d", cfg.pin_data);
57 if (cfg.bits_per_sample != 16) {
58 LOGE(
"Unsupported bits_per_sample: %d", (
int)cfg.bits_per_sample);
62 if (cfg.channels < 1 || cfg.channels > 2) {
63 LOGE(
"Unsupported channels: '%d' - only 2 is supported", (
int)cfg.channels);
67 int rate = cfg.sample_rate;
68 if (cfg.channels == 1) {
72 if (!I2S.begin(rate)) {
73 LOGE(
"Could not start I2S");
80 void end() { I2S.end(); }
83 I2SConfigStd config() {
return cfg; }
86 size_t writeBytes(
const void *src,
size_t size_bytes) {
89 int16_t *p16 = (int16_t *)src;
91 if (cfg.channels == 1) {
92 int samples = size_bytes / 2;
94 int16_t buffer[samples * 2];
95 for (
int j = 0; j < samples; j++) {
96 buffer[j * 2] = p16[j];
97 buffer[j * 2 + 1] = p16[j];
99 result = I2S.write((
const uint8_t *)buffer, size_bytes * 2) * 2;
100 }
else if (cfg.channels == 2) {
101 result = I2S.write((
const uint8_t *)src, size_bytes) * 4;
106 size_t readBytes(
void *dest,
size_t size_bytes) {
112 int availableForWrite() {
114 if (cfg.channels == 1) {
116 result = I2S.availableForWrite() / 2 * 2;
121 result = I2S.availableForWrite() / 4 * 4;
129 int available() {
return 0; }
131 void flush() {
return I2S.flush(); }
137 void writeSample(int16_t sample) {
138 int written = I2S.write(sample);
141 LOGW(
"written: %d ", written);
142 written = I2S.write(sample);
148 for (
int j = 0; j < samples; j++) {
149 int16_t sample = values[j];
158 using I2SDriver = I2SDriverRP2040MBED;
RxTxMode
The Microcontroller is the Audio Source (TX_MODE) or Audio Sink (RX_MODE). RXTX_MODE is Source and Si...
Definition: AudioTypes.h:28