In this article, we will introduce the USB protocol layer. Please see our previous article for information regarding the physical layer and network topology: An Introduction to USB Communication (Part 1).
USB is a polled bus where the host initiates all transactions. For the host to receive information from USB devices, it must request it from them (polling). This polling is performed regularly and is organized into frames. A new frame is started every 1ms. There can also be micro frames (started every 125μs).
Each device on the USB bus is assigned a unique 7-bit address (1 to 127). Everything sent on the USB bus is structured into packets. The packets are building blocks of transactions, and transactions are building blocks of transfers.
Packets are the smallest element of USB transactions. They are constructed out of fields. These fields are:
- SYNC – packets always start with a field used for synchronization between the transmitter and the receiver.
- PID – packet identifier field encodes the packet type (see Figure 2).
- ADDR – this field contains the address of the USB device that should receive the packet.
- ENDP – specifies the endpoint in the USB device being addressed.
- DATA – field containing data (0 to 1024 data bytes).
- CRC – cyclic redundancy check field used for error detection during transmission. A CRC5 field (5bits) is used for token packets and a CRC16 field (16bits) used for data packets.
- EOP – end of packet field.
Packets always start with a SYNC field and end with an EOP field. PID is field is mandatory in every packet. Other fields are optional and their usage depends on the packet type.
The PID field in a packet defines its type.
Token packets specify the type of transaction. Tokens packets can be sent only by the USB host.
- OUT – Host will receive information from a USB device.
- IN – Host will transmit information to a USB device.
- SETUP – Indicates that a control transfer will be performed.
- SOF – This packet splits the USB bus into time frames (see Figure 1). In full-speed Start-of-Frame packet is sent every 1ms. In high-speed, each 1ms frame is split into 8 micro frames (each 125μs). The frame number is increased every 1ms, the micro frames contain the frame number of the last 1ms Start-of-Frame packet.
A data packet as its name suggests is used for data transfers. There are DATA0, DATA1, DATA2, and DATAM type of data packets. The source of a data packet can be the host or a device.
Handshake packets can be used to acknowledge received data packets and report if an error has occurred during a transaction.
A USB transaction is an exchange of packets. There are three types of transactions: IN, OUT, SETUP. They are defined by the PID field in a token packet. A transaction is always initiated by the host. The most common form of transaction is shown in Figure 7. It is constructed from three packets (phases): token packet, data packet, handshake packet.
A USB transfer consists of one or multiple transactions. There are four types of transfers:
- Control transfer – Used by the host for configuration and control requests to a device. All devices must support this type of transfer through their default endpoint.
- Interrupt transfer- These transfers are short and can be scheduled to occur periodically. They provide low (bounded) latency and are used by the host for polling the devices on the bus. The polling rate can be set in the range of 1ms to 255ms for low and highs speed and 125μs to 4096ms for high-speed. Typical devices that can be serviced with interrupt transfers are keyboards, mice, etc.
- Bulk transfer – As the name suggests, this type of transfer is used to transfer large amounts of data. Bulk transfers are not performed at fixed intervals, instead, they are run when there is available bandwidth in a frame. This makes them suitable for applications that do not have strict timing requirements regarding the data being transmitted. Devices that typically use bulk transfers are printers, mass storage, etc.
- Isochronous transfer – These transfers are scheduled at fixed intervals and have a guaranteed bandwidth in the frame. They allow the transfer of a large amount of data with strict timing. There is no error detection in isochronous transfers. Typical applications are audio and video streaming, serial port enumeration, etc.