e009052.pdf
(
907 KB
)
Pobierz
000094-1-UK USB
BASI
CS
The Universal Serial
Part 1: develop your own applications
By B. Kainka
The USB Interface described elsewhere in this issue demonstrates that the
USB may be used for DIY projects related to measurement and control. But
how does one develop USB compatible peripherals, which conditions should
be satisfied, and where can the necessary information be found?
The USB is a serial bus system designed for
connection to and addressing of several
peripheral devices. USB is considerably more
complex than RS232, but also much faster at
up to 1.5 Mbit/s (for low-speed devices) or
12 Mbit/s (for full-speed devices). Further-
more, the designers of the USB have paid a lot
of attention to simplicity and ease of use. It is
fair to say that the concept of Plug and Play,
for the first time, actually applies to the USB,
which allows devices to be connected and
automatically initialised with the PC
switched on or off. With USB, you don’t have to
grapple with interrupt conflicts, wrong
addresses and missing drivers.
Making it ‘simpler’ for the user means a
more complex job for the designer. If you
want to design your own USB peripheral
device, you should be ready to delve into the
USB standard, program your own microcon-
troller and write your own driver. The inset
box provides some suggested starting points.
USB cables
and power supply
Two different connector types are around: type
A and type B. the system is designed such
that these can not be mixed up. As opposed to
RS232, the so-called ‘crossed’ cable does not
exist with USB, all cables have simple 1-to-1
Figure 1. The plugs used.
Bus (USB)
BASI
CS
2
1
The two datalines D+ and D– may only be
employed in combination with special USB
components, i.e., special microcontrollers,
which, as a matter of course, are powered via
the USB connector. After a special authorisa-
tion request, a peripheral is allowed to con-
sume up to 500 mA.
The supply voltage on the USB is allowed
to rise to 5.25 V and drop to 4.2 V. A good
voltage regulator will have no problems step-
ping these levels down to 3.3 V. The entire
system — cable and devices — is dimen-
sioned such that the minimum supply voltage
can not drop below 4.2 V. Devices drawing
more than 100 mA have to report their
requirement with the system and are only
allowed access to the bus when sufficient
current is available.
A clear distinction is made between Bus
Powered and Self Powered USB peripherals.
In many cases, either of the two modes may
be selected. For example, the device may
have a supply input socket for connection to
an external mains adapter.
According to the USB standard, the cur-
rent consumption from the bus is automati-
cally limited. Therefore, if more current is
drawn than allowed, the supply has to be
switched off.
1
234
3
4
000094 - 11
Figure 2. Connections of plugs type A and B.
USB classes
Figure 3. USB socket type B.
Until recently, you would receive a free driver
disk with a new mouse. Not so if you buy an
USB savvy mouse these days. Having con-
nected the mouse despite doubts and worries
about the missing disk, you will be pleasantly
surprised to see that the Windows 98 operat-
ing system will automatically find and install
the driver. In fact, Windows 98 will load the
HID (human interface device) driver, which
has been lurking in your PC until a USB-com-
patible device of the ‘defined’ class is con-
nected up. This class is supported by ready-
made drivers.
HIDs include mice, keyboards, pointer
correspondence between the pins at
either side, and the pin functions are
always the same:
speed is an important factor. The
system adopted for ready-made
cables can be relied on to prevent a
low-speed cable being used where a
full-speed cable is called for. All
cables are full-speed cables unless
permanently attached to the USB
device (like the previously men-
tioned mouse). Where a cable is not
long enough, a special type A-A
extension cable may be used.
While USB plugs are not sold in
the shops, the mating socket is avail-
able. Though a bit thin on the
ground, these (PCB mount) sockets
allow you to experiment with the
USB. The bus will present a +5 V
supply voltage which may be loaded
at up to about 100 mA. Many digital
circuits and microcontrollers operate
from a 5-V supply voltage. By con-
necting such a circuit to the USB you
get the required supply voltage
thrown in as a bonus. One ‘must’
however, is that an efficient short-
circuit protection be provided, like a
regular fuse or a Polyswitch fuse.
1 +5 V
2 Data –
3 Data +
4 Ground
At the rear of a modern PC you will
typically find two sockets of the A
type. These may be used for direct
connection to two devices. Relatively
small low-speed peripherals such as
a mouse generally employ a thin,
fixed cable with a type-A plug. In all
other cases, the peripheral will have
its own USB socket of the B type.
The connection to the PC is then
made by means of a type-A cable.
Such cables are only supplied
ready-made and completely moulded
— spare USB plugs are not available.
The length, cable diameter and
screening of USB cables are subject
to tight specifications. Here, the dif-
ference between full-speed and low-
F
5V
100mA
1
2
3
4
100mA
D –
D +
000094 - 12
Figure 4. The USB as a stabilised power supply.
BASI
CS
devices and joysticks. In addition to HIDs
there are also USB classes for soundcards,
printers and many other peripheral devices.
The upshot is that all ‘regular’ devices are
divided into classes, for which suitable dri-
vers are available. This class allocation auto-
matically creates a certain level of standardi-
sation, after all, the device manufacturers
have to make sure their products comply with
the relevant class specifications.
Standard USB devices are no longer
expensive. Not surprisingly, you may wonder
if it is possible to ‘misuse’ a particular USB
peripheral.
For example, a USB compatible soundcard
will install itself in such a way that can be
used as a regular, internal soundcard. After
all, that is the purpose of the whole exercise:
all available programs should be able to
employ the USB device. The same goes for
programs you make yourself. A quick test
using PORT.DLL from the ‘Compact’ software
supplied by Elektor for the extremely popular
Universal Interface for Windows
(December
1999) was a direct success. So, your own pro-
grams written in Delhi should be able to
make use of any USB peripheral connected to
a PC. However, our experience in this area is
still too limited to be able to make the over-
assertive statement that it ‘always works’.
An experiment with a USB game controller
produced the following results: a USB joystick
port may be installed, among others, using
DOS emulation. This allows direct access to
the virtual port addresses of a game card. In
this way, you have added a simple USB inter-
face with four analogue and four digital
inputs to your system.
PC
USB
1
USB Hub
4
2
3
000094 - 13
Figure 5. Daisy chain structure of the USB with one hub.
USB data packets
1 ms
000094 - 14
Figure 6. Data packets is 1-ms frames.
Bus topology
Upstream and four Downstream
ports. The PC, however, will already
contain a hub to implement two USB
ports. This so-called Root Hub is
located on the PC motherboard.
A further hub may be connected
to the downstream port of a hub. A
total of seven hubs may be cascaded
in this way, resulting in a maximum
of 127 devices. That, however, is a
theoretical number because the
available bandwidth has to be dis-
tributed across all devices connected
to the bus.
The USB is a star-shaped bus with a single
master. To be able to connect several USB
devices you will require a Hub, which is sim-
ply a bus-distributing device with several
ports. This device is literally at the hub of the
bus system. Usually, a USB hub has one
D +
3V3
D -
0V
667 ns
83.3 ns
000094 - 15
Figure 7. Low-speed and high-speed signals.
BASI
CS
3V3
have to synchronise to the datastream.
Because a separate clock signal is not dis-
tributed, the clock rate has to be recovered
from the datastream. The NRZI principle (non
return to zero) is used for that purpose. In this
system, only logic zeroes cause the voltage
level to change — for logic ones, the voltage
remains unchanged.
In general, a USB device has several FIFOs
to receive data in. To the device address is
added an endpoint address which indicates
where the data should arrive or where it orig-
inates from. A USB mouse, for instance,
always has an Endpoint 1 and an Endpoint 0.
The latter is used during the initialisation.
The internal microcontroller writes the actual
data into the Endpoint-1 FIFO at regular
intervals, from where they are transmitted to
the PC.
The USB software forms so-called
Pipes
to
individual Endpoints. A Pipe may be likened
to a data channel consisting of a single wire.
In actual fact, however, the data intended for
a certain Pipe are transmitted in the form of
data packets squeezed in a millisecond
frame. Next, the hardware employs the End-
point address to actually distribute the data
to real memory devices. To achieve a higher
data rate, a device may occupy several Pipes
at a time.
USB Hub
Fullspeed
USB Device
D +
D +
USB
USB
Transceiver
D –
D –
Transceiver
000094 - 16
Figure 8. Recognition of a full-speed device.
3V3
USB Hub
Lowspeed
USB Device
D +
D +
USB
USB
Transceiver D –
D –
Transceiver
000094 - 17
Enumeration
Figure 9. Recognition of a low-speed device
A non-used USB connection is inactive, and
the hub will not send data frames to it. The
two datalines are then Low and represent an
internal resistance of about 15 kΩ. Each USB
peripheral device has an internal resistor of
1.5 kΩ connecting one of the datalines with
+3.3 V. In a full-speed device, the D+ line is
pulled up, in a low-speed device, the D– line.
This information is used by the hub to first
determine the type of peripheral it is talking
to, and then set up the required data rate.
With the enumeration of a device, the suit-
able drivers are automatically loaded. Con-
versely, the system will also detect a device
being disconnected from the bus. In that
case, the driver is removed from memory. In
this way, it is a simple matter to employ a sin-
gle USB device with several different com-
puters. All you have to do is disconnect the
USB signals
The signals on the D+ and D– lines
are difference signals with levels of
0 V and +3.3 V. The microcontroller
in the USB peripheral will typically
operate at a supply voltage of 3.3 V.
The USB is a single-master bus — all
activities originate from the PC. Data
is conveyed in packets of eight to
256 bytes. The PC is authorised to
request data from a peripheral. Con-
versely, a peripheral may transmit
data to the PC.
All data traffic takes place in
frames of about 1 millisecond. Within
a frame, several data packets may be
processed for several devices. Low-
speed and full-speed packets may
occur within the same frame. When
several devices are addressed, a bus
distributing device like the previ-
ously mentioned hub arranges the
data distribution.
Low-speed devices operate at a
data rate of 1.5 Mbit/s, so the length
of a bit is 666.7 ns. On full-speed USB
connections, data travels at a rate of
12 Mbit/s, which means that a bit
last 83.33 ns. The speed is dictated
by the master only. All USB slaves
BASI
CS
Some useful addresses
http://www.usb.org
: here, the makers of USB reside, that is, the
representatives of several large companies that teamed up to
define the USB standard. Their major contribution is the USB
Specification, which is available as a downloadable pdf document.
Essentially, this document contains everything. However, it is
debatable if users should stick to just these sources for the infor-
mation they require.
Craig Peacock (
http://www.beyondlogic.com
) wrote his own dri-
ver for the USB thermometer. Craig supplies extremely useful
technical information and documentation on building your own
USB driver. His website on the USB is well worth a visit.
The starter kit supplied by Anchor-Chips
(
http://www.anchorchips.com
) (now with Cypress,
http://www.cypress.com
) for the EZ-USB (AN2131) is based on
an 8051-compatible processor with internal RAM and an USB
core. Programs written for the 8051 may be loaded into the
processor RAM via the USB interface, and started straight away.
The USB core is so powerful that very few additional instructions
are required to produce useful USB applications.
The Starter Kit from Cypress (
http://www.cypress.com
) for their
USB microcontroller type CY7C63000 comes with an USB ther-
mometer as an application example. Unfortunately, this relatively
cheap kit is no longer available. The Elektor Electronics USB inter-
face published in this issue was developed on the basis of this
thermometer. So, if you missed your chance to obtain a Cypress
USB kit, an excellent alternative is available.
More information and hyperlinks may be found on the author’s
homepage
http://www.home.t-online.de/home/B.Kainka
device from the first PC and insert the plug
into the other one. On the second PC, a new
enumeration procedure is started. In this
way, a printer is easily used on two PCs.
PC1
1
2
3
4
D –
D +
The USB switch
PC1
1
2
3
4
For the serial (RS232) and parallel (Centron-
ics) port, switches are available that obviate
the need to plug and unplug cables when you
want to use a different peripheral. Because
only very few bus lines are involved, a simi-
lar switch for USB is easy to build. This
switch should, however, mimic a USB cable
being unplugged and plugged in again. Also,
to make sure the peripheral is properly reset,
sufficient time should be allowed to elapse
before it is connected up to the bus. After all,
the internal microcontroller has to be
restarted to enable a new enumeration
process to be initiated. From the above it
should be clear why the USB switch has a
neutral position in which the printer is not
connected to either of the two PCs.
Another small detail should be observed.
If you look closely at a USB plug, you will
notice that pins 1 and 4 for the positive sup-
ply voltage and ground are a little longer than
pins 2 and 3 for the datalines. When the cable
is plugged in, the supply pins are connected
first. Consequently, the USB peripheral is reli-
ably powered before the datalines are con-
nected. This system also reduces the risk of
damage caused by static charges building up
on peripheral devices, or equalisation current
that may flow between devices if there is a
problem with the earth connections. In brief,
voltage surges caused by connecting devices
can not cause damage to the electronics con-
nected to the datalines. By the way, a paral-
lel printer cable has an extended metal cover
which touches the computer before the plug
contacts are actually inserted.
D –
D +
Printer
1
2
3
4
D –
D +
000094 - 18
Figure 10. The USB printer switch.
Equalisation currents between
USN devices could also be prevented
by connecting all ground lines
together in the USB switch. This,
however, causes a large earth loop
via the protective earth lines con-
nected to the PC. Under adverse
conditions, earth loops could lead to
serious interference and reduced
reliability of the entire computer sys-
tem. Arguably, that should be
avoided, hence all four USB lines are
switched together in the switch box.
The special behaviour of the
longer connecting pins may be mim-
icked by a switch with five contacts.
When switching over to another USB
device, the supply voltage is con-
nected first, then the datalines D–
and D+. The centre position is ‘neu-
tral’. Depending on the USB periph-
eral involved, a delay of one second
may be required.
It should be noted that this is an
experimental proposition which
could not be extensively tested
before publication of this article. In
particular, the exact type of printer
used will determine if fast or slow
switching should be used, or if the
delay with the centre position is at
all required. Fortunately, it should
not be too difficult to ‘automate’ the
switch box using two relays and a
small circuit.
(000094-1)
Continued next month
Plik z chomika:
TirNaNog
Inne pliki z tego folderu:
bge.jpg
(22 KB)
detail10.htm
(5 KB)
detail1.htm
(5 KB)
detail12.htm
(15 KB)
detail11.htm
(7 KB)
Inne foldery tego chomika:
1974
1975
1976
1977
1978
Zgłoś jeśli
naruszono regulamin