Sei sulla pagina 1di 13

Lab: Interfacing an FPGA to an IDE hard drive

The objective of this lab is to learn how the ATA/IDE interface standard works. With this knowledge it is possible to
interface an IDE device (in this case a hard drive) to a Xilinx FPGA board. 
  
 

Materials
1.  Xilinx demo board
2.  2 bidirectional line buffers (74LS745)
3.  Hard Drive

Theory
The intent of this section is to explain the basic mechanism of how the ATA interface works. After the completion of this
lab it will be possible for one to use the basic features of an IDE hard drive. It is recommended that one refer to a more
detailed book or manual to have a better understanding of the ATA interface. A few references are listed at the end of the
lab.

The IDE interface is relatively simple because hard drives have internal embedded controllers that take high level
commands from the host (Xilinx FPGA) and the hard drive communicates through the use of registers. To select a
register the appropriate control lines must be set (DA0­DA2, CS1FX, CS3FX). After selecting the register, one can read
from or write to the registers by strobing the DIOW and DIOR lines.

The following tables summarize the read and write cycles for a single block (512 bytes): 
 
IDE Read Cycle

IDE Module Drive

1.  Check Drive Ready bit in status register.  
2.  If not ready assert error
3.  Write address to address registers
4.  Write 1 to sector count register
5.  Write command to command register

  1.  Asserts INTRQ when block of data is ready

1.  Read status register to clear INTRQ  
2.  Read 256 words from the data register

 
IDE Write Cycle

IDE Module Drive

1.  Check Drive Ready bit in status register.  
2.  If not ready assert error
3.  Write address to address registers
4.  Write 1 to sector count register
5.  Write command to command register

  1.  Sets Data Request bit of status when ready to receive
data
1.  Write 256 words to the data register  

  1.  Asserts INTRQ when done writing block

1.  Read status register to clear INTRQ  

Registers

The following table includes some frequently used registers. CS1FX and CS3FX are active low. 
 
Register Control Lines Mode
DA0 DA1 DA2 /CS1FX /CS3FX
Command 1 1 1 1 0 Write only
Cylinder high 1 0 1 1 0 Read/Write
Cylinder low 0 0 1 1 0 Read/Write
Drive/Head 0 1 1 1 0 Read/Write
Sector number 1 1 0 1 0 Read/Write

Sector count 0 1 0 1 0 Read/Write
Status  1 1 1 1 0 Read only
Data 0 0 0 1 0 Read/Write
Device Control 0 1 1 0 1 Write only

Table 1.

The Drive/Head, Status, and Device Control registers have bits with important uses. The three registers are detailed
below:

Drive/Head Register 
 
Bit 7 6 5 4 3 2 1 0
Name 1 LBA 1 DRV HEAD

If the drive that you are using supports Logical Block Addressing (LBA) and you want to use LBA then the LBA bit
must be set to 1. If the drive does not support LBA then the bit is ignored.

The DRV bit tells the drive if it is a master or slave drive. For our purposes the DRV should be set to 0.

Status Register 
 
Bit 7 6 5 4 3 2 1 0
Name BSY DRDY DWF DSC DRQ CORR IDX ERR

For the status register bits 7, 6, 3, 0 are useful for our experiments. When the BSY bit is 1 then all of the other 7 bits in
the status register are not valid. The DRDY bit indicates that the drive is ready to receive commands. The DRQ bit is
used when writing data to the drive. The ERR bit indicates that the drive encountered an error while executing a
command.

Device Control Register 
 
Bit 7 6 5 4 3 2 1 0
Name X X X X 1 SRST nIEN 0
When the SRST bit of the register is 1, the drive is reset. When nIEN is 0 interrupts are enabled. The X’s are don’t cares. 
 

IDE Connector

The standard ATA interface cable includes 40 pins. Figure 1 shows the pin assignments for the ATA cable used on 5.25
and 3.5­inch drives. 
  
  

The following is a list of pins that must be used for a basic interface with a hard drive: 
 
PIN # Signal name Description

1 /RESET Reset signal from host

2, 19, 22, 24, 26, 30, 40 GROUND Grounding pins.


3 ­ 18 Host Data 0 through Host Data 15 Data bi­directional lines to and
from host. The lower 8 bits are
used for register and ECC access.
All 16 bits are used for data
transfers.

23 DIOW Drive I/O write strobe. Rising
edge clocks data from the host
data bus to a drive register or data
port.

25 DIOR Drive I/O read strobe. Falling
edge enables data from a drive
register or data port to host data
bus.

31 INTRQ Signal used to interrupt the host
system. Asserted only when the
drive has a pending interrupt. 

32 IOCS16 When active the signal indicates
to the host system that the 16­bit
data port has been addressed and
that the drive is prepared to send
or receive a 16­bit data word.

  Drive I/O lines DA0 ­ DA2 3­bit binary coded address
asserted by the host to access a
register or data port in the drive.

33 DA1 Drive I/O address line1. 

35 DA0 Drive I/O address line0. 

36 DA2 Drive I/O address line2. 

37 /CS1FX  Drive I/O chip select. When active
one of the registers in the
Command Block is selected.

38 /CS3FX  Drive I/O chip select. When active
one of the registers in the Control
Block is selected.

Table 2. 
 

Procedure
Wiring the hard drive to the Xilinx board

  
First we need to connect the pins listed in table 2 to pins in the Xilinx board. On the Xilinx chip use any I/O pins
that are available. For this lab we only need 8 data lines (Data0­Data7); wiring the other 8 data lines (Data8­
Data15) is optional. To reduce the risk of ‘burning’ your Xilinx chip we will use bidirectional line buffers between
the hard drive lines and the Xilinx board. We recommend that you use a chip similar to the 74LS245. This chip is
bidirectional and can drive up to 8 lines. For this lab you will need to drive 16 lines (8 data lines [bidirectional], 5
control lines, 2 strobe lines, and an Interrupt line) using the 74LS245.
The figure below shows the pin out of the 74LS245.

Wiring the buffer chips

1.  Connect the 8 data lines from the hard drive to the input lines of one 74LS245 chip. Connect the corresponding
output lines of the 74LS245 chip to lines on the Xilinx board. The data lines are bidirectional so do not forget to
connect the direction line from the 74LS245 to the Xilinx board. The direction of the data lines will depend on
whether one is reading or writing to the drive.
2.  Connect the 7 control lines (DA0­DA2, /CS1FX, /CS2FX) from the hard drive to the input pins of the 74LS245.
Connect the corresponding output pins of the 74LS245 to the Xilinx board. The direction of these lines is fixed
because the control lines are not bidirectional. The direction line should be set so that data can flow from the
Xilinx board to the hard drive. Note: the direction line is fixed so a bidirectional buffer is not really needed here.
3.  Now connect the interrupt line from the hard drive to the remaining pin of the 2nd 74LS245 chip. From the
viewpoint of the Xilinx board the interrupt line is an input pin and 7 the control lines are output pins. Thus, when
connecting the interrupt line to the buffer the direction must be the opposite of the other 7 control lines.
4.  Connect 8 LEDs to the Xilinx board. If you are using the Xilinx board then you can use the LEDs that are on the
board. You will use the LEDs to verify that you are actually writing or reading to the hard drive.

At this point you should test that the connections are wired correctly. It is suggested that one write a small state machine
to control the buffer chips and verify that they are working as expected.

Sample Board

Here’s a sample board with a hard drive on the left, a parallel port on the top, a text LCD on the right, and five
74LS245’s. The sample project is designed to work with this kind of board:
Sample Schematic

  
The schematic below shows how to connect a state machine that implements reading and writing data. Notice that
only eight data lines are connected, but it is easy to add the other eight data lines. It is included in the sample
project (see below). The terminals in the schematic are connected to a control module (not the hard drive) in the
sample project. This kind of schematic can be used for all of the procedures in this lab with minor modifications.
 

Read (DIOR) and Write (DIOW) Strobe Timing

For the following experiments you will need to strobe the /DIOW and /DIOR lines. Normally these signals are set
to 1 when not in use and to strobe the signal means to set the signal to 0 for at least 165ns and then set the signal
back to 1. Also remember that the data and control lines must be valid for a clock cycle before strobing the DIOW
or DIOR lines and they must be valid for another clock cycle after strobing DIOW/R. Also remember to use a
clock that is slower than 8Mhz. If you use a clock faster than 8Mhz you will have to be careful with the timing
requirements. In order to use an 8Mhz clock you need to strobe for 2 clock cycles.
Note: 
For the following tests make sure the RESET line is not asserted (/RESET = 1). Otherwise the tests will not work. 
 

Hard Drive Ready Test

In this section you will create a simple state machine that will test whether or not the hard drive is ready to receive
commands.

1.  Select the status register by setting the following control lines:

/CS1FX = 1 
/CS3FX = 0 
DA2­DA0 = 111 
 
1.  Now create a state machine that will strobe the DIOR line. Remember that the DIOR line is triggered on a falling
edge.
2.  Once your state machine is running and the hard drive is powered, the lower 8 data lines will contain information
from the status register.
3.  Modify the state machine so that when the BSY (busy) and DRDY (drive ready) bits equal 0 and 1 respectively an
LED in the demo board lights up. The reason for testing the BSY bit is that the other 7 bits of the status register are
not valid until BSY=0.
Reading and Writing Registers 
 

In this section you will write a value to a register in the hard drive and then read the value from the hard drive. We
will use the sector number register since it is a read and write register. You should create a state machine that
executes the following steps.

1.  Put a value on the 8 data lines.
2.  Set the control lines to:

/CS1FX = 1 
/CS3FX = 0 
DA2­DA0 = 011 
 

3.  Now strobe the DIOW line. Note that when writing the rising edge latches the data.
4.  The hard drive should have written the data to the sector number register.
5.  Now strobe the DIOR line. The hard drive should write contents of the sector number register on the lower 8 data
lines.
6.  Display the data on 8 LEDs.
Command Test 
 

The objective of this experiment is to test your ability to issue a command to the drive. For this experiment you
will send a command to the hard drive that will force it to power down. Not all drives support the sleep command
so consult the documentation of your hard drive for more information.

1.  Select the command register by setting the control lines to:

/CS1FX = 1 
/CS3FX = 0 
DA2­DA0 = 111

The command register is accessed with the same control lines as the status register but it is actually a different
register within the hard drive. 
 

2.  Place 10011001 (99H) on the lower 8 bits of the data bus. 99H is the command that tells the hard drive to switch to
sleep mode.
3.  Now strobe the DIOW line. The hard drive will execute the sleep command and will spin down.
Writing Data 
 

For this experiment you will write data to the hard drive. To accomplish this you need to select the location where
the data will be written on the hard drive. You then need to put the data on the data bus and finally strobe the
DIOW line so that the hard drive writes the data.

1.  First you need to enable interrupts for the hard drive. To do this you need to select the Device control register

/CS1FX = 0 
/CS3FX = 1 
DA2­DA0 = 110

Bit 2 of the register enables interrupts. Thus, write 00001000 to this register. 
 

2.  Now you need write the sector to the sector register. The address of the sector register is:

/CS1FX = 1 
/CS3FX = 0 
DA2­DA0 = 011 
 

3.  Each hard drive has a different number of sectors so you will have to check your hard drive’s documentation to
know which sectors are valid. For this experiment you will only write information to one sector. Thus, you can use
sector 1 for this experiment. Write 1 to the sector register.
4.  Now you need to set the sector count register to 1. The address of the sector register is:

/CS1FX = 1 
/CS3FX = 0 
DA2­DA0 = 010 
 

5.  Write 0 to both the cylinder low and cylinder high registers. The address of the cylinder low registers is:

/CS1FX = 1 
/CS3FX = 0 
DA2­DA0 = 100

The address of the cylinder high register is:

/CS1FX = 1 
/CS3FX = 0 
DA2­DA0 = 101 
 

6.  Now you need to set the drive/head register to 1010 0000. Select the drive/head register by setting the control lines
to:

/CS1FX = 1 
/CS3FX = 0 
DA2­DA0 = 110 
 

7.  Now tell the hard drive that you want to write data. Select the command register and write 0011 0000 (30H) to the
register.
8.  When the hard drive is ready to receive data the DRQ bit of the status register will be set to 1. You need to
continuously test the DRQ bit until it is set.
9.  At this point you are ready to write data to the hard drive. The address of the data register is:

/CS1FX = 1 
/CS3FX = 0 
DA2­DA0 = 000

You need to write an entire block of data which is 256 words (assuming 16 data lines are connected). So you will
need to write data 256 times. To simplify the debugging process you should write sequential data or constant data. 
 

10.  After writing the data the hard drive will assert the INTRQ line. You need to read the status register so that the
hard drive clears the INTRQ line. When you read the status register it is a good idea to check the error bit (bit 0 of
the status register).

Reading Data 
 

For this experiment you will read the data that you wrote to the hard drive in the previous experiment. The read
cycle is very similar to the read cycle. The first 10 steps are the same as the write cycle. As in the write cycle you
need to select the location from where the data will be read and then issue the read command.

1.  Repeat steps 1­6 from the Write Data section.
2.  Now tell the hard drive that you want to read data. Select the command register and write 0010 0000 (20H) to the
register.
3.  When the hard drive is ready to send data the INTRQ line will be set to 1. You need to continuously test the
INTRQ line until it is set.
4.  At this point you are ready to read data from the hard drive. Select the data register and then strobe the DIOR line.
You will need to strobe DIOR 256 times.

Reading and Writing Multiple Blocks
If your hard drive supports LBA then reading and writing multiple blocks is simple. The reason for this is that the drive
will accept a logical address. A drive that does not support LBA requires an address in the form of cylinder, head, sector.
Thus, you would have to create a module that translates from a logical address to a cylinder, head, sector address.

Sample Project
A sample project is available at idelab.zip. The sample project includes complete functionality for reading and writing
data and displaying data to a text LCD. The project can be easily modified to do any basic IDE testing.

Suggested Exercises
1.  Develop and implement a state machine to read and write a block of data from hard drive.
2.  Develop an interface so that it is easy to test and debug most of the IDE commands. This would include displaying
the hard drive’s identification information and diagnostic information, and error information.
3.  Develop an interface that allows a host to use all of the power management functions of the hard drive.

Project Improvements
Design a file system for the hard drive.
Address translation between logical and cylinder, head, sector addresses
Implementing master slave interface using two drives.
Hard drive copier.

References
Seagate ATA Interface Reference Manual. Rev C. May 21, 1993

Potrebbero piacerti anche