In this article I will guide you through how to get the Petit FatFs library to work on a PIC18F4620 with a SD memory card connect over SPI. This library will enable you to read and write (to a limit) files from a FAT formated SD-card.
I will assume in this article that you have a basic knowledge of Microchip's PIC microcontrollers and you know how to use MPLAB and C18. I will also assume that you know how the SPI interface, and off course, electronics in general.
HardwareThe hardware is pretty straight forward. It is, off course, a PIC18F4620, it runs off of a 10 MHz crystal that is raised to 40 MHz using the internal PLL circuitry in the PIC. The SD-card is connected to the PIC using Sparkfuns SD-card holder. As the PIC runs on 5V and the SD-card cannot handle 5V but runs on 3,3V there needs to be some level translations of the signals between the PIC and the SD-card. To handle this little problem I'm using these logic level converters. The signals used between the PIC and the SD-card is the SPI-lines, SCK, SDI and SDO, and also write protect, WP, card detect, CD and of course chip select, CS. CS, WP and CD are inverted. In the driver I've used CS on PORTD7, WP on PORTD2 and CD on PORTD3 (as seen the HardwareProfile.h in the attached project).
Below is a picture of the hardware used, on a breadboard:
Schematics are available all over the Internet so I will not include one here (since I don't have done one, but have used common methods for level translations).
First off it is recommended that you have have the latest version of Microchip Application Libraries, can be downloaded from the Microchip website. This is because I use the GenericTypeDefs.h and Compiler.h included with this library.
To get this library up and running the user is required to write some functions of his/her own. These are the functions:
- disk_initialize - Initialize disk drive
- disk_readp - Read partial sector
- disk_writep - Write partial sector
Clicking on the function names will take you to the documentation for those functions on the Petit FatFs website. The prototypes for these functions are in diskio.h and the implementations are in diskio.c. This file is the driver part of the library and makes it spossible for the rest of the FAT library to be able to communicate with the sectors on the SD-card. This driver could be altered if you instead would like to connect it to say a harddrive disk.
With the disk_readp() and disk_writep() you can read and write entire och parts of a sector into or from RAM.
Luckily for us the creator of the FatFs library has provided us with sample code to make the library work on an AVR. And since the code is written in C it was pretty easy to make the necessery adjustments to suit out PIC and C18 instead.
I did also have to make changes to the core of the Petit FatFs; after many hours of debugging I came to the conclusion that the supplied mem_cmp() in the Petit FatFs wasn't working with the C18 compiler, so on line 242 of pff.c to make use of the memcmp() supplied with the C18 library. I also included string.h at the top of pff.c.
In the attached project you can see how to use some of the different functions of the Petit FatFs library. I have just tried the read functions, such as reading a file from a directory and displaying it's content. I have also written a small routine that displays one entire sector, in this I have chosen to display the contents of the FAT table base sector (this sector address is stored in the dirbase member of the FATFS structure.Below is a picture of how the output may look from the serial output of the PIC;
For more information on how to use the Petit FatFs I refer to the library's documentation here.
The attached MPLAB project assumes that you have C18 installed in C:\Program files\Microchip\MCC18\ and the Microchip Application Library installed in C:\Microchip Solutions\.
Good luck and please feel free to comment!