Trouble with i2c and i2s running simultaneously

avi007
Posts: 11
Joined: Fri May 26, 2017 12:58 am

Trouble with i2c and i2s running simultaneously

Postby avi007 » Sun Sep 03, 2017 1:08 pm

Hi,
Using ESP-IDF with ESP32-WROVER kit with jumper wires.

I am running an accelerator and microphone, using freeRtos task. The sensors work fine when only one of them being used. But the problem occurs when I try to do parallel task utilizing dual cores. I2C communication stops working while i2s keeps going. In I2C communication I get the ESP_FAIL error (please note in the absence of i2s communication i2c keep working fine), and after that it does not work anymore. I keep gettting timout error after that.

Please let me know if you have tried something parallel running on esp32.

thank you,
Avi

avi007
Posts: 11
Joined: Fri May 26, 2017 12:58 am

Re: Trouble with i2c and i2s running simultaneously

Postby avi007 » Tue Sep 05, 2017 7:42 pm

Hi,

I have finally had it working to my needs. Here is how I did it (hope it would save somebody few hours):

I ran the wifi + i2s communication on the main thread, where as I ran i2C comm on separate task. This seems to be working

I also noticed that most of the issues that I encountered were caused by memory issue, it was extremely difficult to sort through the error messages and reach to the real problems.

Cheers,
Avi

dreamworker
Posts: 3
Joined: Mon Apr 30, 2018 9:08 am

Re: Trouble with i2c and i2s running simultaneously

Postby dreamworker » Mon Apr 30, 2018 9:26 am

Hi,

I have the same issue. I am running an MPU6050 via I2C which works perfectly fine. But as soon as I install the I2S driver and try to play a beep tone in loop the Sensor starts to react weird. It delivers good data for a while then randomly sends values that obviously are not correct and soon the read seems to timeout for exactly 1000ms. Then comes back and delivers values. This repeats in random time spans.
I tried your solution to move I2S in main loop, tried to run parallel in both cores and tried to run both in one core. No change so far.
I wonder if there are more people having the same issue. @avi007 maybe you could provide some more details on you problem and your solution. Thanks in advance ...

ddodge
Posts: 3
Joined: Tue Apr 09, 2019 5:23 pm

Re: Trouble with i2c and i2s running simultaneously

Postby ddodge » Tue Apr 30, 2019 2:00 pm

Have there been anymore advances on this issue?

User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: Trouble with i2c and i2s running simultaneously

Postby fly135 » Wed May 01, 2019 3:12 pm

ddodge wrote:
Tue Apr 30, 2019 2:00 pm
Have there been anymore advances on this issue?
I am using I2S to record audio and regularly accessing several I2C devices and have not experienced this issue.

John A

ddodge
Posts: 3
Joined: Tue Apr 09, 2019 5:23 pm

Re: Trouble with i2c and i2s running simultaneously

Postby ddodge » Wed May 01, 2019 5:06 pm

Any chance I could ask you to post your setup and code/Sketch please? I have the Adafruit SPH0645 I2S microphone, along with two I2C peripherals connected to the Adafruit Huzzah32 and when I run the sketch, I get one of three results. 1: After a few iterations I stop getting values from all the devices and it looks like it locked up. 2: The devices run, but all the values from the connected devices don't match the values when they are running stand alone. 3: The I2C and I2S devices don't pass the initialization step. Result 3 is what happens 99% of the time. The I2S Microphone runs as expected when it is the only device connected and the I2C devices run as expected together when they are the only devices connected. The issue arises when I bring them all together. Any help would be appreciated. Below is the code I have if anything is blatantly obvious.

Code: Select all

#include "driver/i2s.h"

#include <Wire.h>
#include <Adafruit_LSM303.h>
#include <Adafruit_ADXL345_U.h>

Adafruit_LSM303 lsm303= Adafruit_LSM303();
Adafruit_ADXL345_Unified adxl345 = Adafruit_ADXL345_Unified(345);

const i2s_config_t i2s_config = {
    .mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX), // Receive, not transfer
    .sample_rate = 44100,                         // 16KHz 16000
    .bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT, 
    .channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT, 
    .communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB),
    .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,     // Interrupt level 1
    .dma_buf_count = 4,                           // number of buffers 4
    .dma_buf_len = 8,                     // 8 samples per buffer (minimum) 8
    .use_apll = true,
    //.fixed_mclk = 16000
 };

// The pin config as per the setup
const i2s_pin_config_t pin_config = {
    .bck_io_num = 26,   // BCKL
    .ws_io_num = 25,    // LRCL
    .data_out_num = I2S_PIN_NO_CHANGE,
    .data_in_num = 4   // DOUT
};

int led = 13;

void setup(){
  Wire.begin();
  Serial.begin(115200);
  pinMode(led, OUTPUT);
  digitalWrite(led, HIGH); 
  pinMode(4, INPUT);

 esp_err_t err;
 
  // Configuring the I2S driver and pins.
  // This function must be called before any I2S driver read/write operations.
  err = i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
  if (err != ESP_OK) {
    Serial.printf("Failed installing driver: %d\n", err);
    while (true);
  }
  err = i2s_set_pin(I2S_NUM_0, &pin_config);
  if (err != ESP_OK) {
    Serial.printf("Failed setting pin: %d\n", err);
    while (true);
  }
  Serial.println("I2S driver installed.");

  //*****************************LSM303********************************
  if(!lsm303.begin()){
    Serial.println("Couldnt start LSM303");
    while (true);
  }
  Serial.println("Initialized LSM303");
  // Command for datarate and range not available 
  
  //*****************************ADXL345*******************************
  if(!adxl345.begin()){
    Serial.println("Couldnt start ADXL345");
    while (true);
  }
  Serial.println("Initialized ADXL345");
  adxl345.setRange(ADXL345_RANGE_8_G); // 2, 4, 8 or 16 G
  adxl345.setDataRate(ADXL345_DATARATE_50_HZ); //1600, 800, 400, 200, 100, 50, 25, 12_5, 6_25, 3_13, 1_56, 0_78, 0_39, 0_20, 0_10

}


void loop(){

  // Read a single sample for SPH***********************************************************************************************
  int32_t sample = 0;
  int bytes_read = i2s_pop_sample(I2S_NUM_0, (char *)&sample, portMAX_DELAY); // no timeout
  if (bytes_read > 0) {
    Serial.print("Sound: ");
    Serial.println(sample);
  }

  //LSM303 accel
  lsm303.read();
  Serial.print("AX: "); Serial.print((int)lsm303.accelData.x);
  Serial.print("\t\tAY: "); Serial.print((int)lsm303.accelData.y);
  Serial.print("\t\tAZ: "); Serial.print((int)lsm303.accelData.z);
  Serial.println();

  //LSM303 mag
  Serial.print("MX: "); Serial.print((int)lsm303.magData.x);
  Serial.print("\t\tMY: "); Serial.print((int)lsm303.magData.y);
  Serial.print("\t\tMZ: "); Serial.print((int)lsm303.magData.z);
  Serial.println();

  //ADXL345 accel
  Serial.print("X: "); Serial.print(adxl345.getX());
  Serial.print("\t\tY: "); Serial.print(adxl345.getY());
  Serial.print("\t\tZ: "); Serial.print(adxl345.getZ());
  Serial.println();

  Serial.println("------------------------------------------------");

  delay(2);
  
}

User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: Trouble with i2c and i2s running simultaneously

Postby fly135 » Mon May 06, 2019 4:56 pm

ddodge wrote:
Wed May 01, 2019 5:06 pm
Any chance I could ask you to post your setup and code/Sketch please?
I've heard about people having problems under Arduino with the I2C. I'm using the IDF. The code belongs to my employer. And quite frankly it's a lot of code. I don't do it in a single loop. A bunch of tasks are running in my app. I wrote a manufacturing test app in Arduino and found issues with random I2C init failures. Not a fan of Arduino for the ESP32, except for quick, "don't care if I have to reset several times to get it to work" applications.

Looking at your code I have to wonder what kind of audio you expect to get from I2S only reading a single sample at a time.

John A

egagnon
Posts: 5
Joined: Thu Sep 29, 2022 1:58 pm

Re: Trouble with i2c and i2s running simultaneously

Postby egagnon » Tue Jun 06, 2023 8:10 pm

Was there a resolution to this? I am observing the same issues with the 5.0x release using the latest I2S drivers. The I2C port reading a sensor starts getting random timeout errors only when I initialize the I2S audio peripheral. For the record, I am using an I2C port to read a sensor as well as two I2S peripherals, one to drive a speaker, one to read in a mic. The one driving a speaker causes no issues to the I2C bus/sensor after being initialized but the 2nd one reading an I2S mic causes issues on the I2C bus as soon as it is initialized.

Who is online

Users browsing this forum: Bing [Bot] and 118 guests