This is only a preview of the November 2007 issue of Silicon Chip. You can view 30 of the 104 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Items relevant to "PIC-Based Water-Tank Level Meter; Pt.1":
Items relevant to "Playback Adaptor For CD-ROM Drives":
Items relevant to "Rolling Code Security System; Pt.2":
Items relevant to "A UV Light Box For Making PC Boards":
Purchase a printed copy of this issue for $10.00. |
Playback adapter
for CD-ROM drives
Ever wanted to use an old CD-ROM drive as
a CD player for audio playback? Now you
can do it, with this nifty CD-ROM Playback
Adapter. It can control one or two CD-ROM
drives and has an infrared remote control.
A 16x2 line LCD screen provides track
information and other data.
W
E HAVE OFTEN been asked
how to interface a hard drive
or computer CD-ROM drive to a microcontroller. This is an interesting
question, since there are countless old
CD-ROM drives out there that are still
in perfect functioning order but they
are “obsolete”. Instead of letting them
end up in landfill, you could do your
bit and build this project.
40 Silicon Chip
As well, this project will be good
experience for those readers who wish
to learn more about the ATA interface
and who want to use hard drives and
CD-ROM drives in their own projects.
The interface can be easily modified to
suit any other micro and only requires
a few I/O ports and a reasonably fast
processing core.
The main features of the Playback
Adapter are listed below:
(1) Can connect up to two ATAPI CDROM drives.
(2) Auto detection of up to two connected drives.
(3) Plays your favourite CDs.
(4) Random play and repeat modes.
(5) Controls volume (16 levels) and
balance digitally.
(6) Remote control with user-selectable key definitions.
(7) Works with any RC5 remote control.
(8) ISP (in-system programmable)
if you wish to experiment with the
firmware.
(9) The CD is automatically locked
when playing
(10) LCD screen.
Accessing an ATAPI device
The CD-ROM Playback Adapter
siliconchip.com.au
By MAURO GRASSI
/CS1
/CS0
A2
A1
A0
1
0
0
0
0
Data
/RD
Data
/WR
1
0
0
0
1
Error
Features
1
0
0
1
0
Sector Count
Sector Count
1
0
0
1
1
Sector Number
Sector Number
1
0
1
0
0
Cylinder Low
Cylinder Low
1
0
1
0
1
Cylinder High
Cylinder High
1
0
1
1
0
Device/Head
Device/Head
1
0
1
1
1
Status
Command
0
1
1
1
0
Alt Status
Device Control
Table 1: the ATAPI register file. All ATA and ATAPI devices are controlled by
reading and writing to these registers.
Interfacing to an ATAPI device is
simple because most of the work is
done inside the drive. In effect, it acts
as a black box. It conforms to a standard and the internal implementation
is left to the manufacturer. That is why
the standard was originally called IDE
(integrated drive/device electronics).
It just means that a lot of the complexity of the interface is in the drive and
the drive responds to a uniform set of
commands.
It speaks well of the design that one
of the easiest parts of a computer to
get working is the hard drive or CDROM/DVD drive. Plug any drive into
any motherboard and it will usually
work first time.
Overview of the ATA interface
presented here lets you connect one
or two drives and control each independently using a standard RC5
remote control.
CD-ROM drives that conform to the
parallel ATA (AT attachment) standard can be used with the adapter and
most old drives fall in this category.
In fact, most CD-ROMs will be ATAPI
devices, which is a superset of ATA.
It just means they support the packet
interface, a feature that was added to
the original ATA interface.
The resulting protocol was renamed
ATAPI, with the ‘PI’ standing for
packet interface. Most CD-ROMs, as
well as DVD drives, are ATAPI devices,
although others conform to different
standards, like SCSI and SATA (serial
ATA). These have a different connector and are not compatible with this
project.
siliconchip.com.au
The ATA interface is register-based.
There are essentially two banks of
eight registers, although only one of
the eight registers in the second bank
is ever used.
The interface consists of two chip
select lines, called /CS0 and /CS1
which are active low. There are three
address lines designated A0, A1 & A2,
as well as the read and write control
lines. The latter are designated /RD
and /WR respectively and are also
active low.
In order to access a register, one sets
the register address given by [A2:A0]
and then brings either /CS0 or /CS1
low (but not both). Then it is a matter
of bringing either /RD or /WR low and
reading or writing the data through
data port D7:D0.
Note that the data bus width is actually 16 bits but for accessing the ATA
registers, only the lower eight bits are
used. However, the full width of the
bus is used for data transfers.
Note also that the name of the register sometimes changes depending
on whether you are reading from or
writing to the specific address. For
example, at address 110b and with /
CS1 low and /CS0 high, reading will
give the Alternate Status register (a
read only register), while writing will
affect the Device Control Register (a
write only register).
All commands to control the drive
are sent through the register file (ie,
the set of ATA registers). For example,
the Command Register can be written
with the opcode for a particular operation – eg, “SLEEP” - and the drive will
respond by going into power saving
mode, barring any errors.
Note: the order in which you assert
the control lines on the ATAPI/IDE
bus is important. For example, you
would think that you could assert /
RD or /WR first and then bring /CS1
or /CS0 low. However, this approach
does NOT work on all drives.
The correct procedure is to assert /
CS1 or /CS0 first, then to assert either
/RD or /WR. Of course, because we
are using a general-purpose micro and
these pins are on different ports, it is
impossible to assert them simultaneously. This is not required however,
but would be closer to a native IDE/
ATAPI port interface.
Low level drivers
It is relatively simple to write to an
ATAPI device. As explained, you first
prepare the data and the address, bring
the chip select line low and then apply
either the read or write signal.
This is the minimum you would
need to interface to an ATA device like
a hard drive. It would not be the fastest
interface possible – you’d have to get
November 2007 41
1
3
5
A3 37
39
A1 33
A0 35
29
31
27
21
23
25
D2 13
D1 15
D017
19
D5 7
D4 9
D3 11
1
D7 3
D6 5
D4 11
D613
15
D2 9
D0 7
A
40
34
36 A2
38 A4
30
32
26
28
22
24
12 D12
14 D13
16 D14
18 D15
20
10 D11
2
4 D8
6 D9
8 D10
16
12 D5
14 D7
8 D1
10 D3
4
6
2
IRD1
CON1
CON2
2
3
+5V
8
14
1
5
8
10 F
100
(A0–4)
(D8–D15)
IC2: 74LS00N
IC3: 74LS04N
(D0–D7)
51
8
S3
1k
+5V
9
IC3d
Vdd
40
12
17
4
3
2
1
4
3
2
1
CON4
CON5
PD2/INT0
PD7
Vss
20
+5V +12V
+5V +12V
XTAL1
XTAL2
19
18
7
6
9
29
11
10
8
31
30
100nF
PD5
14
PD4
D7 32
PA7
D6 33
PA6
D5 34
PA5
D4 35
PB7/SCK
PA4
D3 36
PA3
PD0/RxD
D2 37
PA2
D1 38
PD1/TxD
PA1
D0 39
PA0
16
PD6
13
PD3/INT1
D15 28
PC7
RESET
D14 27
PC6
D13 26
PC5
IC1
D12 25
PC4
ATMEGA8515
D11 24
PC3
D10 23
PC2
D9 22
PC1
D8 21
PC0
A4 5
PB4
A3 4
PB3
PB5/MOSI
A2 3
PB2
A1 2
PB1
A0 1
PB6/MISO
PB0
15
100nF
2
IC3a
470
LED3
+5V
22pF
X1
7.3728MHz
+5V
A
K
3
47 F
2
1
22pF
1
7
IC2a 14
6
3
K
A
4
K
A
1 F
1 F
1 F
1 F
GND
OUT
IN
REG1 7805
7
IC3b
470
LED2
S1
5
LED1
470
47 F
IC3c
100nF
15
D1
T2o 7
T1o 14
R2in 8
R1in 13
5
SC CD-ROM
2007
100nF
S2
10 T2in
11 T1in
9 R2o
12 R1o
3
4
1
IC4
MAX232
6
16
T2o 7
T1o 14
2
10 T2in
11 T1in
R2in 8
9 R2o
5
4
6
R1in 13
15
IC5
MAX232
16
12 R1o
3
1
2
CON6
1 F
1 F
9-12V DC
5
4
3
2
1
CON3
DB9F
K
9
8
7
6
+5V
A
K
OUT
1
2
3
IRD1
LEDS
GND
IN
7805, 7812
470
LED4
PLAYER ADAPTOR
1 F
1 F
+5V
1 F
1 F
A
Fig.1: the circuit uses an Atmel ATmega8515 microcontroller (IC1) to interface to the CD-ROM drives (via CON1) and the LCD module (via CON2). IC4, IC5
and CON3 are optional, to provide in-circuit programming for the microcontroller.
+5V
470
LED5
LCD
CONTRAST
VR1
10k
K
42 Silicon Chip
siliconchip.com.au
Fig.2: the Error Screen. The numbers
give information about the state of the
program and the drive when the error
occurred.
involved in DMA (direct memory access) for that – but it would work.
With ATAPI devices like CD-ROM
drives, most operations are initiated
by writing packets rather than single
byte commands. A packet is a string
of 12 or sometimes 16 bytes that are
sent to the drive in sequence.
In order to send packets, a more
involved algorithm than just writing
to the register file is needed. Here you
have to worry about bus timings and
whether the drive is busy or requesting
data. There is a well-defined protocol
for PIO (peripheral input output) access to an ATAPI device.
Feedback is provided by the bits
BSY (bit 7) and DRQ (bit 3) in the
Status register, which can be polled
to determine the current state of the
drive. When the drive shows BSY=1
it does not respond to commands and
reading any register except the Status
register is undefined. In other words,
the only valid information that can be
read from the drive is bit 7 (BSY) of
the status or alternate status registers
(when it is busy).
As an example, the packet to open
the tray of the drive is given by the
12-byte string: 0x1B, 0x00, 0x00, 0x00,
0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00. To send this packet, you
first send the PACKET command 0xA0
to the command register and then follow the packet protocol, as outlined in
Flowchart 1.
The protocol begins with a packet
being written to the drive. Optionally,
there may follow a data read or write
transfer, depending on the packet written to the drive.
The CoD (command/data) and IO
(input/output) bits are in the Sector
Count ATA register (also known as the
Interrupt Reason Register in ATAPI
devices). CoD is bit 0 and IO is bit 1.
When CoD is 0, data is being transferred and when it is 1, a command
(packet) is being transferred.
The IO bit indicates the direction of
transfer. When IO is 0 the host writes
to the drive and when it is 1 the host
siliconchip.com.au
Pin
Name
Description
1
/RESET
A low level on this pin resets all connected drives.
2, 19, 22, 24, 26, 30, 40
GND
All these pins are connected to the common ground plane.
3, 5, 7, 9, 11, 13, 15, 17
[D7:D0]
3=D7, 5=D6 . . . . . . 15=D1, 17=D0
These are the eight least significant bits of the data bus.
4, 6, 8, 10, 12, 14, 16, 18
[D15:D8]
4=D8, 6=D9 . . . . . . 16=D14, 18=D15
These are the eight least significant bits of the data bus.
This pin is not connected and is used to prevent the cable
being connected the wrong way around.
20
KEY
21
DDRQ
23
/WR
Write strobe, active low.
25
/RD
Read strobe, active low.
27
/IOREADY
28
ALE
Address latch enable – not used in this project.
31
IRQ
Interrupt request – not used in this project.
32
IO16
Obsolete since ATA-3 – not used in this project.
33, 35, 36
[A2:A0]
Data request pin – not used in this project.
Device ready pin, active low, not used in this project. Used to
slow a controller if it is too fast for the drive.
35=A0; 33=A1; 36=A2. Address bus
37
/CS0
Chip select 0
38
/CS1
Chip select 1
39
/ACT
A low level on this pin indicates that the drive is working. It
can be connected directly to a LED to show drive activity.
Table 2: this table shows the pin-outs of the ATAPI interface. Note that this
project leaves many pins usused, as they are unnecessary for PIO transfer.
reads from the drive.
When the above packet has been
successfully processed by the drive, it
will respond by opening the tray. This
packet does not require any extra data
transfer but other commands, such as
reading the CD TOC (table of contents),
do require reading from the drive.
Other packet commands such as setting the volume require both reading
and writing to the drive (refer to the
ATAPI specification for the relevant
packet codes).
The firmware
The main component of this project
is the firmware, as the hardware is
little more than a Atmel microcontroller. The firmware is responsible for
interfacing to the drives, decoding the
remote control signals, auto detecting
the connected drives and controlling
playback and volume, among other
things.
All this is done with only 512 bytes
of RAM! The firmware size is approximately 7.2KB and fits inside the
micro’s 8KB flash memory.
ATA and ATAPI commands are either “Mandatory”, “Optional” or not
supported. To make sure that the CDROM Playback Adapter works with just
about any ATAPI device, we’ve used
only “Mandatory” commands as per
the specification (rev 2.6 1996).
Note, however, that we cannot guarantee that it will work correctly with
all ATAPI devices. Some are buggy and
the standard covers a period of many
years. We’ve also come across drives
that don’t conform to the standard in
every detail.
In our case, we tested the adapter
with seven different ATAPI devices,
including both CD-ROMs and DVD
drives, and it worked correctly with
six of these. The seventh drive had a
problem in that it was not detected by
the firmware and closer inspection and
debugging revealed that the micro was
unable to write to the drive’s register
file. Thus it failed the first test of the
auto detect subroutine, as explained
below.
Basically, if a particular CD-ROM or
DVD drive is not detected by the firmware on start-up, it will not be functional with the adapter. In that case, try
using a different drive. Conversely, if
the drive is correctly detected, there
November 2007 43
Flowchart 1: this is the packet writing routine used in the firmware. The interrupt signal INTRQ, intended for PCI
buses on computers, is not used and a method of polling for DRQ and BSY is used in its place.
44 Silicon Chip
siliconchip.com.au
is a high chance that it will work correctly with this adapter.
How it works
Refer now to Fig.1 for the circuit
details. The circuit is essentially just
an Atmel ATmega8515 microcontroller
(IC1) with its general IO pins configured to read and write to up to two
drives. It also controls the LCD screen
and reads the remote control sensor.
Add in a power supply and a few support chips and that’s about it.
ICs4 & 5 are MAX232 line drivers
and are used to interface the microcontroller to the serial port of a computer
(RS-232). These devices are optional
and are only needed if you are planning to experiment by writing your
own firmware. Basically, they allow
the board to be connected to a PC’s
serial port so that the microcontroller
can be programmed in-circuit. The
software to use for this job is called
“Pony Prog 2000” and is free for
download from www.lancos.com/
ppwin95.html
ICs2 & 3 are simple logic gates, used
here as “glue logic” for the interface.
These devices are 74LS00 and 74LS04
quad NAND gates and hex NOT gates
respectively but only one NAND gate
and four NOT gates are used from
these devices.
Infrared receiver
IRD1 is an infrared receiver module,
containing a photodiode, amplifier,
filter and demodulator all in a compact package. It accepts a modulated
infrared signal on a 38kHz carrier and
outputs a demodulated TTL level serial stream.
This stream is fed to pin 12 of IC1
and is decoded by the firmware in
the microcontroller. Note that IRD1’s
output is usually high (around +5V)
and varies as a square wave when an
infrared input is received.
S3 is used to select the remote
control setup option at boot time. For
normal operation it is open and this
allows the signal from IRD1 to pass to
the microcontroller for decoding the
remote control signals. Conversely,
when S3 is pressed, it temporarily
pulls this line low via a 1kW resistor
to allow remote control set-up.
There are five indicator LEDs on the
board. LED4 (red) is the power LED,
while LED3 (orange) lights when the
micro is being programmed or is in the
reset state. This state can be entered
siliconchip.com.au
CD-ROM drives have three sets of jumper pins at the back to configure the drive.
If you have just one drive, it can be configured as either a master (MA) or a
slave (SL) using the jumper link. However, if you are using two drives, then one
must be configured as a master and the other as a slave, as shown here.
using switch S1.
LED1 (green) shows the activity of
the currently selected drive.
Finally, LED2 & LED5 make a pair.
Only one will be lit at any one time.
LED5 (green) indicates that the MASTER device is being controlled, while
LED2 (red) indicates that the SLAVE
device is being controlled. If you have
two drives connected, you may toggle
between them using the Line-In button
on the remote.
Power supply
In order to power the drives, you
will need a power supply capable of
delivering +12V at 2A and +5V at 2A
(eg, a computer power supply). By contrast, the board requires a +5V supply
and draws just 200mA.
Basically, you’ve got two choices
when it comes to the power supply.
The first option is to power the PC
board directly from a 9-12V plugpack
supply and power the drives separately. In this case, the board supply
is fed in via CON6 and is regulated to
+5V using 3-terminal regulator REG1.
Diode D1 provides reverse polarity
protection, in case the supply is connected the wrong way around.
The second option is to plug a
+12V/+5V supply into either CON4
or CON5 on the PC board. The board
will then be directly powered from
this supply, while the supply for the
drives can then be taken from the
unused connector. Note that you will
need a Y-splitter cable if there are
two drives.
In this case, you can use a surplus
computer power supply to power both
the boards and the drives. This will
simply plug straight into either CON4
or CON5. Another option is to use a
ready-made adapter like the Jentec
JTA0202Y (from Taiwan). This unit
supplies +12V and +5V at 2A each,
which is enough to power two drives
and the PC board. It also comes with
the proper plug, so all you need then
is a Y-splitter cable.
Setting up the drives
The two drives must be configured
before being installed. Specifically, if
you wish to connect only one drive,
it can be configured as either a slave
or master device.
Usually this is accomplished by a
jumper setting on the back of the drive.
The drive will usually have a label
indicating the appropriate position
of the jumper.
If you wish to use two drives, however, make sure that one is configured
as a master while the other is configured as a slave. It doesn’t matter which
is which as long as they are not both
slaves or both masters.
How auto-detection works
Let’s now see how the micro detects
any connected ATAPI devices at boot
up.
First, a simple test is done. The miNovember 2007 45
What’s A Finite State Machine?
A finite state machine (also known as a finite state automaton) is a set
of states together with a transition table and a designated state that is the
“initial state”.
The transition table can be thought of as a table with three columns and a
finite number of rows. The first column corresponds to the current state, the
second column corresponds to the input and the third column corresponds to
the next state. These triplets (X, I, Y) are interpreted as follows: if the machine
is in state X and an input I is received, it moves to state Y. While there is no
input, the machine stays in its current state.
For example, in our case, if the firmware is in the neutral state, and the user
presses the Play key on the remote control, then the transition table dictates
that the machine moves to the Playing state. This “rule” would be written as
the triplet (“Neutral”, “PLAY”, “Playing”).
The user interface of this playback adapter is simply implemented as a
finite state machine, meaning there are a number of rules that make up
the transition table. The machine begins in the “neutral” state, after a short
initialisation.
Flowchart 2 shows the finite state machine implemented in the firmware.
The transitions correspond to arrows, while the blue blocks are the possible
states.
cro writes a known value to an ATA
register and then attempts to read that
value. If the value read is the same as
that written, the auto detect subroutine
goes to the next stage.
Conversely, if the drive fails this
test, it is assumed to be absent. Instead,
0xFF is returned as the value read
due to internal pull-ups on all inputs
(which incidentally, is not the value
that is written).
The next stage of auto-detection involves searching for the signature that
all ATAPI devices are required to have
(according to the standard). In fact, all
ATAPI devices have a unique signature
of 0x14 and 0xEB (notice that 0x14 +
0xEB = 0xFF) in the Count Low and
Count High registers on start up.
The inquiry command of the ATA
interface, while mandatory for ATA
devices, is actually aborted by ATAPI
devices. Instead, the effect of this ATA
command on ATAPI devices is to put
the ATAPI signature word in the Count
Low and Count High registers. What is
mandatory for ATAPI devices is to support the ATAPI inquiry command.
The algorithm for detecting the
drives is as follows:
(1) Perform a simple read-write test.
Abort if this test fails, otherwise continue.
(2) Select the drive by writing to the
drive/head register.
46 Silicon Chip
(3) Issue an ATA identify device command.
(4) If the signature 0x14 0xEB in the
Count Low and Count High registers
is present, go to step 5.
(5) If this signature is not present, the
device is either absent or it is not an
ATAPI device. Therefore, we may assume that no ATAPI device is present
and terminate.
(6) If the ATAPI signature is detected,
we issue an ATAPI inquiry command
to get further information about the
drive and conclude that an ATAPI
device is connected. The test is then
terminated.
Firmware operation
Flowchart 2 shows the structure of
the firmware. After initialisation, the
program can optionally jump to a subroutine to set-up the remote control.
This should be done at least once,
preferably the first time the program
is run. Once the remote control has
been successfully set up, the adapter
is ready to be used.
The next stage in the firmware is
the auto detection of the drives. Up to
two drives can be connected and they
should be configured correctly as master
or slave as detailed previously.
The firmware then enters a “finite
state machine” by going to the neutral (or initial) state. It then listens
for activity on the infrared port and
responds to the remote control commands.
There are three playing modes: (1)
the default mode, (2) the repeat mode
and (3) the random mode. In default
mode, the adapter will play the current
track and when that is done, will jump
to the next track.
In repeat mode, the adapter plays
the current track and then repeats it
over and over. This mode is indicated
by the digit ‘1’ appearing as the last
character of the first line of the display
in playing mode.
Finally, in random mode, the
adapter will play the current track and
then select the next track randomly.
This mode is indicated by the letter
‘R’ appearing as the last character of
the first line of the display in playing mode.
In operation, the user can scroll
between the default, repeat and random modes by pressing the ‘Record’
button on the remote control during
play mode.
The volume is controlled by the
Volume Up and Volume Down buttons
on the remote. Up to 16 levels ranging
from muted (0) to full volume (15) can
be selected.
The ‘Mute’ button has the usual
effect of storing the current volume
and then setting the volume to 0. If
pressed again when the volume is 0,
the original volume level is restored.
The percentage balance of the right and
left audio channels can be modified
by the user, by pressing the Channel
Up and Channel Down buttons on the
remote. The percentages range from
0-100% in steps of 5%.
The volume for each channel is then
calculated in terms of the balance using a simple formula:
(1) Volume (Left) = (Balance Left)/100
x Volume Level
(2) Volume (Right)= (Balance
Right)/100 x Volume Level
In playing mode, there are the usual
control options like going to the next
track (pressing the Fast Forward button) or to the previous track (pressing
the Rewind button). You can also
pause playing (by pressing Pause) or
stop playing (by pressing Stop).
The 20+ button on the remote can be
used to either eject the CD or close the
tray (depending on whether the tray
is already closed or open). The LineIn button is used to switch between
master and slave devices, if two drives
siliconchip.com.au
Flowchart 2: this flowchart shows the
“finite state machine” implemented by
the firmware. After a short initialisation
which includes the automatic detection
of connected drives, the firmware goes
into the neutral state. From there it starts
accepting remote control commands that
change the state of the machine. Typical
display readouts corresponding to each
state are also shown.
are connected and have been correctly
detected by the firmware.
The 0-9 number buttons are used
to select a particular track number to
play. Simply press the correct number
siliconchip.com.au
(which will be shown on the screen)
and then press Play to play the selected
track number.
As you can see, the user interface
has been kept deliberately simple
and intuitive. By the way, you can use
virtually any RC5-compatible remote
control since you can assign the buttons during the set-up procedure (more
SC
on this next month).
November 2007 47
|