In-depth analysis of USB protocol

Source: Internet
Author: User

To: http://hi.baidu.com/yangchengbo82/blog/item/bd062316d3df5a4e21a4e99b.html

USB devices are very popular nowadays, and they are always around us. For example, USB flash drives, MP3, and mobile phones all need to communicate with pc usb. With the USB interface, it is very convenient to use our life. When you want to listen to songs, you can insert mp3 to the PC and then download MP3 from the PC. This process is very simple in the user's opinion, do not need to install the driver, do not need to cut off the power of the PC. Truly embodies the "people-oriented technology" philosophy. Why is it so convenient to use a USB flash drive or mp3? Behind the convenience, it means that engineers need to do a lot of work to make it so convenient for everyone to use USB. The following describes the USB protocol in detail. USB communication is very complicated. It is very time-consuming to read and understand the Protocol just now. I hope this article will provide you with great guidance and give you an in-depth understanding of the USB protocol. The USB protocol is a master-slave protocol. In all communications, there is only one master controller, and the others are slave devices. A maximum of 127 slave devices can be connected, because only 7 bits are reserved as the device address. All USB data exchange is initiated by the master controller, and others respond from the device. Now let's take a look at the USB communication protocol on Windows. All the data mentioned later is the data sent by the USB driver on Windows. In order to display all the communication data, I found a USB arm Development Board. Through this development board, I can print all the data of the master controller and analyze it accordingly. At the same time, you can use this development board to debug the main controller driver of the dragon core. When the USB development board is powered on, the USB connection will be initialized first, but the connection line of the PC is not inserted. Then, the Development Board will output the following character from the serial port: USB suspendusb resume from the above, from the USB of the device until the connection line of the PC is inserted. When you insert a connection line to the PC, you will receive a message from the master. The following data is the data that the Development Board communicates with the USB master controller. 1. Receives the device descriptor configuration package from the master.Setup m = 0, n = 0, val = 37 80 06 00 01 00 00 40 00This is the first configuration package data sent by the main controller. Because the master controller does not know the length of the descriptor of the USB device, the length of the package is 0x0040, that is, the length of 64 bytes. Request_standard = 0x6usb_device_descriptor_type (0) according to the USB protocol, analyze the above data and you will know that it is used to obtain the device descriptor. To implement plug-and-play, you need to obtain the description of the inserted device to know what the USB device is like, whether it is a USB flash disk or a hid keyboard. Therefore, the USB device returns the following data to the master: usb_datainstage, CNT = 18, ep0data. Count = 18 12 01 10 01 00 00 00 40 00 80 00 01 04 2C 4A 01This data is the USB device descriptor, which describes the Protocol version of the USB used by the device. Here it is Version 1.1, with the manufacturer ID and product ID, and the offset address of the manufacturer, product, serial number, and other strings. Usb_evt_in, usb_datainstageusb_datainstage, CNT = 0, ep0data. count = 0usb_evt_out through the device descriptor, you can let the main controller know what version of the USB protocol is used for this device, is it a high-speed device, or a low-speed device, who produced it, then, the operating system can find the corresponding driver through the information. If the operating system does not find the corresponding driver, the system will prompt you to insert the disc, or other party to install the corresponding driver. 2. You have received the device Address Configuration package for the master node.Setup m = 0, n = 0, val = 37 00 05 01 00 00 00 00According to the USB protocol, this data is used to configure the IP address of the USB device. The function is to allocate the address of the USB device. Because there can be 127 devices on the USB bus, each device needs to assign a unique address for communication, this is the same as the MAC address of the NIC. In fact, it is like assigning a house number to let everyone know what room the house is. From the following data analysis, the allocation address is 1. request_standard = 0x5usb_deviceaddress = 129usb_evt_in, usb_setaddress (1) 3. Receives the device descriptor configuration package from the master.Setup m = 0, n = 0, val = 37 80 06 00 01 00 00 12 00Once again, I received the configuration package for obtaining the device descriptor. Because I didn't know the size of the device descriptor for the first time, I always sent a maximum data package, which is 64 bytes in size, now that we know that the descriptor is 0 x, we will send it. So the USB device can respond to the device descriptor again. Request_standard = 0x6usb_device_descriptor_type (1) usb_datainstage, CNT = 18, ep0data. Count = 18 12 01 10 01 00 00 00 40 00 80 00 01 04 2C 4A 01The device descriptor is returned again here. Usb_evt_in, usb_datainstageusb_datainstage, CNT = 0, ep0data. Count = 0 4. Received configuration descriptor packageSetup m = 0, n = 0, val = 45 80 06 00 02 00 00 09 00According to the USB protocol, this is a configuration descriptor package, that is, how many configuration methods the USB device has to send to the master. USB devices are diverse to meet different user needs. For example, the mouse and keyboard are different devices. There are MP3 players and various digital cameras. They are all different devices. You can use the following method to describe how many methods are configured, mainly for communication. Request_standard = 0x6usb_configuration_descriptor_type (2) (offset = 0x0) usb_datainstage, CNT = 9, ep0data. Count = 9 09 02 22 00 01 01 00 01 32Here is the configuration descriptor returned by the USB device to the master. It mainly describes how many configurations the device has, such as defining the type of the endpoint and the transmission method of the endpoint, there is also how much power the device uses the USB bus. Usb_evt_in, usb_datainstageusb_datainstage, CNT = 0, ep0data. Count = 0 5. Received string descriptor packageSetup m = 0, n = 0, val = 45 80 06 00 03 00 00 ff 00Obtain the string descriptor. Because the string descriptor is already in the device descriptor, it is mainly an offset address, for example 04 Is the identifier of the manufacturer.Request_standard = 0x6usb_string_descriptor_type (3) (offset = 0x0) usb_datainstage, CNT = 4, ep0data. Count = 4 04 03 09 04Here, the description of the string with the offset address 0 is returned. In fact, it is the language description identifier that stores the string descriptor. Here, the English identifier is 0x0409. Usb_evt_in, usb_datainstageusb_datainstage, CNT = 0, ep0data. Count = 0 6. Received string descriptor packageSetup m = 0, n = 0, val = 45 80 06 4A 03 09 04 ff 00After receiving the string descriptor, you will know that it is the string to return 0x4a, that is, the offset address in the string descriptor. Request_standard = 0x6usb_string_descriptor_type (4) (offset = 0x4a) usb_datainstage, CNT = 36, ep0data. Count = 36 24 03 43 00 41 00 49 00 32 00 30 00 30 00 37 00 30 00 33 00 32 00 35 00 20 00 31 00 2E 00 30 00 2E 00 30 00The USB device returns the string 0x4a to the PC. Here is the string I placed: cai20070325 1.0.0, which serves as the product serial number. Because unicode encoding is used, all the high bytes are 0. Because the USB protocol uses a small-end format to send data, the USB protocol is always low and the USB protocol is always high. In this way, the serial number of the USB device is displayed in the PC. Another step forward. Usb_evt_in, usb_datainstageusb_datainstage, CNT = 0, ep0data. Count = 0 7. Receives the Second Configuration descriptor packageSetup m = 0, n = 0, val = 45 80 06 00 02 00 00 ff 00The second configuration descriptor package is received here. The difference from the first one is that the return length is different. The length returned by the first configuration package is 9 bytes, And the length here is 255. Request_standard = 0x6usb_configuration_descriptor_type (5) (offset = 0x0) usb_datainstage, CNT = 34, ep0data. Count = 34 09 02 22 00 01 01 01 01 32 09 04 00 01 03 00 00 6e 09 21 00 01 01 01 01 22 24 00 07 05 81 03 40 00 20Return the description of all configurations to the PC to let the PC know all the configurations of the USB device. The configuration includes device configuration, interface configuration, endpoint configuration, and special device configuration information. Usb_evt_in, usb_datainstageusb_datainstage, CNT = 0, ep0data. Count = 0 8. Received string descriptor packageSetup m = 0, n = 0, val = 45 80 06 00 03 00 00 ff 00The device returns the string descriptor of the PC. Request_standard = 0x6usb_string_descriptor_type (6) (offset = 0x0) usb_datainstage, CNT = 4, ep0data. Count = 4 04 03 09 04Returns the language definition of the device descriptor. Usb_evt_in, usb_datainstageusb_datainstage, CNT = 0, ep0data. Count = 0 9. Received string descriptor packageSetup m = 0, n = 0, val = 45 80 06 2C 03 09 04 ff 00The product string required to receive the PC. Request_standard = 0x6usb_string_descriptor_type (7) (offset = 0x2c) usb_datainstage, CNT = 30, ep0data. Count = 30 1e 03 42 00 69 00 67 00 53 00 6C 00 6f 00 70 00 65 00 33 00 44 00 20 00 48 00 49 00 44 00The USB device returns the product string to the PC. Usb_evt_in, usb_datainstageusb_datainstage, CNT = 0, ep0data. Count = 0 10. Received string descriptor packageSetup m = 0, n = 0, val = 45 80 06 00 03 00 00 ff 00The string descriptor is received here. Request_standard = 0x6usb_string_descriptor_type (8) (offset = 0x0) usb_datainstage, CNT = 4, ep0data. Count = 4 04 03 09 04The return language ID. Usb_evt_in, usb_datainstageusb_datainstage, CNT = 0, ep0data. Count = 0 11. Received string descriptor packageSetup m = 0, n = 0, val = 45 80 06 2C 03 09 04 ff 00The product string required to receive the PC. Request_standard = 0x6usb_string_descriptor_type (9) (offset = 0x2c) usb_datainstage, CNT = 30, ep0data. Count = 30 1e 03 42 00 69 00 67 00 53 00 6C 00 6f 00 70 00 65 00 33 00 44 00 20 00 48 00 49 00 44 00The USB device returns the product string to the PC. Usb_evt_in, usb_datainstageusb_datainstage, CNT = 0, ep0data. Count = 0 12. Received string descriptor packageUsb_evt_outsetup m = 0, n = 0, val = 37 80 06 00 03 00 00 ff 00The string descriptor is received here. Request_standard = 0x6usb_string_descriptor_type (10) (offset = 0x0) usb_datainstage, CNT = 4, ep0data. Count = 4 04 03 09 04The return language ID. Usb_evt_in, usb_datainstageusb_datainstage, CNT = 0, ep0data. Count = 0 1. Received string descriptor packageSetup m = 0, n = 0, val = 45 80 06 2C 03 09 04 ff 00The product string required to receive the PC. Request_standard = 0x6usb_string_descriptor_type (11) (offset = 0x2c) usb_datainstage, CNT = 30, ep0data. Count = 30 1e 03 42 00 69 00 67 00 53 00 6C 00 6f 00 70 00 65 00 33 00 44 00 20 00 48 00 49 00 44 00The USB device returns the product string usb_evt_in, usb_datainstageusb_datainstage, CNT = 0, ep0data. Count = 0 to the PC. 2. Received string descriptor packageSetup m = 0, n = 0, val = 45 80 06 00 03 00 00 ff 00Request_standard = 0x6usb_string_descriptor_type (12) (offset = 0x0) usb_datainstage, CNT = 4, ep0data. Count = 4 04 03 09 04Usb_evt_in, usb_datainstageusb_datainstage, CNT = 0, ep0data. Count = 0 setup m = 0, n = 0, val = 45 80 06 2C 03 09 04 ff 00Request_standard = 0x6usb_string_descriptor_type (13) (offset = 0x2c) usb_datainstage, CNT = 30, ep0data. Count = 30 1e 03 42 00 69 00 67 00 53 00 6C 00 6f 00 70 00 65 00 33 00 44 00 20 00 48 00 49 00 44 00Usb_evt_in, usb_datainstageusb_datainstage, CNT = 0, ep0data. Count = 0 3. Received string descriptor packageUsb_evt_outsetup m = 0, n = 0, val = 45 80 06 00 01 00 00 12 00Here, we receive a request to return the vendor string. Request_standard = 0x6usb_device_descriptor_type (14) usb_datainstage, CNT = 18, ep0data. Count = 18 12 01 10 01 00 00 00 40 00 80 00 01 04 2C 4A 01Return the vendor string of the device to the PC. Usb_evt_in, usb_datainstageusb_datainstage, CNT = 0, ep0data. Count = 0 4. Received the third configuration descriptor packageSetup m = 0, n = 0, val = 45 80 06 00 02 00 00 09 00Request_standard = 0x6usb_configuration_descriptor_type (15) (offset = 0x0) usb_datainstage, CNT = 9, ep0data. Count = 9 09 02 22 00 01 01 00 01 32Usb_evt_in, usb_datainstageusb_datainstage, CNT = 0, ep0data. Count = 0 5. Received the fourth configuration descriptor packageSetup m = 0, n = 0, val = 45 80 06 00 02 00 00 22 00Request_standard = 0x6usb_configuration_descriptor_type (16) (offset = 0x0) usb_datainstage, CNT = 34, ep0data. Count = 34 09 02 22 00 01 01 01 01 32 09 04 00 01 03 00 00 6e 09 21 00 01 01 01 01 22 24 00 07 05 81 03 40 00 20Return different data according to the length. Usb_evt_in, usb_datainstageusb_datainstage, CNT = 0, ep0data. Count = 0 6. Received the set descriptor packageSetup m = 0, n = 0, val = 45 00 09 01 00 00 00 00Request_standard = 0x9usb_setconfiguration WB. L = 1usb_configure (true) usb_setconfiguration true the above shows that after so many rounds, the master controller has been configured and can be used for this device. In this case, if the installation of the device is complete in Windows, you can use it. 7. Receive the idle descriptor packageSetup m = 0, n = 0, val = 37 21 0a 00 00 00 00 00 When this descriptor is received, the device is idle. 8. Hid received Report descriptor packageSetup m = 0, n = 0, val = 37 81 06 00 22 00 00 00 64 00Because in the configuration descriptor, I describe this device as a HID device, so I will receive the report descriptor of the hid. Request_standard = 0x6request_to_interface (0x22) hid_report_descriptor_typeusb_datainstage, CNT = 36, ep0data. Count = 36 06 00 FF 09 01 A1 01 19 01 29 08 15 00 25 ff 95 3f 75 08 81 02 19 01 29 08 15 00 25 ff 95 3f 75 08 91 02 C0Here the type of report description is returned, indicating the size of each data report sent and the data format. Here, 63 bytes of output and 63 input descriptors are returned. Usb_evt_in, usb_datainstageusb_datainstage, CNT = 0, ep0data. Count = 0usb_evt_out here, the USB device is initialized. As you can see from the above, to configure a USB device, it takes 20 back and forth to complete the configuration, this process is a lot. If an error occurs in any part of the debugging process, the configuration will not be successful. It takes longer to add hardware errors. Obviously, although the USB device is very convenient to use, it takes a lot of effort and time for engineers. Therefore, USB is a very valuable device. At present, USB devices are very popular. In the future, most of the communication between PCs and peripherals will be completed using USB devices. It will completely replace the serial and parallel communication. I saw a USB device developed by a manufacturer. There are more than 200 types, from USB fans to USB phones, from USB networks to USB ornaments, such as USB interface lights. There are also USB-controlled massagers and other fitness equipment. I hope you can develop a better USB device after reading this article. In the dragon core computer, there is no soft drive, so it is started through a USB flash disk, so you need to configure the USB device, so the BIOS in the Dragon core is very complex, the dragon core pmon is still developing a USB-enabled keyboard and USB mouse, so that no PS2 device can be used, and the mouse and keyboard can be inserted at any time, can be used normally, unlike PS2, the operating system needs to be restarted. I want the dragon core computer to add a USB device in the future and connect it directly to other PCs to share data without network configuration.
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.