Advanced USB – The Software Perspective
USB is a ubiquitous technology – there is not a single PC that does not have a USB port and almost all peripheral devices are USB-enabled. The straightforward simplicity of using USB hides the complexity of its underlying implementation. As USB peripherals are generally embedded devices, an understanding of USB is useful to software engineers who are implementing such systems.
In any USB network there is always precisely one host and typically that is a PC or some other computer. Connected to the host are one or more peripheral devices. The USB standard ensures that this configuration is maintained by mandating the use of approved cabling. Hosts and hubs must use the rectangular standard A receptacles, while peripherals use either the rounded-off square standard B or smaller mini B receptacles. Some peripherals, like mice and keyboards, use captive cables but the connection to the host is always through a standard A plug.
USB products support different data rates. The original speeds are known as low- and full-speed. With the release of USB 2, Hi-speed was introduced with a bandwidth of 480 Mb/s. USB 3 takes this even further. With the evolution of USB comes the need for backward compatibility and the responsibility for supporting mixed-speed peripheral devices falls squarely on the shoulders of the host.
The structure of a USB system is hierarchical. It is termed a “tiered star” topology. A USB host includes the root hub, which forms the nexus for all device connections. The host controls the bus, but there may be a number of other hubs in the system. All hubs have a single upstream connection, but may each have many downstream connections. Hubs increase the logical and physical fan-out of the network. Ultimately there are peripherals – the devices controlled by the host or hub. These also referred to as functions in the USB world.
All communications are sent and received via addressable buffers called endpoints, which are physically part of a USB controller and basically consist of a data buffer. A device may have up to 16 endpoints, each of which may have one or two endpoint addresses – an IN address and/or an OUT address. The names relate to the direction of data flow in relation to the host. So a device may have up to 32 endpoint addresses in total. Every device must have an endpoint 0 with both an IN and an OUT address.
When establishing communication with the host – a process termed enumeration – a descriptor for each endpoint is returned to the host. Such a descriptor is a data structure that tells the host about the peripheral’s configuration, protocol, message transfer type, packet size, data transfer interval and so on. Endpoints are then dedicated to the purpose set forth in their associated configuration and fixed.
All USB communication is initiated by the host. It is a true master/slave topology. The host establishes a pipe which is associated with an endpoint on a device. This association also sets up what kind of data transfers may be performed.
USB Data Transfers
In USB terminology, all data is moved by means of a transfer. A transfer consists of a number of transactions, each of which is constructed from packets. Packets may be tokens, data or handshakes. Although multiple transfers may be occurring on the bus simultaneously, with their data interleaved, a transaction is an unbroken stream of bits. USB supports four types of transfer, which are used for different applications: Control, Bulk, Interrupt and Isochronous.
The simplest form of transfer is Control. This is primarily used for device identification and configuration. Modest amounts of data may also be sent using a Control transfer, which means that certain, fairly simple devices may be implemented using just this transfer type. All devices are required to support Control transfers as a minimum, as they are necessary for device set up. Control transfers may be performed at all supported bus speeds and are message-based – no connection needs to be established. Error correction ensures correct data reception.
For high data volume devices, such as hard drives, scanners and printers, Bulk transfer mode is useful. This mode aims to offer high speed, but without a guaranteed throughput. So it makes sense where speed is desirable, but not essential. Bulk transfers may be performed at all but the lowest USB data rates. For a Bulk transfer, a connection is established and then data is streamed. Again, an error correction mechanism is provided to ensure accuracy of data transfer.
The Interrupt transfer mode is rather confusingly named, as the process does not really resemble an interrupt in the usual sense of the term. As transfers are always initiated by the host, interruption is not an option. This mode is intended for devices that deliver low volumes of data, like human/machine interfaces. Interrupt mode may use all available USB speeds and data is streamed after a connection has been established. Once again, error correction is implemented.
Some high data volume devices, such as streaming media, need a guaranteed data delivery time. Isochronous transfer mode is designed for this purpose. Unsurprisingly, this mode cannot be used at the lowest USB speed and data is streamed after a connection has been established. Because delivery time is a priority, no error correction is implemented in this mode.
USB Function and Host Stacks
USB is typically implemented as a stack of software layers. The usual design is quite symmetrical on host and function, but the actual functionality of each stack is quite different.
USB in Embedded Systems
The obvious application of USB in an embedded system is when you are building a device – a peripheral that may connect to a desktop computer. This is a USB function and requires software support for that end of the bus. Examples here include hard drives, printers, audio devices, medical equipment, cameras, keyboards, mice … the list just goes on.
There are times when the USB host need not be a PC and will be an embedded system of some kind. Possibilities here include set-top boxes, point-of-sale devices and healthcare monitoring systems. These are USB hosts and need software support for that end of the bus, which has distinct differences from the function end.
It is possible to identify examples of embedded systems which may act as both USB host and USB function, at different times. A good example is a camera, which may be a function (when a PC is the host) for uploading pictures; it can be the USB host when it connects directly to a printer to get a hard copy of images. In this context, the camera is actually supporting two separate USB systems. It needs software support for both ends of the bus.
Introduction to USB 3.0
USB 3 is built on top of USB 2 and augments its functionality. In USB 2 there is a pair of wires that carry unidirectional data. These are still present with USB 3, but four more wires are added to carry bidirectional SuperSpeed USB data traffic. This is the new, higher data rate offered with USB 3, which can deliver up to 5Gb/s. Also, the amount of power available for devices to draw from the bus is increased. Backwards compatibility was a vital design requirement in USB 3. USB 3 hosts and hubs fully support USB 2 speeds. Also, USB 2 connectors are fine with USB 3 sockets.
A USB 3 host supports all four data-transfer speeds. Similarly a USB 3 hub offers the same support, as it actually contains both a USB 2 hub and a SuperSpeed USB hub. Data transfer at the highest speed is only possible if all the electronics between the device and the host are SuperSpeed USB-enabled. It is permitted to design a device with USB 3 – that is, SuperSpeed USB – support, which is incapable of working at lower speeds. However, it is essential that it can respond to bus resets and standard requests at lower speeds.
It is very useful for a device to derive its power from the bus and this has become a very popular option. In fact, there are many examples of devices that are plugged into a USB socket just to charge their batteries. The supplied power is at a nominal 5 volts, but has a limitation on the amount of available current. The specification for USB 2 defines low- and high-power devices, which may consume up to 100 and 500 mA respectively. These limits have been increased with USB 3 to 150 and 900 mA.
USB is a technology which has been around for a while, but continues to be developed to meet changing needs of users.
USB 3 is now well established and is set to become the standard going forward, offering faster data rates and improved device power delivery, with good backwards compatibility. The minor downside is a change in connectors and cabling.
Colin Walls, technologist, has over 25 years of 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 a member of the marketing team of the Mentor Graphics Embedded Systems Division, and is based in the UK. His regular blog is located at: http://blogs.mentor.com/colinwalls. He may be contacted at email@example.com.