cancel
Showing results for 
Search instead for 
Did you mean: 

Cannot make SPDIF TX work

nt2ds
Associate II

Hello, I have made my own STM32F732RET6 board that has a USB FS configured as a Stereo, 24 bit 48kHz Audio Interface and SAI2 connected to an SN75176BDR (Differential Bus Transceiver IC) because I want to transmit AES3-AES/EBU (same as SPDIF) audio data coming from the USB). So USB Audio -> SPDIF TX. Problem is I cannot get it to work right and ALWAYS get noise in the output signal (I have hardware that can receive AES3-AES/EBU so I can hear it). I am suspecting this is a clock issue where the frequency of the SPDIF signal is not high enough to support 24 bit stereo 48kHz, from what I've read, this would require an SPDIF frequency of 6.144MHz. I don't know if "Real Audio Frequency" in the .ioc "Pinout & Configuration" on SAI2 should match this number (6.144MHz) or the Audio Sampling Frequency (48kHz). I cannot get "Real Audio Frequency" to be exactly 6.144MHz. I don't know if this should be the frequency the the SAI2 Clock Mux outputs (SAI2 Clock Mux sources from PLLSAI1), I can get PLLSAI1 to output a frequency of 6.144MHz but then "Real Audio Frequency" becomes 96kHz (and when this happens I get no sound, no noise, absolutely nothing on the receiving end). When I get PLLSAI1 to output 192MHz, the signal I get contains some of the audio I sent from the computer (USB) but it is mostly noise. Is there any way that SAI2 works exactly on the frequency provided by PLLSAI1 (through the MUX) without a divider? I read an older post about CubeMX generating wrong Clock Config for SPDIF but I really cannot understand how he fixes it. He also mentions something about having to set the Audio Frequency to 96kHz in order to get working 48kHz but further than that I don't understand.
Below I have posted some files so you can hear what it sound like currently.
Code Below, this is from the usbd_audio_if.c file that sends the USB Audio Data to the SAI DMA, no other function has been touched besides AUDIO_PeriodicTC_FS

 

static int8_t AUDIO_PeriodicTC_FS(uint8_t *pbuf, uint32_t size, uint8_t cmd)
{
    if (cmd == AUDIO_OUT_TC)
    {
        // Convert USB 24-bit stereo to SPDIF 32-bit frames
    	uint32_t *dst = sai_tx_buffer;
        uint8_t *src=pbuf;

        for (uint32_t i = 0; i < size / 3; i++)
        {
            // USB is 24-bit little endian: LSB, Mid, MSB
            uint32_t sample = (uint32_t)(src[0] | (src[1] << 8) | (src[2] << 16));

            // Sign extend from 24 to 32 bits

            if (sample & 0x800000){
            	sample = sample | 0xFF000000;
            }



            *dst++ = (uint32_t)sample;

            src += 3;
        }

        // Now transmit aligned SPDIF words
        HAL_SAI_Transmit_DMA(&hsai_BlockB2, (uint8_t*)sai_tx_buffer, size / 3);
    }
    return USBD_OK;
}

Below are both clock configurations, the first with the 3MHz Real Audio Frequency is the one the merely works. Second is the one with 48kHz Real Audio Frequency, matching the Audio Frequency which gives no audio.

192MHz Clock, 3MHz Real Audio Frequency192MHz Clock, 3MHz Real Audio Frequency3.072 Clock, 48kHz Real Audio Frequency3.072 Clock, 48kHz Real Audio Frequency
I've been dealing with this for days, any help would be greatly appreciated, Thanks in Advance! :)

8 REPLIES 8
FBL
ST Employee

Hi @nt2ds 

Did you use CubeMX to generate your code? If so, would you attach ioc file to check PLLSAI1 dividers?

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.


nt2ds
Associate II

I used CubeMX inside CubeIDE to generate the code. The .ioc I have attached now is with the default generated clock for SAI2 which gives a Real Audio Frequency of 3MHz, SAI2 target settings are 48kHz Audio Frequency, 24bit, Stereo, I don't know what Real Audio Frequency should be. I have read an older post about the code generation using a MCLKDIV on SAI but I didn't really understand, I also read that in order to get proper 48kHz I have to set Audio Frequency to 96kHz. Anyway, in the .ioc I have attached I have set the SAI2 settings to how it should be working under ideal conditions.

FBL
ST Employee

Hi @nt2ds 

While I’m not audio expert, I suggest you adjusting PLLSAIN to 246 instead of 240 ( PeriphClkInitStruct.PLLSAI.PLLSAIN = 246; in your code). This change should yield a clock frequency around 196.8 MHz, which can help generate a more accurate SPDIF bit clock close to 3.075 MHz.

FBL_0-1755856860406.png

 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.


nt2ds
Associate II

I did try that but I will again in case I missed something the first time and will keep you updated!

nt2ds
Associate II

Hello, sorry for the later answer (I work in PA that why I need this kind of thing and was working those days). I did try that and it is noticeably better but still the problem persists. What happens with this clock now is some packets are played distorted while very few packets after a short period of time are played properly and this repeats. I cannot measure how often this happens but when I hear something (distorted) for a second or two, for a short moment it plays properly which indicates a clock problem because they are received in a wrong time. I have attached a .wav file which plays a song so you can listen the problem yourself (for reference the song is Tiesto - The Motto). On this post I see he feeds the SAI Clock with the a Frequency the Real Audio Frequency must be (feeding 3.072MHz to SAI, not a bigger value like in my case). I also read that he tells about problems with MCKDIV. What is MCKDIV? From what I understand it is a divider on SAI2 PLL that is not visible on the Clock Configuration windows. Can I make that MCKDIV 1 so that the clock I feed into SAI2 PLL is not divided? 

LCE
Principal II

This sounds more like a buffering and / or sync problem.

Maybe you should build an intermediate buffer which collects some more USB data before going to SPDIF.

And for better clock accuracy, use the extra audio clock input - if this F7 has that. With probably 24.576 MHz (for 48 / 96 kHz).

 

nt2ds
Associate II

I know its not a buffer issue. If it were a buffer issue there would be spaces between audio not noise and distortion. Did you listen to the files I posted on the first message?

LCE
Principal II

Okay.

I cannot download these files at work.

Have you used some "test signals" which are easier to identify when looking at raw data or raw wav files?

I recommend using digitally created simple signals like a sawtooth (+1 LSB per sample) or a square wave (+-FS). Then check the SPDIF output and all the buffers inbetween.