USB transfer protocol. -- Arvin, usb transmission arvin
Question 1: What is the structure of the USB transmission line? Answer 1: a usb transmission line consists of four lines: Ground Wire, power cord, D +, and D-. D + and D-are differential input lines, it uses a voltage of 3.3V (note that it is different from the 5 V level of CMOS), and the power cord and ground wire can provide 5 V voltage to the device, the maximum current is Ma (which can be set in programming. As for the hardware implementation mechanism, do not care about it ). Question 2: How is data transmitted in the USB transmission line? Answer 2: data is transmitted in USB cables from low to high. Question 3: What is the USB encoding scheme? Answer 3: USB adopts non-return-to-zero inversion to transmit data. When the differential data input on the transmission line is 0, the reverse is obtained. When the input is 1, the original value is kept. In order to ensure the accuracy of the signal sending, when a packet is sent on the USB bus, the transmission device needs to insert a bit *** (that is, insert a 0 value after every six consecutive 1 values in the data stream ), this forces the NRZI code to change. This is enough. These are processed by dedicated hardware. Question 4: What is the USB data format? Answer 4: Like others, USB data is composed of binary numeric strings. First, a numeric string forms a domain (with seven types), and a domain forms a package, the packet then forms a transaction (IN, OUT, SETUP), and the transaction finally forms a transmission (Interrupt transmission, parallel transmission, batch transmission, and control transmission ). ---------------------- The following describes the domain, package, transaction, and transmission. Pay attention to the relationship between them. (1) domain: the smallest unit of USB data, which consists of several digits (the number of digits is determined by the specific domain). The domain can be divided into seven types: 1. Synchronization domain (SYNC), eight bits, with a fixed value of 0000 0001, used for synchronization between local clock and input 2. Identification domain (PID ), it consists of four-digit identifiers and four-digit identifiers, indicating the package type and format. This is a very important part. It can be calculated that there are 16 USB identifiers, for details about the category, see question 5. 3. Address domain (ADDR): a seven-bit address represents the address of the device on the host. Address 000 0000 is named as zero. This is the first time a device connects to the host, the default address before being configured and enumerated by the host, so that you can know why a USB host can only connect 127 devices. 4. ENDP allows up to 16 endpoints of a USB device. 5. the frame number domain (FRAM) has 11 bits. Each frame has a specific frame number. The maximum capacity of the frame number domain is 0x800, it is of great significance for synchronous transmission (synchronous transmission is one of the four transmission types, please refer to the following ). 6. DATA field: the length is 0 ~ 1023 bytes. In different transmission types, the data domain length varies, but must be the length of an integer. 7. check domain (CRC): a method for verifying the non-PID domain of the token package and data packet (for packet classification, see the following, CRC verification is widely used in communications. It is a good verification method. As for the specific verification method, I will not mention it here. Please refer to the relevant materials, note that the division of the CRC code is a modulo 2 operation, which is different from the division in the decimal system. (2) packages: There are four types of packages composed of domains: token package, data packet, handshake package, and special package. The first three are important packages, the domain structure of different packages is different. 1. The token package is described as follows: it can be divided into the input package, output package, setting package, and frame start package (note that the input package is used to set the input command, and the output package is used to set the output command, the format of the input package, output package, and set package is the same: SYNC + PID + ADDR + ENDP + CRC5 (five-digit verification code) (For the abbreviations and explanations above, see the introduction of the above domain. For the specific definition of the PID code, see question 5) Format of the frame start package: SYNC + PID + 11-bit FRAM + CRC5 (five-bit verification code) 2. Data Packets: data packets are divided into the DATA0 package and DATA1 package. When the USB sends data, when the length of data sent at a time is greater than the capacity of the corresponding endpoint, data packets must be divided into several packages and sent in batches. data packets 0 and DATA1 are sent in turn, that is, if the first data packet is DATA0, the second data packet is DATA1. However, there are exceptions. In synchronous transmission (one of the four transmission types), all data packets are DATA0 in the following format: SYNC + PID + 0 ~ 1023 bytes + CRC163. Handshake package: the simplest package, in the following format: SYNC + PID (note that each package has a different type, USB defines ten packages, for details, see question 5) (3) Transactions: There are three major transactions: IN transactions, OUT transactions, and SETUP transactions, each transaction is composed of three phases: the token packet, the data packet, and the handshake packet. Here, the stages are used because the packets are sent in a certain time sequence, the three phases of a transaction are as follows: 1. the token package phase starts an input, output, or set transaction. 2. The packet phase: send the corresponding data by Input and Output. 3. Handshake package stage: return the data receipt status. This stage is not found IN the IN and OUT transactions of synchronous transmission. This is quite special. Three types of transactions are as follows (one transaction is described IN three phases): 1. IN transaction: token package phase-the host sends an input packet whose PID is IN to the device, the notification device sends data to the host. In the data packet phase, the device will respond to the situation in three ways (Note: The data packet phase does not always send data, the device enters the handshake packet phase in advance based on Transmission Conditions.) 1) the device endpoint is normal, and the device sends data packets to the host (data exchange between DATA0 and DATA1); 2) the device is busy, an invalid NAK packet cannot be sent to the host. The IN transaction ends before the next IN transaction continues. 3) the corresponding device endpoint is forbidden and the Error Packet STALL is sent, the transaction ends early, and the bus enters the idle state. Handshake package phase-the host sends an ACK packet to the device after correctly receiving the data. 2. OUT transaction: In the token package phase, the host sends an output packet whose PID is OUT to the device, notifying the device to receive data. In the data packet phase, the host sends data to the device, in the alternate handshake package phase between DATA0 and DATA1, the device responds to the following three responses: 1) the device endpoint is correctly received, and the device returns ACK to the incoming host, notifying the host that new data can be sent, if a CRC check error occurs in the data packet, no handshake information is returned. 2) if the device is busy, the system cannot send an invalid NAK packet to the host, notifying the host to send data again. 3) the corresponding device endpoint is forbidden. When an error packet STALL is sent, the transaction is terminated before the bus enters the idle state. 3. SETUT transaction: the token package phase. The host sends an output packet with pid setup to the device, notifying the device to receive data. The data package phase is relatively simple, that is, the host sends data to the device, note that there is only one fixed 8-byte DATA0 package, and the content of these 8 bytes is the standard USB device Request command (a total of 11, please refer to question 7) handshake package stage-after the device receives the command information from the host, it returns ACK and the bus enters idle status, prepare the next transmission (usually an IN or OUT transaction after the SETUP transaction) (4) transmission: Transmission consists of the transactions IN the OUT, IN, and SETUP transactions, there are four transmission types: interrupted transmission, batch transmission, synchronous transmission, and control transmission. The structure of interrupted transmission is the same as that of batch transmission, and synchronous transmission has the simplest structure, control transmission is the most important and complex transmission. 1. interrupted transmission: composed of OUT transactions and IN transactions, used for data transmission between the keyboard, mouse, and other HID devices. 2. batch transmission: composed of OUT transactions and IN transactions, for large-capacity data transmission, there is no fixed transmission rate and no bandwidth occupation. When the bus is busy, USB will first transmit other types of data, and the Batch Transfer is temporarily stopped. 3. synchronous transmission: it consists of an OUT transaction and an IN transaction. There are two special points: first, there is no returned packet stage IN the IN and OUT transactions of synchronous transmission; second, in the data packet phase, all data packets are transmitted as DATA04 and control: the most important and complex transmission, control Transmission consists of three phases (initial setup phase, optional data phase, and status information step). Each phase can be considered as a transmission, that is to say, the control transmission is actually composed of three transmissions. After a USB device is added to the host for the first time, the host exchanges information, device addresses, and read device descriptors through control transmission, this allows the host to identify the device and install the corresponding driver, which is a concern of every USB developer. 1. Initial setup step: it refers to the transmission of a SET transaction. 2. optional data step: it refers to the transmission of an IN or OUT transaction. This step is optional, check whether the initial setup step requires reading/writing data (determined by the standard request Command sent by the data packet phase of the set transaction) 3. Status information step: as the name suggests, this step is to obtain status information, transmission composed of IN or OUT transactions, but note that the IN and OUT transactions are different from the previous INT and OUT transactions. 1) the transmission direction is the opposite, generally, IN indicates that the device sends data to the host, and OUT indicates that the host sends data to the device. Here, IN indicates that the host sends data to the device, and OUT indicates that the device sends data to the host, this is to be combined with the optional data step; 2) in this step, the data packet in the data packet phase is of 0 length, that is, SYNC + PID + CRC16 except for the above two differences, others are the same. (think about how to set these transmission modes in the *** practice ?) Question 5: What are the identification codes? Answer 5: As mentioned above, the ID code consists of four digits. Therefore, it can represent sixteen types of ID codes. In the USB1.1 specification, only ten types of ID codes are used, USB2.0 uses sixteen identifiers. The identifiers are used to describe the attributes of the package. The identifiers are associated with the package. First, we will briefly introduce the data packet type, there are four types of data packets: token package, Data, handshake package, and special package (see question 7 for details). There are 16 types of ID codes: token package: 0x01 output (OUT) starts a transmission from the host to the device, and contains the device address and number 0x09 input (IN) start a transmission from the device to the host, and contain the device address and number 0x05 frame start (SOF) to indicate the start of a frame, it also includes the corresponding frame number 0x0d setting (SETUP) to start a control transmission, used for the host to initialize the data packet to the device: 0x03 even data packet (DATA0), 0x0b odd data packet (DATA1) handshake package: 0x02 the correct packet (ACK) 0x0a is invalid. The received packet (sent) is busy but cannot be received (sent) The message 0x0e is incorrect. The endpoint is forbidden or cannot control the MPs queue request special packet 0x0C leading. This is used to start data transmission for low-speed devices on the downstream port. Question 6: how does a USB Host identify a USB device? Answer 6: When a USB device is plugged into the host, the host performs enumeration configuration on the device through a series of actions (the configuration is an enumeration State, and the State indicates a temporary state ), these statuses are as follows: 1. Access Mode (Attached): After the device is connected to the host, the host detects the access of the device by detecting the level changes on the signal line; 2. Power Supply Mode (Powered ): is to provide power to the device, divided into the Default power supply value when the device is connected, the power supply value after the configuration phase (according to the maximum value required in the data, can be set through programming) 3, the Default state (Default): Before USB is configured, use the default Address 0 to communicate with the host. 4. Address: After configuration, the USB device is reset, you can communicate with the host based on the unique address assigned to it by the host. This state is the address State; 5. The configuration state (Configured ): various standard USB request commands are used to obtain various information about the device and modify or set the information about the device. 6. Suspended state: if the bus power supply device does not have a bus *** within 3 ms, that is, if the USB Bus is idle, the device will automatically enter the Suspended state, the total current power consumption does not exceed 280UA after the suspension is enabled. Question 7: What is the standard USB device Request command mentioned in answer 4? Answer 7: The standard USB device Request command is used to control the data packet phase in the "initial setting step" of transmission (that is, DATA0, consisting of eight bytes ), please refer to the content in question 4. There are a total of 11 request commands for the standard USB device, which are 8 bytes in size and have the same structure and are composed of five fields (the field is the data part of the standard request command ), the structure is as follows (Numbers in parentheses indicate the number of bytes, and the first letter is bm, B, and w respectively indicate bitmap, byte, and dual-byte): bmRequestType (1) + bRequest (1) + wvalue (2) + wIndex (2) + wLength (2) fields have the following meanings: 1. bmRequestType: D7D6D5D4D3D2D1D0D7 = 0 host to device = 1 device to host; d6D5 = 00 Standard Request command = 01 type Request command = 10 user-defined command = 11 Retention Value D4D3D2D1D0 = 00000 receiver = 00001 receiver = 00010 receiver = 00011 receiver = Receiver for other recipients = other values reserved 2, bRequest: request command code. In the standard USB command, 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 this The command code in must be used together with other fields. It can be said that the command code is the core of the Standard Request command code, and 11 USB standard request commands are determined by the command code): 0) 0 GET_STATUS: Used to return the status of a specific recipient. 1) 1 CLEAR_FEATURE: Used to clear or disable certain characteristics of the recipient. 2) 3 SET_FEATURE: used to enable or activate certain characteristics of the command recipient. 3) 5 SET_ADDRESS: used to allocate the address to the device 4) 6 GET_DEscriptOR: used to obtain the specific descriptor of the device by the host 5) 7 SET_DEscriptOR: Modify the descriptor related to the device, or add a new descriptor 6) 8 GET_CONFIGURATION: used by the host to obtain the configuration value of the current device (note the same as above) 7) 9 SET_CONFIGURATION: used by the host to indicate the required configuration of the device 8) 10 GET_INTERFACE: used to obtain an interface descriptor number 9) 11 SET_INTER FACE: the host requires the device to use a descriptor to describe the interface 10) 12 SYNCH_FRAME: the 11 commands used to set and report synchronization frames at the same endpoint on the device must be clear that there is a piece of cloth that is really so long. Please read the books. I will not talk about it here, control transmission is the center of gravity of USB, and these 11 commands are the center of gravity of control transmission, so these 11 commands are the top priority. This is clear, USB is even a beginner. Question 8: In standard USB request commands, we often see Descriptor. What is this? Answer 8: Descriptor is a complete data structure. It can be implemented by programming in C language and stored in a USB device to describe all the properties of a USB device, a usb host uses a series of commands to require devices to send this information. Its function is to pass information to the host through *** commands in the Q & A section, this allows the host to know what functions the device has, which type of devices it belongs to, how much bandwidth it will occupy, what transmission modes it uses, and the size of the data volume. After the host determines the information, the device can really get started, so the descriptor is also a very important part. 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 -- there is a certain relationship between the descriptors above the endpoint descriptor. A device has only one device descriptor, A device descriptor can contain multiple configuration descriptors, while a configuration descriptor can contain multiple interface descriptors. If an interface uses several endpoints, there are several endpoints. This descriptor is composed of certain fields, which are described as follows: 1. device descriptor
Struct _ DEVICE_DEscriptOR_STRUCT {BYTE bLength; // The number of bytes of the device descriptor, 0x12BYTE bDescriptorType; // descriptor type number, 0x01WORD bcdUSB; // USB Version BYTE bDeviceClass; // The device Code allocated by USB, 0x01 ~ 0xfe is a standard device class, And 0xff is a vendor-defined type. // 0x00 is not defined in the device descriptor, for example, hid byte bDeviceSubClass; // The Sub-class code assigned by usb, same as above, value: BYTE bDeviceProtocl specified and allocated by USB; // protocol code of the device allocated by USB, same as BYTE bMaxPacketSize0; // The maximum package size of endpoint 0 WORD idVendor; // vendor No. WORD idProduct; // product no. WORD bcdDevice; // device No. BYTE iManufacturer; // The BYTE iProduct that describes the vendor string; // describes the index BYTE iSerialNumber of the product string; // describes the index BYTE bNumConfiguration of the device serial number string; // The number of possible configurations}
2. Configuration Descriptor
Struct _ CONFIGURATION_DEscriptOR_STRUCT {BYTE bLength; // The number of bytes of the device descriptor, 0x12BYTE bDescriptorType; // descriptor type number, 0x01WORD wTotalLength; // configure the size of all returned BYTE bNumInterface; // The number of interfaces supported by this configuration, BYTE bConfigurationVale; // The parameter value required by the Set_Configuration command, BYTE iConfiguration; // The index value BYTE bmAttribute that describes the configuration string; // select BYTE MaxPower for power supply mode; // the maximum current that the device extracts from the bus}
3. Character Descriptor
Struct _ STRING_DEscriptOR_STRUCT {BYTE bLength; // The number of bytes of the device descriptor, 0x12BYTE bDescriptorType; // descriptor type number, 0x01BYTE SomeDescriptor [36]; // UNICODE encoded string}
4. Interface Descriptor
Struct _ bytes {BYTE bLength; // The number of bytes of the device descriptor, 0x12BYTE bDescriptorType; // descriptor type number, 0x01BYTE bInterfaceNunber; // interface number BYTE bAlternateSetting; // Alternate Interface descriptor number BYTE bNumEndpoints; // Number of endpoints used by this interface, excluding the number of endpoints 0 BYTE bInterfaceClass; // interface type BYTE bInterfaceSubClass; // interface subtype BYTE bInterfaceProtocol; // The Protocol BYTE iInterface that the interface complies with; // string index value that describes the interface}
5. endpoint Descriptor
Struct _ ENDPOIN_DEscriptOR_STRUCT {BYTE bLength; // The number of bytes of the device descriptor, 0x12BYTE bDescriptorType; // descriptor type number, 0x01BYTE bEndpointAddress; // The endpoint address and input/output attributes BYTE bmattrival; // The transmission type attribute WORD wMaxPacketSize of the endpoint; // The maximum packet size BYTE bInterval received and sent by the endpoint; // interval of the host query endpoint}
------------------------------------------------------------- Update: 20161009 Association: http://blog.csdn.net/t5studioEmail:zhsh641@163.com