cancel
Showing results for 
Search instead for 
Did you mean: 

i2s 44khz wav file not playing properly

eunoia
Associate

I am trying to read a file from sdcard using fatfs and sdio and output it to i2s
8khz, 16bit is output well
But 44khz, 16bit file is not played properly

The mcu is using stm32h743
dma is using circular mode and the source is below
Thank you

#define AUDIO_BUFFER_SIZE 2048
uint16_t audioBuffer[AUDIO_BUFFER_SIZE];

void playWaveFileDmaCircularMode(char * filename)
{
	UINT bytesTransferred;
	UINT bytesRead;

	res = f_open(&file, filename, FA_READ);
	res = f_read(&file, &wavHeader, sizeof(WAV_Header), &bytesTransferred);
	while (1) {

		res = f_read(&file, audioBuffer, sizeof(audioBuffer), &bytesRead);
		if (res != FR_OK || bytesRead == 0)
		{
			HAL_I2S_DMAStop(&hi2s2);
			break;
		}
		HAL_I2S_Transmit_DMA(&hi2s2, audioBuffer, AUDIO_BUFFER_SIZE);
	}
}


static void MX_I2S2_Init(void)
{

  /* USER CODE BEGIN I2S2_Init 0 */

  /* USER CODE END I2S2_Init 0 */

  /* USER CODE BEGIN I2S2_Init 1 */

  /* USER CODE END I2S2_Init 1 */
  hi2s2.Instance = SPI2;
  hi2s2.Init.Mode = I2S_MODE_MASTER_TX;
  hi2s2.Init.Standard = I2S_STANDARD_PHILIPS;
  hi2s2.Init.DataFormat = I2S_DATAFORMAT_16B;
  hi2s2.Init.MCLKOutput = I2S_MCLKOUTPUT_DISABLE;
  hi2s2.Init.AudioFreq = I2S_AUDIOFREQ_44K;
  hi2s2.Init.CPOL = I2S_CPOL_LOW;
  hi2s2.Init.FirstBit = I2S_FIRSTBIT_MSB;
  hi2s2.Init.WSInversion = I2S_WS_INVERSION_DISABLE;
  hi2s2.Init.Data24BitAlignment = I2S_DATA_24BIT_ALIGNMENT_RIGHT;
  hi2s2.Init.MasterKeepIOState = I2S_MASTER_KEEP_IO_STATE_DISABLE;
  if (HAL_I2S_Init(&hi2s2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN I2S2_Init 2 */


  /* USER CODE END I2S2_Init 2 */

}

 

1 REPLY 1
AScha.3
Chief III

I made an audio player, data from SD or USB stick, plays wav, flac, mp3 up to 192kHz, with H743 (as you!).

You do something very wrong...

reading 2KB from sd/file and send to I2S /dac. This is NOT circular and always has to wait for the sd-card (sdcard needs about 1...5ms to respond to a command, so cannot work for continuous data stream.)

 

You have to use DMA in circular mode...see other thread:

https://community.st.com/t5/stm32-mcus-products/hal-i2s-txcpltcallback-is-called-only-once/m-p/760714#M270372

 

+ dont make multi treads with same problem.

If you feel a post has answered your question, please click "Accept as Solution".