0

RPi and I2S devices?
Moderators: adafruit_support_bill, adafruit

Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/
Please be positive and constructive with your questions and comments.

RPi and I2S devices?

by Priam on Mon Oct 08, 2018 2:09 pm

I've never used I2S before, total n00b and I've been surfing around the net trying to grok it. I'm looking for an answer for what I'd thought was an easy question, but can't find an example.

From what I've read, I2S seems to have two channels selected by the WS line. But how many devices can I put on the bus?

For example, what I'd like to do from an RPi is put an I2S MEMS microphone breakout (https://www.adafruit.com/product/3421) and an I2S Speaker Bonnet (https://www.adafruit.com/product/3346) together on the same board; the microphone would capture audio to a file on the RPi, and the speaker bonnet would be used to play it back (as well as other sound files).

But, the speaker bonnet seems to have two channels, and the microphone one channel...that's three channels...is that outside of what I2S can do? I wouldn't ever be recording at the same time as playing. So, could I have the microphone "offline" while using the bonnet, and the bonnet offline while using the microphone?

Any insight would be appreciated!

Thanks,
-Mike

Priam
 
Posts: 3
Joined: Sun Nov 12, 2017 12:44 am

Re: RPi and I2S devices?

by adafruit_support_mike on Wed Oct 10, 2018 4:54 am

The short answer is that you can connect two I2S devices to the RasPi.. sorta.

I2S is a clocked-data protocol like I2C or SPI: there's a data line and a bit clock line. The transmitter sets the data line when the bit clock goes low, and the receiver reads the data line when the bit clock goes high.

There's also the Word Select line, which does two things: it tells you which channel is active (low=left, high=right), but it also tells you the bit length of each sample. If WS toggles between high and low every 8 ticks of the bit clock, the bus is carrying 8-bit samples. You can also use 16, 24, and 32-bit words.

A word always starts when WS changes, so the channels are Time Domain Multiplexed (TDM).. one word from the left channel, one word from the right channel, another word from the left channel, and so on.

That's it for the I2S spec, but common practice has added a couple more signals.

You can connect multiple senders and receivers to an I2S bus, and keeping multiple clocks in sync is a nuisance. People have found it useful to create a Master Clock generated by a single device and received by all the other devices to keep everything in sync.

Then someone decided that if some TDM is good, even more TDM must be better. That led to the concepts of 'slots' and 'frames' and the Frame Sync signal.

Slots basically encode the idea of taking turns: slot 0 lasts long enough for one left-word and one right-word, then slot 1 has one left-word and one right-word, and so on. Devices identify their slots by counting ticks of the master clock from when the Frame Sync signal goes high.. devices for slot 0 use the first and second words worth of ticks, devices for slot 1 use the third and fourth words, and so on.

The RasPi doesn't support a Frame Select pin, so you only get the basic left/right channels. Only one I2S device can transmit the left word or the right word, but technically you could have a mic generating signals and an output amp listening to those signals at the same time.

The number of receivers for a given channel has no limit beyond the practical/electrical ones. If you wanted to pipe audio to a dozen speakers, you could use a dozen I2S amps all listening to the same channel.

adafruit_support_mike
 
Posts: 53554
Joined: Thu Feb 11, 2010 2:51 pm

Re: RPi and I2S devices?

by Priam on Wed Oct 10, 2018 5:18 am

That is actually a super-helpful description. I was having a hard time trying to put that together. Thank you for taking the time to put that into words.

So, if I understand right, because the RPi lacks a Frame Select pin, I can't really do this the proper way. But it will work if I set it up in sort-of the following way:

The RPi is always the master providing the clock for sync for itself, the MEMS mic, and the speaker bonnet.

In addition the RPi is a receiver when the MEMS mic is the sender. The MEMS mic can either be on the left or right channel (or both?) Additionally, I'd have to switch off the speaker bonnet amp when the mic is sending otherwise the amp will pick up the mic's stream and play it.

Then when the RPi is the sender (sending an audio file), the speaker bonnet would be the receiver and play the audio stream on both channels. And the mic would what? Need to be off? Or just wouldn't care because it's never a receiver?

Thanks again,
-Mike

Priam
 
Posts: 3
Joined: Sun Nov 12, 2017 12:44 am

Re: RPi and I2S devices?

by adafruit_support_mike on Thu Oct 11, 2018 1:51 am

A RasPi connected to an I2S bus can choose to send or receive on either channel, yes. It can receive data from an I2S mic on the left channel and send other audio data to a speaker on the right channel (or the speaker can be on the left and the mic on the right).

You can use enable pins to choose the devices that will be active on an I2S bus at any given time. Disabling a mic on the left channel would let the RasPi send data to a speaker on the left channel.

You do have to disable the mic though. It isn't a receiver, but it is a transmitter. If it's set to use the left channel, it will try to control the data line any time the Word Select signal is low. If the RasPi is also trying to control the data line at the same time, you'll get all sorts of unexpected behavior.

You connect multiple I2S mics to the left (or right) channel if they have enable pins, and only one is selected to control the data line at any given time.

Having a Frame Sync signal would let you define slots for create slots for the speakers and multiple mics, and would let you run all of them simultaneously, at least in the large sense. There would only be one transmitter controlling the data line at any moment, but each slot would have a device sending bits to the data line during its left and right words.

Trying to handle enabled/disabled mics on the RasPi might be a challenge depending on how the ALSA layer defines control of an I2S channel. Telling the ALSA layer there's a speaker on the left channel might make the OS say, "okay, the ALSA speaker driver owns the left channel at all times" and cause problems if you try to use the same channel as the receiver listening to data from a mic. You'll have to test that directly.

adafruit_support_mike
 
Posts: 53554
Joined: Thu Feb 11, 2010 2:51 pm

Re: RPi and I2S devices?

by besi on Tue Nov 06, 2018 6:32 pm

Hey Priam did you manage to get this to work? I am struggling to get my MEMS Mic to work with the Class D Mono Amp.
I made a post at stackexchange: https://raspberrypi.stackexchange.com/q ... s-playback
And any help would be appreciated.

besi
 
Posts: 7
Joined: Mon Oct 29, 2018 6:06 am

Please be positive and constructive with your questions and comments.


cron