How does a USB communicate?

USB (Universal Serial Bus) is an industry standard protocol that defines cables, connectors and communication protocols used in a bus for connection, communication and power supply between computers and electronic devices. USB was designed to standardize the connection of computer peripherals and has become one of the most widely used interfaces for attaching devices to a host system like a PC.

USB communication works by allowing data transfer between a host device, usually a computer, and a peripheral device, such as a printer or camera. The host contains the USB host controller which manages all the USB ports and devices connected to them. USB devices connect to the host using USB cables and contain a USB controller chip to handle communication. Data transfer is managed through USB protocols, which control the flow of data packets across the interface.

USB communication protocols are important for managing data transfer while ensuring reliable and consistent connections. Key protocols include establishing device connections, configuring devices, managing power distribution, error checking and handling data packets. Standardizing these protocols allows USB devices from different manufacturers to interoperate successfully with a wide variety of hosts and operating systems. This plug-and-play functionality is a key factor behind USB’s widespread adoption.

USB Host Controller

The USB host controller has a key role in initiating communication over the USB bus. As explained on the USB communications Wikipedia page, “Initially, all packets are sent from the host via the root hub, and possibly more hubs, to devices.” The host controller acts as the master that controls the timing of USB data transfers.

According to the How USB Works article on the Microchip Developer site, “All USB data transfers are initiated by the USB host. The host controls communication timing by maintaining time intervals called frames.” So the host controller starts each transaction by sending out packets to the USB device.

USB Device

USB devices play a key role in USB communication. They provide various functions through standardized software interfaces (What is a USB Device and How Does It Work?). At the most basic level, a USB device handles data transfer between the host computer and whatever peripheral is connected via USB, such as a printer, camera, storage drive, etc. The USB device includes the electronics and firmware needed to convert the native functions of the peripheral into USB-formatted data that can be transmitted over the USB cable (What is a USB host controller? What is a USB device controller?).

Essentially, USB devices act as the interface between the host and the peripheral. They take care of converting proprietary data formats into USB data packets and handling low-level protocol communication. This allows peripherals with vastly different functions and data formats to all connect to a computer via the standardized USB architecture.

USB Cabling

A typical USB cable contains 4 wires that connect to 4 pins on the USB connectors. The four pins are:

  • VBUS (pin 1) – Provides power to USB devices
  • D- (pin 2) – USB data pin for transmitting data
  • D+ (pin 3) – USB data pin for receiving data
  • GND (pin 4) – Ground

The D+ and D- pins carry the differential data signals that allow the host and device to communicate. USB uses differential signaling to combat electromagnetic noise that can occur in the cable. With differential signaling, D+ and D- transmit complimentary copies of the data so the receiving end can compare the pairs and extract the original signal.

VBUS and GND provide power to USB devices. A standard downstream port provides 5V power while a charging downstream port can provide up to 1.5A at 5V. The current draw from a device should not exceed what the USB port can provide.

For more details on USB cabling, see this reference: The Smooze A+ Quiz

USB Protocols

USB utilizes several different protocols to manage communication speeds and optimize for different use cases. The earliest versions of USB used low speed and full speed protocols:

  • Low Speed: Used for low bandwidth devices like keyboards and mice. Operates at 1.5 Mbps.
  • Full Speed: The original USB protocol. Operates at 12 Mbps.

Later versions of USB introduced higher speed protocols: USB Data Packets Explained

  • High Speed: Introduced in USB 2.0. Operates at 480 Mbps.
  • SuperSpeed: Introduced in USB 3.0. Operates at 5 Gbps.
  • SuperSpeed+: Introduced in USB 3.1. Operates at 10 Gbps.
  • SuperSpeed++: Introduced in USB 3.2. Operates at 20 Gbps.

The USB protocol allows devices to communicate which speed they support during initialization. The highest mutually supported speed is used for communication between the host and device. Higher speed protocols maintain backwards compatibility with their predecessors.

USB Packets

USB uses four main types of packets to communicate between the host and device: token packets, data packets, handshake packets, and start of frame packets. Each packet type serves a specific purpose in managing the flow of data.

Token packets indicate the type of USB transaction that will follow. For example, an IN token packet tells the device that the host is ready to receive data. An OUT token tells the device that the host will send data. Token packets contain addressing information to identify the specific device endpoint that will be involved in the transaction.

Data packets carry the actual payload being transmitted across USB in either direction. The data field contains the data payload to be sent. Data packets are categorized as either data0 or data1, which allows the USB host and device to distinguish between different data payloads.

Handshake packets confirm receipt of data and signal if a transaction completed successfully or resulted in an error. Common handshake packets include ACK to acknowledge successful receipt, NAK to indicate failure, and STALL to indicate an error condition.

Start of frame (SOF) packets are sent by the host every 1 ms to synchronize timing between the host and devices. SOF packets contain a frame number which increments with each frame.

By combining these different packet types, USB is able to manage all communications between the host and connected USB devices. The layered packet structure allows robust transmission of data payloads and control signals needed to operate the bus.

Source: https://www.beyondlogic.org/usbnutshell/usb3.shtml

USB Descriptors

USB descriptors provide vital information about a USB device and how it is configured to operate (https://electronics.stackexchange.com/questions/121705/whats-the-maximum-size-of-a-usb-descriptor). There are a few main types of descriptors:

Device Descriptors: These provide general information about a USB device such as vendor ID, product ID, and the maximum packet size for endpoints. The device descriptor acts like an identity card for a USB device (https://quizlet.com/170333057/ttk4155-eksamensforberedelse-flash-cards/).

Configuration Descriptors: These specify information about a specific device configuration such as power requirements and the number of interfaces. Configurations allow a single physical device to act as multiple logical devices (https://www.codeproject.com/Articles/1001891/A-USB-HID-Keyboard-Mouse-Touchscreen-emulator-with).

Interface Descriptors: These describe a specific interface of a configuration. An interface represents a group of endpoints that provide a single feature or function. For example, a USB audio device may have separate interfaces for playback and recording (https://www.codeproject.com/Articles/1001891/A-USB-HID-Keyboard-Mouse-Touchscreen-emulator-with).

By chaining together the various descriptors, the USB host can construct a complete picture of the capabilities of a USB device and how to communicate with it.

USB Classes

USB devices are grouped into different classes depending on their usage profiles. Some common USB classes include:

Human Interface Devices (HID) – Used for devices that interact directly with humans, like keyboards, mice and game controllers. HID is one of the most widely supported USB device classes.

Mass Storage – Used for storage devices like USB flash drives and external hard drives. Enables easy plug-and-play functionality for storage.

Audio – Used for audio devices like headphones, speakers and microphones. Provides support for the transmission of digital audio streams.

Video – Used for video-related devices like webcams and capture cards. Optimized for compressed video data transfer.

Communications Device Class (CDC) – Allows devices to use serial communication, like modems and printers. Usually doesn’t require drivers as most modern operating systems already have them.

Proper class selection allows USB devices to function with generic drivers or no drivers at all. Common classes like HID and mass storage enable plug-and-play functionality across operating systems.

USB Software Stack

The USB software stack enables communication between the USB host controller and the USB device functions. It consists of several layers (RealView Real-Time Library RL-USB User Guide):

  • Host controller driver – Manages the physical USB bus and controls data transfers.
  • USB core – Implements core USB protocols and standard requests.
  • Class drivers – Interface between USB core and device function drivers.

The host controller driver acts as the interface between the hardware USB host controller and the software stack. It initializes the controller, manages bus bandwidth, and transfers data between the host and devices (RealView Real-Time Library RL-USB User Guide).

The USB core implements basic USB protocols like configuring devices and transferring data between the host and devices. It handles USB standard control requests and manages USB descriptors, interfaces, endpoints etc. The core interacts with the host controller driver below and device class drivers above.

Class drivers enable the USB core to communicate with device function drivers for different device classes like mass storage, HID, networking etc. They act as intermediaries between the hardware-specific function drivers and the hardware-independent USB core.

Conclusion

In summary, USB communication relies on a host controller and USB device to transfer data and power over USB cables using USB protocols. Key points include:

– USB connections require a host controller to manage data transfers and provide power to devices. The host controller follows the USB specification to communicate with devices.

– USB devices have hardware and firmware that enables them to respond to requests from the host controller. Devices use USB descriptors to identify themselves and their capabilities.

– USB cables carry power and differential data signals between the host and device. Cables have standardized connectors and wire configurations to maintain signal integrity.

– USB communication relies on layered protocols like USB packets, data flows, and device classes. This allows broad interoperability between diverse USB devices.

USB’s plug-and-play connectivity has become universally adopted. The reliability and speed of USB connections enable seamless integration of peripherals and accessories with computers. As bandwidth demands increase, new USB standards will continue advancing USB communication capabilities.