Book
Book
Book
1: Introduction
At over two billion new installed units per year, USB is the most
successful personal-computer interface ever. Every recent PC has
USB ports that can connect to keyboards, mice, game controllers,
scanners, cameras, printers, drives, and more. USB is reliable, fast,
versatile, power conserving, inexpensive, and supported by major
operating systems. USB 3.0s new SuperSpeed bus means USB is
likely to continue to dominate as the interface of choice for an ever-
expanding selection of peripherals.
Lets start
The Universal Serial Bus (USB) is a standard for peripheral devices. It
began development in 1994 by a group of seven companies:
Compaq, DEC, IBM, Intel, Microsoft, NEC and Nortel. USB was
intended to make it fundamentally easier to connect external devices
to PCs by replacing the multitude of connectors at the back of PCs,
addressing the usability issues of existing interfaces, and simplifying
software configuration of all devices connected to USB, as well as
permitting greater bandwidths for external devices. The first silicon
for USB was made available by Intel in 1995.
USB devices are linked in series through hubs. There always exists
one hub known as the root hub, which is built into the host
controller. So-called sharing hubs, which allow multiple computers to
access the same peripheral device(s), also exist and work by
switching access between PCs, either automatically or manually.
Sharing hubs are popular in small-office environments. In network
terms, they converge rather than diverge branches.
Easy to use
Ease of use was a major design goal for USB, and the result is an
interface thats a pleasure to use for many reasons:
Evolution of USB
USB 1.0
The Universal Serial Bus Specification Revision 1.0 was
released in January 1996. USB capability first became available
on PCs with the release of Windows 95s OEM Service Release
2, available only to vendors installing Windows 95 on PCs they
sold. The USB support in these versions was limited and buggy,
and there werent many USB peripherals available, so use of
USB was limited in this era.
USB 1.1
The Universal Serial Bus Specification Revision 1.1 (September
1998) added one new transfer type (interrupt OUT).
USB 2.0
As USB gained in popularity and PCs became more powerful,
demand grew for a faster bus speed. Investigation showed that
a bus speed 40 faster than full speed could remain
backwards-compatible with the low- and full-speed interfaces.
April 2000 saw the release of the Universal Serial Bus
Specification Revision 2.0, which added high speed at 480
Mbps. High speed made USB more attractive for peripherals
such as printers, disk drives, and video cameras. Windows
added support for USB 2.0 in Windows XP SP2. The USB 2.0
specification replaced USB 1.1.
Except for hubs, a USB 2.0 device can support low speed, full
speed, or high speed, and a high-speed-capable device can
support full speed when connected to a USB 1.x bus. USB 1.x
refers to USB 1.0 and 1.1.
A USB 2.0 hub must support all three USB 2.0 speeds. The
ability to communicate at any speed increases the complexity
of the hubs but conserves bus bandwidth and eliminates a
need to use different hubs for different speeds.
USB 3.0
The Universal Serial Bus 3.0 Specification Revision 1.0 was
released in November 2008, with the first USB 3.0 device-
controller hardware expected to follow about a year later.
Windows will likely support USB 3.0 sometime after the
release of Windows 7, the successor to Windows Vista.
Wireless USB
Developers who want to design devices with wireless
interfaces have several choices. The Wireless USB Promoter
Groups Wireless Universal Serial Bus Specification defines the
Certified Wireless USB (WUSB) interface for communicating at
up to 480 Mbps. Cypress Semiconductor's WirelessUSB
enables implementing wireless devices that function as low-
speed USB devices. Another option is to use an adapter that
converts between USB and a wireless interface such as Zigbee,
Bluetooth, or WiFi.
Benefits
Adding storage-device capabilities to a system has several benefits:
1. With a USB device controller, a system can make the contents
of its storage media available to any PC or other USB host
computer.
2. File systems provide a standard way to store and access data.
A PC or other USB host can format the media in a USB storage
device to use the FAT16 or FAT32 file system.
3. Storage media is readily available. Flash-memory cards are
convenient and have enough capacity for many applications.
Requirements
Adding storage capabilities and a USB interface to an embedded
system requires hardware and firmware to support accessing the
storage media and communicating over the USB interface.
Devices
An embedded system that functions as a USB mass-storage
device requires the following hardware: (See figure 1-2)
Embedded Hosts
An embedded system that functions as a USB host for flash or hard
drives requires the following hardware: (See figure 1-3)
A microcontroller or other CPU or intelligent hardware to
manage the embedded systems operation.
A USB host controller, which can be embedded in a
microcontroller chip or on a separate chip that interfaces to
the CPU, microcontroller, or other intelligent hardware.
A generic hard drive, flash drive, or other media connected to
a USB port on the host.
The user looks at the USB system as host, hub and device:
Hosts and Devices
Every USB communication is between a host and a device. The host is
in charge of the bus. Devices communicate only when requested to
do so by the host.
Hosts
A USB host is a computer that contains USB host-controller hardware,
a root hub with one or more USB ports, and program code to manage
communications and events on the bus.
The root hub has one or more connectors for attaching devices.
3-Manage Traffic on the Bus: The host manages the flow of data on
the bus. Multiple peripherals may want to transfer data at the same
time. The host controller divides the available time into segments
called frames (on a full-speed host) or microframes (on a high-speed
host).
Device Responsibilities
In many ways, a devices responsibilities mirror the hosts, but
devices also have unique duties and a USB device has these
responsibilities:
All of the above tasks support the hosts & devices main job, which is
to exchange data.
OTG On-The-Go
An On-The-Go (OTG) device is a special kind of USB device that can
function as a limited-capability host or as a device. An On-The-Go
device has a mini-AB receptacle that can accept a mini-A plug or a
mini-B plug. An example of a USB On-The-Go device is a camera that
can function as a mass-storage device that stores images that PCs can
access via USB and as a host that sends images to a USB printer.
Bus speed
The bus speeds describe the rate that information travels on the bus.
In addition to data, the bus must carry status, control, and error-
checking signals. Plus, all peripherals must share the bus. So the data
throughput for a device is always less than the bit rate on the bus.
This table shows the differences between bus speeds:
Device Endpoints
Each USB logical device is composed of a collection of independent
endpoints. Each logical device has a unique address assigned by the
system at device attachment time. Each endpoint on a device is given
at design time a unique device-determined identifier called the
endpoint number. Each endpoint has a device-determined direction
of data flow (IN/OUT). A device can have up to 30 additional
endpoint addresses. Each of these endpoint addresses has a number
(1 to 15) and direction (IN or OUT).The combination of the device
address, endpoint number, and direction allows each endpoint to be
uniquely referenced. Each endpoint is a simplex connection that
supports data flow in one direction: either input (from device to
host) or output (from host to device).Endpoints other than those
with endpoint number zero are in an unknown state before being
configured and may not be accessed by the host before being
configured.
Note that: The high-speed device may or may not be able to support
its intended functionality when operating at full speed.
Pipes
Pipes represent the ability to move data between software on the
host via a memory buffer and an endpoint on a device. The USB does
not interpret the content of data it delivers through a pipe. Even
though a message pipe requires that data be structured according to
USB definitions, the content of the data is not interpreted by the
USB.
For such an IRP, short packets on input that do not completely fill an
IRP data buffer can have one of two possible meanings, depending
upon the expectations of a client:
Because the Host Controller must behave differently in the two cases
and cannot know on its own which way to behave for a given IRP; it is
possible to indicate per IRP which behavior the client desires. There
are two mutually exclusive pipe communication modes:
1. Stream: Data moving through a pipe has no USB-defined
structure.
2. Message: Data moving through a pipe has some USB-defined
structure.
Stream pipes
Stream pipes deliver data in data packet portion of bus transactions
with no USB-required structure on the data content. Stream pipes
are always unidirectional in their communication flow. A stream pipe
to a device is bound to a single device endpoint number in the
appropriate direction. The device endpoint number for the opposite
direction can be used for some other stream pipe to the device.
Stream pipes support bulk, isochronous, and interrupt transfer types.
Message pipes
Message pipes interact with the endpoint in a different manner than
stream pipes:
1. A request is sent to the USB device from the host.
2. This request is followed by data transfer(s) in the appropriate
direction.
3. A Status stage follows at some later time.
Transfer Types
One reason why USB is suitable for a wide range of devices is its
support for four types of data transfers:
Control transfer. Bulk transfers.
Interrupt transfer. Isochronous transfer.
Note that:
Mass-storage devices rarely use interrupt transfers except for some
full-speed floppy drives. Mass-storage devices dont use isochronous
transfers.
Control transfer
Control transfers enable the host to learn about a device, set a
devices address, and select configurations and other settings.
Control transfers can also send vendor-specific requests that transfer
data for any purpose. Control transfers always use the default
control pipe and it is a message pipe. All USB devices must support
control transfers.
A control transfer has two or three stages. In the Setup stage, the
host sends a request. In the Data stage, the host or device sends
data. Some requests dont have a Data stage. In the Status stage, the
receiver of data in the Data stage returns status information. If there
is no Data stage, the device returns the status information.
The bus frequency and frame timing limit the maximum number of
successful control transfers within a frame for any USB system:
1. For full speed buses, the number of successful control
transfers per frame is limited to less than 29 full-speed eight-
byte data payloads.
2. For low-speed buses, the number of successful control
transfers per frame is limited to less than 4 low speed eight-
byte data payloads.
Tables 2-1 &2-2 show low speed and full speed control transfer
limits.
If endpoint returned busy status and the host wanted to send data or
status transaction, the host will retry sending at different time. If the a
setup transaction was sent to endpoint before completing a previous
initiated control transaction, the current control transaction will be
aborted and the endpoint will serve the new control transaction. If an
error occurred, a halt condition will be initiated. The halt condition will
be removed by accepting the next setup PID. For the Default Control
Pipe, a device reset will ultimately be required to clear the halt or error
condition if the next Setup PID is not accepted. USB protocol provides
good error detection and recovery during control transfers. A
transmitter can determine that its corresponding receiver has
successfully accepted a transmitted packet by information returned in a
handshake. Setup packets may be retransmitted due to a transmission
error however, Setup packets cannot indicate that a packet is an original
or a retried transmission.
Bulk Transfers
Bulk transfers used to transfer relatively large amount of data over variable
time. Requesting a pipe with bulk transfer type provides the requester with
the following:
The token packet contains the device address and the endpoint
number the transaction is directed to. The token packets PID
identifies the packet as one of these types: SETUP (first packet in a
control transfer), OUT (other host-to-device packet), IN (device-to-
host packet), or SOF (start-of-frame marker). See fig. 2-2
The data packet contains any data the host or device is sending in
the transaction. For control transfers, the transfer stage and the
request determine who sends the data. For other transfers, the
endpoints direction determines who sends the data. The PID
contains the data-toggle value, as explained below. See fig. 2-3
The handshake packet is sent by the receiver of the data packet. The
PID contains a code to indicate whether the data was received
without error. A code of ACK means success, NAK means busy and
STALL means either that the device doesnt support a received
request in a control transfer or that the endpoints Halt feature is set.
High-speed bulk OUT endpoints can also return a NYET handshake
code, which means that the endpoint accepted the data in the
current transaction but isnt yet ready for more data. See fig. 2-4
The Data Toggle
The data toggle is a data-sequencing value that guards against lost or
duplicated data.
Each endpoint maintains its own data-toggle value, which alternates
between DATA0 and DATA1. Devices typically store the value in a
register bit. When the host configures a device on power up or
attachment, the host and device each set their data toggles to
DATA0. On detecting an incoming data packet, the host or device
compares the state of its data toggle with the data toggle in the
received data packet. If the values match, the data packets receiver
toggles its value for the next transaction and returns an ACK. On
receiving the ACK, the data packets sender toggles its value for the
next transaction. The next received packet should contain a data
toggle of DATA1, and again the receiver toggles its bit and returns an
ACK. In additional transactions the data toggle continues to alternate
between DATA0 and DATA1. An exception is control transfers, where
the Status stage always uses DATA1.
If the receiver is busy and returns a NAK, or if the receiver detects
corrupted data and returns no response, the sender doesnt toggle
its bit and tries again with the same data and data toggle.