FB pixel

How USB Works: Introduction (Part 1)New

Published


Universal Serial Bus (USB) can be found on almost every device these days, from small thumb drives to large storage devices, like it’s universal! Pun absolutely intended. Apart from being used in storage devices, it is a standardized connection for various peripherals, such as keyboards, mice, printers, cameras, etc.

However, this was not the case until 1996; before that, companies used larger and slower connections, such as serial and parallel ports. This standard was developed through a joint effort, starting in 1994, between Compaq, DEC, IBM, Intel, Microsoft, NEC, and Nortel. Here, the goal was to develop a single interface that could be used across multiple devices, eliminate the many different connectors used, and increase the data transfer rate for electronic devices. Even though USB 2.0 is an older version of the USB standard and has been succeeded by newer versions such as USB 3.0, USB 3.1, USB 3.2, and USB4, it is still relevant and used in various scenarios for its compatibility, legacy support, and cost-effectiveness.

USB Architecture

Universal Serial Bus is an asynchronous, serial bus protocol designed around master-slave architecture, but unlike on-board protocols such as I2C and SPI, it doesn’t allow multiple masters on the bus; therefore, only one host can exist in the system, and all communication with devices is initiated by the host only.

In the USB protocol, a host is an “upstream” component, while a device is a “downstream” component. The host itself consists of two sub-components:

  • Host controller: It is a hardware chipset with a software driver that is responsible for the detection of new devices, management of data, power, and all activities on the bus.
  • Root Hub: It is the hardware entity that primarily provides a starting point or root for the USB hierarchy on the host system. It is directly associated with the physical connection ports to which the devices are connected.

Each device connected to the USB system has an address to which it responds when the host commands. The host allows up to 127 connections, which is limited by the 7-bit address supported by the USB protocol.

In reality, we don’t see computers with 127 ports; hence, to connect more devices than the available number of ports, external hubs are used. To connect the external hubs, the root hub acts as a root entity to which the other hubs are connected. The hub doesn’t have any host capabilities; it only repeats the traffic to and from the devices to the host and vice versa. Figure 1.1 demonstrates how physical ports can be extended for a host with the use of an external hub.

Figure 1.1: USB Host controller, Root Hub and External Hub
Figure 1.1: USB Host controller, Root Hub and External Hub

USB follows a star-tiered topology, and at the center is the USB host. The bus can only allow a maximum of seven tiers to ensure that the USB device even in the lowest tier can communicate within the maximum tolerable propagation delay defined as per the USB specification. The consecutive tiers in the topology are connected via hubs; they have seven downstream ports and one upstream port to connect the upper tier with the lower tier. With this, the hubs can be daisy-chained together to allow support for a total of 127 USB devices.

Figure 1.2: Bus Topology
Figure 1.2: Bus Topology

Physical Interface

Anyone with a smartphone or a thumb drive can identify what a USB cable looks like, right? Well, yes and no. They can most certainly identify the most common type of USB cable, but the standard has definitions for a range of different connectors. To identify the type of connector the cable supports, we first need to look at what the most common individual connector types look like!

Figure 2.1: Types of USB connectors
Figure 2.1: Types of USB connectors

Learning from Figure 2.1 about the different types of connectors available for the USB standard, the cables for non-iPhone mobile phones released after 2007 supported a combination of a USB micro B-type female connector and a USB A-type male connector. USB A-type female connectors are found on the ports of computers and most USB charging ports, and USB micro B-type female connectors were found as the charging port for Android phones before 2014; but today, type C connectors, have replaced them and have been made standard for all the latest (Android and iOS) mobile devices. Other than just charging mobile phones and transferring files with a computer, USB On-The-Go (OTG) is a specficitaion that introduces an alternate use for the USB port on mobile devices. USB OTG allows mobile phones and tablets to act as hosts allowing other USB devices like flash drives, keyboards and mice to be connected to them, and expands their capabilities.

Here’s a fun fact: Even though the iPhones use their proprietary lighting connectors, underneath, they use a USB protocol for communication between PCs and iPhones.

Now that we know how to identify different USB connectors, we can move to the USB cable's power and data delivery part. An insulating jacket encloses the cable to protect the components inside. Underneath the jacket is an outer shield that has a VBUS wire (RED), a ground wire, and a copper drain wire. The drain wire is connected to the ground of the USB and provides a low resistance path for the surrounding electrical noise to be safely diverted to the ground. It reduces electromagnetic and radio frequency interference, which can disrupt data transmissions, and it is highly effective for high-speed USB versions like USB 3.0 and above. An inner shield contains a twisted pair of D+ (green) and D- wires (white) and can have limited cable length varying depending on the USB speed. It’s unusual how High-Speed USB have a maximum recommended cable length of 5 m and can still operate on 480MHz as higher-frequency signals are more susceptible to attenuation and distortion, and the signal quality may degrade over longer distances. The credit for this goes to the better construction of high-speed USB cables as they use twisted pairs to reduce cross-talk and are designed with better shielding and impedance matching to maintain signal integrity over longer distances.

USB Speed

Maximum Recommended Cable Length

Low Speed (1.5 Mb/s)

3 m

Full Speed (12 Mb/s)

3 m

High Speed (480 Mb/s)

5 m

SuperSpeed (5 Gb/s)

3 m

SuperSpeed+ (10 Gb/s)

2 m

Figure 2.2: USB Cable
Figure 2.2: USB Cable

Figure 2.2 illustrates the constituent components of a USB cable i.e.,

  • VBUS wire gives a constant supply voltage of 4.4 to 5.25 V to all the connected devices
  • GND wire provides the ground voltage reference to the device.
  • The D+ and D- are the data lines that function at 3.3V and use a differential transmission with non-return-to-zero inverted (NRZI) encoded with bit stuffing.

The last point might sound daunting, with terms like NRZI, differential transmission, and bit stuffing being thrown around, but the tutorial will give a thorough understanding of what these terms are.

Non-Return-To-Zero Inverted Space (NRZI-S) Encoding

Non-Return-To-Zero Inverted Space is a binary coding scheme commonly used in digital communication systems. The basic idea is that a change in the signal level represents a binary zero while no change represents a binary one. To understand better, let’s examine a signal encoded using NRZI-S encoding given below.

Figure 2.3: NRZI-S Encoding
Figure 2.3: NRZI-S Encoding

Observing the changes marked in Figure 2.3, we can see that change 1 demonstrates that the polarity of the signal was flipped when binary 0 was transmitted, while the polarity remained unchanged for 2-bit times when binary 1 was transmitted. However, when yet another binary 0 was transmitted on the bus i.e. the event for change 2, the polarity of the signal was flipped again. Well, conditions in Figure 2.3 for changes 3,4,5, and 6 demonstrate the same. In simple terms, in NRZI-S encoding, the polarity of the signal is flipped whenever a binary 0 is encountered, hence in this type of encoding instead of representing the bit by a voltage level, a transition in signal is used. This technique helps in the efficient utilization of the bandwidth by achieving higher data rates within a limited bandwidth.

Bit Stuffing

While NRZI encoding does help in the efficient utilization of bandwidth, it introduces synchronization issues, if there is a long sequence of 1s, it might be difficult for the receiver to maintain accurate bit synchronization, leading to potential errors.

Bit stuffing is performed to resolve this issue. The rules of this technique are simple: we need to insert a bit of opposite polarity after a set of N numbers of the same bits to maintain synchronization. In USB, bit stuffing is done by inserting a bit of the opposite value (0) after six consecutive binary 1s.

Figure 2.4: NRZI-S encoding with bit stuffing
Figure 2.4: NRZI-S encoding with bit stuffing

Figure 2.4 demonstrates the bit stuffing with NRZI-S encoding, you can observe how, after six consecutive 1s, a bit of opposite value, i.e. 0 is transmitted as a stuffed bit to maintain synchronization. This explains bit stuffing but how does the receiver know when to expect the stuffed bit? Well, the receiver receives the data with the rule that after six consecutive ones it expects a stuff bit ‘0’ and removes it to extract the actual data bits.

Figure 2.5: Flow Chart for USB Data Transfer
Figure 2.5: Flow Chart for USB Data Transfer

Data Transmission

Data transmission in USB protocol is carried out by differential pair signals; the bus has D+ and D- lines that function at 3.3V. The benefit of using differential signaling is that these signals are not susceptible to electromagnetic interference from surrounding appliances. They also do not emit any electromagnetic radiation that can potentially affect the surrounding electronic devices, which is a very essential point as the USB protocol is designed to work around sensitive devices such as microprocessors and microcontrollers. The question may arise, really, on what scale can the USB affect the surrounding devices if differential pairs are not used? Look at it this way: a Full-Speed USB device works on a data rate of 12Mb/s which is effectively a 12MHz AC signal that can emit a 12MHz radio signal typically behaving like an antenna, which, when placed near the field of a microprocessor or microcontroller, can cause an enormous amount of interference, whereas in the case of a differential pair, both the wires are 180° out of phase with each other and the radio waves cancel out as they will destructively interfere with each other, thereby significantly reducing the radio emission. Hence, using differential pairs, we can increase the data speed for USB without worrying about interference from and to the surrounding devices. To learn more about differential pair signals, you can read our blog about CAN protocol which has an in-depth explanation of differential signaling.

USB communication occurs through different signaling states on the D+ and D- lines. Some of these states transmit the data, while others are used as specific signaling conditions. These states are explained below:

  • Differential 0 and Differential 1: These two states are used for general data communication across USB. Differential 1 is when the D+ line is high and the D- line is low. Differential 0 is when the D+ line is low and the D- line is high.
  • J-State and K-State: USB specifies two more differential states: J-State and K-State. The definitions of these states depend upon the device's speed. On a Full-Speed and High-Speed device, a J-State is a Differential 1, and a K-State is a Differential 0, but it is the opposite on a Low-Speed device.
  • Single-Ended Zero (SE0): This is one of the signaling conditions that occurs when both D+ and D- are driven low.
  • Single-Ended One (SE1): It is a condition that occurs when D+ and D- are both driven high.
  • Idle: This condition that occurs before and after a successful transmission on the bus. An Idle condition depends upon the speed of the device, on a Low-Speed device an idle condition consists of D+ being low and D- being high and the opposite is true for the Full-Speed device.
  • Start of Packet (SOP): Occurs before the start of any Low-Speed or Full-Speed packet when the D+ and D- lines transition from an idle state to a K-State.

Bus State

Indication

Differential 1

D+ High, D- Low

Differential 0

D+ Low, D- High

Single Ended 0 (SE0)

D+ and D- Low

Single Ended 1 (SE1)

D+ and D- High

J State:
Low Speed
Full Speed
High Speed


Differential 0
Differential 1
Differential 1

K State:
Low Speed
Full Speed
High Speed


Differential 1
Differential 0
Differential 0

Figure 2.6: Bus state and conditions
Figure 2.6: Bus state and conditions

USB Speed

USB specification has a definition of multiple speeds such as Low-Speed, Full-Speed, Hi-Speed, SuperSpeed, and SuperSpeed+ and revisions are still in a continuum with the introduction of the latest USB4. Anyone familiar with USB is aware of a significant amount of name changes that have occurred over the years but we will strive to follow the latest naming conventions throughout this introduction. Note that newer hosts can communicate with a Low-Speed device, but a Low-Speed host cannot communicate with a Full-Speed device. The same is true for all the available USB speed specifications.

  • Low-Speed devices: These devices support a data speed of up to 1.5Mb/s; keyboards, mice, etc., fall under this category.
  • Full-Speed devices: These devices support a data speed of up to 12Mb/s; USB-supported mobile phones, audio devices, etc. fall under this category.
  • High-Speed devices: These devices support a data speed of up to 480 Mb/s; USB thumb drives, storage devices, etc., fall under this category.
  • Super-Speed and Super-Speed+ devices: These devices support a newer version of USB protocol i.e., USB 3.2 Gen 2x2, and can support a data speed of up to 20Gb/s. While USB4 was introduced in 2019, and mandates a base speed of 20 Gb/s, it can optionally support 40 Gb/s and 80 Gb/s.

Enough with the facts, let’s understand how a host realizes the connected device is a Low, Full, or High-Speed device. When a USB device is connected to the host, the speed of the device is detected by a pull-up resistor connected to either D+ or D- line. A 1.5kΩ pull-up resistor on the D+ line indicates the device is a Full-Speed device whereas a 1.5kΩ pull-up resistor on D- line indicates the device is a Low-Speed device.

Figure 3: How does the host detect USB device speed
Figure 3: How does the host detect USB device speed

Generally Low-, Full- and High-Speed devices are advertised on their maximum available speeds but in reality the speed of the device is affected by factors such as type of transfer, overhead, available bandwidth, etc.

USB Power

It is no surprise that a USB device is powered by the host it is connected to. The VBUS line on the USB provides a constant 4.4 - 5.25V which can power a USB device. For USB devices there are two device categories: bus-powered and self-powered devices.

Figure 4: USB devices based on power consumption
Figure 4: USB devices based on power consumption

Self-powered devices: These are the devices that are powered by an external power supply as the current consumption for these devices exceeds 500mA, which is the maximum current a USB port can provide to the device.

Bus-powered: These devices draw power from the host, and their current consumption doesn’t exceed 500mA. This category can be further divided into high and low-power devices. High-power devices are designed to consume almost equal to 500mA current, while lower-power devices will always draw a current of less than 500mA. These specifications date to when USB 2.0 was mainstream but as per the current standards i.e. USB 3.x and USB4 and the associated Power Delivery standards, using USB-C ports can deliver up to 100 watts of power depending on the capabilities of the host controller. Despite the capabilities of the host, the USB-C cable with a power brick can deliver up to 240 watts of power and is used for fast charging of mobile phones and even for the charging ports on laptops.

Coming back to USB 2.0, the device is not allowed to consume more than 100mA of current before the configured state. During the configuration steps, the device can request a power budget from which the subcategory of the device is decided, and power is allotted to the port device it is connected to. If the device draws more than 100mA of current before or during the configuration process, the host detaches it from the root hub.

In this tutorial, we understood that the USB architecture only permits one host for the entire system. Then, we learned how communication is done in the USB protocol using differential transmission with the NRZI encoding technique, and bit stuffing. Lastly, we looked at different USB speed specifications and power delivery features of the USB specification. This tutorial series focuses on USB 2.0, as even after a decade, it is still a go-to option for devices running on microcontrollers. It is crucial for an embedded engineer to extend the product support with USB for user-friendliness and debugging. In the next tutorial, we will get an in-depth understanding of how communication works on the USB protocol.

Make Bread with our CircuitBread Toaster!

Get the latest tools and tutorials, fresh from the toaster.

What are you looking for?