cancel
Showing results for 
Search instead for 
Did you mean: 

I2S in polling blocking mode STM32F4 Discovery Board, underrun but correct data on the logic analyser

PiterX
Associate

0693W00000ANGieQAH.pngDear All,

It is my first message here, so I would say hello to everyone.

I'm trying to send some audio signal through I2S bus on the discovery board using the onboard CS43L22 audio codec. I've configured the codec using I2C and I2S on SPI3 peripheral.

I want to attempt to use polling, just as a first step before use interrupts and then use DMA. As I thought it would be good learning exercise.

I tried to play a sine wave but the signal is horribly loud and distorted, so I had a step back and I'm trying to generate just to different numerical values to right and left channels. I'm using the standard Phillips I2S mode and most of the settings are left as default (such as 16bit length etc.)

I think it underruns as it if I set a breakpoint when I check for underruns it stops there.

So I would've thought that I would need to use interrupts and DMA because maybe CPU can't handle that, but what puzzles me that the message is decoded correctly in a logic analyser.

Below is my main while loop:

 while (1)

 {

 while(!((SPI3->SR&SPI_SR_TXE)&&((SPI3->SR&SPI_SR_CHSIDE)==0)))

 {}

//write left channel

SPI3->DR=0xabcd;

 //for right channel:

while(!((SPI3->SR&SPI_SR_TXE)&&(SPI3->SR&SPI_SR_CHSIDE)))

{}

SPI3->DR=0xffff;

  i++;

  if(i==number_of_samples)

  i=0;

if((SPI3->SR&SPI_SR_OVR))

{

//breakpoint

}

if((SPI3->SR&SPI_SR_UDR))

{

//breakpoint, it stops here during debugging

}

 }

Thank you in advance for any clues.

Many thanks,

Piotr Golacki

2 REPLIES 2
Simon V.
ST Employee

Hello,

I would recommend you to test the example included in the HAL library : Applications\Audio\Audio_playback_and_record.

This application shows how to use the different functionalities of Audio device CS43L22 

and ST MEMS microphone (MP45DT02) by switching between play and record audio

using USER button.

Please do not hesitate if you need further help,

Regards,

Simon

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

> I tried to play a sine wave but the signal is horribly loud and distorted

Probably you haven't set the codec through I2C to the same standard on its SDI as you transmit from STM32 (the codec is by default left-justified, see "DSP mode" in the codec's DS).

> So I would've thought that I would need to use interrupts and DMA because maybe CPU can't handle that

That depends on ratio between system clock and I2S clock, and compiler optimization used. I'd say it should be able to handle it in the simple "trial" case.

> while(!((SPI3->SR&SPI_SR_TXE)&&(SPI3->SR&SPI_SR_CHSIDE)))

You don't need to wait for CHSIDE to swap. You also wait for the same polarity of CHSIDE in both conditions, which is wrong, and is probably the cause of underrun.

> what puzzles me that the message is decoded correctly in a logic analyser.

That's strange indeed, possibly the I2S mode in SPI/I2S has more into it than the documentation in RM exposes.

JW