Skip to content

Commit f1c8c23

Browse files
committed
Correct pins for AudioKit
1 parent 4c481d2 commit f1c8c23

File tree

3 files changed

+96
-53
lines changed

3 files changed

+96
-53
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "AudioTools.h" // install https://github.com/pschatzmann/arduino-audio-tools
2+
#include "AudioLibs/I2SCodecStream.h"
3+
4+
AudioInfo info(32000, 2, 16);
5+
SineWaveGenerator<int16_t> sineWave(32000); // subclass of SoundGenerator with max amplitude of 32000
6+
GeneratedSoundStream<int16_t> sound(sineWave); // Stream generated from sine wave
7+
I2SCodecStream out(AudioKitEs8388V1);
8+
StreamCopy copier(out, sound); // copies sound into i2s
9+
10+
// Arduino Setup
11+
void setup(void) {
12+
// Open Serial
13+
Serial.begin(115200);
14+
AudioLogger::instance().begin(Serial, AudioLogger::Info);
15+
16+
// start I2S
17+
Serial.println("starting I2S...");
18+
auto config = out.defaultConfig(TX_MODE);
19+
config.copyFrom(info);
20+
out.begin(config);
21+
22+
// Setup sine wave
23+
sineWave.begin(info, N_B4);
24+
Serial.println("started...");
25+
}
26+
27+
// Arduino loop - copy sound to out
28+
void loop() {
29+
copier.copy();
30+
}

src/AudioBoard.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class AudioBoard {
3030
bool setMute(bool enable) { return driver->setMute(enable); }
3131
bool setVolume(int volume) { return driver->setVolume(volume); }
3232
int getVolume() { return driver->getVolume(); }
33-
Pins getPins() { return pins; }
33+
Pins& getPins() { return pins; }
3434
bool setPAPower(bool enable) { return driver->setPAPower(enable); }
3535
/// set volume for adc: this is only supported on some defined codecs
3636
bool setInputVolume(int volume) {return driver->setInputVolume(volume);}

src/Pins.h

Lines changed: 65 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ struct PinsSPI {
7575
Pin mosi = -1;
7676
Pin cs = -1;
7777
bool set_active = true;
78-
operator bool() { return clk != -1 && miso != -1 && mosi != -1; }
78+
bool pinsAvailable() { return clk != -1 && miso != -1 && mosi != -1; }
79+
operator bool() { return pinsAvailable(); }
7980
bool begin() {
8081
if (set_active) {
8182
// setup chip select
@@ -84,21 +85,27 @@ struct PinsSPI {
8485
digitalWrite(cs, HIGH);
8586
}
8687
// if no pins are defined, just call begin
87-
if (!*this) {
88+
if (!pinsAvailable()) {
89+
LOGI("setting up SPI w/o pins");
8890
p_spi->begin();
8991
} else {
90-
// begin spi and set up pins if supported
91-
#if defined(ARDUINO_ARCH_STM32)
92-
p_spi->setMISO(miso);
93-
p_spi->setMOSI(mosi);
94-
p_spi->setSCLK(clk);
95-
p_spi->setSSEL(cs);
96-
p_spi->begin();
97-
#elif defined(ESP32)
98-
p_spi->begin(clk, miso, mosi, cs);
99-
#elif defined(ARDUINO_ARCH_AVR)
100-
p_spi->begin();
101-
#endif
92+
// begin spi and set up pins if supported
93+
#if defined(ARDUINO_ARCH_STM32)
94+
LOGI("setting up SPI miso:%d,mosi:%d, clk:%d, cs:%d", miso, mosi, clk,
95+
cs);
96+
p_spi->setMISO(miso);
97+
p_spi->setMOSI(mosi);
98+
p_spi->setSCLK(clk);
99+
p_spi->setSSEL(cs);
100+
p_spi->begin();
101+
#elif defined(ESP32)
102+
LOGI("setting up SPI miso:%d,mosi:%d, clk:%d, cs:%d", miso, mosi, clk,
103+
cs);
104+
p_spi->begin(clk, miso, mosi, cs);
105+
#elif defined(ARDUINO_ARCH_AVR)
106+
LOGW("setting up SPI w/o pins");
107+
p_spi->begin();
108+
#endif
102109
}
103110
}
104111
return true;
@@ -109,7 +116,7 @@ struct PinsSPI {
109116
/**
110117
* @brief Default SPI pins for ESP32 Lyrat, AudioDriver etc
111118
*/
112-
PinsSPI ESP32PinsSD{SD, 13, 2, 15, 14, SPI};
119+
PinsSPI ESP32PinsSD{SD, 14, 2, 15, 13, SPI};
113120

114121
/**
115122
* @brief I2C pins
@@ -133,27 +140,33 @@ struct PinsI2C {
133140
Pin sda = -1;
134141
bool set_active = true;
135142
TwoWire *p_wire;
136-
operator bool() { return scl != -1 && sda != -1 && frequency != 0; }
143+
bool pinsAvailable() { return scl != -1 && sda != -1 && frequency != 0; }
144+
operator bool() { return pinsAvailable(); }
137145

138146
bool begin() {
139147
if (set_active) {
140-
p_wire->setClock(frequency);
141148
// if no pins are defined, just call begin
142-
if (!*this)
149+
if (!pinsAvailable()) {
150+
LOGI("setting up I2C w/o pins");
151+
p_wire->begin();
152+
} else {
153+
// begin with defined pins, if supported
154+
#if defined(ESP32) || defined(ARDUINO_ARCH_STM32)
155+
LOGI("setting up I2C scl: %d, sda: %d", scl, sda);
156+
p_wire->begin(sda, scl);
157+
#elif defined(ARDUINO_ARCH_RP2040)
158+
LOGI("setting up I2C scl: %d, sda: %d", scl, sda);
159+
p_wire->setSCL(scl);
160+
p_wire->setSDA(sda);
161+
p_wire->begin();
162+
#else
163+
LOGW("setting up I2C w/o pins");
143164
p_wire->begin();
144-
else {
145-
// begin with defined pins, if supported
146-
#if defined(ESP32) || defined(ARDUINO_ARCH_STM32)
147-
p_wire->begin(sda, scl);
148-
#elif defined(ARDUINO_ARCH_RP2040)
149-
p_wire->setSCL(scl);
150-
p_wire->setSDA(sda);
151-
p_wire->begin();
152-
#else
153-
p_wire->begin();
154-
}
155-
#endif
156165
}
166+
#endif
167+
}
168+
LOGI("Setting i2c clock: %u", frequency);
169+
p_wire->setClock(frequency);
157170
}
158171
return true;
159172
}
@@ -321,9 +334,9 @@ class PinsLyrat43Class : public Pins {
321334
PinsLyrat43Class() {
322335
// sd pins
323336
addSPI(ESP32PinsSD);
324-
// add i2c codec pins
337+
// add i2c codec pins: scl, sda, port, frequency
325338
addI2C(CODEC, 23, 18, 0x20);
326-
// add i2s pins
339+
// add i2s pins: mclk, bck, ws,data_out, data_in ,(port)
327340
addI2S(CODEC, 0, 5, 25, 26, 35);
328341

329342
// add other pins
@@ -348,9 +361,9 @@ class PinsLyrat42Class : public Pins {
348361
PinsLyrat42Class() {
349362
// sd pins
350363
addSPI(ESP32PinsSD);
351-
// add i2c codec pins
364+
// add i2c codec pins: scl, sda, port, frequency
352365
addI2C(CODEC, 23, 18, 0x20);
353-
// add i2s pins
366+
// add i2s pins: mclk, bck, ws,data_out, data_in ,(port)
354367
addI2S(CODEC, 0, 5, 25, 26, 35);
355368

356369
// add other pins
@@ -375,9 +388,9 @@ class PinsLyratMiniClass : public Pins {
375388
PinsLyratMiniClass() {
376389
// sd pins
377390
addSPI(ESP32PinsSD);
378-
// add i2c codec pins
391+
// add i2c codec pins: scl, sda, port, frequency
379392
addI2C(CODEC, 23, 18, 0x20);
380-
// add i2s pins
393+
// add i2s pins: mclk, bck, ws,data_out, data_in ,(port)
381394
addI2S(CODEC, 0, 5, 25, 26, 35, 0);
382395
addI2S(CODEC_ADC, 0, 32, 33, -1, 36, 1);
383396

@@ -404,9 +417,9 @@ class PinsAudioKitEs8388v1Class : public Pins {
404417
PinsAudioKitEs8388v1Class() {
405418
// sd pins
406419
addSPI(ESP32PinsSD);
407-
// add i2c codec pins
408-
addI2C(CODEC, 32, 22, 0x20);
409-
// add i2s pins
420+
// add i2c codec pins: scl, sda, port, frequency
421+
addI2C(CODEC, 32, 33, 0x20);
422+
// add i2s pins: mclk, bck, ws,data_out, data_in ,(port)
410423
addI2S(CODEC, 0, 27, 25, 26, 35);
411424

412425
// add other pins
@@ -431,9 +444,9 @@ class PinsAudioKitEs8388v2Class : public Pins {
431444
PinsAudioKitEs8388v2Class() {
432445
// sd pins
433446
addSPI(ESP32PinsSD);
434-
// add i2c codec pins
447+
// add i2c codec pins: scl, sda, port, frequency
435448
addI2C(CODEC, 23, 18, 0x20);
436-
// add i2s pins
449+
// add i2s pins: mclk, bck, ws,data_out, data_in ,(port)
437450
addI2S(CODEC, 0, 5, 25, 26, 35);
438451

439452
// add other pins
@@ -458,9 +471,9 @@ class PinsAudioKitAC101Class : public Pins {
458471
PinsAudioKitAC101Class() {
459472
// sd pins
460473
addSPI(ESP32PinsSD);
461-
// add i2c codec pins
474+
// add i2c codec pins: scl, sda, port, frequency
462475
addI2C(CODEC, 32, 22, 0x20);
463-
// add i2s pins
476+
// add i2s pins: mclk, bck, ws,data_out, data_in ,(port)
464477
addI2S(CODEC, 0, 27, 26, 25, 35);
465478

466479
// add other pins
@@ -485,19 +498,19 @@ class PinsAudioKitAC101Class : public Pins {
485498
class PinsSTM32F411DiscoClass : public Pins {
486499
public:
487500
PinsSTM32F411DiscoClass() {
488-
// add i2c codec pins
501+
// add i2c codec pins: scl, sda, port, frequency
489502
addI2C(CODEC, PB6, PB9, 0x25);
490-
// add i2s pins
503+
// add i2s pins: mclk, bck, ws,data_out, data_in ,(port)
491504
addI2S(CODEC, PC7, PC10, PA4, PC3, PC12);
492505

493506
// add other pins
494-
addPin(KEY, PA0); // user button
495-
addPin(LED, PD12, 0); // green
496-
addPin(LED, PD5, 1); // red
497-
addPin(LED, PD13, 2); // orange
498-
addPin(LED, PD14, 3); // red
499-
addPin(LED, PD15, 4); // blue
500-
addPin(PA, PD4); // reset pin (active high)
507+
addPin(KEY, PA0); // user button
508+
addPin(LED, PD12, 0); // green
509+
addPin(LED, PD5, 1); // red
510+
addPin(LED, PD13, 2); // orange
511+
addPin(LED, PD14, 3); // red
512+
addPin(LED, PD15, 4); // blue
513+
addPin(PA, PD4); // reset pin (active high)
501514
addPin(CODEC_ADC, PC3); // Microphone
502515
}
503516
} PinsSTM32F411Disco;

0 commit comments

Comments
 (0)