GithubHelp home page GithubHelp logo

pschatzmann / stm32f411-i2s Goto Github PK

View Code? Open in Web Editor NEW
19.0 5.0 2.0 3.64 MB

I2S Arduino Library for STM32 Microcontrollers

License: GNU General Public License v3.0

C 9.96% C++ 90.04%
arduino i2s-audio stm32 stm32f411 blackpill stm32f411e-disco stm32f723xx

stm32f411-i2s's Introduction

Arduino STM32 I2S Library

I wanted to use I2S in Arduino with my STM32F411 Black Pill processor together with my Arduino Audio Tools!

stm32f411

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.

Pins for I2S3

See src/stm32-config-i2s.h.

Supported Sample Rates

  • 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

API

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.

Sending Data

#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() {}

Receiving Data

#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() {}

Documentation

Here is the link to the actual documentation.

You might also find further information in my Blogs

Installation in Arduino

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

Copyright © 2022 Phil Schatzmann

GNU General Public License

stm32f411-i2s's People

Contributors

pschatzmann avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

stm32f411-i2s's Issues

STM32F4 I2s On different Pins

Is there a way to allow me to use SPI 2 for the I2S Instance? I have my blackpill wired up to use pins PB12-WS, PB13-BCK, and PB15 as data out to an amp. Im using the blackpoll to control and determine which folders to retrieve audio from an SD Breakout board.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.