Skip to content

Commit caa231c

Browse files
authored
Merge pull request #2539 from kattni/i2s-bff-code
Adding I2S BFF code.
2 parents 9caf603 + 50aa81d commit caa231c

File tree

8 files changed

+4573
-0
lines changed

8 files changed

+4573
-0
lines changed

Adafruit_I2S_BFF/Arduino/I2S_BFF_QT_Py_RP2040_Audio_Playback/.qt_py_rp2040.test.only

Whitespace-only changes.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// SPDX-FileCopyrightText: 2023 Ladyada for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
/*
6+
This example plays a 'raw' PCM file from memory to I2S
7+
*/
8+
9+
#include <I2S.h>
10+
11+
#include "startup.h" // audio file in flash
12+
13+
// Create the I2S port using a PIO state machine
14+
I2S i2s(OUTPUT);
15+
16+
// GPIO pin numbers
17+
#define pBCLK A2 // QT Py BFF default BITCLOCK
18+
#define pWS A1 // QT Py BFF default LRCLOCK
19+
#define pDOUT A0 // QT Py BFF default DATA
20+
21+
#define USERBUTTON 21 // QT Py RP2040 built in button
22+
23+
// variable shared between cores
24+
volatile bool playaudio = false;
25+
26+
void setup() {
27+
Serial.begin(115200);
28+
//while (!Serial) delay(10);
29+
Serial.println("I2S playback demo");
30+
31+
pinMode(USERBUTTON, INPUT_PULLUP);
32+
}
33+
34+
void loop() {
35+
// on button press tell the other core to play audio clip!
36+
if (!digitalRead(USERBUTTON)) {
37+
playaudio = true;
38+
} else {
39+
playaudio = false;
40+
}
41+
}
42+
43+
void setup1() {
44+
i2s.setBCLK(pBCLK);
45+
i2s.setDATA(pDOUT);
46+
i2s.setBitsPerSample(16);
47+
}
48+
49+
void loop1() {
50+
// the main loop will tell us when it wants us to play!
51+
if (playaudio) {
52+
play_i2s(startupAudioData, sizeof(startupAudioData), startupSampleRate);
53+
}
54+
}
55+
56+
void play_i2s(const uint8_t *data, uint32_t len, uint32_t rate) {
57+
// start I2S at the sample rate with 16-bits per sample
58+
if (!i2s.begin(rate)) {
59+
Serial.println("Failed to initialize I2S!");
60+
delay(500);
61+
i2s.end();
62+
return;
63+
}
64+
65+
for(uint32_t i=0; i<len; i++) {
66+
uint16_t sample = (uint16_t)data[i] << 6; // our data is 10 bit but we want 16 bit so we add some gain
67+
// write the same sample twice, once for left and once for the right channel
68+
i2s.write(sample);
69+
i2s.write(sample);
70+
}
71+
i2s.end();
72+
}

Adafruit_I2S_BFF/Arduino/I2S_BFF_QT_Py_RP2040_Audio_Playback/startup.h

Lines changed: 4377 additions & 0 deletions
Large diffs are not rendered by default.

Adafruit_I2S_BFF/Arduino/I2S_BFF_QT_Py_RP2040_Tone/.qt_py_rp2040.test.only

Whitespace-only changes.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// SPDX-FileCopyrightText: 2016 Sandeep Mistry
2+
// SPDX-FileCopyrightText: 2022 Earle F. Philhower, III
3+
// SPDX-FileCopyrightText: 2023 Ladyada for Adafruit Industries
4+
//
5+
// SPDX-License-Identifier: MIT
6+
7+
/*
8+
This example generates a square wave based tone at a specified frequency
9+
and sample rate. Then outputs the data using the I2S interface to a
10+
MAX08357 I2S Amp Breakout board.
11+
12+
created 17 November 2016
13+
by Sandeep Mistry
14+
modified for RP2040 by Earle F. Philhower, III <earlephilhower@yahoo.com>
15+
16+
17+
bool setBCLK(pin_size_t pin);
18+
- This assigns two adjacent pins - the pin after this one (one greater)
19+
is the WS (word select) signal, which toggles before the sample for
20+
each channel is sent
21+
22+
bool setDATA(pin_size_t pin);
23+
- Sets the DOUT pin, can be any valid GPIO pin
24+
*/
25+
26+
#include <I2S.h>
27+
28+
// Create the I2S port using a PIO state machine
29+
I2S i2s(OUTPUT);
30+
31+
// GPIO pin numbers
32+
#define pBCLK A2 // QT Py BFF default BITCLOCK
33+
#define pWS A1 // QT Py BFF default LRCLOCK
34+
#define pDOUT A0 // QT Py BFF default DATA
35+
36+
const int frequency = 440; // frequency of square wave in Hz
37+
const int amplitude = 500; // amplitude of square wave
38+
const int sampleRate = 16000; // 16 KHz is a good quality
39+
40+
const int halfWavelength = (sampleRate / frequency); // half wavelength of square wave
41+
42+
int16_t sample = amplitude; // current sample value
43+
int count = 0;
44+
45+
void setup() {
46+
Serial.begin(115200);
47+
while (!Serial) delay(10);
48+
Serial.println("I2S simple tone");
49+
50+
i2s.setBCLK(pBCLK);
51+
i2s.setDATA(pDOUT);
52+
i2s.setBitsPerSample(16);
53+
54+
// start I2S at the sample rate with 16-bits per sample
55+
if (!i2s.begin(sampleRate)) {
56+
Serial.println("Failed to initialize I2S!");
57+
while (1); // do nothing
58+
}
59+
60+
}
61+
62+
void loop() {
63+
if (count % halfWavelength == 0) {
64+
// invert the sample every half wavelength count multiple to generate square wave
65+
sample = -1 * sample;
66+
}
67+
68+
// write the same sample twice, once for left and once for the right channel
69+
i2s.write(sample);
70+
i2s.write(sample);
71+
72+
// increment the counter for the next sample
73+
count++;
74+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# SPDX-FileCopyrightText: 2023 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
CircuitPython I2S Tone playback example.
5+
"""
6+
import time
7+
import array
8+
import math
9+
import audiocore
10+
import board
11+
import audiobusio
12+
13+
TONE_VOLUME = 0.1 # Increase this to increase the volume of the tone.
14+
FREQUENCY = 440 # Set this to the Hz of the tone you want to generate.
15+
16+
audio = audiobusio.I2SOut(board.A2, board.A1, board.A0)
17+
18+
length = 8000 // FREQUENCY
19+
sine_wave = array.array("h", [0] * length)
20+
for i in range(length):
21+
sine_wave[i] = int((math.sin(math.pi * 2 * i / length)) * TONE_VOLUME * (2 ** 15 - 1))
22+
sine_wave_sample = audiocore.RawSample(sine_wave)
23+
24+
while True:
25+
audio.play(sine_wave_sample, loop=True)
26+
time.sleep(1)
27+
audio.stop()
28+
time.sleep(1)
Binary file not shown.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# SPDX-FileCopyrightText: 2023 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
CircuitPython I2S WAV file playback.
5+
"""
6+
import audiocore
7+
import board
8+
import audiobusio
9+
10+
LOOP = False # Update to True loop WAV playback. False plays once.
11+
12+
audio = audiobusio.I2SOut(board.A2, board.A1, board.A0)
13+
14+
with open("chikken.wav", "rb") as wave_file:
15+
wav = audiocore.WaveFile(wave_file)
16+
17+
print("Playing wav file!")
18+
audio.play(wav, loop=LOOP)
19+
while audio.playing:
20+
pass
21+
22+
print("Done!")

0 commit comments

Comments
 (0)