Skip to content

Instantly share code, notes, and snippets.

@Roman-Port
Created April 22, 2022 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Roman-Port/f782be7f0ba8cae2ea42ac4e958fb1eb to your computer and use it in GitHub Desktop.
Save Roman-Port/f782be7f0ba8cae2ea42ac4e958fb1eb to your computer and use it in GitHub Desktop.
#include <string.h>
#include <sys/unistd.h>
#include <sys/stat.h>
#include <driver/i2s.h>
#define SAMPLE_RATE 325000
#define BITS_PER_SAMPLE 16
uint8_t buffer[1024];
void app_main(void)
{
//Set up I2S
i2s_config_t i2s_config = {
.mode = (i2s_mode_t)(I2S_MODE_SLAVE | I2S_MODE_RX),
.sample_rate = SAMPLE_RATE,
.bits_per_sample = BITS_PER_SAMPLE,
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
.communication_format = I2S_COMM_FORMAT_STAND_I2S,
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
.dma_buf_count = 16,
.dma_buf_len = 1024,
.use_apll = true,
.tx_desc_auto_clear = false,
.fixed_mclk = 41600000, //20800000?
.mclk_multiple = I2S_MCLK_MULTIPLE_DEFAULT
};
i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
//Set up pins
i2s_pin_config_t pin_config = {
.bck_io_num = 18,
.ws_io_num = 5,
.data_out_num = I2S_PIN_NO_CHANGE,
.data_in_num = 23
};
i2s_set_pin(I2S_NUM_0, &pin_config);
//Set up ports
int64_t targetByteRate = SAMPLE_RATE * (BITS_PER_SAMPLE / 8) * 2;
//Enter loop
size_t read;
size_t totalRead = 0;
int64_t start = esp_timer_get_time();
int64_t end = esp_timer_get_time();
while (true) {
//Read
i2s_read(I2S_NUM_0, buffer, sizeof(buffer), &read, 300);
totalRead += read;
if (read == 0)
printf("WARN: No data is being read from I2S!\n");
//Check if we've reached the end
end = esp_timer_get_time();
if ((end - start) > 5000000)
break;
}
//Print info
double speed = totalRead / ((end - start) / 1000000.0);
printf("FINISHED WRITING. Read %i bytes at %f bytes/sec (%f%% of target)\n", totalRead, (float)speed, (float)((speed / targetByteRate) * 100.0));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment