2015-11-04 03:51 AM
I recently bought a STM32F4 discovery and I'm interested in audio processing. I started trying to record some audio data using the MP45DT02 and the I2S module.
The program is based on the play and record demo, it records the audio data and send it to the pc through usb.With 16kHz of sample frequency it works but with,for example, 48kHz the generate .wav seems to have noise almost like if there were something wrong with the rcc configuration or i2s sample frequency configuration, kike when the audio is playing to a different frequency that the one that was recorded. Ofcourse I modified the size of pdm and pcm buffers according to datasheet, also I generated a new system_stm32f4xx.c with the excel tool (new PLL configuration). But still does not work.if you could help me, I'd appreciate.My main.h now looks like this:#define DECIMATION_FACTOR 64#define SAMPLE_FREQUENCY 48000#define OUT_FREQ SAMPLE_FREQUENCY/2#define INPUT_CHANNELS 1#define PDM_Input_Buffer_SIZE (OUT_FREQ / 1000 *DECIMATION_FACTOR * INPUT_CHANNELS/8 )#define PCM_Output_Buffer_SIZE (OUT_FREQ / 1000 * INPUT_CHANNELS)#define Buffer_Input_SIZE 2048#define VOLUME 50The PDM filter initialization :Filter.Fs = SAMPLE_FREQUENCY;Filter.HP_HZ = 10.;Filter.LP_HZ = (SAMPLE_FREQUENCY / 2.0);Filter.In_MicChannels = 1;Filter.Out_MicChannels = 1;PDM_Filter_Init(&Filter);The i2s configuration:SPI_I2S_DeInit(SPI2);I2S_InitStructure.I2S_AudioFreq = SAMPLE_FREQUENCY * 2;I2S_InitStructure.I2S_Standard = I2S_Standard_LSB;I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_16b;I2S_InitStructure.I2S_CPOL = I2S_CPOL_High;I2S_InitStructure.I2S_Mode = I2S_Mode_MasterRx;I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Disable;I2S_Init(SPI2, &I2S_InitStructure);// Enable the Rx buffer not empty interruptSPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, ENABLE);And all these are the configurations generated in system_stm32f4xx.c*-----------------------------------------------------------------------------System Clock source | PLL (HSE) *-----------------------------------------------------------------------------SYSCLK(Hz) | 168000000 *-----------------------------------------------------------------------------HCLK(Hz) | 168000000 *-----------------------------------------------------------------------------AHB Prescaler | 1 *-----------------------------------------------------------------------------APB1 Prescaler | 4 *-----------------------------------------------------------------------------APB2 Prescaler | 2 *-----------------------------------------------------------------------------HSE Frequency(Hz) | 8000000 *-----------------------------------------------------------------------------PLL_M | 8 *-----------------------------------------------------------------------------PLL_N | 336 *-----------------------------------------------------------------------------PLL_P | 2 *-----------------------------------------------------------------------------PLL_Q | 7 *-----------------------------------------------------------------------------PLLI2S_N | 384 *-----------------------------------------------------------------------------PLLI2S_R | 2 *-----------------------------------------------------------------------------I2S input clock(Hz) | 192000000|To achieve the following I2S config: |- Master clock output (MCKO): OFF |- Frame wide : 16bit |- Audio sampling freq (KHz) : 48 |- Error % : 0.0000 |- Prescaler Odd factor (ODD): 1 |- Linear prescaler (DIV) : 62 | *-----------------------------------------------------------------------------VDD(V) | 3.3 *-----------------------------------------------------------------------------Main regulator output voltage | Scale1 mode *-----------------------------------------------------------------------------Flash Latency(WS) | 5 *-----------------------------------------------------------------------------Prefetch Buffer | OFF *-----------------------------------------------------------------------------Instruction cache | ON *-----------------------------------------------------------------------------Data cache | ON *-----------------------------------------------------------------------------Require 48MHz for USB OTG FS, | EnabledSDIO and RNG clock | *-----------------------------------------------------------------------------2015-11-05 05:09 AM
2015-11-07 12:58 PM
> I don't have oscilloscope but with systick and a counter inside the i2s interrupt I realize that the frequency isn't the same than 48KHz * 64,
That does not necessarily mean that the I2S data clock is not 48kHz - you may do too much processing in there. JW2015-11-09 09:40 AM
How do you transfer data to PC through USB? Is it USB Audio class device, and you realize USB mike, or ''USB'' means ''USB flash stick'' and Discovery acts as USB host?
AFAIK, in the original demo, Discovery writes PCM data into the *.wav file. This means that you should correct RIFF header.2015-11-09 09:42 AM
Please attach a full code of project to let me test.
2015-11-13 12:36 PM
2015-11-13 02:07 PM
> I'm not processing anything, only receive and sending the samples to PC.
How? > The i2s overrun condition is not happening. What is I2S overrun condition and how do you know it is not happening? JW2015-11-13 03:44 PM
Hello, when I say it is not processing anything I mean that only receive the data in the spi_irqHandler, and then when reach a certain amount the data is converted to PCM, a flag is set for sending data in main code to the PC.
The overrun flag is set when ''data are received and the previous data Have not been yet read from SPI_DR. As a result, the incoming data are lost''. This is not happening because I checked the flag and it is not activated. Thanks for reply2015-11-15 07:21 AM
Why don't use USB audio class instead of CDC? With usual terminal program (putty), I've seen so many chars, it's a huge flow of symbols, not a sound... Heh. I couldn't run your program for Windows (my system is Win 8.1 x64) to write wave file from received data. It crashed with error, even rebuilt from sources. Do I need some dll?
Besides this, the MCU program flow needs to be much refactored. I mean usage of DMA and process with PDM filter relatively large blocks of data. With USB audio class, standard data block carries 1 msec of audio data, so, 96 bytes (48 samples of 16 bit/sample) after filter. Such approach will reduce data moving overhead very much. Maybe this is the reason of glitches in audio. Please look at the attachment. It's working project of USB audio microphone. But now it's only 16k. Look at its structure and data flow. Maybe you'll get much success. ________________ Attachments : stm32_usb_mic.zip : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0mq&d=%2Fa%2F0X0000000bde%2FZTQmEGT6dhEVErY157uTiL8cAH9TmiqeCJKYlgF3Pts&asPdf=false2015-11-15 10:44 AM
Hello, CDC for this proyect works good until now except for 48KHz and i'm not sure that this is the issue. The goal is not usb comunication, is PDM at 48khz or other frequency higher than 16KHz. That only the way to probe it.
The crach in the pc app is maybe the path where the .wav are saved. In Terminal.cs, at the begining you must change these lines: static int sampleFrequency = 16000; //16000 //Change here static string allPath = @''F:\Dowloads\SerialPortTerminal\SerialPortTerminal''; //here tooThe path can be the place where you have the project, the sample frequency depends on the stm32f4 configuration. The real problem, I think, is that I cant figure out the proper configuration of the pll and i2sClock and Fs in i2s module for 48KHz sample frequency (or other, I treaty with 30Khz, 44.1KHz). In the PDM application note they say that for PDM the I2S CLK must be : decimation (64 or 80) * audioSampleFrequency. Addicional, in datasheet the say that you must double the frequency because there are two channels. But I'm missing something here. I will check your proyect now. Thanks for the help.