about
Wow! FAT on a SD card! NO WAY!!! This is pretty much like flashing a LED with a uC - its been done a million times before but that shouldn't stop you. I actually have a project that I intend to use an SD card with so this project was really to get part of that software base proved out. I have had some help on this which allowed me to get my code working in just a few hours.
Almost all of the code was provided by the
ELM FAT FS Module through the module itself and the
examples. ELM is a popular FAT implementation optimized for 8 and 16-bit microcontrollers with support for FAT12, FAT16, and FAT32. Additionally the example code shows how to interface to MMC/SD, ATA, and CF cards using a number of popular uCs.
Hardware
- Run the system at 3.3V or adjust the signal and power levels since SD cards are strictly 3.3V devices (2.7V - 3.3V normally)
- All debug and output goes to the UART so a RS232 level shifter or similar is needed to get the output at 9600 baud
- The SD chip select pin is PORTB.4 which is the SS pin in SPI slave mode (however its just GPIO in master mode as used here)
#define SPICS 4 // Port B bit 4 (chip select for MMC)
A simple
schematic can be found here. Note that this design uses voltage dividers to drop the 5V AVR power to the 3.3V logic levels required by the SD card. Alternatively you can directly connect the IO pins to the SD card and run the whole system at 3.3V.
Software
This application is very simple. It simply tries to open and read a few text files I put on my dev SD card. Additionally the program will list every file on the SD card (by recursively scanning the root directory). This by itself isn't very useful (except the joy of using a filesystem with your 8-bit avr) however it lays the groundwork for an application that can read a great deal of data that is stored on the SD card and provides an simple way to transfer said data from your PC to the embedded device.
I would call this proof of concept code rather than solid, completely engineered software so take it with a grain of salt. I didn't setup the timers in the ATmega32 so there is no RTC or timeouts and the flashing led that signals the end of the program uses a
for loop based 'timer' so there could be great gains made in power saving and robustness. Luckily the timer setup is just a few lines of code in an interrupt service routine that calls
disk_timerproc() and the timeouts should start working. I am only using the read functionality so I don't really need an RTC for file timestamps either. That being said this code works really well and is mainly made up of the ELM code which seems well written and complete.
Sample Output
This is little more than some proof that the filesystem is working, still it makes me smile.
Test using FAT FS on SD card
fs mounted (i hope)
failed to open: dummy.txt
Open Error: 1
trying to read file: /test.txt
----------------------------------
this is a test file
sorry
----------------------------------
done reading file: /test.txt
trying to read file: test.txt
----------------------------------
this is a test file
sorry
----------------------------------
done reading file: test.txt
trying to read file: /i.fil
----------------------------------
this
file
has
spaces
and
newlines
----------------------------------
done reading file: /i.fil
trying to read file: /test/data.dat
----------------------------------
this is yet
another test file.
----------------------------------
done reading file: /test/data.dat
scanning root directory
/TEST/TEST~1.FIL
/TEST/data.dat
/test.txt
/i.fil
blinking LED now
--
ChristopherPepe - 19 Jan 2008