USB enumeration process details

Source: Internet
Author: User

In this system, the so-called USB device and host are determined by detecting changes in the pull resistance of the VCC to determine whether there is a device connection. D12 is integrated with a k Ω pull-up resistor, which is not connected to VCC by default. when running the program, you can send a connection command to D12 to connect the k Ω resistor to VCC, in this way, the host detects a device connection.

Its enumeration process is analyzed as follows.

After the device connects to the bus, the device obtains 5 V power from the bus, the program first initializes, the port, and then sends a USB connection command to D12. The host detects the device connection. The host sends the first signal to the device: Bus reset. An interruption occurs when the bus is reset, And the D12 device is enabled at the default address 0, so that the address 0 can be used to transmit commands and data in the next enumeration process, at the same time, the bus reset bit of the interrupt register is set to 1. In the program, D12 interrupts the request to the main loop, enters the interrupt processing program usb_int_handler (), reads the interrupt register, determines the interrupt type, and performs corresponding processing.

The host uses the default address 0 to read the device descriptor.

The specific process is: the host sends the first setup package to D12, each setup package is 8 bytes, the first package get descriptor content is: 80 06 00 01 00 40 00, the data is in hexadecimal notation. 40 indicates that the maximum length of the returned data is 40 h. This setup package is stored in the D12 endpoint 0 buffer and generates an external interruption. (In this case, the interrupt type is saved in the interrupt register of D12: The out interrupt of the endpoint 0, that is, the value of the interrupt register byte 1 should be 0x01, because the buffer zone of the D12 endpoint 0 has only 16 bytes, the microcontroller sends 16 bytes of device descriptor first.

When the host receives the 16-byte characters, it is deemed that there is actually a device connection.

Address allocation.

The host sends the second setup package to D12. This is a data packet containing the specified address. Its content is generally 00 05 02 00 00 00 00, 02 indicates that the address assigned by the host to the device is 0x02. In future communication, the device only responds to the address 0x02. After receiving the setup package, D12 also generates an interrupt (the out interrupt of the endpoint 0). It should be noted that when the MCU processes the interrupt, it must return an empty data packet with a length of 0 to the host.

The host obtains the device descriptor from the new address.

After receiving an empty response packet from the device, the host confirms that the address is allocated successfully. Then the host sends the third setup package to D12 and requests the device descriptor again. The content of this setup package is generally: 80 06 00 01 00 00 12 00. Different from the previous one, this time requires the actual descriptor length. The 12 (hexadecimal number) indicates that all 18 bytes of device descriptors are required. Because only 16 bytes can be sent at a time, this requirement must be completed twice in the program. 16 bytes for the first time and 2 bytes for the second time.

The Host reads the configuration descriptor.

After the device descriptor of 18 bytes is obtained successfully, the host sends the fourth setup package to D12, requesting that the configuration descriptor of the device be obtained. The data in this setup package is: 80 06 00 02 00 00 09 00. 09 indicates that the device returns 9 bytes of data, which is the length of the configuration descriptor.

Read the descriptor set.

After a nine-byte configuration descriptor is obtained, the host sends the fifth setup package to D12, which requires the collection of the configuration descriptor, interface descriptor, and Endpoint descriptor of the device. The content of this setup package is: 80 06 00 02 00 00 ff 00. Because we do not know the actual length of the descriptor set, we need to get 256 bytes.

In this step, the host should have discovered new hardware and installed drivers for the new device. For the above process, the host is processed at the bus driver layer. The following step is the last step of the typical enumeration process. The device driver is required.

Numeric configuration. After the host obtains various descriptors and considers that the device information is complete, it configures the device so that the device enters the configuration state from the address State.

The host sends the sixth setup package to D12, whose data is: 00 09 01 00 00 00 00. The set configuration () function must be called in the program to process this event, allowing all endpoints to enter the working state.

Now, the USB enumeration process is complete and the device can be used properly. During this process, the D12 indicator flashes intermittently based on the communication condition.

 

The most important thing about USB is to understand how the USB Host sends commands to the device, and the device must respond to the commands of the host, the basic unit of USB communication is "packet". Understanding the concept of "packet" is the key to learning USB.
Packages are classified as follows:
They are the token package, data packet, handshake packet, and special packet (in fact, it is determined by the PID)
Token package: it can be divided into input package, output package, set 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)
Frame start package: Sync + PID + 11-bit fram + crc5 (five-digit verification code)
Data Packets: data packets are divided into data0 and data1. when data is sent by USB, when the length of data sent at a time exceeds the capacity of the corresponding endpoint, data packets must be divided into several packages and sent in batches, the data0 package and the data1 package are sent alternately. 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 in data0 format:
Sync + PID + 0 ~ 1023 bytes + crc16
Handshake package: the simplest package with the following format
Sync + PID
The following example illustrates the communication process of USB:
1: The host wants to transmit a string of data to the device. The process is as follows:
(1) The host sends a "token package" to the slave. the type of the token package is the output package, indicating that the host will send data to the slave.
(2) After the host sends a token to the slave, the USB processor sets the interrupt Status Register mark based on the token sent, the slave CPU queries the interrupt Status Register of the USB processor to respond to the host's token package.
(3) Identify the interrupt type from the host, and prepare to receive data from the host.
(4) After the slave is ready, the host starts to send "data packets". Then, the USB processor automatically places the data sent from the master in its internal buffer zone, after receiving this packet, the slave sends the "response packet" to the host"
This is a complete communication process.
From the above we can see that if USB wants to transmit data, the host must first send an in or out token packet and then send the data0 or data1 packet.
Simply describe it with real-life events: if the boss wants employees to do one thing, the boss will first issue a command to tell them what to do. After the employee is ready, the boss then issues the funds for the incident to the employees. When the employees check the funds and find the number is correct, he will send a response to the boss and tell the boss, the money has been received and the amount is correct.
What the boss wants employees to do: corresponding to the token package in USB communication.
The money the boss wants to pay: corresponding to the data packets in USB communication.
Employee's response to the boss: corresponding to the handshake package in the USB communication.
Note the following:
When a USB Host sends a token packet to a device, the receiving token is completed by a USB device instead of a slave CPU. For example, the host sends the following token:
Sync + PID + ADDR + endp + crc5
The USB device determines the type of Token Based on the PID type and determines whether the token communicates with itself based on the ADDR value. The USB device determines which endpoint is communicated with based on the endp value, check whether data transmission is correct. Based on the above token package information, the USB device places the interrupt Status Register in the corresponding position. The slave CPU can query this interrupt Status Register for corresponding operations.

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.