cancel
Showing results for 
Search instead for 
Did you mean: 

I'm working on a data logger. It intakes data with three SPI RX only slaves using DMA double buffer mode. It works fine except occasionally the f_write, which usually takes ~150 ms, takes ~1850 ms. Does anyone know why and if it's avoidable?

MStea.1
Associate II

I'm using a Nucleo F746ZG

The SPI is setup in RX only slave mode and uses DMA Double buffer mode. The data rate changes, but at max I should be writing right at 126 kBytes per second to the drive.

I'm using fatfs and USB driver with the cubemxide source code and slight modifications

4 REPLIES 4
KnarfB
Principal III

You are logging to a USB MSC device, right?

Correct, the USB is acting as a mass storage host.

There is no guaranteed timing in any FLASH device with controller, such as USB "stick" or SD card (or SSD for that matter). They can do whatever they like and whenever they like. From time to time they may need to erase a block and that takes time.

JW

Jack Peacock_2
Senior III

The problem writing to a flash based storage device is the non-deterministic latency built into the firmware of the storage device flash controller. At unpredictable intervals the internal flash drive must perform garbage collection, erasing deleted blocks to recover them for future use. Erasing flash blocks is very slow compared to a sector write.

To overcome this you need a separate task to manage data writes, with a data queue sufficient to buffer writes for however long the worst case latency is on the storage device (perhaps for several seconds). Your data collection task runs at a higher priority to ensure you don't drop data while an erase is in progress.

Jack Peacock