This is based on FatFs 0.8b, the latest release at the time of writing. We wanted to use the SD card slot on the standard arduino ethernet shield, and there’s a standard arduino provided SD library for working the sd card slot. However, despite the claims in the arduino FAQ, where it says, “… the C/C++ microcontroller libraries are under the LGPL”
and “Using the Arduino core and libraries for the firmware of a commercial product does not require you to release the source code for the firmware. The LGPL does, however, require you to make available object files that allow for the relinking of the firmware against updated versions of the Arduino core and libraries. Any modifications to the core and libraries must be released under the LGPL.”
this is actually untrue. Even on the SD card library reference page or Notes on using SD cards no mention is made of the fact that the SD library is actually GPLv3. Because the library is based on the GPLv3 sdfatlib
This is a pretty glaring omission, and a rather rude piece of code to just slip in.
Fortunately, there is also FatFs, and it’s tiny sister, PetiteFs, which are both under a BSD style license. They’re also plain C, and a lot more portable, with the sample code working on not just AVR, but ARM, i386, h8, and on and on.
So, I thought it should be possible to get that working with our mutant arduino here. Two websites were very useful in getting me started in the right direction, (in addition to the FatFs documentation)
- http://www.basementcode.com/avr/sd_fatfs/fatfs.php
- http://frank.circleofcurrent.com/cache/fat_sd.htm
What did I actually have to do?
- Download FatFs sample, and grab
diskio.h, ff.c, ff.h, ffconf.h, integer.h
andmmc.c
and put them in my project - Replace allll instances of BYTE with BYTEF (or anything else you want) in those files. BYTE is already defined (and differently) by the Arduino codebase
- In diskio.h, I had to add the following c++ wrapper
#ifdef __cplusplus extern "C" { #endif
at the start, and close it at the end.
- Edited the ffconf.h to suit, (This is up to you…)
- in mmc.c, I added
#include "WProgram.h"
- in mmc.c, I replaced the #defines for CS_LOW and CS_HIGH with
digitalWrite(4, LOW/HIGH)
. For the Arduino Ethernet shield, chip select is on pin 4. - in mmc.c, the three
power_*
functions were stubbed out. The ethernet shield doesn’t support this. - in mmc.c, the
disk_timerproc
function had the write detect and card inserted logic removed, again, the ethernet shield doesn’t support this. - in mmc.c, because I wanted write support, I had to provide a function,
DWORD get_fattime()
, which simply returns 1. This is the file create time, and if you don’t really care what timestamp you get, a fixed value is fine. You would normally hook this up to an RTC or something.That’s about it. Then, just be careful to make sure that SPI is set up properly (taken care of if you’re using the ethernet on the ethernet shield) and, very importantly, make sure to include
pinMode(4, OUTPUT)
to make sure that the SD cards chip select pin is actually an output. If you forget this, it will sometimes work, and sometimes fail, and be very very very unpredictable.Oh, And you need to set up a timer to call disk_timerproc() every 10ms. The circleofcurrent page shows how to work around this with fixed loops if you’d prefer, but I just set up Timer2 to do this. This list of arduino resources used seems to imply that Timer2 is safe for us, but your mileage may vary. Here’s how we set up Timer2
TCCR2A = _BV(WGM21); // CTC MODE TCCR2B = _BV(CS22) | _BV(CS21) | _BV(CS20); // clkdiv 1024 TIMSK2 = _BV(OCIE2A); /* Output compare match interrupt */ OCR2A = 156; // 156 ticks at 16Mhz/1024 = ~10ms // Needs to be turned on before we start trying to wake up the sd card! sei(); // fatfs disk init code below...
This was all done on an Arduino Mega 2560 board.
I just noticed the Arduino SD library is GPL, and I was led here. I’m going to try the FatFS changes you described. Great work!
Reading about FatFS and all his other projects makes elm-chan my new role model.
Hi
I just started on the Arduino.
I bought the mega 2560 with the Ethernet shield, and I would like to see what the SD card capabilities are. The examples I found do not fully utilize all the file system features. Can you perhaps post the project somewhere, or email it to me?
I want to run a simple web server that will output a page with multiple pictures on it using I want to check the load speed when loading images from the SD. The other projects seem half.
I found another way.
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1291319677/6