I have huge problems with I2S in ESP32, both in "driver/I2S.h" or "ESP_I2S.h" library.
I need something looks simple: receiving samples in master mode using pooling in main loop, but without blocking.
But examples from internet not work as I expect.
For example, the simple code:
Code: Select all
#include <driver/i2s.h>
#define I2S_PORT I2S_NUM_0
// Define input buffer length
#define bufferLen 512
int16_t sBuffer[bufferLen];
void i2s_install()
{
// Set up I2S Processor configuration
const i2s_config_t i2s_config =
{
.mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX),
.sample_rate = 8000,
.bits_per_sample = i2s_bits_per_sample_t(16),
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
.communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_STAND_I2S),
.intr_alloc_flags = 0,
.dma_buf_count = 4,
.dma_buf_len = bufferLen,
.use_apll = false
};
i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL);
}
void i2s_setpin()
{
// Set I2S pin configuration
const i2s_pin_config_t pin_config =
{
//.mck_io_num = 0,
.bck_io_num = 4,
.ws_io_num = 5,
.data_out_num = 15,
.data_in_num = 2
};
i2s_set_pin(I2S_PORT, &pin_config);
}
void setup()
{
pinMode(16, OUTPUT);
pinMode(17, OUTPUT);
Serial.begin(115200, SERIAL_8N1);
// Set up I2S
i2s_install();
i2s_setpin();
i2s_start(I2S_PORT);
}
void loop()
{
digitalWrite(16, HIGH);
size_t bytesIn = 0;
//esp_err_t result = i2s_read(I2S_PORT, &sBuffer, bufferLen, &bytesIn, portMAX_DELAY);
//esp_err_t result = i2s_read(I2S_PORT, &sBuffer, bufferLen, &bytesIn, 2);
esp_err_t result = i2s_read(I2S_PORT, &sBuffer, 128, &bytesIn, 2);
digitalWrite(16, LOW);
if (result == ESP_OK)
{
Serial.println(bytesIn);
}
else
{
Serial.println("_");
}
//delay(20);
}
Serial.println("_") are never executed.
But in UART console I see something strange:
Also, oscilloscope on GPIO16, confirms that readings are made in bursts.0
0
0
0
0
128
128
128
128
128
128
128
128
0
0
0
0
0
0
0
I expected something else. Readings of 128 bytes in equal timestamps:
or readings more or less random amount of data, but in that amount that sums to 8000 stereo samples in every second.0
0
0
128
0
0
0
0
128
0
0
0
0
But I see something else. Bursts of 128 bytes readings, with even not sum to 8000 in second (more precisely, to 8000 * 4 bytes per stereo sample per second)33
34
44
22
22
56
Why, and how I can repair this?
