Está en la página 1de 4

USBs

An introduction to USB
development
By Jack Ganssle

The Universal Serial Bus (USB) was


born out of the frustration PC us-
ers experience trying to connect
an incredibly diverse range of
peripherals to their computers.
It’s the child of vendors whose
laptops require a small profile
peripheral connector. It further
promises to reduce the prolifera-
tion of cables and wall transform-
ers that overwhelm even the
smallest computer installation.
Above all, USB offers users
simple connectivity. It elimi-
nates the vast mix of different
connectors for printers, key-
boards, mice, and other periph-
erals. In a USB environment, DIP
switches aren’t necessary for
setting peripheral addresses
and IRQs. It supports all kinds
of data, from slow mouse
inputs to digitised audio and
compressed video.
Perhaps USB seems an inap-
propriate topic for Embedded
FIGURE 1: The USB “tiered star” topology
Systems Programming. Why
would embedded folks care
about something as PC-cen- two variations of connectors. nodes that interconnect de- The specification recognises
tric as USB? The fact is, every High-speed cables, for 12Mbps vices. One, and only one, “host” two kinds of peripherals: stand-
USB device is an embedded communication, are better device (typically a PC) includes alone (single function units,
system. If you’re building an shielded than their less ex- the “root hub” which forms like a mouse) or compound
application that connects to pensive 1.5Mbps counterparts. the nexus for all device con- devices (those that have more
a PC, realise that USB is sup- Each cable has an “A” connector nections. The host is the USB than one peripheral sharing
planting old-fashioned parallel on one end and a “B” on the system’s master, and as such, a USB port). An example of a
and serial interfaces. Sooner other. Figure 1 shows that “A” controls and schedules all com- compound device is a video
or later you’ll have to migrate connectors go to the upstream munications activities. camera with separate audio
away from RS-232 since there connection while the “B” version Peripherals, the devices con- processor.
are other options (USB, Firewire, attaches downstream. Since the trolled by USB, are slaves respond- Hubs are bridges; they in-
and others). two types are physically differ- ing to commands from the host. crease the logical and physical
ent it’s impossible to install a When a peripheral is attached to fan-out of the network. A hub
USB overview cable incorrectly. the USB network, the host com- has a single upstream connec-
USB is a serial protocol and Cable lengths are limited municates with the device to learn tion (that going to the root hub,
physical link, which transmits to 5m for 12Mbps connec- its identity and to discover which or the next hub closer to the
all data differentially on a single tions and 3m for 1.5Mbps. This device driver is required (a process root), and one to many down-
pair of wires. Another pair pro- sounds backwards, but stems called enumeration). To avoid stream connections.
vides power to downstream from the use of better cables at the DIP switch and IRQ headaches Hubs are themselves USB
peripherals. higher speeds. USB’s topology of the past, the host supplies a devices, and may incorporate
The USB standard speci- is a “tiered star” (see Figure 1). device address to the peripheral some amount of intelligence.
fies two kinds of cables and Hubs are the communication during enumeration. A critical part of the philosophy

 eetindia.com | March 2000 | EE Times-India


of USB is that users may con-
nect and remove peripherals
without powering the entire
system down. Hubs detect
these topology changes. They
also source power to the USB
network; power can come
from the hub itself (if it has a
built-in power supply), or can
be passed through from an
upstream hub.
Once a hub detects a new
peripheral (or the removal of
one), reports the new informa-
tion to the host, and enables
communications with the new-
ly inserted device, it essentially
becomes invisible smart wire,
passing data between the host
and the devices. Though physi-
cally configured as a tiered star,
logically (to the application
code) a direct connection ex- FIGURE 2: Windows USB WDMMode
ists between the host and each
device. to make them analogous to The host device driver process into a stack of drivers
household plumbing. The days of simple interfaces like (see Figure 2). Application code
USB comm overview USB supports four data trans- RS-232 are long gone, as are the (via Windows API calls) com-
USB communications takes place fer types: control, isochronous, frustrations of making incompat- municates with class or custom
between the host and endpoints bulk, and interrupt. ible devices talk reliably. USB is a drivers in the WDM. Within the
located in the peripherals. An Control transfers exchange complex standard that requires WDM stack itself data transfers
endpoint is a uniquely address- configuration, setup, and com- an enormous amount of software use lower-level IRP (I/O request
able portion of the peripheral mand information between the support, both on the firmware packets) rather than API calls.
that is the source or receiver of device and the host. CRCs check side and in the host computer. The low-level USB bus driver
data. Four bits define the device’s the data and initiate retransmis- Most host-end connections, manages USB device power,
endpoint address; codes also sions when needed to guarantee for better or worse, will be PCs enumeration, and various USB
indicate transfer direction and the correctness of these packets. running a Microsoft operating transactions. Below this, the
whether the transaction is a Bulk transfers move large system. USB is not supported at host controller driver talks di-
“control” transfer. Endpoint 0 is amounts of data when timely all in DOS, Windows 3.x, or Win- rectly to the USB hardware in
reserved for control transfers, delivery isn’t critical. Typical ap- dows NT. Windows 95 provided the PC. Both of these drivers are
leaving up to 15 bi-directional plications include printers and some USB drivers, though only supplied with current Windows
destinations or sources of data scanners. Bulk transfers are fillers, in the later versions starting versions; you won’t have to
within each device. claiming unused USB bandwidth with OEM Software Release 2.1. write or modify either.
The idea of endpoints leads when nothing more important All Windows 98 releases include Windows, as well as the USB
to an important concept in USB is going on. CRCs protect these a full set of drivers for common specification, segments drivers
transactions, that of the pipe. All packets. USB applications and Windows into “classes,” where hardware
transfers occur through virtual Interrupt transfers, though 2000 (the next generation of that falls into a single class
pipes that connect the periph- not interrupts in the CPU-divert- both 98 and NT) will too. shares similar interfaces. A class
eral’s endpoints with the host. ing sense, poll devices to see if Some of the most brilliant defines a baseline specification
When establishing communica- they need service. Peripherals ex- firmware engineers quail at the for a given set of capabilities;
tions with the peripheral, each changing small amounts of data thought of writing Windows all devices in a class require
endpoint returns a descrip- that need immediate attention drivers, with good reasons. Un- comparable types of software
tor, a data structure that tells (such as mice and keyboards) use happily, a USB driver is a difficult support.
the host about the endpoint’s interrupt transfers. Error checking beast. The good news is that in An example is the human
configuration and expectations. validates the data. many cases the drivers provided interface device (HID) class,
Descriptors include transfer Finally, isochronous transfers with Windows will handle even which supports devices like
type, max size of data packets, handle streaming data like that your custom peripheral. Let’s mice, joysticks, and keyboards.
perhaps the interval for data from an audio or video device. look at how Windows drivers Another is the monitor class,
transfers, and in some cases, It is time sensitive information function. which controls image position,
the bandwidth needed. Given so, within limitations, it has guar- Microsoft’s roadmap for driv- size, and alignment on video
this data, the host establishes anteed access to the USB bus. ers in Windows 98 and beyond displays. Windows 98 ships with
connections to the endpoints No error checking occurs so the relies on the Win 32 Driver Model a complete HID class driver, so
through virtual pipes, which system must tolerate occasional (WDM), which layers different if your peripheral requires HID-
even have a size (bandwidth), scrambled bytes. parts of the communications like support, you may be able to

 eetindia.com | March 2000 | EE Times-India


use this built-in driver without port chips. The best reference
Power management
writing any host code. to these ICs is IBH Doran’s (a
Current specifications of German consultancy company)
“Dad, you’ve got to turn that laptop off before connecting the
class drivers may be found on Web site at www.ibhdoran.com/ disc drive.” My 12-year-old understands the peril of connecting
the USB home page ( www. usb_link.html. a floppy to a running computer. But this drive’s USB link makes
usb.org); Windows support USB parts are rather hard such worries obsolete. I’m free to install and remove this USB
for classes is available from to categorise, but fall gener- peripheral regardless of power state.
Microsoft, but at this time is ally into three camps: host-side Two of the four wires in a USB cable supply power to peripherals.
somewhat limited (though HID- USB controllers (which live Though nominally +5V, the spec allows for quite a bit of variation
class devices are indeed fully inside the PC, and are prob- in this; designers should allow for as little as about 4V. A peripheral
supported). ably of little interest to ESP that draws up to 100ma can extract all of its power from the bus
Custom drivers are an alter- readers), devices designed as wiring all of the time. Higher current requirements are trickier; if the
device requires less than 500ma, and if the upstream host or hub
native to class drivers. A custom stand-alone USB peripheral
can provide that much power (which is optional), the device can
driver exploits the capabilities controllers (like a smart UART,
be bus-powered if at power-up time, during system configuration,
of a particular piece of hardware these chips handle communi- it consumes less than 100ma. If the device needs more than a
at the end of the USB cable. If cations but you’ll need another half-amp, then it must have its own power supply.
you’ve built a data acquisition microprocessor as the brains USB hosts and hubs manage power by enabling and disabling
system, for example, odds are of your device), and versions of power to individual devices to electrically remove ill-behaved
there’s no class driver available popular processors that include peripherals from the system. Further, they can instruct devices
so you’ll have to write your a USB interface. Using the UART to enter the suspend state, which reduces maximum power
own. Similarly, if your device has metaphor again, this last group consumption to 500 microamps (for low-power, 1.5Mbps
capabilities well beyond that of is like the high-integration CPU peripherals) or 2.5ma for 12Mbps devices.
a standard class you may also with an on-board UART; both These are average states over a one-second period. A low-duty
cycle event can consume more power as long as it meets the
have to write a custom driver to your application code and that
average spec.
support these features. needed for USB control runs on
Visual C++ can compile WDM the same part.
drivers, of course. Download the Beyond these three cat- fered by other vendors, includes cess.html), building on their
Windows 98 Driver Developer’s egories, some vendors offer WDM drivers. Interestingly, BIOS products, offers firmware-
Kit (DDK) from www.microsoft. specialised parts, such as USB Scanlogic claims users can bring side USB stacks that tie into
com/DDK/ddk98.htm, as this re- camera controllers, audio de- an embedded USB app up in commercial real-time operating
source includes example code vices, bridges that link USB to only five weeks (on their hard- systems, such as those from
for several USB drivers. other buses, and specialised ware, of course). Accelerated Technology, Lynx,
BlueWater Systems (www. HID controllers. Philips’ PDIUSBD11 is an and Integrated Systems/Wind
bluewaternz.com/) also has a It’s impossible to do justice intriguing chip that connects a River Systems.
driver development kit whose to various products here. A few USB port to I2C. I2C is a speedy, Development tools are as
wizard greatly eases any sort of highlights follow. two-wire serial interface that important as chips and code.
Windows driver development. Cypress Semiconductor ( a lot of embedded systems USB is a complex protocol that
An add-on, the USB Extensions www.cypress.com) has a variety already employ, and one that tosses a lot of data around. De-
Toolkit, is a boon for USB driv- of high- and low-speed chips comes built into some micro- bugging by looking at the serial
ers, but be aware of the per-unit based on 8-bit RISC cores with controllers. So, using the PDI- stream on a scope is not effi-
royalty charge. instruction sets optimised for USBD11 you could conveniently cient. Several companies offer
Unless you’re building a typi- USB applications. Both one-time tie your current I2C-aware prod- protocol analysers that monitor
cally PC-centric peripheral like a programmable and EPROM uct to a PC’s USB port. the USB link and display trans-
mouse, you’ll likely also create a parts are available. Their devel- A number of vendors of- mitted data in an understand-
host application that exchanges opment kits (the starter kit costs fer versions of their control- able form. Two are Hitex’s USB
data with the USB device and US$99, but the much more lers with an on-board USB Agent ( www.hitex.de/usb/pro-
interacts with the user. An useful developer’s kit runs a port, giving you USB access tan.htm) and CATC’s USB Chief
oscilloscope, say, using an A/D still-reasonable US$495) are the without using a processor Bus & Protocol Analyser ( www.
converter and triggering logic way to go for getting firmware devoted to communications catc.com/home.html).
located at the end of a USB ca- running. alone. Motorola’s products
ble, requires an application with Cypress bought Anchor ( range from a USB-aware 6805 USB in the lab
a scope-like GUI. To exchange www.anchorchips.com) last (MC68HC05JB4) to the Pow- A lot of us use PCs to control
data with the USB device the year; Anchor’s EZ-USB 8051- erPC MPC850. AMD added short-run products, or to handle
application code simply issues based chips use a standard USB to the venerable x86 line simple monitoring tasks in the lab.
standard file-like API calls, using instruction set and come in a with their 186CC. Both Atmel RS-232 and parallel printer ports,
a standard Windows handle to wide variety of RAM and ROM (AT43USB321) and Microchip the staple connection for many of
identify the device. sizes. They, too, offer a US$495 (PIC 16C745) have microcon- these applications, are often just
developer’s kit. troller products with the com- not available on recent PCs. In fact,
The chips Scanlogic’s SL16-USB con- munications link. many laptops now offer these only
Since USB is (for all practical pur- troller ( www.scanlogic.com) is Some vendors offer low-level on an expansion port, yet include
poses) tied to the high-volume a custom-architecture 12Mbps USB drivers you can embed into USB on the main unit.
PC business, dozens of vendors controller. Their development your products. Phoenix ( www. Several companies now sell
offer hundreds of different sup- board, like most of those of- phoenix.com/platform/usbac- data acquisition products that

 eetindia.com | March 2000 | EE Times-India


incorporate a USB link. For ex- nect sensors or instruments to communications speed to a eral is the best around.
ample, National Instruments, USB-based computers. breathtaking 480Mbps. Clearly, Garney, John, Ed Solari,
the people who provide the new classes of applications are Shelagh Callahan, Kosar Jaff,
popular LabView software pack- Compliance not far away. and Brad Hosler. USB Hard-
age, sells the “DAQPad” family The USB sponsoring organisation ware & Software. San Di-
of instruments ( www.ni.com/ has wisely created a compliance Resources ego, CA: Annabooks, 1998.
daq/), with 16 analogue 12-bit program to ensure that devices www.usb.org. Home of the USB This book bills itself as “the
inputs, two 12-bit DAC outputs, meet the standard’s specifica- organisation, a consortium of definitive reference to the
and a mix of digital I/Os. Iotech’s tions. Though no law mandates members who promulgate and Universal Serial Bus,” and so
“Personal Daqs” ( www.iotech. that any device must pass these enhance the standard. The site it is. A large, dense book, it
com/catalog/daq/persdaq. tests, doing so ensures that the has some useful developer infor- covers about every nuance of
html) are small sensors that user’s experience with your mation, especially their message USB communications. This is a
offer up to 80 channels of ana- products will be as trouble-free forum ( www.usb.org/forums/de- “must have” for USB develop-
logue and digital inputs, with a as possible. Products meet- velopers/webboard.html), which ers. The authors worked on the
22-bit A/D converter. ing the compliance program’s stays busy with questions and original USB specification.
If you’re building your own requirements get added to the answers from active developers. Tan, Wooi Ming. Develop-
low-volume/lab-based link to Integrator’s List, a sort of impri- Axelson, Jan. USB Com- ing USB PC Peripherals. San
a PC, and can’t stomach the matur so customers can be sure plete. Madison, WI: LakeV- Diego, CA: Annabooks, 1997.
thought of designing your own that, from a communications iew Research, 1999. This is a slender book that gives
USB hardware/software, con- perspective at least, the unit This is a readable and compre- a good overview and includes
sider the US$79 USBSIMM card works properly. hensive book that covers all as- some useful sample driver and
(usbsimm.home.att.net). This Nothing stands still in this pects of actually building and firmware code on a diskette.
business card-sized controller industry, not even the relatively coding USB devices. Also see
uses an Anchor Chips 2131 (a new USB standard. Version Axelson’s Web site ( www.lvr.
USB-aware 8051 derivative). It’s 2.0, which may be formalised com/usb.htm). Her description
a neat and painless way to con- as you read this, extends the of building a HID-class periph- Email   Send inquiry

 eetindia.com | March 2000 | EE Times-India

También podría gustarte