cancel
Showing results for 
Search instead for 
Did you mean: 

Dealing with erratic write performance of USB drives using FatFS lib and mass storage host

sully
Associate II

Hi folks,

I'm sampling 8 microphones using 4xI2S peripherals using DMA and a double buffer, which I then use to write to 4xWAV/PCM files on a USB drive and am getting buffer overruns intermittently!

I believe I've narrowed it down to the FatFS file write, where I'm recording wildly different performance across different USB drives, but also between writes on the same USB.

To diagnose, I wrote data to 3 different USB drives using 1KB, 2KB, 4KB, 8KB and 16KB blocks. For each block size I executed the write operation 256 times and recorded each time using HAL_GetTick() to get a close-enough measurement.

As expected, the no-name USB 2.0 was the worst performing. The Sandisk USB 3.0 was next but the Toshiba USB 2.0 was almost twice as fast again and with much more consistent times!

Of the write block sizes, 8KB was somehow the magic value, with 745KB/s sustained speed, 11ms minimum, 11ms average and 48ms max. Performance dropped at higher and lower block sizes.

However, every now and then I would get extremely large pauses! Anywhere from 108ms up to 1350!! This obviously caused the DMA channel to overrun.

Does someone know why this extreme difference in write performance occurs and what I might do to get more consistent write times or otherwise work around it?

Thanks

2 REPLIES 2
raptorhal2
Lead

Flash memory does not behave like magnetic memory.

Flash memory is organized into erasable blocks. When the controller needs another block to start writing in, it does a block erase to get it to a writable state. This is probably the reason for the large pauses.

The controller also takes time (less than a block erase) to switch to a new writable page within a block. Your 8KB data size probably matched the drive's page size, giving the best performance.

You might be able to find a speed demon drive that will work for you. The only other work around that comes to my mind is using a larger ram buffer to queue the data.

Internet searches on USB drive speed and write cycles can yield more detailed information.

Cheers, Hal

raptorhal2
Lead

On second thought, to eliminate the large pauses, start with an empty USB drive.

Cheers, Hal