USB SD card reader


My first project using PIC micro-controllers. It’s a little buggy and unfinished yet functional. The aim of this project is to record voice from an input line and save it in the SD card in a PCM uncompressed WAV file.

The schematic

It’s pretty simple: Just a PIC18F2525 or similar (we only need ADC and SPI module). I use a 7-segments LED display to debug (now it’s useless).

Schematic

The code

It’s simple but large because it requires FAT filesystem understanding and MMC access (which involves command/data sending and some requirements from the SD specs.).

  • FAT: A simple library which is capable of reading/writing files and listing directories which isn’t compatible with long file names. It respects the filesystem by updating all the FATs and has a little cache for fast look up. The code reserves free clusters whenever they are needed but it reserves more blocks than the required (configurable through a define) to speed up the writing. It’s very slow to reserve cluster by cluster because two FAT updates are needed (which is slow on a PIC).
  • Partitions: A simple code to look up the partitions which is capable of finding the start offset of a partition.
  • SD/MMC: Little code is needed for dealing with SD cards but I’ve only tested with one card, so I’d expect lots of incompatibility issues.

The rest of the code it’s self-explanatory and deals with sample acquisition and file writing (using 4 buffers because the write time depends on free cluster count and other things which aren’t predictable). 

Conclusions

sdcc is a crap! It has some serious bugs with pointers, just have a look at fat.c and you’ll see some strange code for doing simple things. It fails compiling some pieces of code with some assertion failure and I’ve seen other people code which say that it can generate bad code silently.

There’s a strange (!!!) bug which I couldn’t get rid of: using more than one FAT slows down the performance by ¼ or more! Writing some data to the card took about 1 minute, with 2 FATs (the most common case) took 5 minutes. And it’s stupid because the slowdown it’s in the second FAT writing which takes more time than 10 or 20 512 bytes writing. If anyone solves it email me!

Download sources