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.
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,
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.
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
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 | √ | √ |