Discussion on USB Communication--deep understanding

Source: Internet
Author: User
Tags ack
0. Basic Concepts

A "Transmission" (Control, batch, interrupt, etc.): consists of multiple "transactions";

A "Transaction" (in, Out, SETUP): Consists of a plurality of "Packet".

USB data is transmitted between "host software" and "USB device-specific endpoints". The association between host software and USB device-specific endpoints is called "pipes". A USB device can have multiple pipes (pipes).
1. Package (Packet)

Packet (Packet) is the basic unit of information transmission in a USB system, and all data are transmitted on the bus after being packaged. The data is transmitted on the USB bus in packets, and packets can only be transmitted within frames. The frame period of the high speed USB bus is 125us, the frame period of full speed and Low-speed USB bus is 1ms. The start of the frame is represented by a specific package (SOF package), and the end of the frame is EOF. EOF is not a package, but a level state, which does not allow data transfer during EOF.
Note: Although the frame cycles of the high speed USB bus and the full/low speed USB bus are different, however, the increase of frame number in the SOF packet is the same, because in the high speed USB system, the frame number in the SOF packet is actually high 11 bits, the lowest three bit is not used as the micro-frame number, Therefore, the increase period of the frame number is 1mS.

What is the situation on the USB bus?

packet is the smallest unit of data transmission on the USB bus, which cannot be interrupted or interfered with, or it will cause errors. A number of packets constitute a transaction transmission, a transaction transmission can not be interrupted, belong to a transaction transmission of several packets must be continuous, can not be completed across frames. A transmission consists of one to many transaction transmissions, which can be completed across frames.

The USB packet consists of five parts, namely Sync field (sync), Package identifier field (PID), data field, cyclic redundancy check field (CRC), and Package End field (EOP), and the basic format of the package is as follows:

1.1 pid type (that is, package type)

1.2 Token Packets

This format applies to In, out, SETUP, PING.

PID data transfer Direction

In Device->host

Out Host->device

SETUP Host->device

PING device->host 1.3 start-of-frame (SOF) Packets

The SOF package is sent to device by the host.
1 for Full-speed bus, every 1.00 ms±0.0005 ms send once;
2) for high-speed bus, send once every 125μs±0.0625μs;
The SOF package consists of the following figure:

1.4 Data Packets

There are four types of packets: DATA0, DATA1, Data2,and mdata, and are differentiated by PID. DATA0 and DATA1 are defined to support data-switching synchronization (Database toggle synchronization). 1.5 handshake Packets

ack: for in transactions, it will be issued by the host, and for Out, setup, and ping transactions, it will be issued by device.

Nak: in the data phase, for in transactions, it will be emitted by device; In the handshake phase, it will also be emitted by device for out and ping transactions, and host never sends a NAK package. 2. Services (Transaction)

The process of receiving or sending data information on USB is referred to as transaction processing (Transaction), namely: the delivery of service to a endpoint. A transaction consists of a system packet, which packet consist of, depending on the specific transaction. May consist of the following packages:

• A token packet

• Optional Data Pcket

• Optional Handshake packet

• Optional Special Packet 2.1 input (in) transaction processing

Enter transaction: Represents the process by which a USB host receives a packet from a USB device on the bus.

• "Normal" input transaction processing


• Input transactions When "device is busy"


• Input transaction processing when "Device error"

2.2. Output (out) transaction processing

Output transaction: Represents the process by which a USB host outputs a packet to a USB device on the bus.

• "Normal" output transaction processing

• "Device Busy" output transaction processing

• "Device Error" Output transaction processing

2.3 Setup (Setup) transaction

• "Normal" settings transaction processing


• Set transaction for "Device Busy"


• "Device Error" setting transaction processing

3. USB Transmission Type

In the transmission of USB, 4 types of transport are defined:

• Controlled transmission (control Transfer)

• Interrupt Transfer (Interrupt Transfer)

• Bulk Transfer (Bulk Transfer)

• Synchronous transmission (isochronous) 3.1 control transfer (controlling Transfer)

The control transmission consists of a 2~3 phase:

1) Establishment phase (Setup)

2 Data phase (no data control not at this stage)

3 Status phase (status) • Each phase is composed of one or more (data-phase) transaction transmissions (Transaction).

Control data is used by USB system software to configure the device (when enumerated), other driver software can choose to use control transfer to implement specific functions, data transmission is not to be lost. 3.1.1 Establishment Phase

The host obtains configuration information from the USB device and sets the device's configuration value. The phase-setting data interchange includes a Setup token packet, a DATA0 data packet followed by, and an ACK handshake packet. Its role is to perform a set (conceptually ambiguous) data interchange and define the content of this control transmission (i.e., the number of data packets in or out of data stage, and the direction in which the setup stage has been set).

3.1.2 Data Phase

Depending on the direction of data transfer, the control transmission can be divided into 3 different types:

1) control read (read USB descriptor)

2) control write (configure USB device)

3) No Data control

Data transfer phase: Used to transfer the information between the host and the device.

• Control Read

is the data USB device descriptor that reads data from the device to the host. The procedure is shown in control read, as in the following illustration. For each packet of data, first, the main opportunity sends an in token packet indicating that the data is to be read in. The device then passes the data back to the host via the DATA1/DATA0 data packet. Finally, the host responds in the following ways: When the data is received correctly, the host sends an ACK token packet, and when the host is busy, it emits a nak handshake packet, and when an error occurs, the host issues a stall handshake message packet.

• Control Write

is to upload data from the host to the device, and the data is the configuration information for the USB device, as shown in the figure "Control Wirte" below. For each packet, the host will send out an out token packet indicating that the data is to be sent out. The host then passes the data through the DATA1/DATA0 packet to the device. Finally, the device responds in the following ways: The device sends an ACK token packet when the data is properly received, and the device issues a NAK handshake packet when the device is busy, and the device issues a stall handshake message packet when an error occurs.

3.1.3 State Phase

State phase: The process used to represent the entire transmission has completely ended.
The state phase transmission must be in the direction of the data phase, that is, the original in token packet, this phase should be out token package, otherwise, the original is out token packet, this phase should be in token package.

For "control read", the host sends out a token packet, followed by a 0-length DATA1 packet. At this point, the device will also make the corresponding action, send an ACK handshake packet, Nak handshake packet or stall handshake packet.

Relative to the "control write" transmission, the host will send in token packets, and then the device to express the completion of the state phase of the 0-length DATA1 packet, the host to make the corresponding action: send an ACK handshake packet, Nak handshake packet or stall handshake packet.
3.2 Mass Transfer (Bulk Transfer)

• For the transmission of large amounts of data, require transmission can not be wrong, but the time is not required, applicable to printers, storage equipment and so on.

• Bulk transmission is a reliable transmission and requires a handshake package to indicate the result of the transmission. If the data volume is large, it will use many batch transaction transmission to complete the transmission of all data, the data packet PID in the transmission process according to DATA0-DATA1-DATA0-... In a way that ensures that the sender and receiver are synchronized at the end.
USB allows 3 consecutive transmission errors, retries the transmission, and, if successful, zeros the error count counter, or add the counter. After more than three times, the HOST considers the endpoint functional error (stall) to discard the transport task of the endpoint.
• One-time bulk transfer (Transfer) consists of 1 times to multiple volume transaction transmissions (Transaction).
• Flip Sync: Send the end according to DATA0-DATA1-DATA0-... In order to send packets, only a successful transaction transmission will lead to PID Flip, that is, the sender only after receiving an ACK will flip the PID, send the next packet, otherwise it will retry the transaction transmission. Similarly, if the received packet is not flipped in this order on the receiving end, such as receiving two consecutive DATA0, the receiver considers the second DATA0 to be the retransmission of the previous DATA0.

It performs "error detection" and "retransmission" at the hardware level to ensure "accurate" transmission of data between host and device, that is, reliable transmission. It consists of three kinds of packages (i.e. in transaction or out transaction):

1) Token

2) data

3) handshake

1) for in Token (ie: in Transaction)

ACK: Indicates that the host receives data correctly

Nak: Indicates that the device is temporarily unable to return or receive data (for example: Device busy)

stall: Instructs the device to stop forever, requires the intervention of the host software (for example: Device error)

2 for Out Token (ie: out Transaction)

If the packet received is incorrect, such as CRC error, device does not send any handshake packets

Ack:device has received the packet correctly and notifies the host that it can send the next packet sequentially

Nak:device has received the packet correctly and notified the host to retransmit the data due to Device temporary condition (such as buffer full)

stall: Indicates that the device endpoint has stopped and notifies the host that it is no longer retransmission

3) Bulk reading and writing sequence

That consists of a system in transaction or an out transaction. 3.3 Interrupt transmission (Interrupt Transfer)

An interrupt transfer consists of an in or out transaction.

The interrupt transfer is the same as the bulk transfer on the process, except that the ping is not supported. The difference between them is only the different endpoints that the transaction takes place, the maximum packet lengths that are supported, the different priorities, and so on, something that is transparent to the user.
When a host schedules an interrupt transfer task, it initiates an interrupt transfer based on the query interval specified in the corresponding interrupt endpoint descriptor. Interrupt transmission has a higher priority, second only to synchronous transmission.
The same interrupt transmission also uses the mechanism of PID flip to ensure data synchronization of the receiving and originator. The following figure is the flowchart for the interrupt transfer.

The interrupt transfer mode is always used for querying the device to determine if there is data that needs to be transmitted. Therefore, the direction of the interrupt transfer is always from the USB device to the host.

The DATA0 or DATA1 contains interrupt information, not interrupt data. 3.4 Synchronous Transmission (isochronous Transfer)

1) It consists of two kinds of packages:

1) Token

2) data

Synchronous transport does not support "handshake" and "retransmission capability", so it is not dependent on transmission.

Synchronous transmission is an unreliable transmission, so it does not have a handshake package and does not support PID flipping. The synchronous transmission has the highest priority when the host is scheduling transactional transfers.

Synchronous transmissions are applicable to data that must arrive at a fixed rate or arrive at a specified time and tolerate occasional errors. Real-time transmission is generally used in wheat
G Wind, horn, UVC camera and other equipment. Real-time transmission only token and data two packets stage, there is no handshake packet, so the data is not transmitted incorrectly.

Isochronous data is continuous and real-time in creation, delivery, and consumption. Timing-relatedinformation is implied by the steady rate at which isochronous data is received and transferred. Isochronousdata must be delivered in the rate received to maintain its timing. In addition to delivery rate, the Isochronousdata may also is sensitive to delivery delays. For isochronous pipes, the bandwidth required is typicallybased upon the sampling characteristics of the associated FUNCTI On. The latency required is related to thebuffering available in each endpoint.

A Typical example of isochronous data is voice. If the delivery rate of these data streams isn't maintained,drop-outs in the data stream would occur due to buffer or Fram e underruns or overruns. Even if data isdelivered at the appropriate rate by USB hardware, delivery delays introduced via software may degrade Cations requiring real-time turn-around, such as telephony-based audio.

The timely delivery of the isochronous data is ensured at the expense of potential transient of the data stream. In the other words, the any error in electrical transmission are not corrected by hardware mechanisms as such.  In practice, the core bit error rate of the "USB is expected" to being small enough not to be a issue. USB isochronous data streams are allocated a dedicated portion of USB bandwidth to ensure so data can is delivered at th e desired rate. The USB is also designed for minimal delay of isochronous data transfers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.