User Tools

Site Tools


esp32:wm8782

WM8782 i2s ADC esp32

https://www.cirrus.com/products/wm8782/

some code that helped: https://www.esp32.com/viewtopic.php?t=3658

#include <driver/i2s.h>
 
const i2s_port_t I2S_PORT = I2S_NUM_0;
 
#include "driver/mcpwm.h"
 
void setup() {
  Serial.begin(115200);
 
  Serial.println("Configuring I2S...");
  esp_err_t err;
 
  int SAMPLE_RATE = 8000; //44100; //16000; //44100; //44100; // 100000; //176400; //96000; //96000;
 
  const i2s_config_t i2s_config = {
    .mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX), // Receive, not transfer
    //.mode = i2s_mode_t(I2S_MODE_SLAVE | I2S_MODE_RX), // Receive, not transfer
    .sample_rate =   44100, // 48000,   44100, //                      // 16KHz
    .bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT, // could only get it to work with 32bits
    .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
//    .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, // 2-channels
    .communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB),
    .intr_alloc_flags = 0,
    .dma_buf_count = 16,
    .dma_buf_len = 256
  };
 
  const i2s_pin_config_t pin_config = {
    .bck_io_num = 14,   // BCKL  SCK BCLK
    .ws_io_num = 15,    // LRCL LRCLK  FS
    .data_out_num = -1, // not used (only for speakers)
    .data_in_num = 32   // DOUT SD SDATA, SDIN, SDOUT, DACDAT, ADCDAT
  };
 
  // Configuring the I2S driver and pins.
  // This function must be called before any I2S driver read/write operations.
  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 driver0 installed.");
 
//  PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0_CLK_OUT1);
//  WRITE_PERI_REG(PIN_CTRL, READ_PERI_REG(PIN_CTRL) & 0xFFFFFFF0);
 
  REG_WRITE(PIN_CTRL, 0b111111110000);
  PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0_CLK_OUT1);
  //https://esp32.com/viewtopic.php?f=5&t=1585&start=10
 
  //  mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0A, I2S_MCLK);
  //  mcpwm_pin_config_t pin_config = {
  //    .mcpwm0a_out_num = I2S_MCLK
  //  };
  //  mcpwm_set_pin(MCPWM_UNIT_0, &pin_config);
  //  gpio_pulldown_en(I2S_MCLK);
  //  mcpwm_config_t pwm_config;
  //  pwm_config.frequency = (SAMPLE_RATE) * 256; //when frequency is too high (actually only 1MHZ)there is no signal
  //  pwm_config.cmpr_a = 50.0;
  //  pwm_config.cmpr_b = 50.0;
  //  pwm_config.counter_mode = MCPWM_UP_COUNTER;
  //  pwm_config.duty_mode = MCPWM_DUTY_MODE_0;
  //  mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_0, &pwm_config);
}
 
#include <iostream>
#include <algorithm>
#include <array>
 
const int numCommunicationData = 256; //256; //1400;
unsigned char communicationData[numCommunicationData];
void loop() {
  int num_bytes_read = i2s_read_bytes(I2S_NUM_0, (char *) communicationData, numCommunicationData, portMAX_DELAY);
  //  auto maxv = *std::max_element((int32_t*)communicationData, (int32_t*)communicationData + 255 / 4);
  //  auto minv = *std::max_element((int32_t*)communicationData, (int32_t*)communicationData + 255 / 4);
  //  Serial.printf("%d, %d, %d\n", num_bytes_read, minv, maxv);
 
  auto v1 = *((int32_t*)communicationData + 0);
  auto v2 = *((int32_t*)communicationData + 1);
  Serial.printf("%d, %d, %d\n", num_bytes_read, v1, v2);
}

simmilar board

esp32/wm8782.txt · Last modified: by aistis