cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 Host USB Memory FatFs reads corrupt data

Marco Hess
Associate II
Posted on November 19, 2015 at 08:08

I am on a project with an STM32F407 on a custom board and I am having trouble with reading from a USB memory device.

The code framework is generated with CubeMX and setup to use FatFs and USB in host mode to access a USB memory stick.

While it mostly seem to be working, that is the USB enumerates, FatFs mounts and I can display directories and read and write small files (like an ini config file), I have trouble reading larger files like WAV files that are used for audio play back through a DAC.

When I open the WAV file, I read the header OK but as soon as I need to read more ( I read chunks of 2K bytes), the data I read is corrupt. Right after the f_read, I check the data buffer and there is corruption in the data. The buffer data is recognizable as I used a custom WAV file with a known simple waveform.

The data that I see is like this:

0, 0,

400, 0,

800, 0,

1200, 0,

1600, 0,

2000, 0,

2400, 0,

3568, 10,   <<< CORRUPT FROM HERE

32768, 12,

4096, 14,

40960, 15,

12288, 17,

49152, 18,

20480, 20,

57344, 21,

28672, 23,

I noted that even though the data is corrupt, it is fairly consistent from cycle to cycle i.e. every time I read the file it is the same corruption.

What could be the cause of this? I already spend days on this on something which is quite an obscure problem that may be lurking on the low level libraries or the CubeMX configuration that I may not understand or have missed (documentation is not very clear :( )

Any help much appreciated.

1 REPLY 1
Posted on November 19, 2015 at 13:07

When I do files system and IO sub-system validation I write very long streams of pseudo-random data that are easy to check, and hard for the system to accidentally get right with any common failure modes.

Suggest you validate the FatFs via USB writer thoroughly, and document the failure(s) to ST. If it validates Ok, then review more carefully what you are doing with the data and the file system.

The consistent failure is a bit odd, does it fall on a 64-byte boundary?

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