USB Basic and its Packets

Update 2017 10-15

For chinese, see this page as a reference.

To get started with the USB quickly, I summary the following steps from my experience.

Get the materials

USB Specification

The USB 2.0 specification is about 650 Pages, and it make people frustrated to jump directly into it. But it has the complete description and illustration of the USB. It can be used for the reference for some details.

USB in a Nutshell

This guider document just contains several pages, but it gives a full introduction of the USB, I think this should be the first material to be read.

USB in Wikipedia

The USB wikipedia page also gives the big picture of the USB, and the USB classes. From here, you can know the interfaces or connectors with the help of pictures. Besides, the comparison of different USB version in the table is very clear.

Know the basics

We need to know the USB packet types, and the different transaction. Otherwise we won’t know the data of meaning in the USB Logical Analyser. And at the last I will show you about the data cropped from the USB Logical analyser, this would help us to better know the USB data.

The following contents mostly taken from the USB in a Nutshell.

USB Protocol

The data exchange between host and device using the transaction, and each USB transaction consists of a

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

Which is:

Token Packet + Optional Data Packet(Payload) + Status Packet(ACK)

The packet types illustrate below.

USB Packet Types(4 Types)

The packet types differ with the PID,  and the PID content is illustrated in the USB Packet Field section

 

  • Token Packets(令牌包)

 

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.

Token Packets must conform to the following format,

 

Sync PID ADDR ENDP CRC5 EOP

 

  • Data Packets

 

There are two types of data packets each capable of transmitting up to 1024 bytes of data.

  • Data0
  • Data1

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

Data packets have the following format,

 

Sync PID Data CRC16 EOP

 

  • 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.

 

  • Handshake Packets

 

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

 

  • Start of Frame Packets

 

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.

 

Sync PID Frame Number CRC5 EOP

USB Packet Field

Data on the USBus is transmitted LSBit first. USB packets 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.

 

Token 0001 OUT Token
1001 IN Token
0101 SOF Token
1101 SETUP Token
Data 0011 DATA0
1011 DATA1
0111 DATA2
1111 MDATA
Handshake 0010 ACK Handshake
1010 NAK Handshake
1110 STALL Handshake
0110 NYET (No Response Yet)
Special 1100 PREamble
1100 ERR
1000 Split
0100 Ping

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

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 Logical Analyzer Data example

Below is a picture from the USB analyzer, which is the control setup transaction:

Picture 1: Setup Example

SETUP Example analyze

From the picture, you can see that the Control setup contains ‘1 SETUP txn’ and several ‘IN txn’  also ‘a OUT txn’, from index  75933 to 76023, besides, it has a SOF at the beginning.

Let’s take the SETUP txn (index75994) as the example, the data is 2D 07 68:

In the previous section, it said that Token Packets must conform to the following format:

 

Sync PID (8bits) ADDR (7bits) ENDP (4bits)
CRC5 (5bits)
EOP

Below is the table of the PID cropped from USB wikipedia:

Table 1: PID

Still from the wikipedia:

From the tabbe, we know that the Setup PID takes 8 bits, and it’s the 0b(1011 0100) at the LSB mode, so reverse it(from right to left), we get the 0b(0010 1101), which is 0xd2, the beginning of the SETUP txn data.

From the Token format, the following data is the ADDR, which takes 7 bits, which 7, and you can see the column Dev in Picture 1: Setup Example is 7, which is the same in the packet.

Data1 Example analyze

In the Figure1, the Index 76001 is the DATA1 packet, and which is 0b(1101 0010) in LSB, reverse it (from right to left), we get 0b(0100 1011) , which is the exact the 0x4B.

 

 

Leave a Reply

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