cancel
Showing results for 
Search instead for 
Did you mean: 

Advice for speeding up USB writes?

Chris Rice
Associate III

Hello, we are using an STM32F7 and are saving image data to USB, using mostly the BSP code (disk_write in usbh_diskio.c -> USBH_MSC_Write in usbh_msc.c and so on). However we have found it's quite slow, 6-7 seconds to save one 400x400 image.

I've started profiling and looking at the clocks (very complicated) to see if there is a bottleneck, or if this is something we will have to live with. Does anybody have any advice on improving our throughput? Thanks very much and happy new year.

18 REPLIES 18

4KB presumably cluster size

Pretty sure this is closer to 32KB on the multi GB SD Cards in FAT32

As i recall FATFS chops down larger buffers, the inefficiencies come when you force it to read/write to deblock data into sectors where the content is partially changed.

The other thing to watch is DMA, the platform expects 4-byte (32-bit word) alignment.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

FATFS does a good job of using data-in-place, but it doesn't handle memory alignment limitations of the architecture, CMx or STM32. These would need to be caught at the DISKIO level, or judital use of malloc() and bufferring on the user side.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Thank you... apparently the code #defines a sector size of 512 and extracts a cluster size of 4 sectors from something called a "disk access window" in FATFS.win.d8[BPB_SecPerClus] (haven't begun to pull that thread).

Which do you think is the right way to modify this, changing my sector size or something within the FATFS structure? And I'm sorry for the naive question, but do you know a way to check a USB stick to get this info? (sector size, cluster size, etc).

Partially answered my question... window System Information will tell me a few details on the USB stick, confirms that it is 512 bytes per sector. But it doesn't say anything about clusters.

This suggests I have to modify something buried a little deeper, the sectors/cluster (currently 4) in the FATFS.win.d8 structure.

Before I break things, is this the right direction? Thank you.

Mon2
Senior III

@Chris Rice​ , review this thread:

https://electronics.stackexchange.com/questions/394011/how-to-increase-write-speed-with-usb-high-speed-host-mode-with-stm32f4

and consider to source the referenced WaveShare board for some experimenting. Then you can compare the benchmarks and related solutions. This WaveShare board is based on an external Microchip USB PHY controller but appears to be faster from the posts.

https://www.waveshare.com/wiki/XCore407I

https://www.waveshare.com/

Formatting or using f_mkfs should create something reasonably optimal vs capacity.

chkdsk should output the allocation (cluster) size

C:\ct>chkdsk e:

The type of the file system is FAT.

Volume KINGSTON created 3/17/2018 1:56 PM

Volume Serial Number is E0FD-1813

Windows is verifying files and folders...

File and folder verification is complete.

Windows has checked the file system and found no problems.

2,003,501,056 bytes total disk space.

    32,768 bytes in 1 hidden files.

   360,448 bytes in 11 folders.

1,706,754,048 bytes in 83 files.

 296,353,792 bytes available on disk.

    32,768 bytes in each allocation unit.

    61,142 total allocation units on disk.

    9,044 allocation units available on disk.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Thanks... I actually have an allocation unit of 4,096! So I guess f_write is doing this correctly. Darnit.

So if this isn't a suboptimal buffering problem, any idea what it could be? I just experimented with my PC, and I can save one of my image files to this USB stick in significantly less than a second.

S.Ma
Principal

well, the other way around is to shrink a 8:8:8 24 bit into the classical 5:6:5 16 bit. This will shrink the file by 30% (and hopefully the speed) at minimal color quality loss,