Go Down

Topic: Petit FatFs Wrapper for Arduino (Read 6663 times) previous topic - next topic

frank26080115

Sep 01, 2009, 05:36 am Last Edit: Sep 01, 2009, 05:47 am by frank26080115 Reason: 1
I wrote a library that acts as a wrapper for Petit FatFs written by ChanN
Petit FatFs is found at
http://elm-chan.org/fsw/ff/00index_p.html

This library allows you to read and stream files from a MMC or SD card which is formatted FAT.

My library is found at
http://code.google.com/p/circle-of-current/source/browse/#svn/petit_fatfs

You need to save all of those files into a directory called .....\Arduino\hardware\libraries\petit_fatfs\ to use the library

Usage documentation is in the comments of petit_fatfs.cpp , in a format that was originally ment for use with Doxygen.

I have tested all of the functions, but I'd like the community to try it out

You will need these SPI functions

Code: [Select]
byte rx()
{
 SPDR = 0xFF;
 loop_until_bit_is_set(SPSR, SPIF);
 return SPDR;
}

void tx(byte d)
{
 SPDR = d;
 loop_until_bit_is_set(SPSR, SPIF);
}

void spi_init()
{
 pinMode(10, OUTPUT);
 pinMode(11, OUTPUT);
 pinMode(12, INPUT);
 pinMode(13, OUTPUT);
 
 SPCR = _BV(MSTR) | _BV(SPE);      // Master mode, SPI enable, clock speed MCU_XTAL/4
}


Connect the hardware like so
http://frank.circleofcurrent.com/pics/sdcardconnections.jpg
with the CS pin connected to a pin of your choice (chosen when calling my library's begin function)

Note, the Arduino must be running at 3.3V and not 5V or else you risk damaging your MMC or SD card. Or, you can research into level shifting, read http://www.sparkfun.com/commerce/tutorial_info.php?tutorials_id=65

Here's a demo of some of the functions

Code: [Select]
#include <petit_fatfs.h>

void strobe(int pin)
{
 digitalWrite(pin, HIGH);
 pinMode(pin, OUTPUT);
 digitalWrite(pin, LOW);
 digitalWrite(pin, HIGH);
}

void setup()
{
 strobe(3);
 
 char * s = (char*)calloc(65, sizeof(char));
 
 spi_init();
 Serial.begin(4800);
 
 PFFS.begin(2, rx, tx);
 
 fileReadTest(PFFS.open_file("test.txt"), "test.txt");
 
 FILINFO fnfo;
 
 do
 {    
   dirReadTest(PFFS.read_dir(&fnfo), &fnfo);
 }
 while (fnfo.fname[0] != 0);
 
 Serial.print("Dir Rewind, err = "); Serial.print(PFFS.rewind_dir());
 
 do
 {
   PFFS.read_dir(&fnfo);
 }
 while (fnfo.fattrib & AM_DIR == 0 && fnfo.fname[0] != 0);
 
 Serial.print("Opening directory: "); Serial.println(fnfo.fname);
 
 int err = PFFS.open(&fnfo);
 
 if (err == 0)
 {
   do
   {    
     dirReadTest(PFFS.read_dir(&fnfo), &fnfo);
   }
   while (fnfo.fname[0] != 0);
   
   Serial.print("Dir Rewind, err = "); Serial.println(PFFS.rewind_dir());
 }
 else
 {
   Serial.print("Open dir failed, err = "); Serial.println(err);
 }
 
 Serial.print("Up one dir, err = "); Serial.println(PFFS.up_dir());
 
 if (err == 0)
 {
   do
   {    
     dirReadTest(PFFS.read_dir(&fnfo), &fnfo);
   }
   while (fnfo.fname[0] != 0);
 }
}

void dirReadTest(int err, FILINFO * fnfo)
{
 Serial.print("Read Dir, err = "); Serial.println(err);
 if (err == 0)
 {
   Serial.print("Name: "); Serial.print(fnfo->fname);
   if (fnfo->fattrib & AM_DIR)
   {
     Serial.println(" is a directory");
   }
   else
   {
     Serial.println(" is a file");
     fileReadTest(PFFS.open(fnfo), fnfo->fname);
   }
 }
}

void fileReadTest(int err, char * fp)
{
 if (err == 0)
 {  
   Serial.print("Reading File "); Serial.print(fp); Serial.print(" (err = "); Serial.print(err); Serial.println(" == 0) :");
   
   char * s = (char*)calloc(65, sizeof(char));
   int bytes_read;
   int bytes_cnt = 0;
   
   do
   {
     PFFS.read_file(s, 64, &bytes_read);
     bytes_cnt += bytes_read;
     Serial.print(s);
   }
   while (bytes_read == 64);

   free(s);
   
   Serial.println();
   Serial.print("Read "); Serial.print(bytes_cnt); Serial.println(" bytes");
 }
 else
 {
   Serial.print("Error code "); Serial.print(err); Serial.print(" while opening "); Serial.println(fp);
 }
}

void loop()
{
 
}


(you will need to include those SPI functions above)

I've also got this webpage explaining how to use the full FatFs with AVR microcontrollers
http://frank.circleofcurrent.com/content.php?page_id=fat_sd

westfw

looks nice.  I've been wishing for a smaller read-only FAT implementation...

novice

frank26080115

that Wrapper is much appreciated, and so is your 'pure AVR' material on your site.

Keep up the great work.

Go Up