cancel
Showing results for 
Search instead for 
Did you mean: 

Audio noob question

Pavel A.
Evangelist III

Preparing for some audio related project....

If I understand correctly, I2S can transmit and receive simultaneously like SPI.

Is this mode used practically? Are there codecs that can simultaneously transmit via I2S (generate sound) and receive digitized data (from a mic)?

Or is this typically done using two separate I2S or SAI interfaces?

1 ACCEPTED SOLUTION

Accepted Solutions
LCE
Principal

Actually, I find that the SAIs, once you get used to them, belong to the best and most reliable parts of the STM32.

Here's my init functions for (H723/H735):

SAI1A: master receive

SAI1B: slave receive

SAI4A: slave transmit

SAI4B: TX SPDIF ("asynchronous")

The really nice thing about SAI / I2S: you can test that without a codec on a Nucleo or so:

simply connect the data output of the transmit SAI/I2S to the data input of the receive SAI/I2S.

It's really working great, and with double buffer mode you have all the time in the world...

View solution in original post

13 REPLIES 13
AScha.3
Chief III

> I2S can transmit and receive simultaneously like SPI

yep, it is a SPI, just a special mode (wordclk -> L/R CLK)

Most time is used just one direction, because a dac only receives data, or an adc only sending.

but also on codecs in and out can run same time, bacause record and playback is used same time.

SAI is imho just a more flexible version of I2S, for multichannel etc.

i have my audio streamer/player here, with H743 , using I2S and now SAI to the dac, because it is working better with 32bit data; with 16bit data stereo, I2S was same.

If you feel a post has answered your question, please click "Accept as Solution".

Thank you Alfred.

Do you know a codec that works in full duplex mode over one digital interface?

Hi Pavel,

Define "one digital interface".

For a simplex transfer, i.e. from mcu to DAC, or from ADC to mcu (and in I2S we are always talking about stereo, so it's technically a dual-DAC or dual-ADC, but nobody cares with this level of detail), you need a pair of clocks (bit clock (BCK, CLK, the marking varies, the Philips's official I2S spec surely has some, I am lazy to look it up, and other manufacturers merrily ignore the spec as usually) and a framing clock (left-right clock LRCK, word-select clock WSCK etc.), plus one data line (sometimes marked as DI/DO for data in/out, or SI/SO serial in/out). Some ADC/DAC require an additional master clock, some can generate it internally, some can generate all clocks, leaving the mcu in the role of slave. Some ADC/DAC chips also have a "side-channel", e.g. I2C, through which you configure the innards of ADC/DAC e.g. gain or filters; simpler ones are fixed or have only a few settings selected simply by logic levels on dedicated pins.

CODECs, i.e. chips which integrate ADC with DAC, feature duplex transfer, and have basically two data lines with only one shared pair of clocks.

The SPI/I2S module in STM32 features a simplex I2S (maybe except 'H7, the SPI there is a beast and I am not interested in it). Some STM32 like the classic 'F4xx (e.g. 'F407) have on paper duplex I2S, but that's in fact accomplished by an extra stripped-down module marked SPI_Ex, i.e. it has removed SPI and master functions, so it's I2S slave only, which has the clocks internally connected to its respective full-fledged SPI/I2S module.

The SAI module consists from a couple of simplex units, but its two halves can be synchronized internally, making it effectively duplex.

JW

Jan,

My customer wants to use a codec like this one based on Renesas DA7212.

Both to produce sound and receive & encode aux input (or a mic, not sure yet), simultaneously.

The chip has only one digital interface (plus I2C config i/f).

From the data sheet I cannot figure whether it can do this. The Renesas support is not quick to answer.

Assuming the codec can do this, next comes the choice of a SAI vs I2S

and a suitable STM32 (available to buy).

Yes, it can.

For that DA7212 the I2S clocks are:

BCLK = bit clock for serial interface, with f = 2 * bit resolution * sample rate

WCLK = sample rate

Check if that one needs a MCLK = master clock, which usually drives the sigma-delta sampling.

I prefer the names "SCLK" (serial) and "LRCK" (left-right, because when WCLK = 1 left channel is transferred, right when 0).

The codecs and the MCU interfaces are made for this purpose.

I'm working on that for some time now, and my experience with F767 and H723/H735 is that the SAIs are easier to synchronize, because you can "wire" the clocks internally, compared to the I2S interfaces (or have I overseen anything).

So take a SAI which is made up of 2 sub-parts called blocks.

Set one block as master, the other one as slave to "SAI within same block" or so.

Then you only use the clock GPIOs from the master + master's data + slave's data.

One problem you might encounter:

Most STM32 have a fixed oversampling rate (256?), which is MCLK / WCLK.

That s***s...

But you can work around that by using another I2S and use it only as MCLK.

LCE
Principal

Actually, I find that the SAIs, once you get used to them, belong to the best and most reliable parts of the STM32.

Here's my init functions for (H723/H735):

SAI1A: master receive

SAI1B: slave receive

SAI4A: slave transmit

SAI4B: TX SPDIF ("asynchronous")

The really nice thing about SAI / I2S: you can test that without a codec on a Nucleo or so:

simply connect the data output of the transmit SAI/I2S to the data input of the receive SAI/I2S.

It's really working great, and with double buffer mode you have all the time in the world...

> Both to produce sound and receive & encode aux input (or a mic, not sure yet), simultaneously.

As LCE said above, yes, this is what CODECs do.

0693W00000aIYNTQA4.pngJW

> But you can work around that by using another I2S and use it only as MCLK.

You don't need another I2S for this, most of the times a timer would do. MCK usually doesn't need to be in any particular phase arrangement with BCK/LRCK.

JW

There are codecs on various STM32 demo boards (DISCO, EVAL), so maybe they and the associated code can be used for quick inspiration.

JW