cancel
Showing results for 
Search instead for 
Did you mean: 

I2S doesn't work properly on STM32F407

Lev Shkatov
Associate
Posted on May 20, 2017 at 09:54

I'm trying to launch I2S on STM32F407. Clocks work fine, WS is what I want (8k, 44.1k, etc), CK is 32*WS, MCK is 256*WS. The problem is that data is transfered at a lower speed (twice lower). I.e I send 1000 bytes during 62ms, but it should take 31ms (at 8k sample rate).

Examples of code:

&sharpdefine WAV_BUFFER_SIZE 1000

uint8_t wavReadBuffer[WAV_BUFFER_SIZE];

for(uint16_t i = 0; i < WAV_BUFFER_SIZE; i++){

   wavReadBuffer[i] = 0xff;

}

HAL_I2S_Transmit(&hi2s2, (uint16_t*)wavReadBuffer, WAV_BUFFER_SIZE, 5000);

static void MX_I2S2_Init(void)

{

   hi2s2.Instance = SPI2;

   hi2s2.Init.Mode = I2S_MODE_MASTER_TX;

   hi2s2.Init.Standard = I2S_STANDARD_MSB;

   hi2s2.Init.DataFormat = I2S_DATAFORMAT_16B;

   hi2s2.Init.MCLKOutput = I2S_MCLKOUTPUT_ENABLE;

   hi2s2.Init.AudioFreq = ((uint32_t)8000U);

   hi2s2.Init.CPOL = I2S_CPOL_LOW;

   hi2s2.Init.ClockSource = I2S_CLOCK_PLL;

   hi2s2.Init.FullDuplexMode = I2S_FULLDUPLEXMODE_DISABLE;

   if (HAL_I2S_Init(&hi2s2) != HAL_OK){Error_Handler();}

}

#stm32f4 #i2s
1 REPLY 1
Posted on May 22, 2017 at 10:59

It's just the math: 8000 samples per second with 32 bits per sample ('CK is 32*WS') = 256000 bits per second. 1000 bytes is 8000 bits so it takes 8000/256000=31.25ms.

If it takes more, you either did not set CK to 32*WS, or the bit clock rate (CK) is not  set to 256000 bits per second as it should, maybe inproperly set I2S PLL or whatever is the clock source. Use a LA or oscilloscope to measure the real clock and WS rate. The settings are given by the content of I2S and RCC registers.

JW