From: http://blog.csdn.net/hgd_dingjun/archive/2007/08/17/1747864.aspx
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:
1. Summary
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 message to the device.
No.: Bus reset. The bus reset generates an interruption, and the D12 device is enabled at the default address 0, so that the address 0 is used to transmit commands and data in the next enumeration process, while the bus reset of the interrupt register
Bit 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.
2. Steps
[Phase 1: Send standard request packets from address 0]
The specific process is: the host sends the first setup package to D12. Each setup package is 8 bytes. The content of the first get descriptor package is 80 06.
00 01 00 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 D12 interrupt register stores the interrupt type: The out interrupt of the endpoint 0, that is, the value of the interrupt register byte 1 should be 0x01) after entering the interrupt service program, because the buffer of the D12 endpoint 0 is only
16 bytes, so 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.
[Phase 2, 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 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. D12 receives the setup package and generates
Disconnection (Out interruption of the endpoint 0). It is important to note that when the MCU processes this interruption, it must return an empty data packet with a length of 0 to the host.
Phase 3: 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. In this setup package
General capacity: 80 06 00 01 00 00 12 00
. Different from the previous one, this request
The actual descriptor length, where 12 (hexadecimal number) indicates that all 18 bytes are required.
Device descriptor. Because only 16 bytes can be sent at a time
This requirement is fulfilled in two times. 16 bytes for the first time and 2 bytes for the second time.
[Phase 4: Read 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.
[Phase 5: Read a series of Descriptor sets]
At this stage, you can read n multiple descriptors. If you know the device side, that is, how to write the 68013 firmware, this step is skipped. Enter the transmission phase :)
After obtaining a nine-byte configuration descriptor, 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 (excluding string Descriptor and so on ).
). 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.
.
Note: The original text is incorrect. The actual length is known, that is, after the last stage, that is, after the fourth stage, the configuration descriptor structure obtained is as follows:
// Standard configuration Descriptor
_ Packed typedef struct
{
Byte blength;
Byte bdescriptortype;
Word16 wtotallength;
Byte bnuminterfaces;
Byte bconfigurationvalue;
Byte iconfiguration;
Byte bmattributes;
Byte bmaxpower;
} Config_descriptor;
The wtotallength member is the total length of data to be obtained in Phase 5.
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.
[Phase 6, configuration]
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)
Quantity
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 is greater than the capacity of the corresponding endpoint, data packets must be divided into several packages and divided into wholesale packages.
Send. 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. But there are also exceptions, in synchronous transmission (four types of transmission
), 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.
Simple Description with real-life events: the boss wants employees to do one thing, and the boss
First, an order will be issued to tell the employee what to do. After the employee is ready, the boss will issue the funds to the employee. When the employee makes a check of the funds, the number is correct, he will give the boss a response
Inform the boss that the money has been received and the number 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
USB
The device determines the type of Token Based on the PID type.
Determine whether to communicate with yourself based on the value of ADDR, and determine which endpoint to communicate with based on the value of endp, and check whether data transmission is correct based on verification. Based on the above token Packet
The USB device will position the interrupt Status Register. The CPU can query the interrupt Status Register to perform corresponding operations.
In addition:
When writing the host driver, the control transmission is divided into three phases: ① the establishment phase. ② Data stage. ③ Confirmation stage. The setup phase is initiated by the USB host. It is a setup packet that contains
Some data request commands and some data. If the establishment phase is an input request, data is input in the data phase. If the establishment phase is an output request, data is output in the data phase. If
Segment, even if you do not need to transmit data, you need to send a zero-length data packet. After the data phase, it is the validation phase. The validation phase is the opposite of the Data phase. If it is an input request, it is an output data packet; if it is an output request, it is an input data packet. The validation phase is used to confirm the correct transmission of data.
If the number of data packets sent during control transmission is 0, the data packet stage must be skipped.