A Hardware Solution
Device firmware typically implements support for FAT file systems. Another option is to interface to a chip that supports FAT communications. The uALFATTM chip from GHI Electronics can access media formatted for the FAT12, FAT16, and FAT32 file systems. A microcontroller can communi-
Table 8-6: The data-cluster size varies with the file system and storage capacity. (Source: Windows XP Professional Resource Kit)
cate with the chip using an asynchronous serial interface, SPI, or an I2C bus. The chip also supports MultiMediaCard and SD-Card communications.
The chip responds to text commands. File commands enable opening, closing, reading, writing to, and deleting a file. Directory commands enable creating, changing, listing, and erasing a directory. Also supported are commands for reading and writing directly to sectors in the storage media. The chip requires +3.3V and +1.8V power supplies.
The uALFAT-SD development board includes a MultiMediaCard/SD-Card connector and a regulator to convert 3.3V to 1.8V. A quick way to experiment with the board is to insert a a MultiMediaCard or SD Card, connect the board to a PC, and use a terminal program such as Windows’ Hyperterminal to send commands and receive responses.
To connect to a PC, connect the board’s asynchronous serial pins to the corresponding pins on a Maxim MAX3232 or similar RS-232 transceiver. If your PC doesn’t have an RS-232 port, connect the transceiver’s RS-232 pins to corresponding signals on an RS-232/USB converter. Or use an FTDI Chip USB UART to interface the uAFLAT with a PC’s USB port.
The File Allocation Table
The file allocation table contains an entry for each data cluster in a volume. In a FAT16 volume, each entry is 16 bits. In a FAT32 volume, each entry is 32 bits, with the lower 28 bits used to store a cluster number.
The First Two Entries
The first two entries in the FAT don’t store cluster information. In entry zero, the lowest byte should match the media-type byte in byte 21 in the BPB. (The media-type byte is generally unused. To find out whether a device has removable media, a host can send an INQUIRY command and check the RMB bit in the response, as described in Chapter 6.) All other bits are set to 1.
Formatting a volume sets entry 1 in a FAT16 table to a value from FFF8h to FFFFh. The mass-storage host can set the two highest bits of the entry to indicate error conditions. Bit 15 may be set to zero to indicate a dirty volume, which means that the system shut down or the device was removed before all pending writes completed. Bit 14 may be set to zero to indicate that the file system’s driver had an I/O error the last time the media was made available.
For entry 1 in a FAT32 table, bits 15..0 are the same as for FAT16, and bits 27..16 are 1s.