2021-12-22 05:13 AM
MCU: STM32F446VE
STM32CubeIDE 1.7.0 with CubeMX 6.3.0
SAI1 block A is configured in SPDIF TX mode, clock audio frequency is set to 44.1KHz.
SAI Clock is approximately 2.823529 MHz. HSE freq is 12MHz.
As you can see on screenshot "Real Audio Frequency" almost match selected "Audio Frequency".
The following init code was generated by CubeIDE for periph clocks and SAI:
/** Initializes the peripherals clock
*/
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_SAI1|RCC_PERIPHCLK_SDIO
|RCC_PERIPHCLK_CLK48;
PeriphClkInitStruct.PLLSAI.PLLSAIM = 6;
PeriphClkInitStruct.PLLSAI.PLLSAIN = 96;
PeriphClkInitStruct.PLLSAI.PLLSAIQ = 4;
PeriphClkInitStruct.PLLSAI.PLLSAIP = RCC_PLLSAIP_DIV4;
PeriphClkInitStruct.PLLSAIDivQ = 17;
PeriphClkInitStruct.Clk48ClockSelection = RCC_CLK48CLKSOURCE_PLLSAIP;
PeriphClkInitStruct.SdioClockSelection = RCC_SDIOCLKSOURCE_CLK48;
PeriphClkInitStruct.Sai1ClockSelection = RCC_SAI1CLKSOURCE_PLLSAI;
/* USER CODE END SAI1_Init 1 */
hsai_BlockA1.Instance = SAI1_Block_A;
hsai_BlockA1.Init.Protocol = SAI_SPDIF_PROTOCOL;
hsai_BlockA1.Init.AudioMode = SAI_MODEMASTER_TX;
hsai_BlockA1.Init.Synchro = SAI_ASYNCHRONOUS;
hsai_BlockA1.Init.OutputDrive = SAI_OUTPUTDRIVE_DISABLE;
hsai_BlockA1.Init.FIFOThreshold = SAI_FIFOTHRESHOLD_EMPTY;
hsai_BlockA1.Init.AudioFrequency = SAI_AUDIO_FREQUENCY_44K;
hsai_BlockA1.Init.MonoStereoMode = SAI_STEREOMODE;
hsai_BlockA1.Init.CompandingMode = SAI_NOCOMPANDING;
if (HAL_SAI_Init(&hsai_BlockA1) != HAL_OK)
{
Error_Handler();
}
When I tried to send some samples to SAI, the output bit clock was very wrong, around 240kHz, almost 10 times lower than it should be.
Is this a normal behavior?
Finally I ended up with configuration where SAI clock is set to "5.647059" MHz, and these lines are added to SAI init function:
hsai_BlockA1.Init.NoDivider=SAI_MASTERDIVIDER_DISABLE;
if (HAL_SAI_Init(&hsai_BlockA1) != HAL_OK)
{
Error_Handler();
}
What is the right way to configure SAI in SPDIF Tx mode in CubeMX for 44.1KHz?
2024-02-07 12:16 PM
Same for me, this little line
hsai_BlockA1.Init.NoDivider=SAI_MASTERDIVIDER_DISABLE;
was the difference between something random and proper sound.
Thanks.