I2S Driver

Bakoena
Posts: 1
Joined: Mon Jan 20, 2025 3:48 pm

I2S Driver

Postby Bakoena » Mon Jan 20, 2025 4:05 pm

Hi all,

We have a codec getting audio data from 8 microphones. The audio data is then written to the sd card. When the ESP32 S3 is in master mode, it works perfectly. The problem is when its in slave mode, there is humming noise being introduced to the audio. Even when the mics are removed, the noise is still there. We've probed the data line, without the mics, and the data line is only changing on the last two slots which have audio, while the ones without audio are not changing, on both the master device(device in master mode) and slave device(device in slave mode).
Here's a picture of the waveform where we would expect to not have any audio present.
Screenshot 2025-01-20 180024.png
Screenshot 2025-01-20 180024.png (15.33 KiB) Viewed 343 times
Here is a snippet of the code to set the two devices in either master mode or slave mode

  1. void setupCodecI2SforEthernet(int numberOfActiveChannels, int sampleRate) {
  2.   i2s_config_t i2s_config = {
  3.     // .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX),      // sets i2s mode, I2S_MODE_MASTER -> ESP32 S3 provides clock, I2S_MODE_RX -> ESP32 S3 receives data
  4.     .sample_rate = sampleRate,                               // BCLK frequency, which is the frequency at which data is sampled
  5.     .bits_per_sample = NUMBER_OF_BITS_PER_SAMPLE_SET,         // word length, set in both codec(pcmd3180) library and here, needs to be the same
  6.     .channel_format = I2S_CHANNEL_FMT_MULTIPLE,               // select for multiple channels
  7.     .communication_format = I2S_COMM_FORMAT_STAND_PCM_SHORT,  // check datasheet, provide more information. Basically the format of data being received
  8.     .intr_alloc_flags = ESP_INTR_FLAG_LEVEL6,                 // default interrupt priority
  9.     .dma_buf_count = DMA_BUF_COUNT,                           // the dma buffer count                                                                                                                                                                        // this is the number of dma buffers 16 was 2
  10.     .dma_buf_len = AUDIO_BUFFER_LEN,                          // size of dma buffer
  11.     .use_apll = true,                                         // set to true, clock is more accurate
  12.     .tx_desc_auto_clear = false,                              //
  13.     .fixed_mclk = 0,                                          //                                                                                                                                                      // might not need
  14.     .mclk_multiple = (i2s_mclk_multiple_t)(256),              //                                                                                                                                                     // might not need
  15.     .total_chan = numberOfActiveChannels,                     // total numeber of channels
  16.   };
  17.  
  18.   if (DEVICE_TYPE == MASTER_DEVICE) {
  19.     i2s_config.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX);
  20.   } else if (DEVICE_TYPE == SLAVE_DEVICE) {
  21.     i2s_config.mode = (i2s_mode_t)(I2S_MODE_SLAVE | I2S_MODE_RX);
  22.   }
  23.  
  24.   // Set the chan_mask conditionally
  25.   if (numberOfActiveChannels == 4) {
  26.     i2s_config.chan_mask = (i2s_channel_t)(I2S_TDM_ACTIVE_CH0 | I2S_TDM_ACTIVE_CH1 | I2S_TDM_ACTIVE_CH2 | I2S_TDM_ACTIVE_CH3);
  27.   } else {
  28.     i2s_config.chan_mask = (i2s_channel_t)(I2S_TDM_ACTIVE_CH0 | I2S_TDM_ACTIVE_CH1 | I2S_TDM_ACTIVE_CH2 | I2S_TDM_ACTIVE_CH3 | I2S_TDM_ACTIVE_CH4 | I2S_TDM_ACTIVE_CH5 | I2S_TDM_ACTIVE_CH6 | I2S_TDM_ACTIVE_CH7);
  29.   }
  30.  
  31.   // Install and start I2S driver
  32.   esp_err_t i2s_driver_install_result = i2s_driver_install((i2s_port_t)i2s_num, &i2s_config, 0, NULL);
  33.   // i2s_set_pin error handling
  34.   if (i2s_driver_install_result == ESP_OK) {
  35.     debugln("i2s_driver_install result: Success!");
  36.   } else if (i2s_driver_install_result == ESP_ERR_INVALID_ARG) {
  37.     debugln("i2s_driver_install result: Parameter error!");
  38.   } else if (i2s_driver_install_result == ESP_ERR_NO_MEM) {
  39.     debugln("i2s_driver_install result: Out of memory!");
  40.   } else if (i2s_driver_install_result == ESP_ERR_INVALID_STATE) {
  41.     debugln("i2s_driver_install result: Current I2S port is in use!");
  42.   } else {
  43.     debugln("i2s_driver_install result: Unknown error!");
  44.   }
  45.  
  46.   // Set I2S pins
  47.   esp_err_t i2s_set_pin_result = i2s_set_pin((i2s_port_t)i2s_num, &pin_config);
  48.   // i2s_set_pin error handling
  49.   if (i2s_set_pin_result == ESP_OK) {
  50.     debugln("i2s_set_pin result: Success!");
  51.   } else if (i2s_set_pin_result == ESP_ERR_INVALID_ARG) {
  52.     debugln("i2s_set_pin result: parameter error!");
  53.   } else if (i2s_set_pin_result == ESP_FAIL) {
  54.     debugln("i2s_set_pin result: IO error!");
  55.   } else {
  56.     debugln("i2s_set_pin result: Unknown error!");
  57.   }
  58.  
  59.   if (numberOfActiveChannels == 4) {
  60.     esp_err_t i2s_set_clk_result = i2s_set_clk((i2s_port_t)i2s_num, sampleRate, I2S_BITS_PER_SAMPLE_16BIT, (i2s_channel_t)(I2S_TDM_ACTIVE_CH0 | I2S_TDM_ACTIVE_CH1 | I2S_TDM_ACTIVE_CH2 | I2S_TDM_ACTIVE_CH3));
  61.     // i2s_set_clk error handling
  62.     if (i2s_set_clk_result == ESP_OK) {
  63.       debugln("i2s_set_clk result: success!");
  64.     } else if (i2s_set_clk_result == ESP_ERR_INVALID_ARG) {
  65.       debugln("i2s_set_clk result: Parameter error!");
  66.     } else if (i2s_set_clk_result == ESP_ERR_NO_MEM) {
  67.       debugln("i2s_set_clk result: Out of memory!");
  68.     } else {
  69.       debugln("i2s_set_clk result: Unknown error!");
  70.     }
  71.   } else if (numberOfActiveChannels == 8) {
  72.     esp_err_t i2s_set_clk_result = i2s_set_clk((i2s_port_t)i2s_num, sampleRate, I2S_BITS_PER_SAMPLE_16BIT, (i2s_channel_t)(I2S_TDM_ACTIVE_CH0 | I2S_TDM_ACTIVE_CH1 | I2S_TDM_ACTIVE_CH2 | I2S_TDM_ACTIVE_CH3 | I2S_TDM_ACTIVE_CH4 | I2S_TDM_ACTIVE_CH5 | I2S_TDM_ACTIVE_CH6 | I2S_TDM_ACTIVE_CH7));
  73.     // i2s_set_clk error handling
  74.     if (i2s_set_clk_result == ESP_OK) {
  75.       debugln("i2s_set_clk result: success!");
  76.     } else if (i2s_set_clk_result == ESP_ERR_INVALID_ARG) {
  77.       debugln("i2s_set_clk result: Parameter error!");
  78.     } else if (i2s_set_clk_result == ESP_ERR_NO_MEM) {
  79.       debugln("i2s_set_clk result: Out of memory!");
  80.     } else {
  81.       debugln("i2s_set_clk result: Unknown error!");
  82.     }
  83.   } else {  // can't get here
  84.     debugln("Error!!!");
  85.   }
  86.  
  87.  
  88.   // set sample rate
  89.   esp_err_t i2s_set_sample_rate_result = i2s_set_sample_rates((i2s_port_t)i2s_num, sampleRate);
  90.   if (i2s_set_sample_rate_result == ESP_OK) {
  91.     debugln("Set Sample Rate Success!!!");
  92.   } else if (i2s_set_sample_rate_result != ESP_OK) {
  93.     debugln("Set Sample Rate fail");
  94.   }
  95.   // check sample frequency
  96.   debug("Sample frequency set: ");
  97.   debugln(i2s_get_clk((i2s_port_t)i2s_num));
  98. }

Who is online

Users browsing this forum: No registered users and 86 guests