-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3464292
commit d64f153
Showing
7 changed files
with
158 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,38 @@ | ||
# Arduino Portenta H7 I2S Library | ||
|
||
This library allows you to use the I2S protocol on the Arduino Portenta H7 in PlatformIO. | ||
This library allows you to use the I2S protocol on the Arduino Portenta H7 in PlatformIO. It is compatible with both the M4 and M7 cores on the Portenta. | ||
|
||
Huge thanks to Max Gerhardt from PlatformIO for helping me get this working. | ||
Huge thanks to Max Gerhardt from PlatformIO for helping me get I2S ported from Cube to PlatformIO. | ||
|
||
## Usage | ||
1. Create the I2S object | ||
```cpp | ||
PortentaI2S i2s = PortentaI2S(USE_I2S2, I2S_AUDIOFREQ_44K); | ||
``` | ||
|
||
Currently only I2S2 is supported on the Portenta (this is connected to the I2S header on the Portenta breakout board). However, you can chose any audio frequency you wish from this list: | ||
```cpp | ||
I2S_AUDIOFREQ_192K 192000hz | ||
I2S_AUDIOFREQ_96K 96000hz | ||
I2S_AUDIOFREQ_48K 48000hz | ||
I2S_AUDIOFREQ_44K 44100hz | ||
I2S_AUDIOFREQ_32K 32000hz | ||
I2S_AUDIOFREQ_22K 22050hz | ||
I2S_AUDIOFREQ_16K 16000hz | ||
I2S_AUDIOFREQ_11K 11025hz | ||
I2S_AUDIOFREQ_8K 8000hz | ||
I2S_AUDIOFREQ_DEFAULT 2hz | ||
``` | ||
|
||
2. Initialize the I2S interface | ||
```cpp | ||
i2s.begin(); | ||
``` | ||
|
||
3. Play your audio buffer | ||
```cpp | ||
i2s.play(buffer, bufferLen); | ||
``` | ||
|
||
## Examples | ||
A simple example is provided for both the M4 and M7 cores (usage is the same no matter the core), which plays a sine wave at 440.0Hz (A4) out of the I2S2 interface. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#ifdef CORE_CM4 | ||
|
||
#include <Arduino.h> | ||
#include <portenta-i2s.h> | ||
|
||
PortentaI2S i2s = PortentaI2S(USE_I2S2, I2S_AUDIOFREQ_44K); | ||
|
||
void setup() | ||
{ | ||
i2s.begin(); | ||
|
||
// Generate sine wave | ||
uint16_t samplebuf[8192]; // for both left + right | ||
const int nsamples = sizeof(samplebuf) / sizeof(samplebuf[0]); | ||
int i = 0; | ||
while(i < nsamples) { | ||
double t = ((double)i / 2.0) / 44100.0; | ||
samplebuf[i] = 32767*((sin((440.0 - FREQ_OFFSET) * TAU * t) / 2)); // left channel | ||
samplebuf[i+1] = samplebuf[i]; // right channel (same) | ||
i += 2; | ||
} | ||
|
||
// Play continuously | ||
while(1){ | ||
i2s.play(samplebuf, nsamples); | ||
} | ||
} | ||
|
||
void loop() | ||
{ | ||
|
||
} | ||
|
||
#endif | ||
|
||
#ifdef CORE_CM7 | ||
#include "Arduino.h" | ||
#include <portenta-i2s.h> | ||
|
||
PortentaI2S i2s = PortentaI2S(USE_I2S2, I2S_AUDIOFREQ_44K); | ||
|
||
void setup() | ||
{ | ||
Serial.begin(115200); | ||
i2s.begin(); | ||
|
||
// Generate sine wave | ||
uint16_t samplebuf[8192]; // for both left + right | ||
const int nsamples = sizeof(samplebuf) / sizeof(samplebuf[0]); | ||
int i = 0; | ||
while(i < nsamples) { | ||
double t = ((double)i / 2.0) / 44100.0; | ||
samplebuf[i] = 32767*((sin((440.0 - FREQ_OFFSET) * TAU * t) / 2)); // left channel | ||
samplebuf[i+1] = samplebuf[i]; // right channel (same) | ||
i += 2; | ||
} | ||
|
||
// Play continuously | ||
while(1){ | ||
i2s.play(samplebuf, nsamples); | ||
} | ||
} | ||
|
||
void loop() | ||
{ | ||
|
||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
; PlatformIO Project Configuration File | ||
; | ||
; Build options: build flags, source filter | ||
; Upload options: custom upload port, speed and extra flags | ||
; Library options: dependencies, extra library storages | ||
; Advanced options: extra scripting | ||
; | ||
; Please visit documentation for the other options and examples | ||
; https://docs.platformio.org/page/projectconf.html | ||
|
||
[env:portenta_h7_m7] | ||
platform = ststm32 | ||
board = portenta_h7_m7 | ||
framework = arduino | ||
upload_protocol = stlink | ||
debug_tool = stlink | ||
|
||
[env:portenta_h7_m4] | ||
platform = ststm32 | ||
board = portenta_h7_m4 | ||
framework = arduino | ||
upload_protocol = stlink | ||
debug_tool = stlink |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "Portenta-I2S", | ||
"version": "1.0.0", | ||
"description": "A simple I2S implementation using HAL for the Portenta H7 series.", | ||
"repository": | ||
{ | ||
"type": "git", | ||
"url": "https://github.com/trylaarsdam/portenta-i2s.git" | ||
}, | ||
"authors": | ||
[ | ||
{ | ||
"name": "Todd Rylaarsdam", | ||
"email": "todd@toddr.org", | ||
"url": "https://toddr.org", | ||
"maintainer": true | ||
} | ||
], | ||
"frameworks": "Arduino", | ||
"platforms": "*" | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters