Yep, for the first time this will be (mostly) a code article .
But if one day you want to make a Hi-Speed (480Mbit/s) USB composite device with CDC (communication) and MSD (mass storage) classes, this is the post you'll want to read.
At my current job we're currently working on a complete open software & hardware development platform for physicists,biologists.... hobbyists.
This development platform is primarily aimed at facilitating the realization of high speed (several GHz) functions that quantum physicist need for their experiments, as well as providing data collection capabilities.
Without going into many details, it will physically be a 3U rack where users can plug different eurocard modules that perform specific functions.
The platform is open hardware/software for Windows and Linux, which actually made the project's start quite difficult.
Anyway, the chosen microcontroller is a cortex-m3 based ATSAM3U from Atmel, that provides USB 2.0 connectivity.
Each module put in the 3U rack will be connected by USB to a main computer.
When connected, it will be recognized as a storage device in order to send to the computer the files required to operate it (matlab/labview/etc etc) and also be recognized as a communication device for the computer to send commands to the module.
For this project, we therefore needed (from the host computer) to:
- read/write data to a micro SDCard connected to the uC
- dialog with the uC
and from the microcontroller:
- read all the files stored on the SDCard
And for debugging, we picked the Segger J-Link JTAG:
As you can guess, developping this kind of code from scratch can be insanely time consuming (I mean weeks of coding).
Therefore, there was no other choice than to find libraries that provide a USB Stack with CDC/MSD classes, SDCard interfacing, FAT32 reads, while also supporting DMA transfers.
As you may know, I hate using libraries. They are usually not well documented and you have no clue on how they internally work.
Well, after having spent a few days browsing through them, this impression was justified.
But I finally managed to get things working with:
- the AT91lib from gnumonks
- the fat library from chan
As parts of the at91lib don't officially support the ATSAM3U, there were very few tweaks to do (mentionned in the main.c). There was one particular bug that took me 4 days to find (ATSAM3U DPRAM initialization).
Finding all the files & flags to include in the makefile was quite a challenge, so I hope I'll save you the trouble. With one cheap micro SDCard, we managed to get 5.5Mbyte/s writes. We (yet) haven't tried with other cards.
Anyway, here is the Eclipse source code, which is still in beta : Eclipse template code
Please note that this firmware doesn't have fancy options such as card detection (which we didn't need).