|
by Mark Samuels
Start ý A
Choice PIC ý Getting Connected ý Address
Decoding ý The Identify Drive Command
ý Laying the Foundation ý No
Limits ý Sources and PDF
THE IDENTIFY DRIVE COMMAND
Almost all CompactFlash cards on the
market today have both sector and buffer sizes of 512 bytes. To ensure
thatýs true for a particular card, use the identify drive command,
which can be sent to the card without loading any of the other registers.
When the value 0xEC (identify drive) is strobed into the command register,
the buffer is immediately filled with 512 bytes of useful information,
including sector size, buffer size, model numbers, serial numbers,
number of heads, cylinders, tracks, and too many other goodies to
be listed here. However, they are well documented in the CompactFlash
Association Specification.
The sample source
code I have provided for downloading
shows a simple application of reading from and writing to a CompactFlash
card. The program essentially uses two bytes on a card to store a
counter, which keeps track of how many times the card has been inserted
into the socket. This is accomplished by waiting for the card detect
signal (CD1) to go low, indicating that a card is present.
An identify drive command is executed
to get the sector size (word 5) and buffer size (word 21) of the particular
card that has been inserted. The identify drive command doesnýt require
any parameters, so none of the other registers have to be loaded with
any data. Simply load the command register and strobe ýWE.
The CompactFlash Association Specification
states details about each command and the results, including the breakdown
of the 512 bytes of information dispensed by this particular command.
To get to a specific piece of information, simply strobe ýOE enough
times to get to that data in the sequence. For example, the number
of bytes per sector is stored in the sixth word (word 5), so strobing
ýOE eleven times will make the first byte of that particular data
field appear on the data bus.
Most of the information on the card is
stored as 16 bits, so the subroutine in the code (CF_READ)
actually strobes ýOE twice, storing the first byte read in DATA_LO
and the second byte read in DATA_HI. The program then uses
these values to know how many times to write to the buffer before
that information is actually transferred from the buffer to the card
memory. Next, the program reads a sector at a particular location
but only reads the first two bytes from the buffer. These two bytes
are the counter, which is incremented and written back to that same
location on the card.
One note of caution, running this program
on a CF card that has been formatted and has data already stored on
it may corrupt that data and make the card unreadable until it is
reformatted, because this program will overwrite a specific sector
on the card. That read and written location is clearly annotated in
the source code, so feel free to play around with the target location
and amount of data read or written.
There is a useful shareware application
that I used to read the raw hex data from the card to verify the actions
of the PIC. The program is called WinHex and is available from CNET
and various other shareware archives on the Internet. As long as you
have the means to access a CompactFlash card as some sort of disk
on your PC, you can use WinHex to view each individual byte stored
on the card.
PREVIOUS
NEXT
Circuit Cellar provides up-to-date information for engineers. Visit
www.circuitcellar.com for
more information and additional articles.
For subscription information, call (860) 875-2199, subscribe@circuitcellar.com
or subscribe online.
ýCircuit Cellar, the Magazine for Computer Applications. Posted with
permission. |