PlatformIO Community

ESP32 I2S stops working when building with arduino AND esp-idf framework

I have a simple code reading an i2s microphone on an esp32 written with Arduino. The code runs fine when I build with framework=arduino only.
As soon as I build with famework=arduino,esp-idf the i2s output is only zeros.
I have no issues with my code otherwise. Any ideas?

This works:

[env:featheresp32]
platform = espressif32
board = featheresp32
framework = arduino
build_flags =
-D ESP32
monitor_speed = 115200

I2S output only zeros:

[env:featheresp32]
platform = espressif32
board = featheresp32
framework = arduino, espidf
build_flags =
-D ESP32
monitor_speed = 115200
platform_packages =
framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#idf-release/v4.0

This is the code I am running:

#include <stdio.h>
#include <Arduino.h>
#include “driver/i2s.h”
#include <soc/i2s_reg.h>
#define SAMPLE_RATE 8000
#define MIC_SAMPLES_BUF 250

void setup() {

Serial.begin(115200);

i2s_config_t i2s_config = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX),
.sample_rate = SAMPLE_RATE,
.bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT,
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
.communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_I2S),
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
.dma_buf_count = 20,
.dma_buf_len = MIC_SAMPLES_BUF,
.use_apll = false,
.tx_desc_auto_clear = true,
.fixed_mclk = 0
};

const i2s_pin_config_t pin_config = {
.bck_io_num = 27, // BCKL
.ws_io_num = 13, // LRCL
.data_out_num = -1, // not used (only for speakers)
.data_in_num = 12 // DOUT
};

i2s_driver_install(I2S_NUM_1, &i2s_config, 0, NULL);
i2s_zero_dma_buffer(I2S_NUM_1);
i2s_set_pin(I2S_NUM_1, &pin_config);
}

uint8_t i2sData[MIC_SAMPLES_BUF*4];
size_t bytes_read = 0;

void loop()
{
i2s_read(I2S_NUM_1, &i2sData, sizeof(i2sData), &bytes_read, portMAX_DELAY);
int32_t *samples = (int32_t *)i2sData;
Serial.println(String(samples[0]));
}

found the error. platformio uses version 4.0 of esp-idf when combined with arduino, and for some reason left and right i2s channels are swapped in this version.

Hm no in the latest version we use 4.1 (see https://github.com/platformio/platform-espressif32/releases).

Have you made sure to update your platforms (CLI -> pio platform update) to have “Espressif32 version 2.1.0”? Is the bug still present in the there-used ESP-IDF 4.1?