Long File Name Example Code

CHAN_FATFS

This code is a port of CHAN's FatFS file system code : http://elm-chan.org/fsw/ff/00index_e.html 

The SD File system in the cookbook uses this same code but does not support long file names, directories and time/date stamps from the real time clock.   This is an example program of how to use CHAN's library to get these features.

Notes:

1.)  This code was derived from Simon Ford's SDFileSystem Class.  I castrated the C++ class to get some nice sector access functions to patch into the FatFS library.   All credit goes to simon for his very nice SD sector access functions!

2.)  You cannot use fopen,fclose, etc. to read/write files.   You must use the CHAN API.  This is done for 2 reasons.  The first being that I need faster code and the asbtraction of CHAN's library to C standard streams adds un-needed overhead.   The second reason s that I have no idea how to hi-jack the C libraries to make this happen.   CHAN's API is simple and is much faster.

3.)  I did this port to get long file name's etc.  CHAN's libary has a header file (ffconf.h) that allows you to control many features.  The SdFileSystem class strips out alot of features to save code/memory space.  I wanted more control over what was in the library.  As such I will never provide the work in a compiled form.  You can import the source and configure the library to suit your needs.  Everything is in the chan_fs folder.  You just need to include "ff.h" to get the file system functions

4.)  Other than the SPI and CS access functions,  everything is plain jane C.  I do not plan to abstract the whole library to a C++ class.  THis is a philosphical position on my part.   

5.)  You can modify the SPI and CS pins to suit your layout in the diskio.c code.   Right now I use the MBED SPI/Digital out classes.  It will eventually move to plain C code that directly talks to peripheral registers to improve speed.

6.)  Some of the functions require a drive number.  Any number will map to the SD card.  CHAN's diskio interface allows you to have multiple physical drives.  I simply ignored the drive number and always make sector access to the Sd card.

7.)  I implemented the ioctl to give chan's library the actual sector size.   I think this allows one to use SD cards with other than a 512 byte sector size.  I have a 2GB Kingston microSD that reports a 1024 byte sector size.   Other than the warning from Simons code,  CHAN's FATFs seems to handle it correctly.   You may need to modify the _MAX_SS  macro in 'ffconfig.h" to suite your card.

8.) The OEM code page used for LFN is 437 (U.S. OEM) This should work for most people.   See the 'ffconf.h" file is you think you need something else.   If so, You will need CHAN's raw source to get the proper .h file for the LFN namespace configuration. 

Hope someone else finds this usefull!!!


4 comments

22 Jul 2010

Wow, kinda wish I knew this was here before I started.  I'll give it a look.

It looks like the part I'm interested in is something that your code excludes.  I really want to use the fopen and fprintf functions but can't figure it out

22 Jul 2010

The CHaN api supports a subset of fprintf:

http://elm-chan.org/fsw/ff/en/printf.html

 

12 Aug 2011

Thank.

24 Apr 2014 . Edited: 24 Apr 2014
I have made a change on the driver to read files with a "Long File Name" here you go, I have tested on the DE0-NANO using Nios II and it works perfectly the file is below this page and it is named sd_card_LFN.zip https://sites.google.com/site/ece31289upb/laboratorios/laboratorio-3 Best regards

You need to log in to post a comment