2021-04-17 05:27:40 +01:00
.. currentmodule :: machine
.. _machine.I2S:
class I2S -- Inter-IC Sound bus protocol
========================================
2021-12-15 00:49:22 +00:00
I2S is a synchronous serial protocol used to connect digital audio devices.
2021-04-17 05:27:40 +01:00
At the physical level, a bus consists of 3 lines: SCK, WS, SD.
2021-06-12 05:51:05 +01:00
The I2S class supports controller operation. Peripheral operation is not supported.
2021-04-17 05:27:40 +01:00
2021-12-15 00:49:22 +00:00
The I2S class is currently available as a Technical Preview. During the preview period, feedback from
2021-04-17 05:27:40 +01:00
users is encouraged. Based on this feedback, the I2S class API and implementation may be changed.
I2S objects can be created and initialized using::
from machine import I2S
from machine import Pin
2021-12-15 00:49:22 +00:00
2021-04-17 05:27:40 +01:00
# ESP32
sck_pin = Pin(14) # Serial clock output
ws_pin = Pin(13) # Word clock output
2021-08-05 04:00:35 +01:00
sd_pin = Pin(12) # Serial data output
2021-12-15 00:49:22 +00:00
2021-04-17 05:27:40 +01:00
or
2021-12-15 00:49:22 +00:00
2021-04-17 05:27:40 +01:00
# PyBoards
sck_pin = Pin("Y6") # Serial clock output
ws_pin = Pin("Y5") # Word clock output
2021-08-05 04:00:35 +01:00
sd_pin = Pin("Y8") # Serial data output
2021-12-15 00:49:22 +00:00
audio_out = I2S(2,
2021-08-05 04:00:35 +01:00
sck=sck_pin, ws=ws_pin, sd=sd_pin,
2021-12-15 00:49:22 +00:00
mode=I2S.TX,
bits=16,
2021-04-17 05:27:40 +01:00
format=I2S.MONO,
2021-12-15 00:49:22 +00:00
rate=44100,
2021-04-17 05:27:40 +01:00
ibuf=20000)
2021-12-15 00:49:22 +00:00
audio_in = I2S(2,
2021-08-05 04:00:35 +01:00
sck=sck_pin, ws=ws_pin, sd=sd_pin,
2021-12-15 00:49:22 +00:00
mode=I2S.RX,
bits=32,
2021-04-17 05:27:40 +01:00
format=I2S.STEREO,
2021-12-15 00:49:22 +00:00
rate=22050,
2021-04-17 05:27:40 +01:00
ibuf=20000)
2021-12-15 00:49:22 +00:00
2021-04-17 05:27:40 +01:00
3 modes of operation are supported:
2021-12-15 00:49:22 +00:00
- blocking
- non-blocking
2023-06-08 07:08:09 +01:00
- asyncio
2021-12-15 00:49:22 +00:00
2021-04-17 05:27:40 +01:00
blocking::
2021-12-15 00:49:22 +00:00
2021-04-17 05:27:40 +01:00
num_written = audio_out.write(buf) # blocks until buf emptied
num_read = audio_in.readinto(buf) # blocks until buf filled
2021-12-15 00:49:22 +00:00
2021-04-17 05:27:40 +01:00
non-blocking::
2021-12-15 00:49:22 +00:00
2021-04-17 05:27:40 +01:00
audio_out.irq(i2s_callback) # i2s_callback is called when buf is emptied
num_written = audio_out.write(buf) # returns immediately
2021-12-15 00:49:22 +00:00
2021-04-17 05:27:40 +01:00
audio_in.irq(i2s_callback) # i2s_callback is called when buf is filled
2021-12-15 00:49:22 +00:00
num_read = audio_in.readinto(buf) # returns immediately
2023-06-08 07:08:09 +01:00
asyncio::
2021-12-15 00:49:22 +00:00
2023-06-08 07:08:09 +01:00
swriter = asyncio.StreamWriter(audio_out)
2021-04-17 05:27:40 +01:00
swriter.write(buf)
await swriter.drain()
2021-12-15 00:49:22 +00:00
2023-06-08 07:08:09 +01:00
sreader = asyncio.StreamReader(audio_in)
2021-04-17 05:27:40 +01:00
num_read = await sreader.readinto(buf)
2021-12-15 00:49:22 +00:00
2022-02-10 12:51:48 +00:00
Some codec devices like the WM8960 or SGTL5000 require separate initialization
before they can operate with the I2S class. For these, separate drivers are
supplied, which also offer methods for controlling volume, audio processing and
other things. For these drivers see:
- :ref: `wm8960`
2021-04-17 05:27:40 +01:00
Constructor
-----------
2021-11-29 17:50:34 +00:00
.. class :: I2S(id, *, sck, ws, sd, mck=None, mode, bits, format, rate, ibuf)
2021-04-17 05:27:40 +01:00
Construct an I2S object of the given id:
2021-12-15 00:49:22 +00:00
2021-11-29 17:50:34 +00:00
- `` id `` identifies a particular I2S bus; it is board and port specific
2021-12-15 00:49:22 +00:00
2021-04-17 05:27:40 +01:00
Keyword-only parameters that are supported on all ports:
2021-12-15 00:49:22 +00:00
2021-04-17 05:27:40 +01:00
- `` sck `` is a pin object for the serial clock line
- `` ws `` is a pin object for the word select line
- `` sd `` is a pin object for the serial data line
2021-11-29 17:50:34 +00:00
- `` mck `` is a pin object for the master clock line;
master clock frequency is sampling rate * 256
2021-04-17 05:27:40 +01:00
- `` mode `` specifies receive or transmit
- `` bits `` specifies sample size (bits), 16 or 32
- `` format `` specifies channel format, STEREO or MONO
2022-03-28 07:10:33 +01:00
- `` rate `` specifies audio sampling rate (Hz);
this is the frequency of the `` ws `` signal
2021-04-17 05:27:40 +01:00
- `` ibuf `` specifies internal buffer length (bytes)
2021-12-15 00:49:22 +00:00
For all ports, DMA runs continuously in the background and allows user applications to perform other operations while
sample data is transferred between the internal buffer and the I2S peripheral unit.
2021-04-17 05:27:40 +01:00
Increasing the size of the internal buffer has the potential to increase the time that user applications can perform non-I2S operations
before underflow (e.g. `` write `` method) or overflow (e.g. `` readinto `` method).
Methods
-------
.. method :: I2S.init(sck, ...)
see Constructor for argument descriptions
2021-12-15 00:49:22 +00:00
2021-04-17 05:27:40 +01:00
.. method :: I2S.deinit()
Deinitialize the I2S bus
2021-12-15 00:49:22 +00:00
2021-04-17 05:27:40 +01:00
.. method :: I2S.readinto(buf)
2021-12-15 00:49:22 +00:00
Read audio samples into the buffer specified by `` buf `` . `` buf `` must support the buffer protocol, such as bytearray or array.
"buf" byte ordering is little-endian. For Stereo format, left channel sample precedes right channel sample. For Mono format,
2021-04-17 05:27:40 +01:00
the left channel sample data is used.
2021-12-15 00:49:22 +00:00
Returns number of bytes read
2021-04-17 05:27:40 +01:00
.. method :: I2S.write(buf)
Write audio samples contained in `` buf `` . `` buf `` must support the buffer protocol, such as bytearray or array.
2021-12-15 00:49:22 +00:00
"buf" byte ordering is little-endian. For Stereo format, left channel sample precedes right channel sample. For Mono format,
2021-04-17 05:27:40 +01:00
the sample data is written to both the right and left channels.
2021-12-15 00:49:22 +00:00
Returns number of bytes written
2021-04-17 05:27:40 +01:00
.. method :: I2S.irq(handler)
2021-12-15 00:49:22 +00:00
Set a callback. `` handler `` is called when `` buf `` is emptied (`` write `` method) or becomes full (`` readinto `` method).
2021-04-17 05:27:40 +01:00
Setting a callback changes the `` write `` and `` readinto `` methods to non-blocking operation.
`` handler `` is called in the context of the MicroPython scheduler.
2021-12-15 00:49:22 +00:00
2021-09-10 14:02:35 +01:00
.. staticmethod :: I2S.shift(*, buf, bits, shift)
2021-04-17 05:27:40 +01:00
2021-12-15 00:49:22 +00:00
bitwise shift of all samples contained in `` buf `` . `` bits `` specifies sample size in bits. `` shift `` specifies the number of bits to shift each sample.
Positive for left shift, negative for right shift.
2021-04-17 05:27:40 +01:00
Typically used for volume control. Each bit shift changes sample volume by 6dB.
2021-12-15 00:49:22 +00:00
2021-04-17 05:27:40 +01:00
Constants
---------
.. data :: I2S.RX
for initialising the I2S bus `` mode `` to receive
.. data :: I2S.TX
for initialising the I2S bus `` mode `` to transmit
.. data :: I2S.STEREO
for initialising the I2S bus `` format `` to stereo
.. data :: I2S.MONO
for initialising the I2S bus `` format `` to mono