USB transmission, encoding, data format, master the necessary knowledge of usb

1. Endpoint: a data buffer located on the USB device or host, used to store and send various USB data, each endpoint has a unique definite address and different transmission characteristics (such as input endpoint, output endpoint, configuration endpoint, bulk transfer endpoint)

2. Frame: the concept of time. In USB, a frame is 1MS, which is an independent unit and includes a series of bus actions. USB divides a frame into several parts, each of which is a USB transmission action.

3. Uplink and downlink: the device to the host is the uplink, and the host to the device is the downlink

Let's start with a question-and-answer format.

Question 1: What is the structure of the USB transmission line?

Answer 1: A USB transmission line consists of four lines: ground line, power line, D+, D-. D+ and D- are differential input lines, which use a voltage of 3.3V (note that it is the same as the 5V level of CMOS Different), and the power line and ground line can provide 5V voltage to the device, the maximum current is 500MA (can be set in programming, as for the hardware implementation mechanism, don't care about it).

Question 2: How is the data transmitted in the USB transmission line

Answer 2: Data is sent from low to high in the USB cable.

Question 3: The encoding scheme of USB?

Answer 3: USB uses non-return-to-zero inversion to transmit data. When the differential data on the transmission line is input 0, it is inverted, and when 1 is input, the original value is maintained. In order to ensure the accuracy of signal transmission, when sending a When the packet is sent, the transmission device will perform a bit insertion operation (that is, insert a 0 after every 6 consecutive 1s in the data stream), thereby forcing the NRZI code to change. This knowledge is enough, these are handled by specialized hardware.

Question 4: What is the data format of USB?

Answer 4: Like others, USB data is composed of binary number strings. First, the number string constitutes a domain (there are seven types), the domain constitutes a packet, the packet constitutes a transaction (IN, OUT, SETUP), and the transaction finally constitutes a transmission. (Interrupt transfers, parallel transfers, bulk transfers, and control transfers). The following briefly introduces the domain, package, transaction, and transport, please pay attention to the relationship between them.

(1) Domain: It is the smallest unit of USB data, consisting of several bits (as to how many bits are determined by the specific domain), the domain can be divided into seven types:

1. Synchronization field (SYNC), eight bits, the value is fixed at 0000 0001, used to synchronize the local clock with the input

2. The identification field (PID), which is composed of four-digit identifier + four-digit identifier inverse code, indicates the type and format of the packet, which is a very important part. It can be calculated here that there are 16 kinds of USB identification codes. Please refer to question 5 for specific categories.

3. Address domain (ADDR): seven-bit address, representing the address of the device on the host, address 000 0000 is named as zero address, which is the first time any device connects to the host, before being configured and enumerated by the host The default address, so you can know why a USB host can only connect 127 devices.

4. Endpoint field (ENDP), four bits, it can be seen that a USB device has a maximum of 16 endpoints.

5. Frame number field (FRAM), 11 bits, each frame has a specific frame number, the maximum capacity of the frame number field is 0x800, which is of great significance for synchronous transmission (synchronous transmission is one of four transmission types, please see below ).

6. Data field (DATA): The length is 0~1023 bytes. In different transmission types, the length of the data field is different, but it must be an integer number of bytes in length.

7. Check field (CRC): A method of checking the non-PID fields in token packets and data packets (see below for the classification of packets). CRC check is widely used in communication and is a kind of It is a very good verification method. As for the specific verification method, I will not say much here. Please refer to the relevant information. It is only necessary to note that the division of the CRC code is a modulo 2 operation, which is different from the division in the decimal system.

(2) Packet: There are four types of packets composed of domains, namely token packets, data packets, handshake packets and special packets. The first three are important packets. The domain structure of different packets is different. The introduction is as follows.

1. Token package: It can be divided into input package, output package, setting package and frame start package (note that the input package here is used to set the input command, and the output package is used to set the output command, not the data number)

The format of the input package, output package and setting package is the same:

SYNC+PID+ADDR+ENDP+CRC5 (five-digit check code)

(For the explanation of the above abbreviations, please refer to the introduction of the above domain, and for the specific definition of the PID code, please refer to Question 5)

The format of the frame start packet:

SYNC+PID+11-bit FRAM+CRC5 (five-bit check code)

2. Data packet: It is divided into DATA0 packet and DATA1 packet. When USB sends data, when the length of the data sent at one time is greater than the capacity of the corresponding endpoint, the data packet needs to be divided into several packets, sent in batches, DATA0 packet It is sent alternately with the DATA1 packet, that is, if the first data packet is DATA0, then the second data packet is DATA1. But there are exceptions. In synchronous transmission (one of the four types of transmission), all data packets are DATA0, and the format is as follows:

SYNC+PID+0~1023 bytes+CRC16

3. Handshake Packet: The packet with the simplest structure, the format is as follows

SYNC+PID

(Note that each of the above packages has different types. USB1.1 defines ten types of packages. For details, please refer to Question 5)

(3) Transactions: There are three major transactions: IN transaction, OUT transaction and SETUP transaction. Each transaction consists of three stages: token packet, data packet, and handshake packet. The meaning of stage here is because of the sending of these packets. There is a certain chronological order. The three stages of the transaction are as follows:

1. Token packet phase: start an input, output or set transaction

2. Data packet stage: send corresponding data according to input and output

3. Handshake packet phase: Returns the data reception status. This phase is not present in the IN and OUT transactions of synchronous transmission, which is special.

The three types of transactions are as follows (a transaction is described in three phases below):

1. IN transaction:

Token packet stage - the host sends an input packet with a PID of IN to the device, notifying the device to send data to the host;

Data packet phase - the device will make three responses according to the situation (note: the data packet phase does not always transmit data, and it will enter the handshake packet phase in advance according to the transmission situation)

1) The device endpoint is normal, and the device sends data packets to the host (DATA0 and DATA1 alternate);

2) When the device is busy and cannot send a data packet to the host, it sends a NAK invalid packet, the IN transaction ends early, and the next IN transaction does not continue;

3) The corresponding device endpoint is disabled, and the error packet STALL packet is sent, the transaction ends in advance, and the bus enters the idle state.

Handshake packet phase - After the host receives the data correctly, it will send an ACK packet to the device.

2. OUT transaction:

Token packet phase - the host sends an output packet with a PID of OUT to the device, notifying the device to receive data;

Data packet stage - relatively simple, that is, the host will send data to the device, DATA0 and DATA1 alternate

Handshake packet phase - the device will make three responses according to the situation

1) The device endpoint receives correctly, the device returns ACK to the incoming host, notifying the host that new data can be sent, if the data packet has a CRC check error, it will not return any handshake information;

2) If the device is busy and cannot send data packets to the host, it will send a NAK invalid packet to notify the host to send data again;

3) The corresponding device endpoint is disabled, the error packet STALL packet is sent, the transaction ends in advance, and the bus directly enters the idle state.

3. SETUP transaction:

Token packet stage - the host sends an output packet with a PID of SETUP to the device, notifying the device to receive data;

Data packet stage - relatively simple, that is, the host will send data to the device. Note that there is only one DATA0 packet fixed to 8 bytes. The content of these 8 bytes is the standard USB device request command (there are 11 in total, the specific Please see question 7)

Handshake packet phase - After the device receives the command information from the host, it returns ACK, after which the bus enters an idle state and prepares for the next transmission (usually a transmission consisting of an IN or OUT transaction after a SETUP transaction)

(4) Transmission:

Transmission consists of OUT, IN, and SETUP transactions. There are four types of transmission, interrupt transmission, batch transmission, synchronous transmission, and control transmission. The structure of interrupt transmission and batch transmission is the same, and synchronous transmission has the simplest structure. The control transfer is the most important and the most complex transfer.

1. Interrupt transmission: composed of OUT transaction and IN transaction, used in data transmission of HID devices such as keyboard and mouse

2. Bulk transmission: It is composed of OUT transaction and IN transaction. It is used for large-capacity data transmission. It has no fixed transmission rate and does not occupy bandwidth. When the bus is busy, USB will give priority to other types of data transmission, and temporarily stop batching. transfer. such as U disk

3. Synchronous transmission: It consists of OUT transactions and IN transactions. There are two special places. First, there is no return packet phase in the IN and OUT transactions of synchronous transmission; second, in the data packet phase, all data packets are DATA0, such as video and audio transmission

4. Control transmission: the most important and most complex transmission, the control transmission consists of three stages (initial setting stage, optional data stage, status information step), each stage can be regarded as a transmission, that is to say, control The transmission is actually composed of three transmissions, which are used for

After the USB device is connected to the host for the first time, the host exchanges information, device address and reads the device descriptor through control transmission, so that the host can identify the device and install the corresponding driver, which is what every USB developer should be concerned about question.

1. Initial setup step: it is a transmission consisting of SET transactions

2. Optional data step: It is a transmission consisting of IN or OUT transactions. This step is optional, depending on whether the initial setup step requires read/write data (standard request command sent by the packet phase of the SET transaction). Decide)

3. Status information step: As the name implies, this step is to obtain status information, which is a transmission composed of IN or OUT transactions, but it should be noted that the IN and OUT transactions here are different from the previous INT and OUT transactions in two points:

1) The transmission direction is opposite, usually IN means the device sends data to the host, OUT means the host sends data to the device; here, IN means the host sends data to the device, and OUT means the device sends data to the host, this is for and optional data combination of steps;

2) In this step, the data packets in the data packet phase are all 0 length, that is, SYNC+PID+CRC16

Except for the above two points, the others are the same, so I won't say much here.

(Thinking: How should these transmission modes be set in actual work?)

Question 5: What are the identification codes?

Answer 5: As mentioned above, the identification code is composed of four-digit data, so it can represent sixteen kinds of identification codes. In the USB1.1 specification, only ten kinds of identification codes are used, and sixteen kinds of identification codes are used in USB2.0. , The role of the identification code is to describe the attributes of the package. The identification code is associated with the package. First, briefly introduce the types of data packets. The data packets are divided into four types: token packets, data, handshake packets and special packets. (Please refer to question 7 for specific classification), there are the following sixteen kinds of identification codes:

Token Pack :

0x01 Output (OUT) initiates a transfer in the direction from the host to the device, and contains the device address and label

0x09 Input (IN) initiates a transfer in the direction of the device to the host and contains the device address and label

0x05 Start of Frame (SOF) indicates the start of a frame and contains the corresponding frame number

0x0d SETUP initiates a control transfer for the host to initialize the device

data pack :

0x03 Even data packet (DATA0),

0x0b Odd packet (DATA1)

Handshake package:

0x02 Acknowledgment of the reception of an error-free packet (ACK)

0x0a is invalid, the receiving (sending) side is busy and cannot receive (send) information

0x0e error, endpoint is forbidden or does not support control pipe request

Special packet 0x0C preamble, used to start data transmission of low-speed devices on downlink ports

Question 6: How does the USB host identify the USB device?

Answer 6: When the USB device is plugged into the host, the host will enumerate and configure the device through a series of actions (configuration is a state of enumeration, and the state represents a temporary state). These states are as follows:

1. Attached: After the device is connected to the host, the host discovers the access of the device by detecting the level change on the signal line;

2. Powered state (Powered): It is to supply power to the device, which is divided into the default power supply value when the device is connected, and the power supply value after the configuration stage (according to the maximum value required in the data, which can be set by programming)

3. Default state (Default): Before the USB is configured, it communicates with the host through the default address 0;

4. Address state: After configuration, after the USB device is reset, it can communicate with the host according to the unique address assigned to it by the host. This state is the address state;

5. Configured: Obtain various information of the device through various standard USB request commands, and change or set certain information of the device.

6. Suspended: The bus-powered device has no bus operation within 3ms, that is, if the USB bus is in an idle state, the device will automatically enter the suspended state. After entering the suspended state, the total current Power consumption does not exceed 280UA.

Question 7: What exactly is the standard USB device request command mentioned in Answer 4?

Answer 7: The standard USB device request command is used in the data packet phase (ie, DATA0, consisting of eight bytes) in the "initial setup step" in the control transfer. Please refer to the content of Q&A 4. There are a total of 11 standard USB device request commands, all of which are 8 bytes in size and have the same structure. They consist of 5 fields (the field is the data part of the standard request command). The structure is as follows (the numbers in brackets indicate the number of bytes, The first letters bm, b, w represent bitmap, byte, double byte respectively):

bmRequestType(1)+bRequest(1)+wvalue(2)+wIndex(2)+wLength(2)

The meaning of each field is as follows:

1、bmRequestType:D7D6D5D4D3D2D1D0

D7=0 host to device

= 1 device to host;

D6D5=00 standard request command

=01 class request command

=10 user-defined commands

=11 reserved value

D4D3D2D1D0=00000 The recipient is the device

=00001 The recipient is the device

=00010 Receiver is endpoint

=00011 The recipient is another recipient

= other other values ​​reserved

2. bRequest: Request command code. In standard USB commands, each command defines a number, and the value of the number is the value of the field. The number and command name are as follows (note that the command code here should be used in combination with other fields , it can be said that the command code is the core of the standard request command code, and it is precisely because of these command codes that the 11 USB standard request commands are determined):

0) 0 GET_STATUS: used to return the status of a specific receiver

1) 1 CLEAR_FEATURE: Used to clear or disable certain features of the receiver

2) 3 SET_FEATURE: Used to enable or activate some features of the command receiver

3) 5 SET_ADDRESS: used to assign an address to the device

4) 6 GET_DEscriptOR: Used by the host to obtain the specific descriptor of the device

5) 7 SET_DEscriptOR: Modify the relevant descriptor in the device, or add a new descriptor

6) 8 GET_CONFIGURATION: used by the host to obtain the configuration value of the current device of the device (note that it is different from the above)

7) 9 SET_CONFIGURATION: used by the host to indicate the required configuration adopted by the device

8) 10 GET_INTERFACE: used to get the current interface descriptor number

9) 11 SET_INTERFACE: used by the host to request the device to describe the interface with a descriptor

10) 12 SYNCH_FRAME: used by the device to set up and report an endpoint's synchronization frame

The above 11 commands are really as long as a piece of cloth. Please read the book. I won't say much here. Control transmission is the focus of USB, and these 11 commands are the focus of control transmission, so this 11 commands are the top priority. If this is understood, USB is even an entry.

Question 8: Descriptor is often seen in standard USB request commands. What is the origin of this?

Answer 8: Descriptor is a descriptor, which is a complete data structure, which can be implemented by programming such as C language, and stored in the USB device to describe all the properties of a USB device. The USB host requests the device through a series of commands. sent this information. Its function is to transmit information to the host through commands such as in the question and answer section, so that the host can know what function the device has, what kind of device it belongs to, how much bandwidth it takes, what kind of transmission method to use, and the amount of data. Size, only after the host determines this information, the device can really start to work, so the descriptor is also a very important part, it must be mastered. There are 5 standard descriptors, and USB defines numbers for these descriptors:

1 - device descriptor

2 - Configuration Descriptor

3 - character descriptor

4 - Interface Descriptor

5 - Endpoint Descriptor

There is a certain relationship between the above descriptors. A device has only one device descriptor, and a device descriptor can contain multiple configuration descriptors, and a configuration descriptor can contain multiple interface descriptors. An interface uses several For each endpoint, there are several endpoint descriptors. This descriptor is composed of certain fields, which are described as follows:

1. Device descriptor

struct _DEVICE_DEscriptOR_STRUCT

{

BYTE bLength; //The size of the device descriptor in bytes, which is 0x12

BYTE bDescriptorType; //Descriptor type number, 0x01

WORD bcdUSB; //USB version number

BYTE bDeviceClass; //The device class code allocated by USB, 0x01~0xfe is the standard device class, 0xff is the manufacturer-defined type

//0x00 is not defined in the device descriptor, such as HID

BYTE bDeviceSubClass; //Subclass code allocated by usb, same as above, the value is specified and allocated by USB

BYTE bDeviceProtocl; //Device protocol code assigned by USB, same as above

BYTE bMaxPacketSize0; //The maximum packet size of endpoint 0

WORD idVendor; //Vendor ID

WORD idProduct; //Product number

WORD bcdDevice; //Device factory number

BYTE iManufacturer; //Describe the index of the manufacturer string

BYTE iProduct; //Describe the index of the product string

BYTE iSerialNumber; //Describe the index of the device serial number string

BYTE bNumConfiguration; //Number of possible configurations

}

2. Configuration descriptor

struct _CONFIGURATION_DEscriptOR_STRUCT

{

BYTE bLength; //The size of the device descriptor in bytes, which is 0x12

BYTE bDescriptorType; //Descriptor type number, 0x01

WORD wTotalLength; //Configure the size of all quantities returned

BYTE bNumInterface; //Number of interfaces supported by this configuration

BYTE bConfigurationVale; //Parameter values ​​required by the Set_Configuration command

BYTE iConfiguration; //The index value of the string describing the configuration

BYTE bmAttribute; //Selection of power supply mode

BYTE MaxPower; //The maximum current the device draws from the bus

}

3. Character descriptor

struct _STRING_DEscriptOR_STRUCT

{

BYTE bLength; //The size of the device descriptor in bytes, which is 0x12

BYTE bDescriptorType; //Descriptor type number, 0x01

BYTE SomeDescriptor[36]; //UNICODE encoded string

}

4. Interface descriptor

struct _INTERFACE_DEscriptOR_STRUCT

{

BYTE bLength; //The size of the device descriptor in bytes, which is 0x12

BYTE bDescriptorType; //Descriptor type number, 0x01

BYTE bInterfaceNunber; //The number of the interface

BYTE bAlternateSetting;//Alternate interface descriptor number

BYTE bNumEndpoints; //The number of endpoints used by this interface, excluding endpoint 0

BYTE bInterfaceClass; //Interface type

BYTE bInterfaceSubClass;//Interface subtype

BYTE bInterfaceProtocol;//The protocol followed by the interface

BYTE iInterface; //The string index value describing the interface

}

5. Endpoint descriptor

struct _ENDPOIN_DEscriptOR_STRUCT

{

BYTE bLength; //The size of the device descriptor in bytes, which is 0x12

BYTE bDescriptorType; //Descriptor type number, 0x01

BYTE bEndpointAddress; //Endpoint address and input and output attributes

BYTE bmAttribute; //The transfer type attribute of the endpoint

WORD wMaxPacketSize; //The maximum packet size received and sent by the endpoint

BYTE bInterval; //The time interval for the host to query the endpoint

}

After understanding the above eight questions, you can enter the next step of USB learning

Guess you like

Origin blog.csdn.net/daocaokafei/article/details/124135511
usb
usb