I2S interface changed to SPI2 to avoid interference with SDIO

This commit is contained in:
ChrisMicro 2017-05-15 07:01:24 +02:00
parent 99fe3918d3
commit 9f6a578c78
1 changed files with 22 additions and 16 deletions

View File

@ -20,15 +20,14 @@
*/ */
#include "I2S.h" #include "I2S.h"
// to use the example check your board schematic // SPI2 pins are used for I2S.
// if this is the correct pin setting for your board. // SPI2 is avalailable on the BalckF407VE and does not conflict with peripherals like SDIO
// general setup without master clock I2SClass I2S(SPI2, PC3 /*DIN*/ , PB12 /*LRC*/, PB10 /*SCLK*/);
I2SClass I2S(SPI3, PC12 /*DIN*/ , PA4 /*LRC*/, PC10 /*SCLK*/);
#define SAMPLINGFREQUENCY 44100 #define SAMPLINGFREQUENCY 44100
#define WAVEBUFFERLENGTH SAMPLINGFREQUENCY * 1 // 1 seconds #define WAVEBUFFERLENGTH SAMPLINGFREQUENCY * 1 // 1 seconds
#define DAC_DC_VALUE 0x4000 #define DAC_DC_VALUE 0x4000 // DAC value when there is no sound amplitude
#define DAC_MAX_AMPLITUDE 0x2000 // experimental maximum on my speakers without distortion #define DAC_MAX_AMPLITUDE 0x2000 // experimental maximum on my speakers without distortion
int16_t wave_i16[WAVEBUFFERLENGTH]; int16_t wave_i16[WAVEBUFFERLENGTH];
@ -43,19 +42,26 @@ void setup()
for ( int n = 0; n < WAVEBUFFERLENGTH; n++ ) wave_i16[n] = ( sin( 2 * PI * frequency / SAMPLINGFREQUENCY * n ) ) * amplitude; for ( int n = 0; n < WAVEBUFFERLENGTH; n++ ) wave_i16[n] = ( sin( 2 * PI * frequency / SAMPLINGFREQUENCY * n ) ) * amplitude;
} }
uint32_t BufferIndex = 0; void playBuffer(int16_t *monoBuffer, uint32_t bufferLength)
void loop()
{ {
// write left channel uint32_t n;
I2S.write( wave_i16[ BufferIndex++ ] + DAC_DC_VALUE ); for ( n = 0; n < bufferLength; n++ )
// mono amplifier, right channel not available, write zeros
I2S.write( 0 );
if ( BufferIndex > WAVEBUFFERLENGTH)
{ {
delay( 3000 ); int16_t dacValue = wave_i16[ n ] + DAC_DC_VALUE ;
BufferIndex = 0;
// write left channel
I2S.write( dacValue );
// write right channel
// mono amplifier, right channel could be anything
// we just write the same value as left channel
// just in case you want to connect a stereo I2S-DAC
I2S.write( dacValue );
} }
} }
void loop()
{
playBuffer(wave_i16,WAVEBUFFERLENGTH);
delay( 3000 ); // delay next playing
}