Audio I2S and Raspberry - Page 1
Forum section: electronics / general

author: luiHS
author_rank: Frequent Contributor
timestamp: May 15, 2021, 08:25:02 am
content: Hi.I am developing a product, which uses an RT1064 microcontroller for process tasks, and a Raspberry for playing video and audio.As a sound amplifier I have chosen a STA369 chip that integrates 4 channels configurable as simple or bridged. I will use a 3 channel setup, two simple and one bridged. This chip is controlled by I2C, and the audio input is digital by I2S.I have not done anything before using I2S, and the question is whether it is a standard port that can be used with any I2S device. It would be a matter of connecting the I2S output of Raspberry to STA369 and configuring the Raspberry so that the sound is output through I2S.

author: gamalot
author_rank: Super Contributor
timestamp: May 15, 2021, 11:17:39 am
content: IIRC, There is no MCLK output on the I2S interface of the Raspberry Pi.

author: Jacon
author_rank: Regular Contributor
timestamp: May 15, 2021, 01:10:13 pm
content: Quote from: gamalot on May 15, 2021, 11:17:39 amSo, would you explain, how those countless HiFi I/O HATs for RasPi are working ?For example, this one:https://www.hifiberry.com/docs/data-sheets/datasheet-dac-adc-pro/

author: luiHS
author_rank: Frequent Contributor
timestamp: May 15, 2021, 01:30:49 pm
content: Quote from: gamalot on May 15, 2021, 11:17:39 amThanks, I've been looking for information on that.My question now is if that clock signal MCLK can be injected externally with an oscillator. I have seen schematics with the PCM5121 DAC for Raspberry, which use external oscillators by switching between two of them based on the sampling frequency.In the STA369 the external clock frequency provided to the XTI pin must be a multiple of the input sampling frequency (fs).According with the datasheet table may be these 6 values for sound sampled to 44.1Hz and 48Khz:576*fs, 128*fs, 256*fs, 384*fs, 512*fs, 768*fsThis means, for example, for 256*fs,11.2896 Mhzfor a sound sampled at 44.1Khz and12.288 Mhzfor a sound sampled at 48Khz. Both oscillators available from Mouser.https://www.mouser.es/ProductDetail/Epson-Timing/SG-8018CE-122880M-TJHPA3?qs=%252B6g0mu59x7IJ%252BhMyNAL4Rw%3D%3Dhttps://www.mouser.es/ProductDetail/Epson-Timing/SG-8018CE-112896M-TJHPA3?qs=OTrKUuiFdkahoS5c5bbz0w%3D%3D

author: Jacon
author_rank: Regular Contributor
timestamp: May 15, 2021, 03:31:01 pm
content: This thread:https://www.raspberrypi.org/forums/viewtopic.php?t=8496may be helpfull...

author: SiliconWizard
author_rank: Super Contributor
timestamp: May 15, 2021, 07:11:19 pm
content: Quote from: luiHS on May 15, 2021, 01:30:49 pmNope. Not that I know of. But I may be missing something. Most DACs out there won't take a MCLK which is not synchronized with the BCLK (bit clock), and conversely, I don't think there is any clock input you can use on the RPi as a MCLK for I2S.There are DACs, such as the PCM5102A (https://www.ti.com/product/PCM5102A), which can generate MCLK internally (with an integrated PLL) from BCLK. Those can be used directly with RPi's without any nasty trick involved.

author: luiHS
author_rank: Frequent Contributor
timestamp: May 15, 2021, 07:58:19 pm
content: Quote from: SiliconWizard on May 15, 2021, 07:11:19 pmThank you, something like that was afraid, that the clock signal can not be independent of other clock signals. This is a problem then, to be able to connect 4-wire I2S devices to a Raspberry.I will have to use a PCM5100 or PCM5122 DAC (PCM5102 is out of stock everywhere), and amplifiers with analog input. It's a shame because the STA639 has volume control, tone control, 8 EQ filters per channel and 4 configurable amplifiers, controlled by I2C.Another option is to add a CS8421 chip, which generates the MCLK signal from 3-wire I2S, but it is a somewhat expensive chip.

author: oPossum
author_rank: Super Contributor
timestamp: May 15, 2021, 10:28:02 pm
content: I2S can be configured to operate in slave mode and use an externally supplied SCLK (BCLK). This can be derived from an external MCLK so everything stays in sync.An example using a CPLD:https://alexxdiy.blogspot.com/2020/05/high-quality-raspberri-pi-i2s-hat-part.html

author: technix
author_rank: Super Contributor
timestamp: May 16, 2021, 11:16:45 am
content: Raspberry Pi I2S interface have no MCLK, instead the external chips are supposed to source MCLK from a separate oscillator.

author: SiliconWizard
author_rank: Super Contributor
timestamp: May 16, 2021, 05:34:52 pm
content: Quote from: luiHS on May 15, 2021, 07:58:19 pmOne option would be to do just what the PCM51xx do internally: add a PLL. So you could use any I2S-compatible chip, including the STA639, with an additional PLL.Use the BCLK as input for the PLL, and set it to generate a synchronized MCLK - typical MCLK would be 256xFs, for instance, so if your I2S data stream is a typical 64 bits per frame (thus BCLK = 64xFs), you'll need to set up a 4x multplier for the PLL. You get the idea. Tons of PLL ICs out there.

author: luiHS
author_rank: Frequent Contributor
timestamp: May 17, 2021, 03:37:30 am
content: Quote from: SiliconWizard on May 16, 2021, 05:34:52 pmOk thanks I will try ICS502, 511 or 512 to multiply BCLK x4 and get MCLK in sync.

author: Bassman59
author_rank: Super Contributor
timestamp: May 17, 2021, 04:11:30 pm
content: Quote from: technix on May 16, 2021, 11:16:45 amI haven't looked, but I assume that the Raspberry Pi's I2S ports can be configured to work in slave mode (BCLK, LRCLK as inputs), and it's a simple matter to build a "hat" with a 24.576 MHz oscillator for the modulator clock and picking converters which can divide MCLK down to BCLK and LRCLK internally and provide them as outputs.There's a separate issue of implementing support for more than one sample frequency, and to support the 44.1 kHz family as well as the 48 kHz family you need two oscillators.Or you can use converters which can take in a reference clock like 27 MHz and will synthesize the correct MCLK and divide that down to BCLK or LRCLK. It all depends on the goals.

author: technix
author_rank: Super Contributor
timestamp: May 17, 2021, 04:24:30 pm
content: Quote from: Bassman59 on May 17, 2021, 04:11:30 pmYou may be able to get away with running Pi I2S in master mode and run the codec on an independent clock. The internal I2S PLL for the Pi is reasonably good IMO. My WM8978G board runs in that mode and it works good enough in an non-trivial playback test (shairport-sync streaming audio from my iPhone, test song is Daft Punk'sGet Luckyfrom Apple Music for its dynamic range, and it works reasonably well.)Quote from: Bassman59 on May 17, 2021, 04:11:30 pmThis is where small FPGAs can shine, converting an virtually arbitrary input frequency into any output frequency you like using its internal PLL, accessed over I2C.

author: luiHS
author_rank: Frequent Contributor
timestamp: May 18, 2021, 02:00:50 am
content: Finally, I will use a TAS5805M, this is also I2S but it doesn't need MCLK signal. It is only two channel but cheaper and at the end of the datasheet there is a beautiful scheme to create a fully digital 2.1 amplifier controlled by I2C.I also found information from people who have already used it to connect to Raspberry by I2S, with the start commands to send by I2C from a C program.https://www.diyaudio.com/forums/class-d/335099-tas5825m-based-amp.htmlI would have liked to try the multiplier PLL thing to create the MCLK signal for the STA369, or to try a DAC like the PCM5100, but the priority is to design the circuit with the least complication and loss of time. I knew almost nothing about I2S, but I see that it is not that simple, at least not with a Raspberry as it lacks the MCLK signal.

author: SiliconWizard
author_rank: Super Contributor
timestamp: May 19, 2021, 05:14:32 pm
content: Well, the TAS5805M is a decent amplifier for what you want to do. As can be read in its DS, "The device has an internal PLL that is used to take SCLK (Bit Clock) as reference clock and create the higher rate clocks required by the DSP and the DAC clock.", which is why an external MCLK is not needed. Same as with the PCM5102A and such.As to the RPi, it's pretty common for SBCs that their I2S signals do not include MCLK, neither as an output or as an input.The latter would require a hybrid master/slave mode, which is even less common on those SBCs. You can usually do this with MCUs supporting I2S, though. I have done this a few years ago with a dsPIC33 MCU, but there were pitfalls. If the external MCLK was not synchronized to the CPU clock, then you'd need to go through a code trick (software sync) in order for the I2S peripheral to start properly. That was bad. I dunno if the same pitfalls apply to other MCUs out there - but I'm suspecting so.