I2S Record to SDCard using FATFs

jerome
Posts: 27
Joined: Wed Jan 23, 2019 2:28 pm

Re: I2S Record to SDCard using FATFs

Postby jerome » Mon Mar 11, 2019 5:57 pm

Thanks for your answer.
Does this mean recording audio to SDCard using FATFs is not possible ?
Using an oscilloscope, the minimum time I measure to continuously write blocs of 16128 bytes to SD is about 50ms to 70ms (depending on the SD Card) as follow :

Code: Select all

void mytask()
{
     while(1)
     {
             // wait trigger to start recording
             ...

             do
             {
                   // toggle one IO to check with scope
                   ...
                   fwrite(recorded 16128 bytes from I2S);
                   // delay 1ms to avoid watchdog error                   
                   vTaskDelay(1 / portTICK_RATE_MS);

             } while (!trigger to stop recording);
     }
}
On the other side, filling one bloc of 16128 bytes from I2S @ 24/96K/Stereo is about 17ms.
According to the above, even if using huge circular buffers, recording is not possible.

Few questions :

1) Do you think using a faster SDCard could help ? I already tried with 2 different models SDHC
2) Is there anything to setup in make menuconfig that could improve fwrite() ? BTW I'm using long file names for FAT.
3) What is the most efficient between writing blocks of 512 bytes VS blocks of 16K bytes ?

I'm stuck on this, any idea would be greatly appreciated.

User avatar
fly135
Posts: 605
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: I2S Record to SDCard using FATFs

Postby fly135 » Mon Mar 11, 2019 6:00 pm

jerome wrote:
Mon Mar 11, 2019 5:57 pm
I'm stuck on this, any idea would be greatly appreciated.
Take a look at this post....

viewtopic.php?f=13&t=3787

jerome
Posts: 27
Joined: Wed Jan 23, 2019 2:28 pm

Re: I2S Record to SDCard using FATFs

Postby jerome » Wed Mar 13, 2019 9:37 am

I found my issue I think.
On the ESP32 I was using large circular buffers, stored in external SPI RAM (128 buffers of 16KB each allocated with calloc()), sending those buffers to fwrite() were causing problems, seems like the SPI RAM access is very slow ?
I checked using only 2 buffers located into the ESP32 internal RAM (declared as global var buffers[2][16384], and I did not longer observe SDcard hanging for several hundreds of milliseconds.
Will further investigate on this, but 24/96/stereo seems to work fine now.

Can anybody confirm storing buffers in external SPI cannot work for continuously writing to SDCard at a high data rate (16KB each 25ms) ?
Thanks

ESP_Sprite
Posts: 2651
Joined: Thu Nov 26, 2015 4:08 am

Re: I2S Record to SDCard using FATFs

Postby ESP_Sprite » Thu Mar 14, 2019 8:55 am

FWIW, external SPI ram, when used as a ringbuffer, is theoretially limited to 40MByte/second at 80MHz or 20MByte/s at 40MHz. As you are reading and writing about 1MByte/sec (total 2MByte/sec), I don't think the SPI RAM per se is the issue... however, the core can't do anything else while it's waiting for SPI RAM. You may be better off restructuring your code so it runs on two cores; if one blocks on a SPI RAM access, the other can still do useful work.

jerome
Posts: 27
Joined: Wed Jan 23, 2019 2:28 pm

Re: I2S Record to SDCard using FATFs

Postby jerome » Thu Mar 14, 2019 3:51 pm

It seems it is related to fread/fwrite functions.
When the arguments to those functions are buffers allocated with calloc, sdcard (or FATFs) hangs.
On the other side, when using static buffers declared with buffer[16384], everything works fine, no more hangs.

Nobody else observed this issue ?

Menu config is configured as shown in the screenshot, anything that may cause such a issue ?
Thanks
spiRAM.png
spiRAM.png (62.41 KiB) Viewed 720 times
spiRAM2.png
spiRAM2.png (27.98 KiB) Viewed 719 times

ESP_Sprite
Posts: 2651
Joined: Thu Nov 26, 2015 4:08 am

Re: I2S Record to SDCard using FATFs

Postby ESP_Sprite » Fri Mar 15, 2019 2:22 pm

If you allocate buffers that large using calloc, it will also allocate them in PSRAM, meaning the data has to go through another round-trip through the SPI bus. If you allocate them statically, they normally will be allocated in internal memory, which is much faster. That may be the root (and if not, at least a confounding factor) of your problem. Note that you can specify where you want to allocate your buffers by using heap_alloc_caps().

Who is online

Users browsing this forum: No registered users and 17 guests