Library i2s
To use this library in your code:
I2S Serial communication Bus.
An I2S bus can be either in master or slave mode. In master mode, the bus generates the clock and word-select signals. In slave mode, the bus uses these signals as input.
Optionally, the peripheral can also generate the master clock (independently whether it is the master or not), which may be used for synchronization. The master clock is running at a multiple of the sample rate.
ESP32 and ESP32S2
The ESP32 has two I2S peripherals, each of which can be configured in simplex (one way) or duplex (two way) mode. Only pins 0, 1, and 3 can be used as output for the master clock. The ESP32 does not support the master clock as input.
The ESP32S2 has one I2S peripheral. Only pins 18, 19, and 20 can be used as output for the master clock. The ESP32 does not support the master clock as input.
The channels on the peripheral are not independent. It's not possible to have two simplex channels on the same peripheral.
Other ESP32 variants
The ESP32C3, and ESP32C6 have one I2S peripheral. There are no restrictions on which pins can be used for the master clock. The master clock can be used as input or output.
The ESP32S3 has two I2S peripherals. Only pins 18, 19, and 20 can be used as output for the master clock. Any pin can be used as master clock input.
The channels on the peripheral are independent. It's possible to have two simplex channels on the same peripheral. However, while the two channels have independent master clocks, only one master clock can be routed to a pin.
Examples
import i2s
TX ::= 32
SCK ::= 26
WS ::= 25
// For this example, we just assume that this data should be
// written repeatedly to the I2S channel.
SOME-DATA ::= #[...]
main:
// Since we only use the transmit channel, we call the variable
// "channel" instead of "bus".
channel := i2s.Bus
--master=false
--tx=TX
--sck=SCK
--ws=WS
channel.configure
--sample-rate=44100
--bits-per-sample=16
start-index := 0
while true:
preloaded := channel.preload SOME-DATA[start-index..]
if preloaded == 0: break
start-index += preloaded
if start-index == SOME-DATA.size: start-index = 0
channel.start
channel.write SOME-DATA[start-index..]
while true:
channel.write SOME-DATA
Classes
I2S Serial communication Bus, primarily used to emit sound but has a wide range of usages.