Home About Writings Projects Other nthpulse.net

< Return to Writings

Understanding and implementing the ATAPI interface on a microcontroller

This page is work in progress!


Contents

Introduction
Physical layer
ATA
ATAPI
ATAPI task file
Implementing a physical interface
Reading and writing the task file
ATAPI device detection
ATAPI device identification
Algorithm for issuing an ATAPI packet
Further reading & sources

Introduction

This page contains my structured notes on the interface and protocol used by IDE disc drives, which I have studied in order to implement it myself. Writing this page both improves my own knowledge and provides a educational resource for others interested in creating a DIY implementation.

The current intended scope of this page is enough to send ATAPI commands to an optical disc drive for track control and information. I am deliberately choosing to not concern myself with DMA (direct memory access), and to only use PIO (programmed input/output) as this is sufficient for my needs and is just easier to work with. This is a summary of information I pulled together from various sites, resources and specifications.

Disambiguation: There are a few names used interchangeably for the 40-pin interface. The big three are:

When (P)ATA or IDE are mentioned in this article, they can be assumed to mean the 40-pin bus that was historically used to connect hard disks and optical drives to a motherboard or controller.


Physical layer

Below is the pinout of the 40-pin connector used by IDE devices:

Table 1. IDE pinout

alternate name signal Pin nr. Pin nr. signal alternate name
nRESET 1 2 GND
D7 3 4 D8
D6 5 6 D9
D5 7 8 D10
D4 9 10 D11
D3 11 12 D12
D2 13 14 D13
D1 15 16 D14
D0 17 18 D15
GND 19 20 Key
DMARQ 21 22 GND
nDIOW 23 24 GND
nDIOR 25 26 GND
IORDY 27 28 CSEL
nDMACK 29 30 GND
INTRQ 31 32 nIOCS16
A1 33 34 nPDIAG
A0 35 36 A2
nCS0 nCS1FX 37 38 nCS3FX nCS1
nDASP 39 40 GND

Signals with a n prefix are active low

Table 2. Signal descriptions

Signal Description Direction
nRESET When asserted, resets all drives on an IDE chain Host -> Drive
A0 - A2 3 bit address bus used to select a register in a drive's task file Host -> Drive
D0 - D15 Bidirectional 16 bit data bus between host and drives Bidirectional
nCS0 Register select. Selects the command block of the task file. Host -> Drive
nCS1 Register select. Selects the control block of the task file. Host -> Drive
nDIOW IO write pulse. Assert when writing to task file. Host -> Drive
nDIOR IO read pulse. Assert when reading from task file. Host -> Drive
IORDY Driven low by a drive to signal to the host to slow down read/write cycles by inserting wait states. Drive -> Host
nDMARQ DMA request. Asserted by a drive to request a DMA transfer to or from the host Drive -> Host
nDMACK DMA acknowledge. Asserted by the host to initiate a DMA transfer in response to DMARQ_n Host -> Drive
INTRQ Interrupt request. Signals a completed command or transfer to the host. Drive -> Host
nDASP Drive active/slave present. Used by drive 1 (slave) to indicate presence during boot sequence. After that, it may be used by either drive to indicate activity. Open-collector output. See implementor's note on page 53 of SFF-8020i
CSEL When a drive is jumpered for cable select, this pin determines its address. If CSEL is grounded, the drive's address is 0. If CSEL is open, the drive's address is 1.
nPDIAG Asserted by drive 1 to indicate to drive 0 that it has passed diagnostics. Drive 1 -> Drive 0
nIOCS16 Obsolete. Unused by optical drives. If this signal is asserted and PIO mode is used for data transfer, data transfers will be 16 bit. If negative, PIO data transfers will be 8 bit Drive -> Host


ATA

ATA devices contain two blocks of 8 registers each which are collectively refered to as the "task file". The task file is accessible over the IDE bus. A register is selected using the A0 - A2 pins, and a block is selected using either one (but not both) of nCS0 or nCS1. Register functions are dependent on whether the address is being written or read. Vacant cells in the address and CS columns have a value of zero. The zeroes were omitted for readability.

Table 3. contains the contents of the task file for non-packet ATA devices such as hard disks. These registers are used by a host (e.g. IDE controller card) to perform read and write operations. This is useful context to know, but the task file for packet devices (CD and DVD drives) looks different.

Table 3. ATA task file map

nCS1 nCS0 A2 A1 A0 Register purpose
Read Write
Command block
1 Data
1 1 Error register Features register
1 1 Sector Count
1 1 1 Sector Number
1 1 Cylinder Low
1 1 1 Cylinder High
1 1 1 Drive/Head
1 1 1 1 Status Command
Control block
1
1 1
1 1
1 1 1
1 1
1 1 1
1 1 1 Alternate status Device Control
1 1 1 1 Drive address


ATAPI

ATAPI (ATA packet interface) is an extension of ATA used in optical drives that allows SCSI commands to be given to ATA devices by transmitting them in packets. Like ATA, ATAPI devices also have a task file addressable over the 40-pin physical interface, but the register functions and purposes are different. Confusingly, some DIY implementations, such as ATAPIduino, refer to these by the ATA names.

Table 4. ATAPI task file map

nCS1 nCS0 A2 A1 A0 Register purpose
Read Write
Command block
1 Data
1 1 ATAPI Error register ATAPI Features register
1 1 ATAPI interrupt reason register
1 1 1
1 1 ATAPI byte count register (bits 0-7)
1 1 1 ATAPI byte count register (bits 8-15)
1 1 1 Drive select
1 1 1 1 ATAPI Status ATA Command
Control block
1
1 1
1 1
1 1 1
1 1
1 1 1
1 1 1 Alternate ATAPI status Device Control
1 1 1 1

The control block contains various registers that are reserved for future use or are not used by ATAPI. Only one register in this block is used, at address 0b110. Writing to this register allows for "Device Control" as per its name, and according to SFF-8020i (the ATAPI specification), can be used to initiate a soft reset, or enable/disable the generation of interrupts on the INTRQ pin. Reading this register gives "Alternate ATAPI Status", which has the same value as "ATAPI status" in the command block, but reading the alternate status does not clear or acknowledge an interrupt. It may be possible to reduce a DIY ATAPI implementation by one pin & signal by choosing to not need to address the control block, if interrupts and soft resets will not be used.

The taskfile is used by ATAPI devices for status indication and low-level control of the device, but any CD/DVD related functionality is performed by sending commands as SCSI packets.


ATAPI task file register descriptions

ATAPI Error register

Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
Sense key MCR ABRT EOM ILI

Sense key: 4-bit value indicating the nature of the error. See page 183 of SFF-8020i
MCR: media change requested
ABRT: aborted command
EOM: end of media detected
ILI: illegal length indication

ATAPI Features register

Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
OVERLAP DMA

DMA: when 1, data for the Command will be transferred by DMA.
OVERLAP: when 1, the drive may release the ATA bus before the current command completes.

ATAPI interrupt reason register

Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
RELEASE IO CoD

RELEASE: indicates that the drive has released the ATA bus before completing the command in progress.
The states of the IO and CoD bits in this register, and the DRQ bit of the status register give the interrupt reason. Refer to Table 15 of SFF-8020i for a look-up.

ATAPI byte count LO & HI registers

Only used for PIO (as oppposed to DMA). Indicates the size of a transfer, in number of bytes.

Drive select register

Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
1 1 DRV

When the DRV bit is 0, then Device 0 (master) is selected. If 1, then Device 1 (slave) is selected.

ATAPI status register

Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
BSY DRDY DMA READY SERVICE/DSC DRQ CORR CHECK

BSY: Set whenever the drive is accessing the command block.
DRDY: Indicates that the drive is able to respond to an ATA command.
DMA READY: Indicates that the drive is ready to start a DMA transfer.
SERVICE: signals that a drive is requesting service or interrupt. Set when an interrupt is requested, cleared when a Service (opcode 0xA2) command is issued.
DRQ: Data request. Indicates that the drive is ready for a data transfer.
CORR: Indicates that a correctable error occurred.
CHECK: Indicates that an error occured during execution of the previous command.

ATA Command register

An 8-bit opcode corresponding to a command is written to this register, instead of individual bits having their own purposes.

Alternate status register

Contents are the same as the status register, but reading this register does not clear/acknowledge an interrupt.

Device control register

Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
1 SRST nIEN 0

SRST: Software reset
nIEN: When 0, enables interrupts. When 1, the INTRQ pin is high-impedance.


Implementing a physical interface

The microcontroller or other component used for the ATA signalling must signal at 5 volts. If this is not the case, then logic level shifters will be required.

For ATAPI, the entire width of the data bus is required (16 IO pins). It is most convenient to allocate an entire IO port each for the data bus' upper and lower bytes, or if possible, for the entire bus if you are working with 16-bit IO ports. This way, direct port manipulation can be used to read and write data (a single value can be written/read to/from a port) instead of having to individually handle each bit using functions like digitalRead() and digitalWrite().

3 pins are required for the address bus, 2 for the chip select signals, and 2 for the read and write strobes. It is also useful to dedicate a pin for the ATA reset signal.

This minimal signal set requires 24 IO pins.

It may be desirable to use the INTRQ signal from the drive in order to trigger an interrupt, so this signal may be optionally routed.

Since DMA is ignored in this article, any DMA related signals are also ignored.


Writing and reading the task file


ATAPI drive detection

A signature is placed in the "byte count HI" and "byte count LO" registers at power up and reset. The signature will also be placed when carrying out the "IDENTIFY PACKET DEVICE" command (also called "ATAPI IDENTIFY DEVICE"). Source

  1. Select the drive by setting the "Drive Select" register appropriately
  2. Read the "byte count HI" and "byte count LO" registers and inspect the values for the ATAPI signature.
    Byte count LO should contain 0x14, and Byte count HI should contain 0xEB.
    For non-ATAPI devices, the values will instead be 0x00 and 0x00 respectively.

ATAPI device identification

  1. Write the IDENTIFY PACKET DEVICE command (opcode 0xA1) to the ATA command register
  2. This command will insert 255 data words into the data register. Reading the data register will cause the next data word to be placed in sequence.
  3. Refer to page 62 of SFF-8020i for the contents of each data word. The drive's packet length, firmware and model number can all be identified from the output of this command. The data words must be read in sequence from the beginning until reaching the one that is required.
  4. Continue reading the output until all words have been read and the command completes.

Algorithm for issuing an ATAPI packet

Depending on the drive, a packet will have a length of either 12 bytes or 16 bytes. For devices that require 16-byte packets, it is sufficient to issue a 12-byte packet and then issue 4 bytes that are equal to 0x00.

ATAPI packets consist of 12 or 16 bytes, and it may be tempting to think that they are issued one byte at a time, using the lower half of the 16-bit data bus. In reality, packets are transferred to a drive 2 bytes at a time using the full width of the data bus. For a drive with packet length of 12 bytes, the packet will be issued in 6 write cycles, with evenly-ordered bytes being placed in the lower half and odd-ordered bytes being placed in the upper half.

For example:
During the first write, D7 to D0 of the data bus will contain byte 0, and D15 to D8 wil contain byte 1.
During the second write, D7 to D0 will contain byte 2, and D15 to D8 will contain byte 3.

Issuing a packet

  1. Write the PACKET command (opcode 0xA0) to the ATA command register
  2. Poll the status register and wait for DRQ to be set
  3. Form a data word (16-byte unit) for each of the packet's byte pairs, and write to the data register

Further reading & sources

Page created: 22MAR2026
Last modified: 14MAY2026