USB Packets- Explained
In this article, we explain what USB packets are and their purpose.
In the USB communication protocol, data is transmitted in packets.
And each of these packets together compose a complete data exchange between the USB host and device.
Data exchange between the host and device with all the packets together is known as USB transactions.
You can think of USB transactions as complete data exchange that occurs between a host and device where all necessary packets are transmitted.
Each USB transaction is composed of either 2 or 3 packets, depending on the transfer type being used, in addition to the start of frame packet.
There are 4 possible packets that comprise a USB transaction.
These packets are the token packet, the data packet, the handshake packet, and the setup (or special) packet.
These are shown in the image below.
Each packet is responsible for transmitting data.
This data communicates with the host or the device what needs to be setup,
or executed, or exchanged.
Token Packets
Token packets indicate the start of a USB transaction.
The token packet defines whether the host will read data from the device, the host will send data to the device, or the host will send SETUP data to the device.
If you want to read data from the device, then you would specify IN for the token packet. For example, if you are working with a sensor as a device, you would want to read data from this sensor in order to read the data that the sensor is outputting. The host will read these values.
If you want to write data to a device, then you would specify OUT for the token packet. For example, if you are working with a memory device, you would want to write data to this memory device, so that it can record it. The host will write these values.
In a specific type of USB transfer mode called control transfer mode, the host can send SETUP data to the device. This SETUP data set up help initialize the device, as the host requests. It is a type of OUT packet, because the host sends data to the device.
Below is the table of the token packet identifiers.
We only address IN, OUT, and SETUP and not SOF, as
we don't use SOF in our token packets.
Data Packets
Every USB transaction consists of a data packet.
Data packets are the packets of which you would think the most when you think of data transmission, because it is the actual data that is sent or received.
The data packets consist of 2 packets of data, Data0 and Data1.
Data0 and Data1 do the exact same task, transmitting data.
Data0 and Data1 are used alternatively just as a method of error checking. But their job is only to transfer data.
You don't have to worry about sending separate data packets and knowing which will be Data0 and Data1, because the hardware manages this operation.
You just have to choose whether you want to send the actual data on the Data0 packet or the Data1 packet.
The hardware will then take care of the rest of managing the other packet.
Below is the table of the Data packet identifiers.
Data0 and Data1 are for low-speed and high-speed USB speeds.
Data2 and MDATA are only for high-speed applications.
Handshake Packets
Handshake packets signify the end of a USB transaction.
It is the packet that gives confirmation whether the packet was successfully received or not.
The component that sends the handshake packet is the receiving component. If the host is sending data to the device, the device will send the handshake packet. If the host is receiving data from the device, the host will send the handshake packet.
If the receiving component receives the data packet without any errors, it will send an ACK to the transmitting device. This ensures that the receiver acknowledges the packet was received without any errors.
The receiving component does not receive the data, it sends a NAK to the transmitting device.
If the receiving component does not receive the data due to an error, it sends a STALL to the transmitting device.
If the receiving component does not receive the data in high-speed USB transmission, it sends a NYET to the transmitting device.
This is all shown in the handshake packet identifiers below.
The special packet is a packet that is used when working with the USB in control transfer
mode. This will be reviewed at a later time.
So this was a brief summary of USB packets and their importance as part of the greater USB transaction.
Packets are the fundamental pieces of USB transactions that are crucial when commicating
through USB protocol.
Related Resources
