Hi!
I'm working on a project that requires recording data with high-quality audio (44.1 kHz @ 24-bit is desirable). The hardware includes ESP32 WROOM 32E, PMOD I2S2, and a Micro SD Module.
The code was adapted from this video and the first comment https://www.youtube.com/watch?v=WsDwj6SENOE.
Obviously I'm having problems with SD latency to write data.
I think I read all the topics related, but most of them are 10 years old (most of the answers provided by fat16).
Since I'm not able to run his benchmark test code, I asume I achieved maximum data rate with this code, because data is stored in binary, buffer size is 512. .open() and .close() are called once per recording.:
// ... includes, pin definitions, etc
// Buffer size to save data to SD is 512 bytes.
#define buffSize1 256
#define buffSize2 buffSize1/2
#define max_buffSize 512
const int sampleRate = 12000; // PMOD I2S2 sample rate. long int@ 12 kHz funciona.
int rxbuf[buffSize1], txbuf[buffSize1],
l_in[buffSize2] , r_in[buffSize2],
l_out[buffSize2], r_out[buffSize2];
// ... setups ...
void loop() { // Since the idea is to record T_ON minutes, then shut down T_OFF minutes
record(T_ON);
esp_deep_sleep_start();
}
void record (unsigned long record ){
unsigned long endTime = millis() + record;
if (!SD.begin(SD_CS)) { // Hacer funcion de error.
while (true) {
digitalWrite(LED_BUILTIN, HIGH);
delay(100);
digitalWrite(LED_BUILTIN, LOW);
delay(250);
}
}
File myFile = SD.open("/data.bin", FILE_WRITE);
if (!myFile) {
Custom_Error();
}
while(millis() < endTime){
size_t readsize = 0;
// Read PMOD I2S2 AD
err = i2s_read(I2S_NUM_0, &rxbuf[0], buffSize1 * 4, &readsize, 1000);
if (err == ESP_OK && readsize == buffSize1 * 4) {
int y = 0;
for (int i = 0; i < buffSize1; i += 2) { // Split left and right channel.
}
for (int i = 0; i < buffSize2; i++) { // Some DSP operations (optional).
}
y = 0;
for (int i = 0; i < buffSize2; i++) { // Generate I2S buffers.
}
// Write PMOD I2S2 DA
i2s_write(I2S_NUM_0, &txbuf[0], max_buffSize, &readsize, 10);
// Save data to SD.
myFile.write((const uint8_t *)r_out, sizeof(r_out));
myFile.flush();
}
}
myFile.close();
}
Maybe someone can help me improve this code. But I would like to ask if is possible to save data to multiple SD Cards?
My data es 32 bits (I2S works for me with 32 bits, but not 24 - ADC capability), so I was thinking to write each byte on a different SD Card with the same SPI bus (which would require 4 chip select and 4 sd card modules).
Or, use 2 SPI busses and save 2 bytes in each SD Card (with 2 sd cards, one per SPI bus).
Have someone tried something like this before?
Having other smalled boards as slaves is a possibility (attiny, micro, dont know, the cheaper the better).
A detail, this device is battery powered and the idea is to record information for 1 or 2 weeks.