2025-01-11 03:12 AM
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 */
}