I wanted to use I2S in Arduino with my STM32F411 Black Pill processor together with my Arduino Audio Tools!
Unfortunately STMDuino does not provide this functionality.
My first trials failed miserably using the DMA versions of the HAL API, so I decided to generate a working solution using the STM Cube IDE and then convert this to Arduino library, that provides the following functionality:
- The DMA is used to transfer the data
- The API is using Callbacks to transfer the data.
- The following settings are supported:
- I2S Protocol can be defined with i2s_default_standard variable (default is I2S_STANDARD_PHILIPS)
- Mode can be selected with i2s_default_mode variable (default is I2S_MODE_MASTER_TX)
- Full Duplex is supported with i2s_default_fullduplexmode variable (default is I2S_FULLDUPLEXMODE_ENABLE)
- Sampling rate can be selected with is2_default_samplerate variable(default value is I2S_AUDIOFREQ_44K)
- Only 16bit data is supported
- I also incuded the codec drivers that are part of some stm32 evaluation boards.
Subsequently, I have extended the functionality to support other variants.
- I2S_AUDIOFREQ_192K
- I2S_AUDIOFREQ_96K
- I2S_AUDIOFREQ_48K
- I2S_AUDIOFREQ_44K
- I2S_AUDIOFREQ_32K
- I2S_AUDIOFREQ_22K
- I2S_AUDIOFREQ_16K
- I2S_AUDIOFREQ_11K
- I2S_AUDIOFREQ_8K
Below I demonstrate the basic API provided by this library. However, I recommend that you use the I2SStream class from the Arduino Audio Tools library which uses this functionality.
#include "AudioTools.h"
#include "stm32-i2s.h"
using namespace stm32_i2s;
SineWaveGenerator<int16_t> sineWave(32000); // subclass of SoundGenerator with max amplitude of 32000
I2SSettingsSTM32 i2s_settings;
Stm32I2sClass I2S;
int sample_rate = 8000;
int channels = 1;
void readToTransmit(uint8_t *buffer, uint16_t byteCount, void*) {
uint16_t samples = byteCount / 2;
int16_t *buffer_16 = (int16_t*) buffer;
for (uint j = 0; j < samples; j+=2) {
int16_t sample = sineWave.readSample();
buffer_16[j] = sample;
buffer_16[j+1] = sample;
}
}
void setup() {
Serial.begin(115200);
sineWave.begin(channels, sample_rate, N_B4);
i2s_settings.sample_rate = I2S_AUDIOFREQ_8K;
if (!I2S.beginWriteDMA(i2s_settings, readToTransmit)){
Serial.println("I2S Error");
}
}
void loop() {}
#include "AudioTools.h"
#include "stm32-i2s.h"
using namespace stm32_i2s;
CsvStream<int16_t> out(Serial, 2); // ASCII output stream
I2SSettingsSTM32 i2s_settings;
Stm32I2sClass I2S;
void writeFromReceive(uint8_t *buffer, uint16_t byteCount, void*){
out.write(buffer, byteCount);
}
void setup() {
Serial.begin(115200);
i2s_settings.sample_rate = I2S_AUDIOFREQ_8K;
if (!I2S.beginReadDMA(i2s_settings, writeFromReceive){
Serial.println("I2S Error");
}
}
void loop() {}
Here is the link to the actual documentation.
You might also find further information in my Blogs
You can download the library as zip and call include Library -> zip library. Or you can git clone this project into the Arduino libraries folder e.g. with
cd ~/Documents/Arduino/libraries
git clone https://github.com/pschatzmann/stm32-i2s.git
I recommend to use git because you can easily update to the latest version just by executing the git pull
command in the project folder.
Copyright © 2022 Phil Schatzmann