#include #include #include #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "nvs.h" #include "nvs_flash.h" #include "esp_system.h" #include "esp_log.h" #include "driver/i2s.h" //Board ESP32 Dev Module //error type esp_err_t err; //buffers int rxbuf[256], txbuf[256]; #define SAMPLE_RATE 8000 #define BUFF_COU 8 #define BUFF_LEN 64 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ void setup() { Serial.begin(115200); delay (1000); //enable MCLK on GPIO0 //REG_WRITE(PIN_CTRL, 0xFF0); //PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0_CLK_OUT1); //------------------------------------------------------------ i2s_config_t i2s_config = { .mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_RX), // i2s_mode_t work mode .sample_rate = SAMPLE_RATE, // int sample rate .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, // i2s_bits_per_sample_t bits per sample .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT, // i2s_channel_fmt_t channel format .communication_format = I2S_COMM_FORMAT_I2S_MSB, // i2s_comm_format_t communication format .intr_alloc_flags = 0, // int Flags used to allocate the interrupt. One or multiple (ORred) ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info .dma_buf_count = BUFF_COU * 2, // int DMA Buffer Count .dma_buf_len = BUFF_LEN, // int DMA Buffer Length .use_apll = true, // bool using APLL as main clock, enable it to get accurate clock .tx_desc_auto_clear = true, // bool auto clear tx descriptor if there is underflow condition (helps in avoiding noise in case of data unavailability) .fixed_mclk = true // int using fixed MCLK output. If use_apll = true and fixed_mclk > 0, then the clock output for is fixed and equal to the fixed_mclk value }; err = i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL); if (err != ESP_OK) { Serial.print("Failed installing driver: "); Serial.print(err); while (true); } //------------------------------------------------------------ i2s_pin_config_t pin_config = { .bck_io_num = 32, // INMP441 SD 32,MAX98357A BCLK 32 .ws_io_num = 25, // INMP441 WS 25,MAX98357A LRC 25 .data_out_num = 15, // MAX98537A DIN 15 OUT .data_in_num = 33 // INMP441 SCK 33 IN }; err = i2s_set_pin(I2S_NUM_0, &pin_config); if (err != ESP_OK) { Serial.print("Failed setting pin: "); Serial.print(err); while (true); } Serial.println("I2S driver installed."); } //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ void loop() { size_t readsize = 0; //read 256 samples (128 stereo samples) err = i2s_read(I2S_NUM_0,&rxbuf[0],256*2, &readsize, 1000); if (err == ESP_OK && readsize==256*2) { for (int i=0; i<256; i++) { txbuf[i] = rxbuf[i]; } //write i2s_write(I2S_NUM_0, &txbuf[0],256*2, &readsize, 1000); } // end of cycle } // end of void loop()