USB 3.0: What embedded software developers need to know
Editors note: USB 3.0 offers higher speed options, new device power management and delivers greater power to peripherals. But to take full advantage of these benefits software developers need to be aware that they come at the cost of some compatibility with earlier versions.
USB is widely deployed in embedded devices of all kinds, resulting in simple interconnectivity and interoperability, but this simplicity comes at a cost: the internal functions of USB are quite complex. This is of no consequence to the user of a USB-enabled device, but the embedded software developer does need some understanding of USB internals. Even if a commercial USB stack is employed, an appreciation of how it works enables it to be used optimally.
First things first
Anyone who has used a computer in the last decade or so is familiar, at least from a user perspective, with USB – the Universal Serial Bus. Similarly, anyone who used a computer before that time is likely to understand what a great benefit USB is, giving plug-and-play capability to almost any device we want to connect to a PC.
A USB subsystem can support up to 127 devices – a capacity that has always seemed adequate and continues to address current needs for most users. USB 2.0 offered three possible speeds:
- Low Speed – 1.5Mb/s
- Full Speed – 12Mb/s
- High Speed- 480Mb/s
The quoted bit rates are the theoretical maximum speed. Even though the USB protocols use the bandwidth efficiently, it is never possible to effect data transfers at those speeds. As we will see later, USB 3.0 offers an additional, higher speed option.
There are two types of USB device: a Host and a Function. A USB host is typically a computer; a function (a particularly confusing name for software engineers) is a peripheral device. I tend to use the terms “function” and “peripheral” interchangeably.
A USB subsystem has a “tiered star” topology, as illustrated in Figure 1. The host, which controls and initiates all activity on USB, includes the Root Hub, which is the nexus of all USB communication. The Root Hub connects to functions or to other hubs that provide fan-out for the bus.
In a USB function – an embedded device with a USB interface – there is a USB peripheral controller chip. As shown in Figure 2, this chip has two key functions: it interfaces to the USB system (connecting to a host or hub) and it contains between one and sixteen buffers called endpoints.
Endpoints. A USB endpoint is the physical termination of USB communications, which is interfaced to the controlling CPU of the USB function (peripheral device). A device can have up to 16 endpoints, each of which is numbered (0 to 15). Each endpoint can include up to 2 buffers (addresses): an IN and an OUT. So, a device can have a maximum of 32 endpoint addresses. At a minimum, every USB function must include an Endpoint 0 with both IN and OUT buffers.
Pipes. USB is a master/slave protocol; all communication is initiated by the host. The host establishes pipes, which are associated with a function’s endpoints. Unlike an endpoint, which has a physical manifestation, a pipe is just a logical concept. When a pipe is defined, the type of data transfer that it supports is determined.
Transfers. USB data is conveyed by means of a transfer, which consists of a number of transactions, each of which is made up of packets(tokens, data, or handshakes). There may be multiple transfers occurring on the USB system at any given time, with their associated transactions mixed together. A transaction, though, is an unbroken stream of data on the bus.
There are 4 types of transfer:
Control is used for identification and configuration, but may carry a small data payload. It features error correction, is message based, and may work at any speed. This type of transfer is supported by all devices and specifically uses Endpoint 0.
Bulk is used for high data volume devices, where speed is desirable but timing is not critical. It features error correction, is streamed, and may work at any speed except Low Speed.
Interrupt is used for low data volume devices. It features error correction, is streamed, and may work at any speed.
Isochronous is used for very high data volume devices, where timing is more critical than precision. It does not feature error correction, is streamed, and may work at any speed except Low Speed. This type of transfer offers a guaranteed delivery time.
USB Software Layers
USB is normally implemented as a stack of five layers:
- Hardware Controller Driver – This is the lowest level software which directly accesses the USB interface hardware.
- USB Communications Stack – This layer manages the USB protocol, building and interpreting the stream of bits on the bus.
- Class Driver – The “character” of the USB device is defined by this layer. There is a large number of standard class driver types spanning a wide range of device functionality. Although it is possible to create a custom class drive, when you are building a peripheral device with a USB interface, this is discouraged, as the developer would also need to provide a class driver for every conceivable USB host stack. This is onerous and it eliminates the plug-and-play attraction of USB. The best approach is to design a device to behave in a similar way to a standard device type.
- Middleware – This software provides the API for the top layer and may take various forms. For example, if the USB function in use is a mass storage device, this layer will implement a file system.
- Application – The top layer of the stack is the application on the host computer that wishes to access the USB device. On the USB function, this layer is the controlling software of the device.
It is important to appreciate that, although they have a similar structure, a USB host stack is very different from a USB function stack. Both function and host stacks are likely to be available from software vendors.
Using USB in Embedded Systems
Although an embedded system can be implemented as either a USB host or a USB function, it is most common for a device to be a peripheral of some kind, where USB function behavior is logical. An example of a USB host implementation in an embedded system is a set top box.
In any case, it is essential to select the appropriate USB stack – host or function. Most software vendors – typically the suppliers of operating systems – are likely to offer both types of product.
What if a device needs to behave as a USB function sometimes and a host at others? An example might be a digital camera, where it would be a function to upload pictures to a computer, but need to be a host to connect directly to a printer. Under these circumstances, it would be necessary to implement both a host and a function stack and the hardware would need to support both interfaces. There is a standard, derived from USB 2.0, called USB On-The-Go, which enables dynamic swapping between host and function, but this has not been widely implemented.
USB 3.0: Much more than USB 2.0
USB 3.0 is much more than USB 2.0 with an increased speed option. It is a dual bus design, with additional physical channels – six wires instead of the two wires used by earlier USB versions. Two of the wires are used to implement USB 2.0 functionality; the other four wires are used to carry traffic at the new standard’s higher speed called SuperSpeed. The theoretical maximum data transfer rate for SuperSpeed is 5Gb/s – a 10X increase over USB 2.0. Traffic is also bi-directional; this is why there are four new wires. With USB 2.0, data travels in a single direction at any given time.
Speeds and compatibility
A USB 3.0 host supports all four speeds: Low, Full, High and SuperSpeed. A USB 3.0 hub includes two components: a SuperSpeed hub and a USB 2.0 hub.
Data transfer between a USB 3.0 host and a peripheral may only occur at SuperSpeed if every component – cables and hubs – fully supports this capability.
Backwards compatibility is mostly very good, as hosts and hubs support USB 2.0 speeds. Also USB 2.0 connectors and USB 3.0 can be used interchangeably, but the additional wiring (providing SuperSpeed capability) only exists with the USB 3.0 parts.
A typical USB 3.0 network layout is shown in Figure 3, where maximum data transfer speeds are shown. Figure 4 shows the effect of replacing the USB 3.0 hub with a USB 2.0 device.
It has always been possible for USB devices to draw power from the bus. Indeed it is increasingly common for devices to use a USB socket exclusively as a power source.
USB devices are classed as “high power” and “low power” and identify themselves on initialization, which enables the hub to detect possible power overloads, where current limits are exceeded. The limits in USB 2.0 for low and high power devices were 500mA and 100mA respectively. With USB 3.0 these limits have been raised to 900mA and 150mA.
There is an ongoing discussion about increasing the power limits further. There are many proprietary charger units on the market, which deliver power via a USB socket, but offer much higher currents. Proposals include the possibility for delivering up to 100W (20A) of power, which would increase the breadth of application of USB power considerably.
Another aspect of power addressed by USB 3.0 is peripheral power management. There are new protocols that address this area, enabling a host to request that peripherals comply with its current power saving mode.
The USB implementation challenge
The implementation of USB is challenging and USB 3.0 only exacerbates this issue. The simplicity of usage only comes about as a result of the underlying complexity. Additionally, for a USB-enabled device to interoperate with any other device it must comply strictly with the published standards and, often, certification of this compliance is needed.
For both these reasons, it is rarely practical to implement USB from scratch. USB interface chips are available from a number of silicon vendors; likewise, USB software stacks are provided by most RTOS vendors.
USB has always been subject to constant extension and enhancement and USB 3.0 is the latest major specification. There are strong indications that this new standard will find wide, if not universal, acceptance and implementation. It is already supported by most new PCs, so demand to implement USB 3.0 in embedded systems is increasing.
USB offers higher speed options, new device power management and delivers greater power to peripherals. This comes at the cost of some compatibility issues as cables, connectors, and hubs have been enhanced.
Colin Walls has over thirty years experience in the electronics industry, largely dedicated to embedded software. A frequent presenter at conferences and seminars and author of numerous technical articles and two books on embedded software, Colin is an embedded software technologist with Mentor Embedded (the Mentor Graphics Embedded Software Division), and is based in the UK. His regular blog is located at mentor.com/colinwalls. He may be reached by email at firstname.lastname@example.org