3.1 Brief Overview of USB Architecture

The Universal Serial Bus (USB) has become an integral part of modern computing, serving as a ubiquitous interface for connecting several devices. This section will describe a high-level overview of the USB architecture and the USB Device Stack.

USB Topology and Components

USB 2.0, also known as High-Speed USB, presents a tiered-star topology where a single host controller interfaces with multiple USB ports, each potentially leading to a complex tree of hubs and peripheral devices. At the heart of this topology is the root hub, integrated into the host controller, which branches out into additional hubs or directly to devices, allowing up to 127 devices to be connected to a single host. Key components of USB 2.0 include the host controller, hubs that serve as repeaters and data traffic managers, and the devices themselves, for example, storage drives or input peripherals. Each connection in this topology utilizes four wires: two for data transfer using differential signaling to reduce noise and enhance signal integrity, and two for delivering power to support bus-powered devices. The USB 2.0 specification also introduces sophisticated error-handling mechanisms and power management features, ensuring robust performance and energy efficiency across the diverse ecosystem of USB-connected devices.

USB Data Flow

Data flow is managed through a host-centric communication model, where the host controller initiates all data transfers and orchestrates the traffic between devices. USB 2.0 supports four types of data transfer: control transfers for command and status operations, bulk transfers for large, non-time-sensitive data payloads like file transfers, isochronous transfers for time-sensitive data such as audio or video streams, and interrupt transfers for small, periodic data like keyboard and mouse inputs. Each transfer type is designed to optimize the reliability, speed, and bandwidth allocation appropriate to the nature of the data. Communication occurs in packets, with the host scheduling services for the various endpoints on the devices and employing a token-based protocol to ensure that data packets are delivered correctly and retried in case of errors. The data flow of USB 2.0 is also characterized by its ability to handle bidirectional data transfer, with endpoints designated as either 'in' for device-to-host communication or 'out' for host-to-device communication, thus enabling a flexible and efficient data exchange framework.

USB Connection Process

The connection process begins with device enumeration, which occurs when a new device is plugged into a USB port. The host controller detects the device and assigns it a unique address for communication. Following detection, the host queries the device for its descriptors, which contain important information such as device capabilities, class, and vendor-specific information. This step is crucial for the host to load the appropriate drivers and establish proper communication protocols.

Once the host has successfully identified the connected device by reading its descriptors (such as the device and configuration descriptors), it configures the device by sending a set configuration request to select one of the configurations offered by the device. This step activates the device's communication endpoints, which are predefined in the device's descriptors. Endpoints specify the types of data transfers (control, bulk, interrupt, or isochronous) and their data flow direction (IN or OUT). The host uses these endpoints to establish the necessary communication channels for data exchange with the device.

After configuration, the device is ready for use, and data can be transferred between the host and the device according to the established parameters. If the device is unplugged, the host controller detects the disconnection and removes the device's address, freeing resources and the address for future use.

USB Device Stack

The USB Device Stack provides embedded application developers with a framework to design and develop several types of USB devices. It facilitates the development of standard USB devices through function drivers that implement standard USB Device class specifications. The stack is structured in layers, where the Hardware Abstraction Layer (HAL) interacts with the USB peripherals and endpoints, the core layer handles transactions between the HAL and the class layer, and the class layer handles the class-specific interactions with the host.

The USB Device Stack is available in the MPLAB® Code Configurator (MCC) Melody installation, accompanied by example applications highlighting stack usage. These examples will also be modified or updated to build custom applications.

The USB Device Stack currently features the following:
  • Support for different USB device classes (CDC, HID and Vendor)
  • Support for multiple configurations
  • Support for full-speed operation
  • Support for deferred control transfer responses
  • A completely non-blocking architecture
  • Support for both polled and interrupted operations
  • Support for Graphical User Interface (GUI) configuration through MCC
The USB Device Stack features a modular and layered architecture, as illustrated in the figure below.
Figure 3-1. USB Device Stack Architecture

More information on the USD Device Stack can be found in the USB Device Stack User Guide.