Practical Electronics/Memory Devices/CompactFlash
CompactFlash cards are a very common flash-based removable storage technology that was first released in 1994. It is heavily based on the PCMCIA standard. CompactFlash (CF) cards are larger than most other memory cards, but they are more rugged than most and have the advantage of a simple and open interface protocol, as opposed to devices like SD cards, which have proprietatry interfaces.
CompactFlash cards have 50 pins, but only 15 are absolutely necessary for a minimal interface, which means that many mid-range microcontrollers have enough pins to use one. CompactFlash cards have so many pins partly because they use a parallel data bus, presenting and outputting 8 or 16 bits at a time, which are all sent together. Devices like SD cards have a serial interface, which requires much more complicated timing to use.
All flash-based memory operates in blocks - you cannot access individual bytes or words like you can in RAM. CF cards almost invariably have a 512 byte block size. If you want to read, write or change any data, you must always deal with the entire block. This is a limitation of all flash memory, but it is the trade-off made for such enormous capacity.
A CF card can be used to store any data at all, and this included raw, unformatted bytes, or a full file system, such as FAT or ext3. We will firstly deal with simple raw data operations. File systems can be implemented on top of this if required.
The CompactFlash format
Physically, CF cards are rectangular, with slots in the sides for guides in the socket and a female connector on the card, which mates with male pins in the socket. There are two form factors for CF card, Type I and Type II, the only difference being the physical thickness of the card. Type II cards are 5mm thick and Type I are 3.3mm thick. Everything else is identical, so a Type I card will fit in a Type II socket, but not the other way around.
CompactFlash cards are generally more rugged that other formats. They can withstand shock of 2000G (a three-metre drop onto a hard surface) and are rated to at least 10000 mating cycles. In normal usage, a CompactFlash card can be expected to last around a hundred years.
The pins of a CompactFlash card are arranged in a 2×25 rectangular array with 0.05 inch spacing (50 mils or 1.27mm). CF cards have slots on each side, which are used to ensure the card cannot be inserted upside down or backwards. When looking into the card's connector, the wider slot is on the left when oriented as in the image below.
The function of the pins can depend on the mode that the card is being used in. See here for a full pinout description.
CompactFlash cards support three modes of memory access: PC Card Memory mode, PC Card I/O mode and True IDE Mode.
PC Card Memory mode
In PC Card Memory mode (or Common Memory mode), the task file registers are mapped into the common memory space of the CompactFlash card.
In this mode, the card can perform either 8-bit or 16-bit I/O operations to any of the common memory addresses.
PC Card I/O mode
In PC Card I/O mode (or I/O Function mode), the task file is mapped into the I/O memory space.
True IDE mode
In True IDE mode, task file registers are mapped into the I/O memory space, like in the I/O mode. True IDE mode has the advantage that direct interfaces with existing IDE buses are possible, as the CF card appears exactly like a hard-drive.
True IDE mode is specified by holding ATA SEL (pin 9) low during power-up. There are sometimes ways to switch into True IDE mode, but these cannot be relied on as they are optional in the specification.
In True IDE mode, the PCMCIA protocol and all configuration is disabled. Only I/O operations to the task file and data register allowed, with no memory or attribute registers accessible.
A minimal interface
CompactFlash cards can be used with only a fraction of the pins connected to a controller device. This is very useful when a microcontroller or other device has limited pins. The downside is that this method is slower than using all the available pins. This method uses an 8-bit interface, which is ideal for most low- to mid-range microcontrollers with 8-bit architectures.
When in common memory mode, all necessary registers can be accessed using just 3 address lines, and the interface is particular simple. The information needed for the card to read and write data is first loaded into control registers, and then the data is read out a byte at a time.
CompactFlash cards have no intrinsic file system in their memory, it is just blocks of raw data. File systems that allow the card to be read as a disc by a computer, such as FAT, NTFS or ext3, are placed into memory just like any other data, but have special significance to a computer that reads them.
Windows file systems
When using a disc formated with FAT, the first 512 bytes of data (sector 0) contain the master boot record (MBR), which tells the computer how big the disc is, how the memory is arranged and the name of the card, amongst other information. If you corrupt or remove this information by writing into sector 0, Windows will be unable to recognise the CF card as usable memory. It may even be impossible to recognise the disc enough to reformat it normally.
In this case, an "unconditional" format is required. Using the command prompt, enter the following (replace X: with the relevant drive letter).
format X: /fs:fat /u
You may need to do this twice to fully restore the disc to a Windows-compatible format. This method may destroy some data on the CF card. To prevent this, always begin indexing the CF card from sector 1 or higher. This may still corrupt the file system if you write blindly, but you will be able to mount the disc to retrieve data with tools such as WinHex.
- Winhex: A powerful disc-editing tool that lets you read the raw bytes from a storage device such as a CF card.