audio compiles

This commit is contained in:
2021-08-16 20:37:22 +02:00
parent 77a1f3417a
commit cebb49a7ae
8 changed files with 333 additions and 164 deletions

View File

@@ -0,0 +1,211 @@
#include "audio.h"
/* generic I2S example for any STM32duino HAL core F4
Original code by Rene Böllhoff
translated to STM32duino by Matthias Diro ("madias" -> STM32duino forum)
Features: Circular Buffer with DMA IRQ (half full, full), Master Clock enabled
This example uses the SPI3 port tested on STM32F407VET "black" board. On other boards please define LED0_BUILTIN and LED1_BUILTIN
*/
#define I2S_BUFFER_SIZE 64
I2S_HandleTypeDef hi2s3;
DMA_HandleTypeDef hdma_spi3_tx;
uint32_t dma_buffer[I2S_BUFFER_SIZE];
// sinus oszillator
float osc_phi = 0;
float osc_phi_inc = 440.0f / 44100.0f; // generating 440HZ
void MX_DMA_Init(void);
extern "C" void DMA1_Stream5_IRQHandler(void) // this function must be included to avoid DMA to crash!
{
HAL_DMA_IRQHandler(&hdma_spi3_tx);
}
void Error_Handler2(byte errorcode)
{ // if something goes wrong counter the blinks for rudimentary debugging
digitalWrite(LED_BUILTIN, 1);
while (1)
{
for (int x = 0; x < errorcode; x++)
{
digitalWrite(LED_BUILTIN, 1);
delay(100);
digitalWrite(LED_BUILTIN, 0);
delay(100);
}
delay(1000);
}
}
void FillBuffer(uint32_t *buffer, uint16_t len)
{
float a;
int16_t y;
uint16_t c;
for (c = 0; c < len; c++)
{
// calculate sin
a = (float)sin(osc_phi * 6.2832f) * 0.20f;
osc_phi += osc_phi_inc;
osc_phi -= (float)((uint16_t)osc_phi);
// float to integer
y = (int16_t)(a * 32767.0f);
// auf beide kanäle
buffer[c] = ((uint32_t)(uint16_t)y) << 0 |
((uint32_t)(uint16_t)y) << 16;
}
}
void StartAudioBuffers(I2S_HandleTypeDef *hi2s)
{
// clear buffer
memset(dma_buffer, 0, sizeof(dma_buffer));
// start circular dma
HAL_I2S_Transmit_DMA(hi2s, (uint16_t *)dma_buffer, I2S_BUFFER_SIZE << 1);
}
extern "C" void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s)
{
// second half finished, filling it up again while first half is playing
FillBuffer(&(dma_buffer[I2S_BUFFER_SIZE >> 1]), I2S_BUFFER_SIZE >> 1);
}
extern "C" void HAL_I2S_TxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
{
// first half finished, filling it up again while second half is playing
FillBuffer(&(dma_buffer[0]), I2S_BUFFER_SIZE >> 1);
}
// setting up I2S
extern "C" void MX_I2S3_Init(void)
{
hi2s3.Instance = SPI3;
hi2s3.Init.Mode = I2S_MODE_MASTER_TX;
hi2s3.Init.Standard = I2S_STANDARD_PHILIPS;
hi2s3.Init.DataFormat = I2S_DATAFORMAT_16B;
//hi2s3.Init.MCLKOutput = I2S_MCLKOUTPUT_DISABLE;
hi2s3.Init.MCLKOutput = I2S_MCLKOUTPUT_ENABLE;
hi2s3.Init.AudioFreq = I2S_AUDIOFREQ_44K;
hi2s3.Init.CPOL = I2S_CPOL_LOW;
hi2s3.Init.ClockSource = I2S_CLOCK_PLL;
hi2s3.Init.FullDuplexMode = I2S_FULLDUPLEXMODE_DISABLE;
if (HAL_I2S_Init(&hi2s3) != HAL_OK)
{
Error_Handler2(1); // on error: one blink
}
}
// setting up DMA
void MX_DMA_Init(void)
{
/* DMA controller clock enable */
__HAL_RCC_DMA1_CLK_ENABLE();
/* DMA interrupt init */
/* DMA1_Stream5_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Stream5_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Stream5_IRQn);
}
// setting up pins and clocks; routing SPI3 to DMA
extern "C" void HAL_I2S_MspInit(I2S_HandleTypeDef *hi2s)
{
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_SPI3_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct;
/* I2S standard configurations:
SPI2
PB15 DIN
PB12 LRC
PB13 SCLK
PC6 MCK
SPI3
PB5 DIN
PA4 LRC
PB3 SCLK
PC7 MCK
*/
//I2S3 used GPIO configuration in this example:
// PB5 DIN / SD
// PA4 LRC /WD
// PB3 SCLK /CK
// PC7 MCK
GPIO_InitStruct.Pin = GPIO_PIN_4;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF6_SPI3;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_3 | GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF6_SPI3;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
// master clock
GPIO_InitStruct.Pin = GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF6_SPI3;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
// Peripheral DMA init
hdma_spi3_tx.Instance = DMA1_Stream5;
hdma_spi3_tx.Init.Channel = DMA_CHANNEL_0;
hdma_spi3_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_spi3_tx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_spi3_tx.Init.MemInc = DMA_MINC_ENABLE;
hdma_spi3_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
hdma_spi3_tx.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
hdma_spi3_tx.Init.Mode = DMA_CIRCULAR;
hdma_spi3_tx.Init.Priority = DMA_PRIORITY_LOW;
hdma_spi3_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
if (HAL_DMA_Init(&hdma_spi3_tx) != HAL_OK)
{
Error_Handler2(5); // on error: five blinks
}
__HAL_LINKDMA(hi2s, hdmatx, hdma_spi3_tx);
}
extern "C" void HAL_MspInit(void) // maybe useful, not included in this example
{
HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
/* System interrupt init*/
/* MemoryManagement_IRQn interrupt configuration */
HAL_NVIC_SetPriority(MemoryManagement_IRQn, 0, 0);
/* BusFault_IRQn interrupt configuration */
HAL_NVIC_SetPriority(BusFault_IRQn, 0, 0);
/* UsageFault_IRQn interrupt configuration */
HAL_NVIC_SetPriority(UsageFault_IRQn, 0, 0);
/* SVCall_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SVCall_IRQn, 0, 0);
/* DebugMonitor_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DebugMonitor_IRQn, 0, 0);
/* PendSV_IRQn interrupt configuration */
HAL_NVIC_SetPriority(PendSV_IRQn, 0, 0);
/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
// void initAudio(void)
// {
// }
// void handleAudio(void)
// {
// }
void initAudio()
{
// HAL_MspInit(); // not important by default
HAL_I2S_MspInit(&hi2s3); // setting up pins and clocks; routing SPI3 to DMA
MX_DMA_Init();
MX_I2S3_Init();
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, 0);
StartAudioBuffers(&hi2s3);
digitalWrite(LED_BUILTIN, 1);
}
void handleAudio()
{
// just a dummy code in loop, audio out is generated by ISR
digitalWrite(LED_BUILTIN, 1);
delay(250);
digitalWrite(LED_BUILTIN, 0);
delay(250);
}

View File

@@ -0,0 +1,7 @@
#pragma once
#include "Arduino.h"
#include "board.h"
void initAudio(void);
void handleAudio(void);

View File

@@ -0,0 +1,7 @@
#pragma once
// #define PIN_I2S_FS PB12 /* I2S4_WS, Frame Sync (LRclock)
// #define PIN_I2S_SCK PB13 /* I2S4_CLK, serial Clock */
// #define PIN_I2S_SD PA1 /* I2S4_SD, Serial Data */
// #define PIN_I2S_SDMODE PA13 /* DAC_SDMODE, SDmode HIGH = enabled (left channel), LOW = shutdown */

View File

@@ -1,166 +1,18 @@
// Arduino Zero / Feather M0 I2S audio tone generation example.
// Author: Tony DiCola
//
// Connect an I2S DAC or amp (like the UDA1334A) to the Arduino Zero
// and play back simple sine, sawtooth, triangle, and square waves.
// Makes your Zero sound like a NES!
//
// NOTE: The I2S signal generated by the Zero does NOT have a MCLK /
// master clock signal. You must use an I2S receiver that can operate
// without a MCLK signal (like the UDA1334A).
//
// For an Arduino Zero / Feather M0 connect it to you I2S hardware as follows:
// - Digital 0 -> I2S LRCLK / FS (left/right / frame select clock)
// - Digital 1 -> I2S BCLK / SCLK (bit / serial clock)
// - Digital 9 -> I2S DIN / SD (data output)
// - Ground
//
// Released under a MIT license: https://opensource.org/licenses/MIT
#include "Adafruit_ZeroI2S.h"
#include "Arduino.h"
#include "audio.h"
#define SAMPLERATE_HZ 44100 // The sample rate of the audio. Higher sample rates have better fidelity,
// but these tones are so simple it won't make a difference. 44.1khz is
// standard CD quality sound.
/* TODO: check hardware STM32L432KC (32pin QFN)
#define AMPLITUDE ((1<<29)-1) // Set the amplitude of generated waveforms. This controls how loud
// the signals are, and can be any value from 0 to 2**31 - 1. Start with
// a low value to prevent damaging speakers!
*/
#define WAV_SIZE 256 // The size of each generated waveform. The larger the size the higher
// quality the signal. A size of 256 is more than enough for these simple
// waveforms.
// Define the frequency of music notes (from http://www.phy.mtu.edu/~suits/notefreqs.html):
#define C4_HZ 261.63
#define D4_HZ 293.66
#define E4_HZ 329.63
#define F4_HZ 349.23
#define G4_HZ 392.00
#define A4_HZ 440.00
#define B4_HZ 493.88
// Define a C-major scale to play all the notes up and down.
float scale[] = { C4_HZ, D4_HZ, E4_HZ, F4_HZ, G4_HZ, A4_HZ, B4_HZ, A4_HZ, G4_HZ, F4_HZ, E4_HZ, D4_HZ, C4_HZ };
// Store basic waveforms in memory.
int32_t sine[WAV_SIZE] = {0};
int32_t sawtooth[WAV_SIZE] = {0};
int32_t triangle[WAV_SIZE] = {0};
int32_t square[WAV_SIZE] = {0};
// Create I2S audio transmitter object.
Adafruit_ZeroI2S i2s;
#define Serial Serial
void generateSine(int32_t amplitude, int32_t* buffer, uint16_t length) {
// Generate a sine wave signal with the provided amplitude and store it in
// the provided buffer of size length.
for (int i=0; i<length; ++i) {
buffer[i] = int32_t(float(amplitude)*sin(2.0*PI*(1.0/length)*i));
}
}
void generateSawtooth(int32_t amplitude, int32_t* buffer, uint16_t length) {
// Generate a sawtooth signal that goes from -amplitude/2 to amplitude/2
// and store it in the provided buffer of size length.
float delta = float(amplitude)/float(length);
for (int i=0; i<length; ++i) {
buffer[i] = -(amplitude/2)+delta*i;
}
void setup()
{
initAudio();
}
void generateTriangle(int32_t amplitude, int32_t* buffer, uint16_t length) {
// Generate a triangle wave signal with the provided amplitude and store it in
// the provided buffer of size length.
float delta = float(amplitude)/float(length);
for (int i=0; i<length/2; ++i) {
buffer[i] = -(amplitude/2)+delta*i;
}
for (int i=length/2; i<length; ++i) {
buffer[i] = (amplitude/2)-delta*(i-length/2);
}
}
void generateSquare(int32_t amplitude, int32_t* buffer, uint16_t length) {
// Generate a square wave signal with the provided amplitude and store it in
// the provided buffer of size length.
for (int i=0; i<length/2; ++i) {
buffer[i] = -(amplitude/2);
}
for (int i=length/2; i<length; ++i) {
buffer[i] = (amplitude/2);
}
}
void playWave(int32_t* buffer, uint16_t length, float frequency, float seconds) {
// Play back the provided waveform buffer for the specified
// amount of seconds.
// First calculate how many samples need to play back to run
// for the desired amount of seconds.
uint32_t iterations = seconds*SAMPLERATE_HZ;
// Then calculate the 'speed' at which we move through the wave
// buffer based on the frequency of the tone being played.
float delta = (frequency*length)/float(SAMPLERATE_HZ);
// Now loop through all the samples and play them, calculating the
// position within the wave buffer for each moment in time.
for (uint32_t i=0; i<iterations; ++i) {
uint16_t pos = uint32_t(i*delta) % length;
int32_t sample = buffer[pos];
// Duplicate the sample so it's sent to both the left and right channel.
// It appears the order is right channel, left channel if you want to write
// stereo sound.
i2s.write(sample, sample);
}
}
void setup() {
// Configure serial port.
Serial.begin(115200);
Serial.println("Zero I2S Audio Tone Generator");
// Initialize the I2S transmitter.
if (!i2s.begin(I2S_32_BIT, SAMPLERATE_HZ)) {
Serial.println("Failed to initialize I2S transmitter!");
while (1);
}
i2s.enableTx();
// Generate waveforms.
generateSine(AMPLITUDE, sine, WAV_SIZE);
generateSawtooth(AMPLITUDE, sawtooth, WAV_SIZE);
generateTriangle(AMPLITUDE, triangle, WAV_SIZE);
generateSquare(AMPLITUDE, square, WAV_SIZE);
}
void loop() {
Serial.println("Sine wave");
for (int i=0; i<sizeof(scale)/sizeof(float); ++i) {
// Play the note for a quarter of a second.
playWave(sine, WAV_SIZE, scale[i], 0.25);
// Pause for a tenth of a second between notes.
delay(100);
}
Serial.println("Sawtooth wave");
for (int i=0; i<sizeof(scale)/sizeof(float); ++i) {
// Play the note for a quarter of a second.
playWave(sawtooth, WAV_SIZE, scale[i], 0.25);
// Pause for a tenth of a second between notes.
delay(100);
}
Serial.println("Triangle wave");
for (int i=0; i<sizeof(scale)/sizeof(float); ++i) {
// Play the note for a quarter of a second.
playWave(triangle, WAV_SIZE, scale[i], 0.25);
// Pause for a tenth of a second between notes.
delay(100);
}
Serial.println("Square wave");
for (int i=0; i<sizeof(scale)/sizeof(float); ++i) {
// Play the note for a quarter of a second.
playWave(square, WAV_SIZE, scale[i], 0.25);
// Pause for a tenth of a second between notes.
delay(100);
}
void loop()
{
handleAudio();
}

View File

@@ -0,0 +1,75 @@
#include "storage.h"
Sd2Card card;
SdFatFs fatFs;
void initStorage(void)
{
bool disp = false;
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial);
Serial.print("\nInitializing SD card...");
while(!card.init(SD_DETECT_PIN)) {
if (!disp) {
Serial.println("initialization failed. Is a card inserted?");
disp = true;
}
delay(10);
}
Serial.println("A card is present.");
// print the type of card
Serial.print("\nCard type: ");
switch (card.type()) {
case SD_CARD_TYPE_SD1:
Serial.println("SD1");
break;
case SD_CARD_TYPE_SD2:
Serial.println("SD2");
break;
case SD_CARD_TYPE_SDHC:
Serial.println("SDHC");
break;
default:
Serial.println("Unknown");
}
// Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
if (!fatFs.init()) {
Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
return;
}
// print the type and size of the first FAT-type volume
uint64_t volumesize;
Serial.print("\nVolume type is FAT");
Serial.println(fatFs.fatType(), DEC);
Serial.println();
volumesize = fatFs.blocksPerCluster(); // clusters are collections of blocks
volumesize *= fatFs.clusterCount(); // we'll have a lot of clusters
volumesize *= 512; // SD card blocks are always 512 bytes
Serial.print("Volume size (bytes): ");
Serial.println(volumesize);
Serial.print("Volume size (Kbytes): ");
volumesize /= 1024;
Serial.println(volumesize);
Serial.print("Volume size (Mbytes): ");
volumesize /= 1024;
Serial.println(volumesize);
Serial.println("\nFiles found on the card (name, date and size in bytes): ");
File root = SD.openRoot();
// list all files in the card with date and size
root.ls(LS_R | LS_DATE | LS_SIZE);
Serial.println("###### End of the SD tests ######");
}
void handleStorage(void) {
// do nothing
}

View File

@@ -0,0 +1,13 @@
#pragma once
// include the SD library:
#include <STM32SD.h>
// If SD card slot has no detect pin then define it as SD_DETECT_NONE
// to ignore it. One other option is to call 'card.init()' without parameter.
#ifndef SD_DETECT_PIN
#define SD_DETECT_PIN PB9
#endif
void initStorage(void);
void handleStorage(void);