I2S Record to SDCard using FATFs
Posted: Thu Mar 07, 2019 12:43 pm
Hi All
I have to record to a wave file from I2S input coming from an external ADC (es8388 similar to LyraT board).
My code works fine up to stereo recording 16 bits at 44.1 KHz.
With higher resolution (48K or 96K), a lot of glitches appears in the wave file while recording.
I tried using different DMA buffers sizes and count, I also tried to setup SDCard in high speed mode, unfortunately nothing helps.
I'm sure the issue comes from the SDCard performance while writing, since if I disable SD card writing and pass thru I2S ADC to I2S DAC I can see a clean sine wave on the DAC output, also for 48K or 96K sampling rates.
I'm not using ESP-adf with pipelines to record, I'm using my own code as follow :
Any experience on this ? on how to improve performances of fwrite while using FATFs system ?
Also is there something to do with portMAX_DELAY passed in i2s_read() ?
Any help would be greatly appreciated, as I'm currently out of ideas.
Thanks.
I have to record to a wave file from I2S input coming from an external ADC (es8388 similar to LyraT board).
My code works fine up to stereo recording 16 bits at 44.1 KHz.
With higher resolution (48K or 96K), a lot of glitches appears in the wave file while recording.
I tried using different DMA buffers sizes and count, I also tried to setup SDCard in high speed mode, unfortunately nothing helps.
I'm sure the issue comes from the SDCard performance while writing, since if I disable SD card writing and pass thru I2S ADC to I2S DAC I can see a clean sine wave on the DAC output, also for 48K or 96K sampling rates.
I'm not using ESP-adf with pipelines to record, I'm using my own code as follow :
Code: Select all
// LOOP RECORDING
do
{
//read data from I2S bus
esp_err_t res = i2s_read(I2S_NUM, (void*)i2s_read_buff, I2S_DMA_BUFF_LEN_BYTES, &bytes_read, portMAX_DELAY);
if(res != ESP_OK)
{
ESP_LOGE(TAG, "i2s_read error (ESP_FAIL) -> STOP RECORDING");
break;
}
if(bytes_read != I2S_DMA_BUFF_LEN_BYTES)
{
ESP_LOGE(TAG, "i2s_read error (bytes_read != I2S_DMA_BUFF_LEN_BYTES)");
}
// write to SD Card
size_t bytes_written_sd = fwrite(i2s_read_buff, bytes_read, 1, fileRec);
if(bytes_written_sd != 1)
{
ESP_LOGE(TAG, "fwrite error -> STOP RECORDING");
break;
}
} while((xEventGroupGetBits(s_status_event_group) & STOP_RECORDING_BIT) == 0)
Also is there something to do with portMAX_DELAY passed in i2s_read() ?
Any help would be greatly appreciated, as I'm currently out of ideas.
Thanks.