A brief introduction to the USB protocol

Before we start we have to learn some basics about the USB protocol and the communication between host and device.

For each USB device, one of the two data lines is pulled to a high level via a pull-up resistor. Depending on whether this resistor is connected to D+ or D-, it is either a full speed device (pull-up for D +) or a low speed device (pull-up for D-).

The end of a USB connection that is closer to the host is called the upstream end transceiver and the end that is closer to the device is called the downstream end transceiver. The data lines of the upstream end transceiver are pulled to ground via pull-down resistors.

Each USB device is also described by a minimum number of bit fields, the so-called descriptors.

  • Each device has a device descriptor that provides very general information, such as the USB version used or the manufacturer or product ID or the number of device configurations.

  • The different device configurations are mapped using configuration descriptors. B. describe the current consumption or the number of interfaces of a device configuration. The host can only activate one device configuration at a time.

  • Each interface is described by an interface descriptor and, unlike the device configurations, several interfaces can be active at the same time. An interface descriptor describes i. e. the number of endpoints and the class of a device.

  • The individual endpoints of each interface are described by endpoint descriptors. These include e.g. information such as the size of the end point and whether it is an IN or OUT endpoint.

  • Optional string descriptors

    • Manufacturer String

    • Product String

    • Serial Number String

String descriptor 0

Common string descriptor

A communication between host and device require the help of endpoints and pipelines. Each endpoint is defined as a sink or source of data and is connected to the host via a pipeline. In total, each USB device can have up to 16 IN and 16 OUT endpoints, whereby the size is variable and usually depends on the USB controller used. A special end point is endpoint 0, because this end point is used both as an IN and an OUT end point. This end point is also the standard end point that is used when configuring the device.

A data transfer at is called a USB transaction. The USB standard knows three different types of transactions:

Such a transaction can be carried out with one of four different types of transfer, each type of transfer offering its own particular advantages.

To communicate with the host, each USB device must be able to understand and operate a certain number of commands, the so-called standard requests. The host uses these requests to request as much information as possible about the new device. After the host has collected all the necessary information, the requests are used for the configuration and initialization of the inserted USB device.

As soon as a new USB device is plugged in, one of the two data lines is pulled to a high level, which informs the host about a new device. Before the host begins to "poll" the new device, it performs a reset of the device by pulling both data lines to low for at least 10 seconds (this is not a hardware reset!). The reset resets the address of the USB device to 0. This address can now be used by the host to configure the device using endpoint 0. Before the actual communication, however, the host must first determine the size of the end point 0, which is stored in the device descriptor of the connected device (bit field bMaxPacketSize0).

The host starts the query of the device descriptor with a control transfer. Each control transfer contains a SETUP stage, which contains a SETUP transaction and an 8-byte setup package, which specifies the request of the host. This setup package contains all the request details from the host to the device and is filled by the host with the corresponding values ​​of the request. In the DATA Stage, the device then replies with the requested data if the request from the host requires a response from the device. Finally, the host sends a CRC in the STATUS stage and if the transmission was error-free, the message is confirmed by the device with an ACK.

After the second reset, the host transmit a SET_ADDRESS request to set the USB address of the connected device. The address used depends on the devices already connected and is variable. The host then transmit a new GET_DESCRIPTOR request, only that in this case wLength is set to 18, which corresponds to the complete length of the device descriptor. The device responds again with the device descriptor, only this time the host does not abort the transfer, but receives all data and acknowledges the transfer with an ACK. The host now knows how the device is structured and carries out further requests to request all other descriptors. As soon as the host is sure which device it is, it loads an appropriate driver (e.g. a mouse driver) and the device can be used.

Last updated