The Microchip Applications Library has a lot of great code that is easy to integrate and can bump up the feature set of your product in hours. So far I have had the opportunity to use some of the USB (MSD,HID,MS-Host) stacks along with their FAT library (FSIO). The USB stacks, along with their well written application notes are very good and quite easy to integrate, even if you know nothing about USB. Hovever, their FAT library is not the best out there. It takes up quite a lot of code + data space, is slower than Evolution (when there’s no external duress) and not extensible – for instance to support multiple volumes or long file names.
FatFs can solve all these problems. It seems to the handiwork of a one samurai army, who has done a lot of other cool projects too. In spite of doing all this stuff that I would gladly sell off my left testicle for, he actually refers to himself as a “mediocre” embedded systems engineer!! Anyhow, it’s also released with a free, open source, do-whatever-crap-you-can-with-it license.
This post will explain how to integrate FatFs to a Pic24 project. Sample code is also available for download. Most of the information contained here is just an aggregation of the things I found trawling the posts on Microchip forums.
Before going into the details, the files involved and their function should be evident. This is summarised in the table below. Note that both FatFs and FSIO have separated the FAT layer from the lower disk access layer. So you can build the system on different types of memory, furthermore in the case of FatFs your application can manipulate multiple memory types simultaneously. In this example I will only consider SD cards (over SPI) and USB Mass storage devices. You should also download the latest FatFs library and the sample
|Function||MAL Files||FatFs Files|
|FAT File System||fsio.c/h + FS config files||ff.c/h + ffconf.h|
|SD card access||sd-spi.c/h||diskio.c/h|
1. Integrating FatFs (with an SD card access)
In this scenario, your PIC is interfaced to an SD/MMC card via SPI and you have to set up a FAT system to access data on the SD card. You should not have to change anything to the ff.c/h files. But you have to add a call to the timer function and implement a function to return the timestamp for the files. Then you have to modify the diskio.c routines to match your particular SPI port. There are a few sections in Chan’s code marked as “Platform Dependent” that you need to modify.
2. Integrating FatFs with the MAL USB Library
Your device is a USB Mass Storage Device Something that might not be apparent on first glance is that for the case of a USB MS-device, you DO NOT need to implement a FAT system on the Device. The USB Host will take care of implementing the file system and/or formatting the device. So all you have to do is plug the MAL USB MS-Device routines to your diskio.c/h routines (see usbdevFatFs.c/h in the example code).
Your device is a USB Mass Storage Host: In this scenario, your PIC is a USB Mass Storage Host and wants to read/write data on a USB Mass Storage Device like a thumb drive. Now in this case, your application does have to implement a FAT (or some other) file system on TOP of the USB layers in order to read / write the thumb drive. The difference between the case 2.b and 1. is that the diskio.c routines are different and they access the USB BUS and the SPI BUS respectively.
Finally, What if your PIC talks to an SD card, and also acts as both a USB Mass Storage Device and Mass Storage Host? This is a case when you can use the multiple volume support in the FatFs (one volume is the SD card and the other is the USB MSD thumb drive). All you have to do is pass the volume number to the FAT routines, and you have to modify diskio.c to call the appropriate low level routines based on the passed in drive volume number (see the diskio.c file in the example code). The example code is based on the USB Mass Storage host+device usecase, it’s tested and it works!