Issue
150 January 2003
Construct
an ATA Hard Drive Controller
by
Fred Eady
It’s
about time you had full control of your hard drive.
The controller you’ve been waiting for is just one
project away. This month, Fred shows you how easy
it is to build an ATA hard drive controller. Amazingly,
all you need is a good micro and a few everyday
parts.
Start
The Firmware Getting
Fat
Sources and PDF
Do
you remember your first computer with a hard drive?
What about when 5 MB was a lot of hard drive space?
Have you ever wanted to put something of your own on
a hard drive without having to rely on somebody else’s
expensive and proprietary hardware and driver code?
Ever want to read, write, and control a hard drive with
a microcontroller?
If
you answered "yes" to any one of these questions,
then this project is for you. I’ll show you how to build
an ATA hard drive controller with a microcontroller
and a few common parts. In addition, you’ll learn how
to write simple code that will form the basis for deploying
a stand-alone, networkable, microcontroller-based data
storage system.
Even
if you aren’t interested in communicating with hard
drives, there’s something here for those of you who
don’t need a microcontroller-driven hard drive controller.
The bonus track is in the hardware. Do you need an in-system
programming-capable test stand for an Atmel ATmega128
with RS-232, Ethernet, and 64-KB of external 16-bit
memory? Well, this project is for you too. Hard drives
or no hard drives, let’s get started.
THE
HARDWARE
Hanging
a standard PC or laptop hard drive from the 40-pin connector
shown in Photo 1 is the reason why we’re gathered here
today. I wanted the ATA hard drive controller’s electronics
to be flexible. So, in addition to the standard RS-232
port, which is driven by a Sipex SP233ECT, I added 10-Mbps
Ethernet capabilities with the RTL8019AS/ LF1S022 combination.
|

(Click
here to enlarge)
|
Photo
1—The board is clean and simple. All of the supporting
capacitors and resistors are SMT parts mounted on
the opposite side of the board. |
The
ATmega128 has plenty of internal SRAM (4 KB), but I
thought adding 64 KB of 16-bit external SRAM would be
nice. Adding the SRAM is sort of like buying rope: you
can always make the rope shorter, but it’s a pain to
add rope later if you need it.
The
ATmega128 has enough I/O structure to service the big
SRAM with some help from a couple of 74HCT573 latches.
As you can see in Figure 1, the external SRAM is attached
to the ATmega128 in the standard manner. This allows
those of you who aren’t interested in placing bits on
a spinning piece of magnetically coated aluminum to
do your thing with the big chunk of SRAM and the raw
power of the ATmega128. With the SRAM in this configuration,
the results of the hard drive I/O operations can be
buffered by the external SRAM or operated on by the
AVR directly.
|

(Click
here to enlarge)
|
Figure
1—No surprises here, either. The craftiness of this
design lies in the way the firmware utilizes the
hardware resources. |
The
Ethernet interface, shown in Figure 2, is actually a
wire-by-wire copy of the Packet Whacker microcontroller
NIC. The original Packet Whacker firmware that was written
for the ATmega series of AVRs is used in the ATA hard
drive controller code, as well. The Packet Whacker code
already has hooks to allow for the easy transmission
and reception of the hard drive data.
|

(Click here to enlarge)
|
Figure
2—If this looks familiar, it’s because it’s actually
a Packet Whacker that’s been melded into the ATA
controller design. The Packet Whacker code was reused,
as well; it can be found wound into the ATA controller
source. |
The
RS-232 port has a dual purpose. Running at 57.6 kbps,
it’s fast enough to spit out a sector’s worth of ASCII
data to a terminal emulator for debugging. In addition,
it can be used efficiently in an application to transfer
data and commands between the AVR-based hard drive controller
and a peripheral device. The 10-pin header makes assembling
a serial cable easy, if you use 9- or 25-pin IDC shell
connector parts and ribbon cable.
I’ve
standardized with 10-pin male headers for all of the
external ports with the obvious exceptions of the Ethernet
and hard drive I/O ports. As long as you put the right
connector in the correct header socket, using the 10-pin
headers with the keyed shrouds eliminates the possibility
of inserting the ISP and serial connectors incorrectly.
The
10-pin arrangement is also standard for most of the
ISP dongles that support the AVR series of ISP-capable
microcontrollers. I used a Kanda AVR ISP dongle and
a version of the company’s ISP software to program the
hard drive controller’s ATmega128. Because the dongle
interface is dedicated to certain pins on the AVR and
power isn’t transferred within the programming cable,
I was able to keep the dongle attached to the hard drive
controller throughout the programming and debugging
process.
The
ATmega128 is clocked at 14.746 MHz to keep the data
rate error percentage at a minimum for the 57.6-kbps
serial port. For this project, the ATmega128 I used
was a 5-V part that can run at 16 MHz. The 14.746 MHz
is the closest standard crystal to the maximum clock
speed that will clock the big AVR with the least amount
of serial data bit error rate.
The
first spin of the ATA hard drive controller used a 2-mm
header that mated directly to the 44 I/O pins found
on 2.5² laptop drives. My experiences with the 2-mm
parts were not good ones. The pins and connectors are
fragile, and I really don’t like working with the 1-mm
ribbon cable.
In
the process of attempting to work at 2 mm, I purchased
a gaggle of new surplus 2.5" Hitachi 540-MB drives.
That purchase, as it turns out, was a good thing. After
junking the 2-mm idea, I purchased some surplus 850-MB,
3.5" drives that turned out to be mostly junk.
I never really had any inclination to put real data
on them, so it’s not a total loss. At less than $10
per drive, what did I expect?
When
I bought the laptop drives, I also purchased some 2-mm
to 0.1² (or 2.5² to 3.5²) converter boards. The idea
was to be able to attach the laptop drives to a PC for
the debugging and verification of the ATA drive controller’s
firmware and hardware.
The
moral of the 2-mm hard drive story is that, thanks to
my foresight, you’ll see how I brought the ATA hard
drive controller to life with the 2.5" Hitachi
drives and converter boards. This may sound funny, but
when I was formatting the 3.5" 850-MB drives, I
was hoping that a few of them would show some errors.
I wanted to verify that the hard drive controller could
detect them, and then show you what they looked like.
I really didn’t expect them to be trashed so badly.
So, the drive error examples will come from the 3.5"
drives, and the good data examples will feature the
smaller Hitachi drives.
The
ATA hard drive controller requires a single 5-VDC power
source. Also, the Hitachi drives require only 5 VDC.
However, the larger 3.5" drives need 12 VDC in
addition to the 5 VDC. The original spin of the hard
drive controller used a 2-mm, 44-pin hard drive I/O
attachment point. The extra four pins on the 2-mm connector
provided 5 VDC and ground for the 2.5" drives right
at the hard drive I/O connector. In this spin, the 44-pin,
2-mm pin set is replaced with the standard 40-pin 0.1"
pin set, and there isn’t a power supply outlet at the
hard drive I/O connector.
The
hard drive controller is equipped with a standard 4-pin
floppy drive power plug. As you might have figured out,
the inclusion of a standard PC power connector on the
hard drive controller allows you to power the 3.5"
drive and the hard drive controller’s electronics from
a common off-the-shelf PC power supply.
If
the 2.5" drives are used, you’ll have to provide
an attachment to supply power to the extra I/O-based
power pins on the drive. That’s where the 2.5"
to 3.5" drive I/O adapters come in. The adapters
I purchased have a 3.5" drive power connector that
has only the 5-VDC lines tapped into the 44-pin, 2-mm
drive connector. The drive converter board allows you
to use the smaller 2.5"² drives with the standard
40-pin 0.1" cables and a PC power supply. Although
using a commodity power supply is the easiest way to
go, any other suitable power supply method will work
just as well. Photo 2 is a shot of an Hitachi 2.5"
drive and its associated converter board attached to
the ATA hard drive controller.
|

(Click here to enlarge)
|
Photo
2—The 540-MB drive formats quickly and is easy to
handle even with the converter boards attached.
This is made for quick turnarounds in the initial
development stages when I was experimenting, debugging,
and doing a lot of hard drive formatting.
|