USB: A brief tutorial

If you need your embedded application to talk to a PC then increasingly the way to go is USB. Partly this is because of the performance it can supply but also for the very practical reason that many PCs and most portables no longer have parallel or serial ports. But unlike the good old parallel or serial cables these interfaces are far from simple to implement, debug or program. Here is a quick summary of some terms you might encounter during USB Driver.


The Standards

I will not go into the history, but if we take a brief look on USB, the basic standards are as  below :

USB 1.1

The original USB standard provides a fast Master/Slave interface using a tiered star topology supporting up to 127 devices with up to 6 tiers (hubs). A PC is normally the master or Host and each of the peripherals linked to it act as slaves or Devices.  One of the aims of the design was to minimise the complexity of the Devices by doing as much in the Host as possible. Data transfer rates are defined in the specification as - Low Speed 1.5 Mbits/sec and Full Speed 12 Mbits/sec and the maximum length of each cable section is 5 metres. The USB specification allows each device to take up to 500mA of power (limited to 100mA during startup).

USB 2.0

There are some minor variations from USB 1.1 within the USB 2.0 specification and since USB 2.0s inception most interfaces have been designed to conform to the USB 2.0 standard. The 2.0 specification is a superset of 1.1 and the major functional difference which is the addition of a High Speed 480 Mbits/sec data transfer mode.  Be warned, however, that the Spec does allow a product (eg an interface chip) to say that it is "USB 2.0 compatible" without necessarily implying that it  actually implements the High Speed mode.

USB 3.0

Released in 2008 with motherboards and products appearing in 2010.  It has been designed to be backward compatible with 2.0 with a socket that will fit most combinations of legacy plugs and as well as supplying more power (900mA) it also adds a Super Speed >4.8 Gbits/sec data transfer mode so should be able to deliver 400 MBytes/sec after protocol overheads. It is becoming popular for use with external hard disks and other high speed applications.

Wireless USB

A short range  high speed radio communications protocol ( 480 Mbit/s up to 3 m and 110 Mbit/s up to 10 ) which seems to aim to compete with Bluetooth.

Signals, Throughput & Protocol

USB 1 & 2 cables use 4 lines - Power, Ground and a twisted pair differential +/- data lines using NRZI encoding. The USB connectors are designed so that power and ground are applied before the signal lines are connected. When the Host powers up it polls each of the Slave devices in turn (using the reserved address 0), it assigns each one a unique address and finds out from each device what its Speed is and the and type of data transfer it wishes to perform. This process is called enumeration and it also takes place whenever a device is plugged into an active network.  The connectors design (contact to power then signal) along with the process of enumeration and a lot of host software allows devices to be described as  "Plug-and-Play".
 A typical transaction will consist of a number of packets  - a token indicating the type of data that the Host is sending or requiring, the data and in some cases an acknowledgement. Each packet is preceded by a sync field and followed by an end of packet marker.
These transactions are used to provide four basic data transfer mechanisms:
Control - used by the Host to send commands or query parameters. Packet lengths are 8 bytes for Low speed,  8-64 for Full and 64 for High Speed devices.
Interrupt - badly named it is in fact a polled message from the Host which has to request specific data of the Device. Used by Devices which will be sending small amounts of data (e.g. mice or keyboards).
Bulk - Used by Devices that send or receive data in quantity such as a printer. Variable length blocks of data are sent or requested by the Host (max length is 64-byte- full speed, 512 -high speed), are verified with a CRC and their receipt is acknowledged. This mechanism is not used by time critical peripherals as it takes whatever bandwidth is left by the other mechanisms.
Isochronous - Used for devices that stream data in real time without any error recovery such as audio channels. For them losing some data occasionally is better than the glitch resulting from a re-transmit. Packet sizes can be up to 1024 bytes.


As devices are enumerated, the host keeps track of the total bandwidth that the isochronous and interrupt devices request. They can consume up to 90 percent of the bandwidth that is available. After 90 percent is used up, the host denies access to any other isochronous or interrupt devices. Control packets and Bulk Transfers are guaranteed at least 10 percent with Control taking priority.
The USB Host does this by dividing the available bandwidth into frames containing 1,500 bytes, with a new frame starting every millisecond. During a frame, isochronous and interrupt transfers get a slot so they are guaranteed the bandwidth they need. Control and then Bulk transfers use whatever time is left.

Throughput for 2.0
 

Maximum Theoretical transfer rates
Transfer TypeLow-speedFull-Speed (all Kbytes/sec)High-speed
Control2483215,872
Interrupt0.86424,576
BulkNA1,21653,248
IsochronousNA1,02324,576

But note that
 in practice the actual throughput achieved also depends on the host's performance which may only allow 70% of the highest values to be achieved. Although  if we have one highest priority application which is using USB, then we can achieve the 90% or more of the highest value.

Protocol:


When the USB Device is enumerated as well as getting an address from the Host it presents the Host with a good deal of information about itself in the form of a series of descriptors.
1.The Device Descriptor tells the Host what the Vendor and Product ID are (which the Host may use to load a driver)
        1.1The Configuration Descriptors (there may be a number of these - the Host chooses one) offers a power consumption value
                    1.1.1 and a number of Interface Descriptors (a device may be a printer/scanner/fax with separate descriptors)
                     1.1.1.1each of these will define a number of Endpoints which are the sources and destinations for data transfers
                             1.1.1.1.1The Endpoint Descriptors provide the following detail  ---
                                            1.1.1.1.1.1transfer type (Bulk, Interrupt, Isochronous), direction, packet sizes, bandwidth requirement and repeat interval.
There can be  4 Endpoints for Low speed devices 16 in and 16 out for Full and High speed devices- these include the mandatory Endpoint 0. 
But what is an Endpoint ?  Wikipedia defines it as "the the identifier for the entity on one end of a transport layer connection" in practice it may be a register in the device or a buffer into which data ("addressed" to the Endpoint) is going to be transferred. Those familiar with TCP/IP nomenclature will see that it resembles what is referred to in that software as a Socket.
Logically each USB peripheral sets up a one to one link between endpoints on the device and applications software.  The driver software and the interfaces at each end translate between a software call on the host to a peripheral endpoint and the required message details.  The host's application software simply moves data to or from the endpoint on the peripheral without needing to know the connection details.
The Host software does this via pathways known as Pipes between it and the Endpoints  There are two types of pipe defined within the specification...
Message pipes which are bidirectional, for which the USB standard defines the format and which can only be used by Control Transfers
Stream pipes which can be In or Out and which can be used by Interrupt, Bulk and Isochronous Transfers. The USB standard does not determine the layout of the data in these streams (of course the messages passing the data across the bus are structured and contain endpoint addresses but these fields are stripped out before delivery to the pipe).
All the above are specified by the USB protocol so that devices will operate in a uniform manner both at the Bus level but also at the next level up the Transport Level.
Hopefully your USB interface chip will handle all the detailed operations that make up the actions we have discussed above however you will need to know these terms in order to select the appropriate Transfer mechanism, set your Device up correctly and to use the Pipe to transfer your data - not to mention having an idea of what is going on when you get an error message.

Devices, Hosts and On-The-Go

USB Device

Most early on-chip USB interfaces and USB interface chips provided support allowing your embedded system to connect to the USB as a Device.

USB Host

The Master for the transaction - may be a PC but your application will have to be Master if you want to plug a USB memory stick into it and read the files off the stick. Increasingly interfaces capable of being Host are being incorporated into Microcontroller chips.

On-The-Go   OTG

The initial USB standard assumed the presence of a Host  - the PC.  However it has become important to connect units which may, under some circumstances be a Device but may be required under others to be a Host.  For example a Printer is normally happy to be a Device when connected to a PC but may need to be connected to a Camera in the absence of a PC when the Printer will need to be the Host.  The OTG protocol provides an arbitration mechanism that allows units to negotiate who is going to be Host. OTG introduces an additional data pin ID that determines the initial status for the Host/Device negotiation. OTG products may also be known as Dual Role Devices.









No comments:

Post a Comment