2023-09-05 07:12 PM - edited 2023-09-05 07:13 PM
Hello,
I took over working code that I have for a DSP project from an STM32F439 to an STM32H743. On the F4 I had the external audio codec connected to the I2S peripheral but switched over to the SAI on the H7. Now I am experiencing excessive noise when reading sample data from a buffer in the SRAM. To illustrate the problem I wrote a simple test program that I attached.
Both the left and right channel are doing basic audio feed-through. The only difference is that the right channel is using data from a larger buffer in the SRAM with a 256 sample buffer index delay (like in the actual DSP application, where it would read processed data).
The left channel looks just fine on the audio analyzer:
Right channel is showing a lot more noise:
It has to be a software problem, because when I use direct audio feed-through inside the DMA ISR same as with the left channel, the right channel looks fine as well. When I read the buffer in the SRAM with the left channel, the left channel is showing the same weird behavior. Both instruction and data cache as well as the MPU are disabled.
void HAL_SAI_RxHalfCpltCallback(SAI_HandleTypeDef *hsai)
{
// Convert and copy the input sample to the RxBuffer
int32_t lSample = (rxBuffer[0]) | (rxBuffer[1]<<16);
lSample |= (rxBuffer[1] & 0x80) ? 0xFF000000 : 0x00000000;
int32_t rSample = (rxBuffer[2]) | (rxBuffer[3]<<16);
rSample |= (rxBuffer[3] & 0x80) ? 0xFF000000 : 0x00000000;
float_t fSampleL = (float_t)lSample * fScale24;
float_t fSampleR = (float_t)rSample * fScale24;
audioInputBuffer[currentInputBufferIndex] = fSampleL;
audioOutputBuffer[currentInputBufferIndex] = audioInputBuffer[currentInputBufferIndex];
fSampleR = audioOutputBuffer[currentOutputBufferIndex];
//Convert and copy the output sample to the TxBuffer
int outSampleL = (int)(fSampleL*fMult24);
int outSampleR = (int)(fSampleR*fMult24);
txBuffer[0] = (outSampleL)&0xFFFF;
txBuffer[1] = (outSampleL>>16)&0xFF;
txBuffer[2] = (outSampleR)&0xFFFF;
txBuffer[3] = (outSampleR>>16)&0xFF;
currentInputBufferIndex = (currentInputBufferIndex + 1) % AUDIO_BUFFER_SIZE;
currentOutputBufferIndex = (currentOutputBufferIndex + 1) % AUDIO_BUFFER_SIZE;
}
void HAL_SAI_RxCpltCallback(SAI_HandleTypeDef *hsai)
{
//Convert and copy the input sample to the RxBuffer
int32_t lSample = (rxBuffer[4]) | (rxBuffer[5]<<16);
lSample |= (rxBuffer[5] & 0x80) ? 0xFF000000 : 0x00000000;
int32_t rSample = (rxBuffer[6]) | (rxBuffer[7]<<16);
rSample |= (rxBuffer[7] & 0x80) ? 0xFF000000 : 0x00000000;
float_t fSampleL = (float_t)lSample * fScale24;
float_t fSampleR = (float_t)rSample * fScale24;
audioInputBuffer[currentInputBufferIndex] = fSampleL;
audioOutputBuffer[currentInputBufferIndex] = audioInputBuffer[currentInputBufferIndex];
fSampleR = audioOutputBuffer[currentOutputBufferIndex];
//Convert and copy the output sample to the TxBuffer
int outSampleL = (int)(fSampleL*fMult24);
int outSampleR = (int)(fSampleR*fMult24);
txBuffer[4] = (outSampleL)&0xFFFF;
txBuffer[5] = (outSampleL>>16)&0xFF;
txBuffer[6] = (outSampleR)&0xFFFF;
txBuffer[7] = (outSampleR>>16)&0xFF;
currentInputBufferIndex = (currentInputBufferIndex + 1) % AUDIO_BUFFER_SIZE;
currentOutputBufferIndex = (currentOutputBufferIndex + 1) % AUDIO_BUFFER_SIZE;
}
Does anyone have an idea what I am doing wrong?
Thanks!
2023-09-06 10:52 AM
Update:
I tested the exact same code on the STM32F439 with the SAI configured exactly the same way. Works perfectly fine, no issues. Therefore, I assume that this is definitely not a hardware issue and that the SAI is configured correctly.