cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 Discovery I2S frequency configuration problem

em3ly
Associate II
Posted on November 04, 2015 at 12:51

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 50

The 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 interrupt

SPI_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, | Enabled

SDIO and RNG clock | *-----------------------------------------------------------------------------
9 REPLIES 9
em3ly
Associate II
Posted on November 05, 2015 at 14:09

Can anybody give me some ideas? 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, according with the PDM software decoding datasheet

Posted on November 07, 2015 at 21:58

> 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.

JW

romanetz4
Associate II
Posted on November 09, 2015 at 18:40

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.

romanetz4
Associate II
Posted on November 09, 2015 at 18:42

Please attach a full code of project to let me test.

em3ly
Associate II
Posted on November 13, 2015 at 21:36

Hello pechenko.roman and waclawek.jan.  I'm still with the same problem but I dont saw the post until today.  The  stm32f4 usb is working as a cdc device. In the PC an app  gets the samples and make a .wav. I'm not processing anything, only receive and sending  the samples to PC.The i2s overrun condition is not happening. I found the original code in https://github.com/Anon1mo/STM32F4-WAVE-RECORDER.   I modified some parameters in RCC and i2s configuration. It work with 16Kz but not with 48KHz, With 48KHz happens what I explain in the first post. The stm32f4 proyect is in cocoox ant the pc app in visual studio 2010. The poyect is here:  https://github.com/em3ly/pdm_i2s_48khz_problem.git.

Thank you both for your answers

Posted on November 13, 2015 at 23:07

> 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?

JW

em3ly
Associate II
Posted on November 14, 2015 at 00:44

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 reply

romanetz4
Associate II
Posted on November 15, 2015 at 16:21

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=false
em3ly
Associate II
Posted on November 15, 2015 at 19:44

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 too

The 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.