FAT File System for Microchip Microcontrollers

This is FAT file system software for Microchip microcontrollers enables the microcontroller to read and write the SD/SDHC/MMC cards using a File system. This software supports a 10 character filename typically implemented by the application developer as the standard 8.3 filename convention used by DOS. SD/MMC cards written by this driver can be read by a PC and likewise the driver can read files written by a PC. The user manual is available here.

A reference hardware design for a data logger utilizing SD/MMC for storage and implemented with an PIC24FJGA002 microcontroller can be found here

This code is an implementation of the ELM Generic File System module which has been ported to the Microchip PIC24/dsPIC33 series microcontrollers with the Microchip C30, XC16 and CCS PCD compilers and the PIC18F series Microcontrollers with the CCS PCH compiler. The drivers for the PIC18 and PIC24/dsPIC33 have been extensively debugged and modified.

For the PIC18F series microcontrollers, the driver is supplied with a hybrid Data Logger application, which also features a DOS like command line interface incorporating a software real time clock (highly desirable for a file system). The data logger application, based on a real logger, was designed to record 12 bytes of data every 50ms and was tested successfully at 12 bytes per 10ms. However, the actual performance depends on many factors such as microcontroller clock speed, SPI bus speed, memory card access speed and other tasks being handled by the microcontroller. The data logger application demonstrates the concept of caching the incoming raw data while SD/MMC write operations of the data are in effect. Note that this is a proof of concept application. The marriage of the data logger function and the DOS command line interface are intended to show the developer how to use the various functions.

The file system drivers support standard (high performance) and LITE configuration modes. The standard mode implements a separate 512 byte read/write buffer for the file system and for each file. The LITE mode, enabled via a #define directive in the source code, offers all the features of the standard mode and is well suited to applications and controllers that have limited RAM available. The LITE mode shares a single 512 byte read/write buffer between the file system and all open files and is capable of supporting hundreds of files open concurrently. For high performance applications that require two or more files open concurrently, the standard mode is highly recommended.

     RAM requirements:         Standard           Lite
     File System overhead        560              560
     Per File Overhead           540               38

Subset of file specific functions:

    • FRESULT f_read (FIL *fp, void *buff, WORD btr, WORD *br); // Read from a file 
      FRESULT f_close (FIL *fp); // Close a file 
      FRESULT f_lseek (FIL *fp, DWORD ofs); // Seek file pointer 
      FRESULT f_stat (char *path, FILINFO *finfo); // Get file status
      FRESULT f_opendir (DIR *scan, char *path); // Initialize to read a directory
      FRESULT f_readdir (DIR *scan, FILINFO *finfo); // Read a directory item
      FRESULT f_getfree (DWORD *nclust); // Get number of free clusters 
      FRESULT f_mountdrv (void); // Force initialize the file system 
      void f_get_error_mesg(FRESULT Mesg, char *destination); // Convert the FRESULT return code to a string 
      FRESULT f_write (FIL *fp, void *buff, WORD btw, WORD *bw); // Write file 
      FRESULT f_sync (FIL *fp); // Flush cached information of a writing file 
      FRESULT f_unlink (char *path); // Delete a file or directory 
      FRESULT f_mkdir (char *path); // Create a directory 
      FRESULT f_chmod (char *path, BYTE value, BYTE mask); // Change file attriburte  
      FRESULT f_rename ( char *path_old, char *path_new); // Rename a file / directory
      FRESULT f_open (FIL *fp, char *path, BYTE mode); // Open or create a file

For the PIC24/dsPIC33 series microcontrollers, the driver is supplied with a sample application which features a DOS like command line interface.

Sample CLI commands:

    • CREATE name – create the file ‘name’ 
      DATE [dd/mm/yy] – set / display time  
      COPY src dst – copy file ‘scr’ to ‘dst’ 
      DIR – list directory 
      DUMP name – hex dump file ‘name’ 
      EXPLORE – explores the SD card data structure 
      INIT – initialise the SD card file system data structures 
      LOG [START|STOP] – display / change logging status 
      MD name – create directory ‘name’ 
      RESET – reset the PIC Microcontroller 
      STATUS – display system status 
      TIME – set / display time  
      TYPE name – list contents of text file ‘name’
      DELETE name – delete the file ‘name’

Aside from hardware and software challenges, the integration of SD and MMC cards in embedded systems is not without headaches. Licenses for integration of SD Cards or MMC Cards into products may be required from Microsoft, IBM, the SD Card Association (for SD Cards) and the Multimedia Association (for MMC Cards).

Microsoft has been granted patents covering the File Allocation Table (FAT) file system. These patents pertain to the integration of long file names with legacy DOS file names in the same data structure. In order to avoid infringing on Microsoft’s patents in this area, our software does not support long file names nor does it support formatting the media. IBM holds patents pertaining to extended file system attributes. Our code does not support extended file attributes.

It is up to the purchaser of this software to ascertain for themselves if licenses are required from any or all of these or other organizations for the integration of the media or our software into their products. In the event licenses are required from any of these or other organizations, it is up to the purchaser of this software to acquire, at their own cost, such licenses directly from the license holders.

How to implement an SD/MMC Card with a PIC Microcontroller

For a 3.3volt PIC microcontroller system, the minimum interface between an SD/MMC card and the PIC is four I/O lines which include the three SPI bus lines (SCK, SDO and SDI) with the fourth line being the chip select. Optionally two additional lines connect to the SD/MMC socket for Card Detect (CD) and Write Protect (WP). For 5 volt PICs, level translation is required between the 5 volt I/O of the PIC and the 3.3 volt I/O of the SD/MMC Card. Level translation for the microcontrollers’ SDO, SCK and CS outputs can be implemented with simple resistor voltage dividers. Coming from the DO output of the SD/MMC Card to the SDI input of the microcontroller is not so straight forward and requires a TTL buffer. This is because the PIC microcontroller, in SPI mode, has the SDI input configured as a Schmidt trigger input and the guaranteed logic high out of the SD/MMC card is less than the guaranteed logic high level of the PIC.

For information on how to implement an SD/MMC card interface on a 3.3 volt microcontroller, and for sharing the SPI bus with other peripherals, refer to this reference design: BE_Reference_Design_EtherPIC_PIC18F97J60. Similarly, for information on how to implement an SD/MMC card interface to a 5 volt microcontroller (therefore requiring level shifting), and for sharing the SPI bus with other peripherals, refer to this reference design: BE_Reference_Design_PIC18F4620_ENC28J60.

When selecting a PIC microcontroller for applications that write to a media card, there are two main criteria. Firstly, there is sufficient RAM to support the access method and  secondly, the PIC supports an SPI bus. SD/MMC Cards must be written in 512 byte blocks. This is also the sector size of the SD/MMC card. So for example, to append a byte to a file, the sector must be read into a buffer, the location modified, and the sector written back to disk.

If using the Brush Electronics’ file system driver in standard mode (high performance), a 512 byte sector buffer is required for file system management as well as a 512 byte buffer for each open file. This means a PIC with 1500 bytes of RAM, such as the PIC18F452, when using the standard (high performance) file system configuration mode, can realistically have only a single file open at a time. The PIC18F4620, which is pin compatible with the PIC18F452 has 3900 bytes of RAM available and therefore can have multiple files open simultaneously.

When the file system driver is configured for LITE mode, a single sector buffer is shared by the file system and all open files. This means a PIC with 1500 bytes of memory, such as the PIC18F452, can have multiple files open simultaneously. The LITE configuration mode is not recommended for high performance applications that require access to multiple open files.

SD/MMC Card integration troubleshooting guidelines can be found here.