Fri Sep 16 16:56:09 CST 2022
USB data is composed of binary digital Strings, first the digital strings form the Domain (there are seven of them), the Domain then forms the Packet, the packet then forms the Transaction (IN, OUT, SETUP), and the transaction finally forms the Transfers (interrupt Transfer, parallel Transfer, bulk Transfer, and control Transfer).
The following is a brief description of Domain, Packet, Transaction, and Transfers, please note the relationship between them.
Domain is the smallest unit of USB data, consisting of a number of bits (as to how many bits by the specific domain decision), the domain can be divided into seven types.
1, Synchronization Domain (SYNC), eight bits, the value is fixed 0000 0001, for local clock and input synchronization
2, Pocket identification Domain (PID), consisting of four-bit identifier + four-bit identifier inverse code, indicating the type and format of the package, which is a very important part, here we can calculate that there are 16 types of USB identification code, see question five for specific classification.
3, Address Domain (ADDR): seven-bit address, representing the address of the device on the host, address 000 0000 is named zero address, is the first time any device connected to the host, the default address before being configured by the host, enumerated, thus you can know why a USB host can only receive 127 devices.
4, Endpoint Domain (ENDP), four bits, from which we can know that a USB device has a maximum number of endpoints 16.
5, Frame Domain (FRAM), 11 bits, each frame has a specific frame number, Frame Domain maximum capacity 0x800, for synchronous Transfer is important (synchronous Transfer for one of the four types of Transfer, see below).
6、Data Domain (DATA): length 0~1023 bytes, in different Transfer types, the length of the data domain varies, but must be an integer number of bytes in length
7, Cyclic Redundancy Check Domain (CRC): a method of checking the non-PID domain of token packets and data packets (see below for packet classification), CRC checks are widely used in communications.
The Domains forms the Packet. There are four types of packets composed of domains as Token Packets, Data Packets, Handshake Packets and Special Packets, the first three are important packets, different packets with different domain structures, introduced as follows:
1, Token Packet: can be divided into input packet, output packet, setting packet and frame start packet (note that the input packet here is used to set the input command, the output packet is used to set the output command, not the number of data put)
The format of the input packet, output packet and setup packet is the same as :
SYNC+PID+ADDR+ENDP+CRC5
Format of the frame start packet:
SYNC+PID+11-bit FRAM+CRC5
2, Data Packets: divided into DATA0 packets and DATA1 packets, when the USB send data, when the length of the data sent at one time is greater than the capacity of the corresponding endpoint, it is necessary to divide the packet into several packets, wholesale send, DATA0 packets and DATA1 packets sent alternately, that is, if the first packet is DATA0, then the second packet on DATA1. but there are exceptions. In synchronous Transfer (one of the four types of Transfer), all packets are DATA0, in the following format:
SYNC+PID+0~1023 bytes Data +CRC16
3, Handshake Packet: the most simple structure of the packet, the format is as follows
SYNC+PID
(Note that each of the above packets have different types, USB1.1 defines a total of ten types of packets)
4, Start of Frame packets (SOF): The SOF packet consists of an incrementing 11-bit frame number. On a full speed bus, this packet is sent by the host every 1ms and on a high speed bus every 125 us. This packet is used to synchronize isochronous transfer.
SYNC + PID + FRAM + ENDP + CRC + EOP
There are three major transactions: IN transactions, OUT transactions and SETUP transactions, each transaction consists of token packets, data packets, handshake packets these three stages, the meaning of the stages used here because these packets are sent in a certain time sequence, the three stages of the transaction are as follows.
1, Token Packet stage: start a transaction of input, output or setting
2, Data Packet stage: send the corresponding data by input and output
3, Handshake Packet stage: return data reception, there is no this stage in the IN and OUT transactions of synchronous Transfer, which is relatively special.
The three types of transactions are as follows (the following describes a transaction by three phases).
1. IN transaction.
Token packet phase - the host sends an input packet with a PID of IN to the device, notifying the device that it wants to send data to the host.
Data packet phase - the device will react in three ways depending on the situation (note: the data packet phase does not always transmit data either, depending on the Transfer situation it will also enter the handshake packet phase earlier)
1) The device endpoint is normal and the device sends packets inside the incoming host (alternating between DATA0 and DATA1).
2) the device is busy and cannot send packets to the host then sends NAK invalid packets, the IN transaction ends early and continues until the next IN transaction.
3) The corresponding device endpoint is disabled and sends an error packet STALL packet, the transaction also ends early and the bus enters the idle state.
Handshake packet phase - the host sends ACK packets to the device after receiving the data correctly.
2. OUT transaction.
Token packet phase - the host sends an output packet with PID OUT to the device to notify the device that it wants to receive data.
Data Packet stage - relatively simple, the host will send data to the device, and DATA0 and DATA1 will be transmitted alternately.
Handshake packet stage - the device will react in three ways depending on the situation
1) the device endpoint is received correctly and the device returns an ACK to the incoming host, notifying the host that new data can be sent; if a CRC checksum error occurs in the packet, no handshake information will be returned.
2) the device is busy and cannot send packets to the host then send NAK invalid packets to notify the host to send data again.
3) The corresponding device endpoint is forbidden to send error packets STALL packets, the transaction ends early and the bus directly enters the idle state.
3. SETUP transaction.
Token packet stage - the host sends an output packet with PID SETUP to the device, notifying the device to receive data.
Data packet stage - relatively simple, the host will send data to the device, note that there is only a fixed 8-byte DATA0 packet, the content of these 8 bytes is the standard USB device request command.
Handshake packet stage - the device receives the command information from the host and returns an ACK, after which the bus enters an idle state and prepares for the next Transfer (after the SETUP transaction is usually an IN or OUT transaction constituting the transfer).
Transfer consists of OUT, IN, SETUP transactions among them, there are four types of Transfer, interrupt Transfer, bulk Transfer, synchronous Transfer, control Transfer, which interrupt Transfer and bulk transfer of the same structure, synchronous Transfer has the simplest structure, and control Transfer is the most important and most complex Transfer.
1, Interrupt transfer: consists of OUT transaction and IN transaction, used in the data transfer of keyboard, mouse and other HID devices.
2, Batch transfer: consists of OUT transactions and IN transactions, used for high-capacity data transfer, there is no fixed Transfer rate, and does not occupy bandwidth, when the bus is busy, USB will give priority to other types of data transfer, and temporarily stop the batch transfer, sunch as USB disk.
3, Synchronous Transfer: consists of OUT transactions and IN transactions.There are two special places.First in the synchronous Transfer of IN and OUT transactions is no return packet stage; Second in the packet stage all packets are DATA0, such as video and audio Transfer.
4, Control Transfer: the most important and most complex Transfer, control Transfer consists of three stages (initial setup stage, optional data stage, status information steps), each stage can be seen as a Transfer, that is, control Transfer is actually composed of three Transfers, used in USB devices are first added to the host, the host through the control transfer to exchange information, device address and read the device descriptors, so that the host to identify the device and install the appropriate driver, which is the concern of every USB developer.
1>, The initial setup step: is a SET transaction consisting of the Transfer
2>, Optional data step: is a Transfer consisting of IN or OUT transactions, this step is optional, depending on the initial setup step has not requested to read / write data (the standard request command sent by the packet phase of the SET transaction is determined)
3>, The status information step: as the name suggests, this step is to obtain the status information, composed of IN or OUT transactions constitute the Transfer, but note that the IN and OUT transactions here and the previous INT and OUT transactions have two differences.
1) The direction of Transfer is opposite, usually IN means that the device sends data to the host, and OUT means that the host sends data to the device; here, IN means that the host sends data to the device, and OUT means that the device sends data to the host, which is intended to be combined with the optional data step.
2) In this step, the packets in the packet phase are all 0-length as SYNC+PID+CRC16
Okay , this is all of data format of USB. Thanks for your reading.
By HornmicLink_Henry @ 220916 17:12