Syncing SAI and DMA for I2S
Hello,
I have a STM32U585. I’m trying to use SAI to transmit and receive I2S audio as a slave. The I2S master is an external codec that’s configured for stereo 16bit 16kHz audio. Mic/input from codec->MCU and speaker/output from MCU->codec. The first stereo sample received on the codec is routed to the left speaker and second to the right.
This works as expected for the mic. For the speakers, sometimes even samples will play on the right side and odd on the left. I’ve loaded the DMA buffer with dummy values 0xAAAA and 0x0000 to demonstrate. MISO is the signal of interest here. I’d like 0xAAAA to be on Ch 1 consistently.


This occurred about 50% of the time. It’s observable for the entire runtime and doesn’t correlate with how long the software has been running. I suspected a sync issue SAI/DMA so I implemented a quick hack to poll the GPIO to start the DMA transfer after the falling edge of WS to help sync. This improves reliability from 50% to 90%. I’ve included a simplified snippet of the code.
HAL_SAI_Abort(rx);
HAL_SAI_Abort(tx);
while (HAL_GPIO_ReadPin(WS_PORT, WS_PIN) == GPIO_PIN_RESET);
while (HAL_GPIO_ReadPin(WS_PORT, WS_PIN) == GPIO_PIN_SET);
HAL_SAI_Receive_DMA(rx, rx_data, sizeof(rx_data));
HAL_SAI_Transmit_DMA(tx, tx_data, sizeof(tx_data));
I am using SAI1 block A for async Tx and SAI1 block B for sync Rx both with DMA.


Appreciate your help, thanks.
