Universal Serial Bus (USB) is a standard interface for connecting peripheral
devices to a host computer. The USB system was originally devised by a group of
companies including: Compaq, Digital Equipment, IBM, Intel, Microsoft, and
Northern Telecom to replace the existing mixed connector system with a simpler architecture.
USB was designed to replace the multitude of cables and connectors required
to connect peripheral devices to a host computer. The main goal of USB was to
make the addition of peripheral devices quick and easy. All USB devices share
some key characteristics to make this possible. All USB devices are
self-identifying on the bus. All devices are hot-pluggable to allow for true Plug'n'Play capability. Additionally, some devices can draw
power from the USB bus which eliminates the need for extra power adapters.
To ensure maximum interoperability the USB standard defines all aspects of
the USB system from the physical layer (mechanical and electrical) all the way
up to the software layer. The USB standard is maintained and enforced by the
USB Implementer's Forum (USB-IF). USB devices must pass a USB-IF compliance
test in order to be considered in compliance and to be able to use the USB
logo.
The USB standard specifies several different flavors of USB: Low-Speed,
Full-Speed and High-Speed. USB-IF has also released additional specs that
expand the breadth of USB. These are On-The-Go (OTG) and Wireless USB. Although
beyond the scope of this document, details on these specs can be found on the
USB-IF website.
The key difference between Low, Full, and High speed is bandwidth.
|
Low |
1.5 Mbps |
|
Full |
12 Mbps |
|
High |
480 Mbps |
The USB specification can be viewed and downloaded on the USB-IF website.
USB is a host-scheduled, token-based serial bus protocol. USB allows for the
connection of up to 127 devices on a single USB port. A host PC can have
multiple ports which increases the maximum number of USB devices that can be
connected to a single computer.
These devices can be connected and disconnected at will. The host PC is
responsible for installing and uninstalling drivers for the USB devices on an
as-needed basis.
A single USB system comprises of a one USB host and one or more USB devices.
There can also be zero or more USB hubs in the system. A USB hub is special
class of device. The hub allows the connection of multiple downstream devices
to an upstream host or hub. In this way, the number of devices that can be
physically connected to a computer can be increased.
A USB device is a peripheral device that connects to the host PC. The range
of functionality of USB devices is ever increasing. The device can support
either one function or many functions. For example a single digital camera may
present several devices to the host when it is connected via USB. It can
present a Camera Device, a Mass Storage Device, etc.
All the devices on a single USB bus must share the bandwidth that is
available on the bus. It is possible for a host PC to have multiple busses
which would all have their own separate bandwidth. Most often, the ports on
most motherboards are paired, such that each bus has two downstream ports.
Figure 1: Sample USB Bus Topology.
A USB bus can only have a single USB host device. This host can support up to
127 different devices on a single port. There is an upper limit of 7 tiers of
devices which means that a maximum of 5 hubs can be connected inline.
The USB bus has a tiered star topology. At the root tier is the USB host.
All devices connect to this device either directly or via a hub. According to
the USB spec, a USB host can only support a maximum of seven tiers between a the host and a USB device.
All USB devices are connected by a four wire USB cable. These four wires are
VBUS, GND and the twisted
pair: D+ and D-. USB uses differential signaling on the D+ and D- to encode
binary information. The idle polarity of the signal indicates whether the
device is a low/full speed or high speed device.
Figure 2: USB Cable
A USB cable has two different types of connectors: A
and B. A connectors connect upstream towards the Host
and B connectors connect downstream to the Devices.
USB cables have two different types of connectors: A and B. At the most basical level, A type connectors
connect a device to the host or in the host direction and the B connectors
connect to downstream devices. This is part of the spec to prevent loopbacks in
the USB bus. The USB spec has been expanded to include a Mini-B connector to
support small USB devices and the OTG spec has introduced a mini AB connector
to allow for device to device connections.
This introduction is a general summary of the USB spec. Total Phase strongly
recommends that developers consult the USB specification on the USB-IF website
for detailed and up to date information.
USB devices vary greatly in terms of function and communication
requirements. Some devices are single-purpose, such as a mouse or keyboard.
Other devices may have multiple functionalities that are accessible via USB
such as a printer/scanner/fax device.
The USB-IF Device Working Group defines a discreet number of device classes.
The idea was to simplifying software development by specifying a minimum set of
functionality and characteristics that is shared by a group of devices and
interfaces. Devices of the same class can all use the same USB driver. This
greatly simplifies the use of USB devices and saves the end-user the time and
hassle of installing a driver for every single USB device that is connected to
their host PC.
For example, input devices such as mice, keyboards and joysticks are all
part of the HID (Human Interface Device) class. Another example is the Mass
Storage class which covers removable hard drives and keychain flash disks. All
of these devices use the same Mass Storage driver which simplifies their use.
However, a device does not necessarily need to belong to a specific Device
Class. In these cases, the USB device will require its own USB driver that the
host PC must load to make the functionality available to the host.
The endpoint is the fundamental unit of communication in USB. All data is
transferred through virtual pipes between the host and these endpoints. Each
endpoint is a unidirectional receiver or transmitter of data.
Endpoint 0 is a special endpoint that does not have a descriptor. All
devices use this endpoint for standard control transfers to configure and setup
the device.
Endpoints are not all the same. Endpoints specify their bandwidth
requirements and the way that they prefer to transfer data. There are four
basic types:
Used for device configuration.
This is a transaction that is guaranteed to occur within a certain time
interval. The device will specify the time interval at which the host should
check the device to see if there is new data. This is used by input devices
such as mice and keyboards.
Periodic and continuous tranfer
for time-sensitive data. There is no error checking of the data sent in
these packets. This is used for devices that need to reserve bandwidth and have
a high tolerance to errors. Examples include multimedia devices for audio and
video.
General transfer scheme for large chunks of data.
This type of transfer has the lowest priority. If the bus is busy with other
transfers, this transaction may be delayed. The data is guaranteed to arrive
without error. If an error is detected in the CRCs,
the data will be retransmitted. Examples of this type of transfer are files
from a mass storage device or the output from a scanner.
When a device is plugged into a host PC, the device undergoes Enumeration.
Essentially this means that the host recognizes the presence of the device and
assigns it a unique 7-bit address. The host PC then queries the device for its
descriptors, which contains information about the specific device. There are
various types of descriptors as outlined below.
Figure 3: USB Descriptors
Hierarchy of descriptors of a USB device. A device has a single Device
descriptor. The Device descriptor can have multiple Configuration descriptors,
but only a single one can be active at a time. The Configuration descriptor can
define one or more Interface descriptors. Each of the Interface descriptors can
have one or more alternate settings, but only one setting can be active at a
time. The Inteface descriptor defines one or more
Endpoints.
All these transactions occur in three phases: Token, Data, and Handshake.
Figure 4: The Three Phases of a USB
Transfer
A USB transaction has three phases
All communication on the USB bus is host-directed. In the Token phase, the
host will generate a Token packet which will address a specific device/endpoint
combination. A Token packet can be IN, OUT, or SETUP.
|
IN |
the host will receive data to be
transmitted from the addressed dev/ep. |
|
OUT |
the host will transmit data to
the addressed dev/ep as receiver. |
|
SETUP |
the host will transmit control
information to the device. |
In the data phase, the transmitter will send one or more Data Packets. It is
also possible for a device to send a NAK or STALL packet at this time
indicating that it isn't able to service the IN token that it received.
Finally, in the Handshake phase the receiver can send an ACK, NAK, or STALL
indicating the success or failure of the transaction.
All of the transfers described above follow this general scheme with the
exception of the Isochronous transfer. In this case,
no Handshake phase occurs because it is more important to stream data out in a
timely fashion. It is acceptable to drop packets occasionally and there is no
need to waste time by attempting to retransmit those particular packets.
All USB packets are sent LSB first and MSB last. All packets begin with a
SYNC field. It is the Start of Packet (SOP) marker and is also used to
synchronize the incoming data from the transmitter with the local clock of the
receiver. This SYNC field is 8 bits for full/low speed and 32 bits for high
speed. The boundary between the SYNC field and the PID field are delimited by a
2-bit marker at the end of the SYNC field.
The PID field encodes the packet type. It is an 8-bit field that consists of
the 4-bit packet type followed by a 4-bit one's complement of the PID packet
type as a check field. If a recieved PID fails its
check, the remainder of the packet will be ignored by the USB device.
There are four types of PID which are described in table 1.
|
The format of the IN, OUT, and SETUP Token packets is shown in figure 5. The format
of the SOF packet is shown in figure 6. The format of
the Data packets is shown in figure 7. Lastly, the
format of the Handshake packets is shown in figure 8.
Figure 5: Token Packet Format
Figure 6: Start-Of-Frame (SOF) Packet
Format
Figure 7: Data Packet Format
Figure 8: Handshake Packet Format