Knowledge Sharing Chapter [6] In-depth understanding of USB communication protocol

1 Basic concepts

A [transmission] (control, batch, interrupt, isochronous): consists of multiple [transactions];

A [Transaction] (IN, OUT, SETUP): consists of one or more [Packets].

USB data is transferred between [host software] and [USB device-specific endpoint]. The association between [host software] and [USB device-specific endpoint] is called [pipes]. A USB device can have multiple pipes.

1.Packet

Packet is the basic unit of information transmission in the USB system. All data is packaged and transmitted on the bus. Data is transmitted on the USB bus in packet units, and packets can only be transmitted within frames. The frame period of the high-speed USB bus is 125us, and the frame period of the full-speed and low-speed USB bus is 1ms. The start of the frame is represented by a specific packet (SOF packet), and the end of the frame is EOF. EOF is not a packet, but a level state. No data transmission is allowed during EOF.

Note: Although the frame period of the high-speed USB bus and the full-speed/low-speed USB bus are different, the frame number in the SOF packet increases at the same speed, because in the high-speed USB system, the frame number in the SOF packet is actually the high value of the counter. 11 bits, the lowest three bits are not used as the micro frame number, so the increment period of the frame number is also 1mS.

What's going on on the USB bus?
Insert image description here
A packet is the smallest unit of data transmission on the USB bus and cannot be interrupted or interfered with, otherwise an error will occur. Several data packets constitute a transaction transmission, and a transaction transmission cannot be interrupted. Several packets belonging to a transaction transmission must be continuous and cannot be completed across frames. A transfer consists of one to multiple transaction transfers and can be completed across frames.

The USB packet consists of five parts, namely synchronization field (SYNC), packet identifier field (PID), data field, cyclic redundancy check field (CRC) and end of packet field (EOP). The basic format of the packet is as follows:
Insert image description here
1.1 PID type (i.e. packet type)
Insert image description here
Insert image description here
1.2 Token Packets
Insert image description here
This format is suitable for IN, OUT, SETUP, and PING.

PID data transmission direction

IN Device->Host

OUT Host->Device

SETUP Host->Device

PING Device->Host

1.3 Start-of-Frame(SOF) Packets
SOF packets are sent from Host to Device.

For the full-speed bus, it is sent every 1.00 ms ±0.0005 ms;

For the high-speed bus, it is sent every 125 μs ±0.0625 μs;

The structure of the SOF packet is shown in the figure below:
Insert image description here
Insert image description here
1.4 Data Packets
Insert image description here
have four types of data packets: DATA0, DATA1, DATA2, and MDATA, and are distinguished by PID. DATA0 and DATA1 are defined to support data toggle synchronization.

1.5 Handshake Packets
Insert image description here

ACK: For IN transactions, it will be issued by the host; for OUT, SETUP and PING transactions, it will be issued by the device.
NAK:
In the data phase, for IN transactions, it will be sent by the device; in the handshake phase, for OUT and PING transactions, it will also be sent by the device; the host never sends NAK packets.

2. Transaction

The process of receiving or sending data information on USB is called transaction, that is: The delivery of service to an endpoint. A transaction consists of a system packet. Which packets it consists of depends on the specific transaction. May consist of the following packages:

A token packet, an
optional data packet,
an optional handshake packet,
and an optional special packet.

2.1 Input (IN) transaction processing

Input transaction processing: Indicates the process in which the USB host receives a data packet from a USB device on the bus.
[Normal] Input transaction processing
Insert image description here
[Device busy] Input transaction processing
Insert image description here
[Device error] Input transaction processing
Insert image description here

2.2. Output (OUT) transaction processing

Output transaction processing: Indicates the process in which the USB host outputs a data packet to a USB device on the bus for reception.

[Normal] output transaction processing
Insert image description here
[Device busy] output transaction processing
Insert image description here
[Device error] output transaction processing
Insert image description here

2.3 SETUP transaction processing

Setting transaction processing of [Normal]
Insert image description hereSetting transaction processing of [Device busy]
Insert image description here
Setting transaction processing of [Device error]
Insert image description here

3. USB transfer type

In USB transmission, 4 transmission types are defined:

Control Transfer

Interrupt Transfer

Bulk Transfer

Isochronous

3.1 Control Transfer
Control transfer consists of 2 to 3 stages:

Setup

Data phase (no data control does not have this phase) (DATA)

Status

Each phase consists of one or more (data phase) transaction transmissions (Transaction).

Control data is used by the USB system software to configure the device (during enumeration). Other driver software can choose to use control transfer to implement specific functions. Data transfer cannot be lost.

3.1.1 Establishment phase

The host obtains configuration information from the USB device and sets the device's configuration values. The data exchange in the establishment phase includes the SETUP token packet, followed by the DATA0 data packet and the ACK handshake packet. Its function is to perform a set (concept vague) data exchange and define the content of this control transmission (ie: the number of IN or OUT data packets in the Data Stage, and the sending direction, have been set in the Setup Stage) .
Insert image description here

3.1.2 Data stage

According to the direction of data transmission in the data phase, control transmission can be divided into 3 types:

Control reading (read USB descriptor)

Control writing (configure USB device)

No data control

Data transmission stage: used to transmit data between the host and the device.

control read

It is to read data from the device to the host and read the data USB device descriptor. The process is shown in [Control Read] in the figure below. For each data packet, first, the host will send an IN token packet to indicate that it wants to read data. Then, the device sends the data back to the host through the DATA1/DATA0 data packet. Finally, the host will respond in the following way: when the data has been received correctly, the host sends an ACK token packet; when the host is busy, it sends a NAK handshake packet; when an error occurs, the host sends a STALL handshake packet .
control write

It is to transfer data from the host to the device. The transferred data is the configuration information of the USB device. The process is shown in the figure below [Control Wirte]. For each data packet, the host will send an OUT token packet to indicate that the data is to be sent. Immediately afterwards, the host transmits the data to the device through the DATA1/DATA0 data packet. Finally, the device will respond in the following ways: when the data has been received correctly, the device sends an ACK token packet; when the device is busy, the device sends a NAK handshake packet; when an error occurs, the device sends a STALL handshake packet .
Insert image description here

3.1.3 Status phase

Status phase: used to indicate that the entire transfer process has completely ended.

The direction of transmission in the status phase must be opposite to the direction of the data phase, that is, it was originally an IN token packet, and this phase should be an OUT token packet; conversely, if it was originally an OUT token packet, this phase should be an IN token packet.

For [Control Read], the host will send an OUT token packet, followed by a 0-length DATA1 packet. At this time, the device will also take corresponding actions and send ACK handshake packet, NAK handshake packet or STALL handshake packet.

Relatively speaking, for the [Control Write] transmission, the host will send an IN token packet, and then the device will send a 0-length DATA1 packet indicating the completion status phase. The host will then take corresponding actions: send an ACK handshake packet, NAK handshake packet, or STALL handshake packet.

3.2 Bulk Transfer

Used to transmit large amounts of data, which requires error-free transmission but no time requirements. It is suitable for printers, storage devices, etc.

Bulk transfer is a reliable transfer and requires a handshake packet to indicate the result of the transfer. If the amount of data is relatively large, multiple batch transaction transmissions will be used to complete the transmission of all data. During the transmission process, the PID of the data packet is flipped in the manner of DATA0-DATA1-DATA0-... to ensure the synchronization of the sending end and the receiving end.

USB allows less than 3 consecutive transmission errors and will retry the transmission. If successful, the error counter will be cleared, otherwise the counter will be accumulated. After more than three times, the HOST considers that the endpoint function is incorrect (STALL) and abandons the transmission task of the endpoint.

A batch transfer (Transfer) consists of one to multiple batch transaction transfers (Transaction).

Flip synchronization: The sender sends data packets in the order of DATA0-DATA1-DATA0-…. Only successful transaction transmission will cause PID flip, which means that the sender will flip the PID and send the next data only after receiving the ACK. package, otherwise the transaction transmission will be retried. Similarly, if the receiving end finds that the received data packets are not flipped in this order, for example, two DATA0s are received in succession, then the receiving end thinks that the second DATA0 is a retransmission of the previous DATA0.

It ensures "accurate" data transmission between the host and device by performing "error detection" and "retransmission" at the hardware level, that is, reliable transmission. It consists of three packages (i.e. IN transaction or OUT transaction):

1 token
2 data
3 handshake

Insert image description here

For IN Token (即:IN Transaction)

ACK: Indicates that the host received the data correctly

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

STALL: Instructs the device to stop permanently and requires host software intervention (such as: device error)

For OUT Token (即:OUT Transaction)

If the received data packet is incorrect, such as CRC error, Device does not send any handshake packet.

ACK: The Device has received the data packet correctly and notified the Host to send the next data packet in sequence.

NAK: The Device has received the data packet correctly and notified the Host to retransmit the data. Due to the temporary condition of the Device (such as the buffer is full)

STALL: Indicates that the Device endpoint has stopped and notifies the Host not to retransmit.

  1. Bulk read and write sequence
    Insert image description here
    consists of a system IN transaction or OUT transaction.

3.3 Interrupt Transfer

Interrupt transfers consist of IN or OUT transactions.

The process of interrupt transfer is the same as batch transfer except that PING is not supported. The only difference between them is that the endpoint where transaction transmission occurs is different, the maximum packet length supported is different, the priority is different, and other things that are transparent to users.

When the host schedules an interrupt transmission task, it will initiate an interrupt transmission according to the query interval specified in the corresponding interrupt endpoint descriptor. Interrupt transfers have a higher priority, second only to synchronous transfers.

Similarly, interrupt transmission also uses the PID flip mechanism to ensure data synchronization at the sending and receiving ends. The figure below is the flow chart of interrupt transmission.

Interrupt transfer mode is always used to query the device to determine whether there is data to be transferred. Therefore the direction of interrupt transfer is always from USB device to host.
Insert image description here
DATA0 or DATA1 contains interrupt information, not interrupt data.

3.4 Isochronous Transfer

It consists of two packages:

token

data

Isochronous transmission does not support "handshake" and "retransmission capabilities", so it is an unreliable transmission.

Synchronous transmission is an unreliable transmission, so it does not have a handshake packet and does not support PID flipping. When the host schedules transaction transmission, synchronous transmission has the highest priority.

Synchronous transmission is suitable for data that must arrive at a fixed rate or at a specified time and can tolerate occasional errors. Real-time transmission is generally used for microphones, speakers, UVC Camera and other equipment. Real-time transmission only requires two information packet stages: token and data, and there is no handshake packet, so data will not be retransmitted if there is an error in the transmission.
Insert image description here

address

Guess you like

Origin blog.csdn.net/zhi_Alanwu/article/details/127767324