USB FS/HS cheat sheet

When I was learning USB 2.0 I struggled with some concepts because a lot of things need to be remembered without having any usable visualization tool. I looked at Beagle board and similar devices, but because of their cost they were out of my reach. At some point I decided to get a relatively cheap logic analyzer with supported sampling of up to 1Ghz which opened a window for inspecting and visualizing the actual traces and finally understanding all the bits and pieces of USB protocol (control, bulk and interrupt transfers).

USB is a master/slave bus, where master is called a USB host and device a is a USB device.

In USB host is initiator of all communication and is aware of all connected devices.

Most basic unit of transfer blocks

Most basic building block in USB is something called a packet. There are many kinds of packets, and they are identified by something called PID or packet identifier. What follows after pid is completely determined by the PID value.

PID is a 8 byte value where first nibble is the actual identifier and last nibble is the essentially redundancy check value used to validate the first nibble.

Possible PID values are:

NameValueUsed by transfer types
OUT0001all
IN1001all
SOF0101Start of Frame
SETUP1101control
DATA00011all
DATA11011all
DATA20111isochronous
MDATA1111isochronous, split transactions
ACK0010control, bulk, interrupt
NAK1010control, bulk, interrupt
STALL1110control, bulk, interrupt
NYET0110control, bulk OUT, split transactions
PRE1100all
ERR1100all
SPLIT1000all
PING0100control write, bulk OUT
EXT0000
PID values

With our understanding of PID header, let’s cover the end of packet. End of packet is indicated by special signalling implemented withing the host or device controller. This means that the length is not stored within the packet and is defined by marker which is not seen by our packet reading/processing code.

Now, let’s look at few logic analyzer captures of USB packets.

Start of Frame Packet

Start of frame is a special packet transmitted by USB Host. It is usually used as a mean of synchronization and is transmitted every millisecond and contains a frame number.

Let’s look at the example of such packet

SOF

From the picture we can deduce that frame starts with SYNC signalling pattern, followed by SOF PID byte, 11-bit frame number, CRC-5 value and finally ends with EOP signalling pattern.

When using Bulk transfers within firmware and libusb I did not have to specifically use any synchronization so I can’t tell more about it than to search the web for more info.

Setup Packet

Setup packet is a special packet used during Control Transfers. Control Transfer is the the transfer type which needs to be supported by all devices and is used during the enumeration/configuration process.

Setup Packet

Here we can see the sample of decoded Setup Packet, and first thing that we notice is that it’s more structured than plain SOF Packet. It starts with SYNC signalling pattern, PID byte, 7-bit address, 4-bit endpoint, 5-bit CRC and ends with EOP signalling pattern.

DATA0 Packet

DATA0 packet is used to transfer actual data from host to device or vice-versa. It’s meaning usually depends on context and previously used packet (IN, OUT or SETUP).

DATA0 Packet

DATA0 packet starts with SYNC signalling pattern, followed by PID byte, 8-bytes of data, 16-bit CRC and ends with EOP signalling pattern. The size of DATA0 packet size is determined by EOP pattern; it is not carried as a value withing the packet.

DATA1 Packet

The meaning of DATA1 packet is the same as DATA0. The purpose of having DATA0 and DATA1 is to toggle data synchronization. If the reader sees two DATA0 or DATA1 packages in succession, it means that it skipped one DATA packet.

DATA1 Packet

ACK Packet

ACK Packet is a special kind of packet which only carries a that a previously received packet (usually DATA, IN or OUT) was successfully received.

ACK Packet

ACK packet starts with SYNC signalling pattern, followed by PID byte and ends with EOP signalling pattern.

NAK Packet

NAK (negative acknowledge) means the device is busy or has no data to return . If host sends data to device which is busy doing something else, the device may simply return NAK. In the case where hosts requests data and device does not have any, the device also may return NAK.
This is a number of accepted NAK returns is limited and is defined either by driver or by USB descriptor config. Host MUST never returns NAK.

NAK Packet

Transfer Types

USB defines 4 kinds of transfer types:

  • Control transfer
  • Bulk transfer
  • Interrupt transfer
  • Isochronous transfer (not covered here)

Control transfers

This transfer type carries standardized request structure that needs to be supported by every USB device.
Device may also support custom protocol over this transfer type.

Every device need to pin endpoint 0 to Control transfer, but may also use other endpoint for custom-purpose control transfer.

Every host allocates a fixed bandwidth for control device which is not expanded if device has additional Control transfer endpoints.

Setup stage

Setup stage consists of SETUP package, followed by DATA package and ACK/NAK packet.

Setup stage

Data stage

Data stage consists of one or more IN&OUT transactions, depending on kind of setup stage.

Data stage

Data size

Bus SpeedMaximum Data Packet Size (bytes)
Low speed8
Full Speed8, 16, 32, or 64
High Speed64

These bytes include only information transferred, excluding PID and CRC bits.
Also , in Data stage all packets except last one (Data packet) must be of maximum size for endpoint.
If the the actual payload is multiple of maximum size, than last packet must be Zero Length packet.

Status stage

Status stage consists of one IN or OUT transactions where the device reports success or failure of the whole transfer.

Status stage

Outstanding conditions

If device sees new Setup stage in between existing Transfer, it must discard existing operation, and start processing new control transfer.

Recap

So, Control Transfer consist of one Setup stage, followed by zero or more data stages and one status stage.

ControlTransfer :== SetupStage, DataStage*, ControlStage

Bulk Transfer

Bulk transfer is most useful to transfer data into and out of USB device. It’s not real-time transfer, so because of that it’s not intended to be used for transferring audio, video or other time-sensitive data streams. Ideal use cases are data transfers which can very in latency and bandwidth (USB disk drives, printers, Smart Cards, etc…).

USB Low speed devices do not need to support bulk transfers, and in general devices are not required to support bulk devices if they use protocol not needing such functionality.

Bulk transfers consist of IN or OUT transactions, and the end is reached when the transaction transmits number of bytes less than maximum defined number of bytes that each transaction can transmit.

Bulk transfer transaction

Bulk transfer transaction consists of one IN or OUT packet, one or more DATA packets and final Handshake packet.

IN Transaction Example

In Packet
Data Packet
Handshake Packet

OUT Transaction Example

Out Packet
Data Packet
Handshake Packet

Correlating DSView Endianess with WireShark

There is no interpretation of bytes by WireShark, so byte stream is as it’s repoted.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *