Now in its fifth edition, bridges the gap between the technical specifications and the real world of designing and programming devices that connect over the Universal Serial Bus (USB). Readers will learn how to select the appropriate USB speed, device class, and hardware for a device; communicate with devices using Visual C#; use standard host drivers to access devices, including devices that perform vendor-defined tasks; save power with USB's built-in power-conserving protocols; and create robust designs using testing and debugging tools. This fully revised edition also includes instruction on how to increase bus speed with SuperSpeed and SuperSpeedPlus, implement wireless communications, and develop for USB On-The-Go and embedded hosts
About the Author
Jan Axelson is the author four previous editions of USB Complete and seven additional books about computers and electronic technology, including Embedded Ethernet and Internet Complete, Serial Port Complete, and USB Mass Storage. Her articles have appeared in Circuit Cellar, EDN, Embedded Systems Programming, and Nuts & Volts.
Read an Excerpt
The Developer's Guide
By Jan Axelson
Lakeview Research LLCCopyright © 2015 Janet L. Axelson
All rights reserved.
USB is the most successful personal-computer interface ever. PCs, tablets, phones, and other devices have USB ports that can connect to everything from keyboards, mice, and game controllers to cameras, printers, drives, audio and video devices, and more. USB is versatile, reliable, fast, power-conserving, inexpensive, and supported by operating systems for computers large and small.
With continued improvements and enhancements such as SuperSpeedPlus and more flexible power delivery, USB is likely to continue to dominate as the interface of choice for an ever-expanding range of devices.
This chapter introduces USB, including its advantages and limits, some history about the interface and recent enhancements to it, and a look at what's involved in designing and programming a device with a USB interface.
Uses and limits
USB is a likely solution any time you want to use a computer to communicate with a device. The computer can be a conventional PC or a device with an embedded processor. Some PCs have internal devices, such as fingerprint readers, that connect via USB. The USB interface is suitable for mass-produced consumer devices as well as specialized, small-volume products and one-of-a-kind projects.
To be successful, an interface has to please two audiences: the users who will buy the devices and the developers who design the hardware and write the code that communicates with the devices. USB has features to please both groups.
Benefits for users
From the user's perspective, the benefits of USB are ease of use, fast and reliable data transfers, low cost, and power conservation. Table 1-1 compares USB with other interfaces.
Easy to use
Ease of use was a major design goal for USB. The features of the interface include:
One interface for many devices. USB is versatile enough for just about any standard peripheral function as well as devices with specialized functions. Instead of having a different connector and cable type for each peripheral function, one interface serves many.
Many ports. A typical PC has multiple USB ports, and hubs enable adding more ports.
Hot pluggable. Users can connect and disconnect a USB device whenever they want, whether or not the system and device are powered, without damaging the PC or device. The operating system detects when a device attaches.
Automatic configuration. When a user connects a USB device to a PC, the operating system detects the device and loads the appropriate software driver. The first time the device connects, the operating system may prompt the user to identify a driver, but other than that, installation is automatic. Users don't need to reboot before using a new device.
No user settings. USB devices don't have user-selectable settings such as port addresses and interrupt-request (IRQ) lines, so users have no jumpers to set or configuration utilities to run.
No power supply required (sometimes). The USB interface includes power-supply and ground lines that provide a nominal +5V from the PC or a hub. A device that requires up to 500 mA (USB 2.0) or 900 mA (USB 3.1) can draw all of its power from the bus instead of using a dedicated supply. Systems that support the USB Power Delivery Rev. 2.0, v1.0 specification can provide up to 5A at voltages as high as 20V to devices.
Convenient cables. USB connectors are small and compact compared to connectors used by other interfaces such as RS-232. To ensure reliable operation, the USB specification defines electrical requirements for cables and connectors. A cable segment can be as long as 5m depending on bus speed and connector type. With hubs, again depending on bus speed and connector type, a device can be as far as 30 m from its host computer. USB Type-C connectors bring two advances for user convenience: the connectors can attach either side up, and both ends of the cable are identical so users don't need to figure out which end goes where.
Wireless options. USB originated as a wired interface, but technologies are available for wireless communications with USB devices.
USB supports five bus speeds: low speed (1.5 Mbps), full speed (12 Mbps), high speed (480 Mbps), SuperSpeed (5 Gbps), and SuperSpeedPlus (10 Gbps). In PCs, USB 3.1 host controllers support all five speeds, USB 3.0 host controllers support all but SuperSpeedPlus, and USB 2.0 host controllers support low, full, and high speeds. Embedded systems that function as USB hosts can support fewer speeds depending in part on which devices the system supports.
The bus speed determines the rate that information travels on the bus. In addition to application data, the bus must carry status, control, and error-checking information, and multiple devices can share a bus. Thus, the throughput for an individual device's application data is somewhat less than the bus speed.
The USB 1.0 specification defined low and full speeds. Full speed was intended for most peripherals that had been using RS-232 (serial) and parallel ports. Full-speed data-transfer rates are comparable to the speeds of these earlier interfaces. Mice tend to use low speed because the less stringent cable requirements allow more flexible cables. Low-speed devices may have lower manufacturing cost due in part to cheaper cables. High speed became an option with the release of USB 2.0, USB 3.0 defined SuperSpeed, and USB 3.1 defined SuperSpeedPlus.
USB's reliability is due to both its hardware and protocols. The hardware specifications for USB drivers, receivers, and cables ensure an electrically quiet interface that reduces noise that could result in data errors. The USB protocols enable detecting errors in received data and notifying the sender so it can retransmit. Hardware performs the detecting, notifying, and retransmitting without software or user support.
Because the host computer provides most of the intelligence to control the interface, components for USB devices are inexpensive. A device with a USB interface is likely to cost the same or less than an equivalent device with a different interface.
Power-saving circuits and protocols reduce a device's power consumption while keeping the device ready to communicate when needed. Reducing power consumption saves money, helps the environment, and for battery-powered devices, allows a longer time between charges.
Benefits for developers
Many of the user advantages described above also make things easier for developers. For example, USB's cable standards and error checking mean that developers don't have to specify cable characteristics or develop error- checking protocols.
Other advantages help hardware designers who select components and design the circuits in devices and the programmers who write firmware embedded in the devices and software to communicate with devices.
USB's four transfer types and five speeds make the interface feasible for many types of peripherals. USB has transfer types suited for exchanging large and small blocks of data with and without time constraints. For data that can't tolerate delays, the host computer can guarantee bandwidth. These abilities are especially welcome under Windows and other desktop operating systems where accessing peripherals in real time is often a challenge. Although the operating system, device drivers, and application software can introduce unavoidable delays, USB makes it as easy as possible to achieve transfers that are close to real time even on desktop systems.
Unlike other interfaces, USB doesn't assign specific functions to signal lines or make other assumptions about how the system will use the interface. For example, the status and control lines on the PC's parallel port were defined with the intention of communicating with line printers. USB makes no such assumptions and is suitable for just about any peripheral type.
USB classes define protocols for communicating with common peripherals such as printers, keyboards, and drives. Developers can program a device to conform to a class specification instead of having to reinvent everything from the ground up.
Operating system support
This book focuses on Windows programming for PCs, but other operating systems also have USB support, including Linux, Mac OSes, and Android. Some real-time kernels also support USB.
At the most basic level, an operating system (OS) that supports USB must do three things:
Detect when devices are attached and removed from the system.
Communicate with newly attached devices to find out how to exchange data with them.
Provide a mechanism that enables software drivers to pass communications between the USB hardware and applications that want to access USB peripherals.
At a higher level, operating-system support may also include class drivers that enable applications to access specific device types. If the operating system doesn't include a driver for a device, the device vendor must provide the driver.
Microsoft continues to improve and add to the class drivers included with Windows. Supported device types include human interface devices (keyboards, mice, game controllers), speakers and other audio devices, drives, still-image and video cameras, scanners, and printers. Filter drivers can support device-specific features and abilities within a class. Applications use Application Programming Interface (API) functions or other software components to communicate with the drivers that access devices.
Devices that have vendor-specific functions can sometimes use a supported class such as the communications-device or human-interface device class. Other options for vendor-specific functions include Microsoft's WinUSB driver and generic drivers from other sources. Some chip companies offer generic drivers that developers can use with the provider's chips.
Writers of USB device drivers for Windows can use Microsoft's Windows Driver Frameworks (WDF) libraries to simplify the task of writing drivers.
Every USB device must include a hardware USB interface and must respond to requests that identify and configure the device. Some controllers perform some or all of these functions entirely in hardware while others require firmware support.
Many USB device controllers are based on popular processor architectures such as ARM Holdings' ARM, Intel Corporation's 8051, or Microchip Technology's PIC®. If you're already familiar with a chip architecture that has a USB-capable variant, you don't need to learn a new architecture. Other controllers provide a serial or parallel interface to any external processor with a compatible interface. Most chip companies provide example code to help you get started.
USB Implementers Forum
The USB Implementers Forum, Inc. (USB-IF) (usb.org) is the non-profit corporation founded by the companies that developed the USB specification.
The USB-IF's mission is to support the advancement and adoption of USB technology. To that end, the USB-IF offers information, tools, and testing support. The information includes specification documents, white papers, and FAQs. The tools include software and hardware to help in developing and testing products. Support for testing includes compliance tests to verify proper operation and compliance workshops where developers can have their products tested and certified to display a USB logo.
Addressing USB's limits
All of USB's advantages mean that the interface is a good candidate for a remarkable variety devices. A single interface can't handle every task, but USB can do the job in many situations that at first might seem challenging or impossible.
Limits of the USB interface include distance constraints, no support for peer-to-peer communications or broadcasting, and lack of support in older hardware and operating systems.
Distance. USB was designed as a desktop-expansion bus where devices are relatively close at hand. Other interfaces, including RS-485 and Ethernet, allow much longer cables. However, to extend the distance between a device and its host computer, a USB device can function as a bridge to a long-distance interface that connects to the end device.
Peer-to-Peer Communications. With a few exceptions, every USB communication is between a host computer and a device. The host is a PC or other computer that contains host-controller hardware. The device contains device-controller hardware. Hosts can't talk to each other directly, and devices can't talk to each other directly. Other interfaces, such as Ethernet and Thunderbolt 2, allow direct device-to-device communication.
USB provides a partial solution with USB On-The-Go (OTG) hardware and protocols. An OTG device can function as both a device and a limited-capability host that communicates with other devices.
Two USB hosts can communicate with each other using a bridge cable that contains two USB devices with a shared buffer. USB 3.1 defines a host-to-host cable for SuperSpeed and SuperSpeedPlus debugging and other uses. With driver support, this cable can support host-to-host communications.
Broadcasting. USB doesn't support sending data simultaneously to multiple devices. The host computer must send data to each device individually. Ethernet supports broadcasting to multiple devices.
Legacy Hardware. Older "legacy" computers and peripherals don't have USB ports, but the need to support legacy equipment is fading as these systems are retired.
If you need to connect a legacy peripheral to a USB port, a solution is an intelligent adapter that converts between USB and the older interface. Several sources have adapters for use with peripherals with RS-232, RS-485, and parallel ports. An adapter is useful only for devices that use protocols supported by the adapter's driver in the host computer. For example, most parallel-port adapters support communications only with printers, not with other parallel-port peripherals. RS-232 adapters work with most RS-232 devices.
If you want to use a USB device with a computer that doesn't support USB, a solution is to add USB capabilities to the computer. To do so, you need to add USB host-controller hardware and use an operating system that supports USB. The hardware is available on expansion cards that plug into a PCI slot or on a replacement motherboard.
If upgrading the PC to support USB isn't feasible, you might think an adapter would be available to translate a peripheral's USB interface to the PC's RS-232, parallel, or other interface. An adapter is rarely an option when the computer has the legacy interface because an adapter that contains the needed host-controller hardware and code is expensive to design and manufacture for its limited market.
Even on new systems, users may occasionally run applications on older operating systems such as DOS. Without a device driver, the operating system can't access a USB device. Although it's possible to write a USB device driver for DOS, few device vendors provide one. For mice and keyboards, the system's UEFI or BIOS typically provide support to ensure that the devices are usable any time, including from within DOS and from the setup screens that you can view on boot-up.
For developers, challenges to USB are the complexity of the protocols, operating-system support for some applications, and for small-scale developers, the need to obtain a Vendor ID.
Protocol Complexity. A USB device must have the intelligence to respond to requests and other events on the bus. Device- controller chips vary in how much firmware support they require to perform USB communications. For controllers that need extensive firmware support, example code can provide a quick start.
On the host computer, applications communicate with class or device drivers that in turn communicate with the lower-level USB drivers that manage communications on the bus. While device-driver writers need some knowledge of USB protocols, device drivers insulate application programmers from having to be familiar with low-level protocols.
Evolving Support in the Operating System. The class drivers included with Windows and other OSes enable applications to communicate with many devices. Often, you can design a device to use one of the provided drivers. If not, you may be able to use or adapt a driver provided by a chip company or other source. If you need to provide your own driver, a third-party driver toolkit can help in developing the driver.
Fees. The USB-IF's website provides the USB specifications, related documents, software for compliance testing, and much more at no charge. Anyone can develop USB software without paying a licensing fee.
Excerpted from USB Complete by Jan Axelson. Copyright © 2015 Janet L. Axelson. Excerpted by permission of Lakeview Research LLC.
All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.
Table of Contents
1 USB Basics,
2 Inside USB Transfers,
3 A Transfer Type for Every Purpose,
4 Enumeration: How the Host Learns about Devices,
5 Control Transfers: Structured Requests for Critical Data,
6 Chip Choices,
7 Device Classes,
8 How the Host Communicates,
9 Matching a Driver to a Device,
10 Detecting Devices,
11 Human Interface Devices: Capabilities,
12 Human Interface Devices: Reports,
13 Human Interface Devices: Host Application,
14 Using WinUSB for Vendor-defined Functions,
15 Using WinUSB's System INF File,
16 Using Hubs to Extend and Expand the Bus,
17 Managing Power,
18 Testing and Debugging,
19 Packets on the Bus,
20 Electrical and Mechanical Interface,
21 Hosts for Embedded Systems,