A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.espressif.com/projects/arduino-esp32/en/latest/migration_guides/../api/i2s.html below:

I2S - - — Arduino ESP32 latest documentation

Arduino ESP32 I2S About

I2S - Inter-IC Sound, correctly written I²S pronounced “eye-squared-ess”, alternative notation is IIS. I²S is an electrical serial bus interface standard used for connecting digital audio devices together.

It is used to communicate PCM (Pulse-Code Modulation) audio data between integrated circuits in an electronic device. The I²S bus separates clock and serial data signals, resulting in simpler receivers than those required for asynchronous communications systems that need to recover the clock from the data stream.

Despite the similar name, I²S is unrelated and incompatible with the bidirectional I²C (IIC) bus.

The I²S bus consists of at least three lines:

Note

All lines can be attached to almost any pin and this change can occur even during operation.

It may also include a Master clock line:

I2S Configuration Master / Slave Mode

In Master mode (default) the device is generating clock signal sck and word select signal on ws.

In Slave mode the device listens on attached pins for the clock signal and word select - i.e. unless externally driven the pins will remain LOW. This mode is not supported yet.

Operation Modes

Setting the operation mode is done with function begin and is set by function parameter mode.

Simplex / Duplex Mode

Due to the different clock sources the PDM modes are always in Simplex mode, using only one data pin.

The STD and TDM modes operate in the Duplex mode, using two separate data pins:

In this mode, the driver is able to read and write simultaneously on each line and is suitable for applications like walkie-talkie or phone.

Data Bit Width

This is the number of bits in a channel sample. The data bit width is set by function parameter bits_cfg. The current supported values are:

Sample Rate

The sample rate is set by function parameter rate. It is the number of samples per second in Hz.

Slot Mode

The slot mode is set by function parameter ch. The current supported values are:

Arduino-ESP32 I2S API Initialization and deinitialization

Before initialization, set which pins you want to use.

begin (Master Mode)

Before usage choose which pins you want to use.

bool begin(i2s_mode_t mode, uint32_t rate, i2s_data_bit_width_t bits_cfg, i2s_slot_mode_t ch, int8_t slot_mask=-1)

Parameters:

This function will return true on success or fail in case of failure.

When failed, an error message will be printed if the correct log level is set.

end

Performs safe deinitialization - free buffers, destroy task, end driver operation, etc.

Pin setup

The function to set the pins will depend on the operation mode.

setPins

Set the pins for the I2S interface when using the standard or TDM mode.

void setPins(int8_t bclk, int8_t ws, int8_t dout, int8_t din=-1, int8_t mclk=-1)

Parameters:

setPinsPdmTx

Set the pins for the I2S interface when using the PDM TX mode.

void setPinsPdmTx(int8_t clk, int8_t dout0, int8_t dout1=-1)

Parameters:

setPinsPdmRx

Set the pins for the I2S interface when using the PDM RX mode.

void setPinsPdmRx(int8_t clk, int8_t din0, int8_t din1=-1, int8_t din2=-1, int8_t din3=-1)

Parameters:

setInverted

Set which pins have inverted logic when using the standard or TDM mode. Data pins cannot be inverted.

void setInverted(bool bclk, bool ws, bool mclk=false)

Parameters:

setInvertedPdm

Set which pins have inverted logic when using the PDM mode. Data pins cannot be inverted.

void setInvertedPdm(bool clk)

Parameters:

I2S Configuration

The I2S configuration can be changed during operation.

configureTX

Configure the I2S TX channel.

bool configureTX(uint32_t rate, i2s_data_bit_width_t bits_cfg, i2s_slot_mode_t ch, int8_t slot_mask=-1)

Parameters:

This function will return true on success or fail in case of failure.

When failed, an error message will be printed if the correct log level is set.

configureRX

Configure the I2S RX channel.

bool configureRX(uint32_t rate, i2s_data_bit_width_t bits_cfg, i2s_slot_mode_t ch, i2s_rx_transform_t transform=I2S_RX_TRANSFORM_NONE)

Parameters:

This function will return true on success or fail in case of failure.

When failed, an error message will be printed if the correct log level is set.

txChan

Get the TX channel handler pointer.

i2s_chan_handle_t txChan()
txSampleRate

Get the TX sample rate.

txDataWidth

Get the TX data width (8, 16 or 32 bits).

i2s_data_bit_width_t txDataWidth()
txSlotMode

Get the TX slot mode (stereo or mono).

i2s_slot_mode_t txSlotMode()
rxChan

Get the RX channel handler pointer.

i2s_chan_handle_t rxChan()
rxSampleRate

Get the RX sample rate.

rxDataWidth

Get the RX data width (8, 16 or 32 bits).

i2s_data_bit_width_t rxDataWidth()
rxSlotMode

Get the RX slot mode (stereo or mono).

i2s_slot_mode_t rxSlotMode()
I/O Operations readBytes

Read a certain amount of data bytes from the I2S interface.

size_t readBytes(char *buffer, size_t size)

Parameters:

This function will return the number of bytes read.

read

Read the next available byte from the I2S interface.

This function will return the next available byte or -1 if no data is available or an error occurred.

write

There are two versions of the write function:

The first version writes a certain amount of data bytes to the I2S interface.

size_t write(uint8_t *buffer, size_t size)

Parameters:

This function will return the number of bytes written.

The second version writes a single byte to the I2S interface.

Parameters:

This function will return 1 if the byte was written or 0 if an error occurred.

available

Get if there is data available to be read.

This function will return I2S_READ_CHUNK_SIZE if there is data available to be read or -1 if not.

peek

Get the next available byte from the I2S interface without removing it from the buffer. Currently not implemented.

This function will currently always return -1.

lastError

Get the last error code for an I/O operation on the I2S interface.

recordWAV

Record a short PCM WAV to memory with the current RX settings. Returns a buffer that must be freed by the user.

uint8_t * recordWAV(size_t rec_seconds, size_t * out_size)

Parameters:

This function will return a pointer to the buffer containing the recorded WAV data or NULL if an error occurred.

playWAV

Play a PCM WAV from memory with the current TX settings.

void playWAV(uint8_t * data, size_t len)

Parameters:

playMP3

Play a MP3 from memory with the current TX settings.

bool playMP3(uint8_t *src, size_t src_len)

Parameters:

This function will return true on success or false in case of failure.

When failed, an error message will be printed if the correct log level is set.

Sample code
#include <ESP_I2S.h>

const int buff_size = 128;
int available_bytes, read_bytes;
uint8_t buffer[buff_size];
I2SClass I2S;

void setup() {
  I2S.setPins(5, 25, 26, 35, 0); //SCK, WS, SDOUT, SDIN, MCLK
  I2S.begin(I2S_MODE_STD, 16000, I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO);
  I2S.read();
  available_bytes = I2S.available();
  if(available_bytes < buff_size) {
    read_bytes = I2S.readBytes(buffer, available_bytes);
  } else {
    read_bytes = I2S.readBytes(buffer, buff_size);
  }
  I2S.write(buffer, read_bytes);
  I2S.end();
}

void loop() {}

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4