USB (Universal Serial Bus)

Common USB Packet Fields

The complexities and speed of the USB protocol are such that it is not practical to expect a general purpose micro-controller to be able to implement the protocol using an instruction-driven basis. Dedicated hardware is required to deal with the time-critical portions of the specification, and the circuitry grouping which performs this function is referred to as the Serial Interface Engine (SIE).

The packet could be thought of as the smallest element of data transmission. Each packet conveys an integral number of bytes at the current transmission rate. Before and after the packet, the bus is in the idle state.

In spite of this packets often expect fields of data to cross byte boundaries. The important rule to remember is that all usb fields are transmitted least significant bit first. So if, for example, a field is defined by 2 successive bytes, the first byte will be the least significant, and the second byte transmitted will be the most significant.

A packet starts with a sync pattern to allow the receiver bit clock to synchronise with the data. It is followed, by the data bytes of the packet, and concluded with an End of Packet (EOP) signal. The data is actually NRZI encoded, and in order to ensure sufficiently frequent transitions, a zero is inserted after 6 successive 1's (this is known as bit stuffing).

A typical USB packet may consist of the following fields,

Sync
All packets must start with a sync field. The sync field is 8 bits long at low and full speed or 32 bits long for high speed and is used to synchronise the clock of the receiver with that of the transmitter. The last two bits indicate where the PID fields starts.

PID
PID stands for Packet ID. This field is used to identify the type of packet that is being sent. The following table shows the possible values.

Group PID Value Packet Identifier Description
Token 0001 OUT Token Host to device transfer
1001 IN Token Device to Host transfer
0101 SOF Token Start of Frame marker
1101 SETUP Token Host to device control transfer
Data 0011 Data0 Data packet
1011 Data1 Data packet
0111 Data2 High-Speed Data packet
1111 MDATA Split/High-Speed Data packet
Handshake 0010 ACK Handshake The data packet was received error free
1010 NAK Handshake Receiver cannot accept data or the transmitter could not send data
1110 STALL Handshake Endpoint halted or control pipe request is not supported
Special 0110 NYET No response yet
1100 PRE Preamble to full-speed hub for low-speed traffic
1100 ERR Error handshake for Split Transaction
1000 Split Preamble to high-speed hub for low/full-speed traffic
0100 Ping High-speed flow control token
EXT Protocol extension token

There are 4 bits to the PID, however to insure it is received correctly, the 4 bits are complemented and repeated, making an 8 bit PID in total. The resulting format is shown below.

PID0 → PID1 → PID2 → PID3 → nPID0 → nPID1 → nPID2 → nPID3

ADDR
The address field specifies which device the packet is designated for. Being 7 bits in length allows for 127 devices to be supported. Address 0 is not valid, as any device which is not yet assigned an address must respond to packets sent to address zero.

ENDP
The endpoint field is made up of 4 bits, allowing 16 possible endpoints. Low speed devices, however can only have 2 additional endpoints on top of the default pipe. (4 endpoints max)

CRC

A CRC is a value calculated from a number of data bytes to form a unique value which is transmitted along with the data bytes, and then used to validate the correct reception of the data. USB uses two different CRCs, one 5 bits long (CRC5) and one 16 bits long (CRC16).

Cyclic Redundancy Checks are performed on the data within the packet payload. All token packets have a 5 bit CRC while data packets have a 16 bit CRC.

EOP
End of packet. Signalled by a Single Ended Zero (SE0) for approximately 2 bit times followed by a J for 1 bit time.

USB Packet Types

Each USB transaction consists of four different packet types.

  • Token Packet (Header defining what it expects to follow),
  • Optional Data Packet, (Containing the payload)
  • Status Packet (Used to acknowledge transactions and to provide a means of error correction)
  • start of frame packets indicate the start of a new frame.

As we have already discussed, USB is a host centric bus. The host initiates all transactions. The first packet, also called a token is generated by the host to describe what is to follow and whether the data transaction will be a read or write and what the device’s address and designated endpoint is. The next packet is generally a data packet carrying the payload and is followed by an handshaking packet, reporting if the data or token was received successfully, or if the endpoint is stalled or not available to accept data.

Token Packet; which is initiated by the host and determines if the host will send or receive data.

SYNC PID ADDR ENDP CRC EOP
8 bitslow/full 32 bitshigh 8bits 7 bits 4 bits 5 bits n/a

There are three types of token packets,

  • In - Informs the USB device that the host wishes to read information.
  • Out - Informs the USB device that the host wishes to send information.
  • Setup - Used to begin control transfers.

Data Packet; it contains the actual intended message with the length depending on the data. A device can return a NAK or Stall packet to indicate if they are not able to respond. There are two types of data packets each capable of transmitting up to 1024 bytes of data. Data0 and Data1

High Speed mode defines another two data PIDs, DATA2 and MDATA.

  • Maximum data payload size for low-speed devices is 8 bytes.
  • Maximum data payload size for full-speed devices is 1023 bytes.
  • Maximum data payload size for high-speed devices is 1024 bytes.
  • Data must be sent in multiples of bytes.

Data packets have the following format;

SYNC PID FRAME NUMBER CRC EOP
8 bitslow/full 32 bitshigh 8 bits 11 bits 5 bits n/a

Handshake Packet; It holds the status of a data transfer as ACK, NAK, NYET, and STALL and used for acknowledging data or reporting errors.

. It essentially signals whether or not the transfer is successful. There are three type of handshake packets which consist simply of the PID

  • ACK - Acknowledgment that the packet has been successfully received.
  • NAK - Reports that the device temporary cannot send or received data. Also used during interrupt transactions to inform the host there is no data to send.
  • STALL - The device finds its in a state that it requires intervention from the host.

Handshake Packets have the following format,

SYNC PID EOP
8 bitslow/full 32 bitshigh 8 bits n/a

Start-of-Frame Packet; It indicates the start of a new frame that is sent every one millisecond. Frames, as noted earlier, will have resulted in a complete USB message. The SOF packet consisting of an 11-bit frame number is sent by the host every 1ms  500ns on a full speed bus or every 125 µs  0.0625 µs on a high speed bus. It splits the USB bus into time segments and schedules the data transfers.

These packets are formed into frames and sent through a USB transaction. The length and frequency of the transaction depends upon the transfer type being used for an endpoint.

SYNC PID DATA CRC EOP
8 bitslow/full 32 bitshigh 8 bits upto 8 byteslow 1023 bytesfull 1024 byteshigh 16 bits n/a

Summary

Packet type Sync PID Device Address End Point CRC EOP Data Frame Transfer
Token 5 bits
Special 6 bits
Handshake
Data 16 bits





John Doe
06:28:26pm On 2021.01.18
I have a Rokhy 256gb type C USB dual high speed OTG and I need to back up my files on my All In One and my Note 4 so I can restore my All In One to factory setting because I believe it has a virus on it, but I have a lot of biblical work, photos and scre.
John Doe
06:30:33pm On 2021.01.18
USBC sounds like one of those overpriced California colleges.
John Doe
03:29:13pm On 2022.09.26
yet epileptic smile <a href=http://bag33ondu.com>bag33ondu.com</a> <a href= http://bag33ondu.com >bag33ondu.com</a> http://bag33ondu.com spanner bulk generate .
John Doe
01:32:55pm On 2022.09.30
mide kanseri ameliyatД± sonrasД± kilo kaybД±kas aДџrД±sД± kilo kaybД± <a href="https://antibiyotik-kilo-kayb.thehottrader.online/">antibiyotik kilo kayb?</a> karaciДџer yaД.
John Doe
02:23:41pm On 2022.10.13
<a href="https://turhaberleri.online/">Türkiye Haberleri</a>.