espressif / arduino-esp32

Arduino core for the ESP32
GNU Lesser General Public License v2.1
13.52k stars 7.39k forks source link

I2S not working after upgrade from 1.0.2 to 1.0.4 #3588

Closed bzeeman closed 4 years ago

bzeeman commented 4 years ago

Hardware:

Board: Custom Board with WROOM-32D, Confirmed on SparkFun ESP32 thing Core Installation version: works in 1.0.2, does not work in 1.0.3 or 1.0.4 IDE name: Arduino IDE? & Platform.io Flash Frequency: 80Mhz PSRAM enabled: no Upload Speed: 115200 Computer OS: macOS 10.15.2

Description:

If I update to use Arduino-core 1.0.3 or 1.0.4, I2S stops working as expected. Values in the buffer are either 0x01 or 0x00. In 1.0.2 Values are a range of Ints. Confirmed Bug in 1.0.3 and 1.0.4

Sketch: (leave the backquotes for [code formatting]


#include <Arduino.h>
#include "driver/i2s.h"
#include "FreeRTOS.h"
#include "freertos/task.h"

#define I2S_PORT (I2S_NUM_0)
#define I2S_SAMPLE_RATE (8000)
#define I2S_BITS (I2S_BITS_PER_SAMPLE_32BIT)
#define I2S_FORMAT (I2S_CHANNEL_FMT_ONLY_RIGHT)
#define RECORDING_LEN 10//seconds
#define RECORDING_SIZE (I2S_SAMPLE_RATE * I2S_BITS / 8 * RECORDING_LEN)

#define BUFFER_SIZE 4096

// The pin config as per the setup
const i2s_pin_config_t pin_config = {
    .bck_io_num = 14,   // BCKL
    .ws_io_num = 15,    // LRCL
    .data_out_num = -1, // not used (only for speakers)
    .data_in_num = 34   // DOUT
};
void printDataToSerial(const int* data, size_t len){
    size_t bytesPrinted = 0;
    size_t fileSize = len;
    while(bytesPrinted < fileSize){

      for(int i = 0; i < 1024; I+=16){//taking every 16th value to no overload the serial monitor
        Serial.printf("%I\n", data[i]);
        Serial.flush();
      }
      bytesPrinted += 1024;
    }
}

void reader() {
   int* i2s_read_buff = (int*)calloc(BUFFER_SIZE, sizeof(int));
   //uint8_t* fs_wr_buff = (uint8_t*)calloc(BUFFER_SIZE, sizeof(char));
   int16_t* fs_wr_buff = (int16_t*)calloc(BUFFER_SIZE, sizeof(int16_t));
   size_t bytes_read = 0;
   Serial.print(RECORDING_SIZE);
   while(bytes_read < RECORDING_SIZE){
        size_t br;
        i2s_read(I2S_PORT, i2s_read_buff, BUFFER_SIZE * 4, &br, portMAX_DELAY);
        printDataToSerial(i2s_read_buff, BUFFER_SIZE);
        bytes_read+=br;

   }
   //Serial.println("Finishing File. . .");
   free(i2s_read_buff);
   free(fs_wr_buff);
    unsigned long recLength = bytes_read / I2S_SAMPLE_RATE;//because it's 8 bit sample  its a byte sample rate too.
    Serial.printf("Rec Length: %d\n", recLength);
}

void setup() {
  Serial.begin(115200);
  delay(500);

  esp_err_t err;

  const i2s_config_t i2s_config = {
  .mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX),
  .sample_rate =  I2S_SAMPLE_RATE,              // The format of the signal using ADC_BUILT_IN
  .bits_per_sample = I2S_BITS, // MEMS NEEDS 32 bit
  .channel_format = I2S_FORMAT,
  .communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB),
  .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
  .dma_buf_count = 4,
  .dma_buf_len = 1024,
  };
  err = i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL);
  if (err != ESP_OK) {
      Serial.printf("Failed installing driver: %d\n", err);
      while (true);
  }
  err = i2s_set_pin(I2S_PORT, &pin_config);
  if (err != ESP_OK) {
      Serial.printf("Failed setting pin: %d\n", err);
      while (true);
  }
  Serial.println("I2S driver installed.");

  Serial.printf("Bit Rate: %d\nSample Rate: %d\nRecording Length: %d\n", I2S_BITS, I2S_SAMPLE_RATE, RECORDING_LEN);

}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println("About to open this I2S can of whoop...");
  reader();
  delay(1000);
}

### Debug Messages:

There is no messages per-se but this is what occurs:
Code compiled on 1.0.2 gets valid data from I2S, Once I upgraded to 1.0.4, the buffer is now filled with 0x00000001.

VERSION 1.0.2 Output

16:26:35.320 -> ets Jun  8 2016 00:22:57
16:26:35.320 -> 
16:26:35.320 -> rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
16:26:35.358 -> configsip: 0, SPIWP:0xee
16:26:35.358 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
16:26:35.358 -> mode:DIO, clock div:1
16:26:35.358 -> load:0x3fff0018,len:4
16:26:35.358 -> load:0x3fff001c,len:1100
16:26:35.358 -> load:0x40078000,len:9232
16:26:35.358 -> load:0x40080400,len:6400
16:26:35.358 -> entry 0x400806a8
16:26:35.465 -> ⸮T⸮U⸮⸮⸮2-hal-cpu.c:168] setCpuFrequencyMhz(): PLL: 320 / 4 = 80 Mhz, APB: 80000000 Hz
16:26:36.030 -> I2S driver installed.
16:26:36.030 -> Bit Rate: 32
16:26:36.030 -> Sample Rate: 8000
16:26:36.030 -> Recording Length: 10
16:26:36.030 -> About to open this I2S can of whoop...
16:26:36.030 -> 3200000
16:26:36.666 -> -208535552
16:26:36.666 -> -208994304
16:26:36.666 -> -209453056
16:26:36.666 -> -209846272
16:26:36.666 -> -210206720

VERSION 1.0.3 Output

16:29:58.928 -> ets Jun  8 2016 00:22:57
16:29:58.928 -> 
16:29:58.928 -> rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
16:29:58.928 -> configsip: 0, SPIWP:0xee
16:29:58.928 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
16:29:58.928 -> mode:DIO, clock div:1
16:29:58.928 -> load:0x3fff0018,len:4
16:29:58.967 -> load:0x3fff001c,len:1100
16:29:58.967 -> load:0x40078000,len:9564
16:29:58.967 -> ho 0 tail 12 room 4
16:29:58.967 -> load:0x40080400,len:6320
16:29:58.967 -> entry 0x400806a8
16:29:59.078 -> ⸮T⸮U⸮⸮⸮2-hal-cpu.c:178] setCpuFrequencyMhz(): PLL: 320 / 4 = 80 Mhz, APB: 80000000 Hz
16:29:59.603 -> I2S driver installed.
16:29:59.603 -> Bit Rate: 32
16:29:59.603 -> Sample Rate: 8000
16:29:59.603 -> Recording Length: 10
16:29:59.636 -> About to open this I2S can of whoop...
16:29:59.636 -> 3200000
16:30:00.270 -> 1
16:30:00.270 -> 1
16:30:00.270 -> 1
16:30:00.270 -> 1
16:30:00.270 -> 1
16:30:00.270 -> 1
16:30:00.270 -> 1
16:30:00.270 -> 1
16:30:00.270 -> 1
16:30:00.270 -> 1
16:30:00.270 -> 1
16:30:00.270 -> 1
16:30:00.270 -> 1

It helps to use the serial Plotter: From 1.0.2

Screen Shot 2019-12-20 at 4 33 07 PM

From 1.0.3

Screen Shot 2019-12-20 at 4 35 08 PM
bzeeman commented 4 years ago

I may have found the problem/issue/bug. When I change it to ONLY_LEFT instead of ONLY_RIGHT it works. So potentially the issue was that ONLY_RIGHT should not have worked before? I am a little unclear on how I might define when ws is high or low, so It is possible whatever changed made it so that ws was LOW (Should be LEFT) but I was specifying to only get the right channel? Either way, something changed between 1.0.2 and 1.0.3/4

stale[bot] commented 4 years ago

[STALE_SET] This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

stale[bot] commented 4 years ago

[STALE_DEL] This stale issue has been automatically closed. Thank you for your contributions.