ChaN's FatFS

I need some way to write and read data from an SD card. For this an interface to the FAT filesystem would be very nice; I can then read and write file on an SD card and also access that same data on my PC.

On the support page with all the application notes, there is a nice app.note (AN10916) that contains two different libraries. The first one is EFSL, the second is FatFS.

ChaN's FatFS is a very nice and wll maintained implementation of a Fat filesystem for microcontrollers. It has more functions than EFSL (is also a bit bigger) but it can be configured such that functions that we do not need are not included. Also, FatFS is capable of handling the newer SD card standard (v2) that allows the use of high capacity SD cards. It has baan confirmed to work with 8 GB SD cards.

When I started using FatFS I found some strange problems.
In my application I had problems when initializing the system and half way writing a file I got errors. Time to investigate these problems ...

On the LPCXpresso forum also problems have been reported. So I decided to create a test program to check "fitness for use". I need to be able to run a FatFS implementation on my own LPC1754 board or on the lpc1769 module on Embedded Artists Base Board.

Things I like to do:

  • Test with different clock frequencies of the SSP port.
  • Report all errors from FatFS to the user via the standard UART port.
  • Write/Read test to validate larger block writes.
  • Write/Sync test to validate smaller writes (e.g. when using FatFS for data- or error-logging).
  • Timing tests.

There is a basic user interface in which you can type commands.

"speed 1m"  will set the SPI clock to 1 MHz, "speed 25m"  will set it to 25 MHz. Any number can be filled in to get the frequency of your choice. Please note that the real SPI frequency may be lower due to the fact that it is not possible to create the exact wanted frequency. E.g: if you select "speed 11m", the system will respons with setting the clock to 10 MHz.
"write filename"  will write 50 sectors to the given file. If the file already exists or if there is any other problem, it will be reported. If the system is able to write the file it will respond by printing the time for each f_write() call.

It is best to check the source code to see what it actually does.

Testing

To help you locating any errors during initialization I have logged the initialization sequence as seen with my SD card. Please have a look at this page for a complete overview of the SP transactions.

On startup the program defaults to a high SPI clock of 1 MHz and standard timeout values as specified by ChaN.
I found that my write sometimes fails due to a timeout. Somehow the SD_SendDataBlock() function returns an SD_FALSE due to the fact that the timer expires while waiting for the write command to complete. Instead of changing the timeout in sd.c I decided to make the 10 ms timer interval that is used for the timer configurable.

Commands used

Here is a list of all the commands understood by the test progam:

  • speed <number>
    Set the SPI high clock frequency to the given speed. The number can be followed by a 'k' or 'm' to set the frequency in kHz or MHz.
    Note that not all frequencies are possible. The actual frequency will be the highest possible value that is not bigger than the requested speed.
  • timeout <number>
    Sets the timeout interval used in ms.
    Per default FatFS's SD interface expects that the disk_timerproc() function is called every 10 ms. I've made this value changeable to allow testing for timeout errors.
  • dir
    Gives a directory list of the SD card
  • write <fname>
    Writes a file with 50 x 512 bytes using a test pattern. Writing in blocks of 512 bytes.
  • read <fname>
    Reads a file and checks if the data is the same as the test pattern written.
  • log <fname>
    Creates a file with text, writing per string and following each write with an f_sync().
  • type <fname>
    Type the content of a text file to the terminal to validate the log file.
  • del <fname>
    Delete the file with the given name

If on startup, or at any other moment, an error occurs, reset the software by performing a "timeout 20" and a "speed 1M" command sequence. This should fix any problems by resetting all data structures.

Downloads

  • FatFS_Test_v0.3.zip
    Test program to test FatFS, configured for the EA Base Board with lpc1769 module.
    Please note: this is still in development and there is no documentation.