Quick USB Learning

Source: Internet
Author: User

Quick USB Learning (Single Chip Microcomputer Application)
(Prepared by flashman, qq28479691. If you want to reprint it, please specify the source)

USB
I believe everyone knows the advantages of this technology, and it is still very profitable. Hey hey, let's not talk about it here. If you don't understand it, go and read the book. Here we will talk about how to quickly master the USB technology. In this article, I am studying USB
On the other hand, it is to facilitate beginners to get started. I have read many books about USB before, but most of the books are unclear,
The reason is that the editor has not considered the problem from the perspective of beginners. The book to be recommended here is "computer USB system principle and Master/Slave design" compiled by Ma Wei. the book systematically presents the USB
Principle System.
Before learning, you should first understand the important keywords of USB. These keywords will also appear in the following learning:
1. endpoint: a data buffer zone located on a USB device or host. It is used to store and send various USB data. Each endpoint has a unique fixed address, has different transmission features (such as input, output, configuration, and batch transmission)
2. frame: the concept of time. In USB, a frame is 1 ms. It is an independent unit and contains a series of bus actions. USB divides one frame into several parts, each part is a USB transmission action.
3. uplink and downlink: The device goes upstream to the host, and the host goes downstream to the device.

Let's start learning in the form of a question and answer.

Question 1: What is the structure of the USB transmission line?
Answer:
Case 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 the voltage of 3.3v (note that, with the 5 V level of CMOS
Different), and the power cord and ground wire can provide the device with a 5 V voltage and a maximum current of MA (which can be set in programming, as for the hardware implementation mechanism, do not care about it ).

Question 2: How data is 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:
Case 3: USB uses 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 maintained. To ensure the accuracy of the signal sending, when
When the packet is sent, the transmission device needs to insert a bit (that is, insert a 0 value for every six consecutive values in the data stream) to force 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, packet, 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-bit, with a fixed value of 0000 0001, used for local clock synchronization with input
2. The ID domain (PID) is composed of four identifiers and four identifiers. It indicates the package type and format. This is a very important part. It can be calculated here, there are 16 USB identification codes. For details, 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 structures of different packages are different.

1. Token package: 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, instead of data)
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-digit verification code)

2,
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

3. Handshake package: the simplest package with the following format
Sync + PID

(Note that each of the above packages has different types. A total of 10 packages are defined in USB1.1. For details, see question 5)

(3) Transactions: There are three major transactions: In transaction, out transaction, and setup transaction. each transaction is composed of three stages: token package, data packet, and handshake packet, here, the phase is used because the packet sending has a certain time sequence. The three phases of the transaction are as follows:
1. Token package stage: Start an input, output, or set transaction
2. Data Packet phase: send data according to input and output
3. Handshake package stage: return the data receiving status. This stage is not in the in and out transactions of synchronous transmission. This is special.
The three types of transactions are as follows (one transaction is described in three phases below ):
1. In transactions:
Token package stage-the host sends an in-based input package to the device, notifying the device to send data to the host;
Data Packet phase-the device will make three responses based on the situation (Note: The data packet phase does not always send data, but also enters 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 (data exchange between data0 and data1 );
2) the device is busy and cannot send an invalid Nak packet to the host. The in transaction ends before the next in transaction continues;
3) the device endpoint is disabled. When an error packet stall is sent, the transaction is terminated and the bus enters idle state.
Handshake package phase-the host sends an ACK packet to the device after correctly receiving the data.

2. Out transaction:
Token package stage-the host sends an output packet with a PID of out to the device, notifying the device to receive data;
Data Packet phase-relatively simple, that is, the host sends data to the device, and data0 and data1 Alternate
Handshake package phase-the device will respond to the situation in three ways
1) if the device endpoint receives the correct message, the device returns an ACK to the incoming host to notify the host to send new data. If a CRC check error occurs in the data packet, no handshake information is returned;
2) when the device is busy, it cannot send an invalid Nak packet to the host, notifying the host to send data again;
3) the device endpoint is disabled. When an error packet stall is sent, the transaction ends before it expires, and the bus enters the idle state.

3. setut transaction:
Token package phase-the host sends an output packet with the PID setup to the device, notifying the device to receive data;
Data Packet phase-relatively simple, that is, the host sends data to the device. Note that there is only one 8-byte data0 packet, these 8 bytes of content are the standard USB device request commands (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 a transfer consisting of an in or out transaction after the setup transaction)

(4) transmission: Transmission consists of transactions in the out, in, and setup transactions. There are four types of transmission: interrupted transmission, batch transmission, synchronous transmission, and control transmission, the structure of interrupted transmission is the same as that of Batch Transfer. synchronous transmission has the simplest structure, and control transmission is the most important and complex transmission.
1. interrupted transmission: it consists of an out transaction and an in transaction. It is used for data transmission between the keyboard, mouse, and other hid devices.
2. batch transmission: it consists of an out transaction and an in transaction. It is used for large data transmission with no fixed transmission rate and no bandwidth occupation. When the bus is busy, USB preferentially transmits data of other types, and temporarily stops Batch Transfer.
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 data0
4,
Control Transmission: the most important thing is the most complex transmission. Control Transmission consists of three stages (initial setup stage, optional data stage, and status information step ), each stage can be considered as a transmission, that is, control
Transmission is actually composed of three transmissions. After a USB device is attached to a host for the first time, the host exchanges information, device addresses, and read device descriptors through transmission control, allows the host to identify the device and install
Install the corresponding driver, which is a concern of every USB developer.
1. Initial setup step: A transmission composed of set transactions
2. optional data step: 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. State information step: as the name suggests, this step is to obtain the state information, which is transmitted by an in or out transaction, however, note that the in and out transactions are different from the 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 to combine with the optional data steps;
2) in this step, the data packet in the data packet phase is of 0 length, that is, Sync + PID + crc16
Except for the differences between the above two points, the other points are the same.

(THINKING: How should we set these transmission modes in actual operations ?)

Question 5: What are the identification codes?
Answer:
Case 5: As mentioned above, the identification code consists of four digits. Therefore, it can represent sixteen types of identification codes. In the USB1.1 specification, only ten types of identification codes are used, USB2.0 uses sixteen identification codes
The role of code recognition is to describe the attributes of the package. The ID code is 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 (for details, see
Question 7). There are 16 identification codes:
The token package 0x01 output (out) starts a transmission from the host to the device and contains the device address and label.
0x09 input (in) starts a transmission from the device to the host, and contains the device address and label
0x05 frame start (SOF) indicates the start of a frame and contains the corresponding frame number.
0x0d setup starts a control transmission for the host to initialize the device
Data Packet 0x03 even data packet (data0 ),
0x0b odd data packet (data1)
Handshake packet 0x02 confirm to receive the correct packet (ACK)
0x0a is invalid. The received (sent) message is busy but cannot be received (sent) information.
0x0e error. The endpoint is forbidden or cannot control MPs queue requests.
Special packet 0xoc leading, 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 in to the host, the host uses a series of actions to enumerate the configuration of the device (Here we use enumeration, I always think it is a bit strange, but many books say this, in addition, the configuration is an enumeration State, and the State indicates the temporary State). These States are as follows:
1. Access state (attached): After a device is connected to a host, the host detects the access of the device by detecting the level changes on the signal line;
2. Power Supply Mode (powered): refers to the power supply 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 phase (by the maximum value required in the data, can be set through programming)
3. Default Mode: Before USB is configured, it communicates with the host through the default address 0;
4. Address: After configuration, after the USB device is reset, it can communicate with the host based on the unique address assigned to it by the host;
5. configured: uses various standard USB request commands to obtain various information about the device and modify or set the information about the device.
6. suspended state: the bus power supply device has no bus operation within 3 ms, that is, if the USB Bus is idle, the device will automatically enter the suspended state, the total current power consumption cannot exceed 280ua.

Question 7: What is the standard USB device Request command mentioned in answer 4?
Answer:
Case 7: The standard USB device Request command is used to control the data packet phase in the "Initial setup step" of transmission (that is, data0, consisting of eight bytes ), please refer to the content in question 4. Standard USB settings
There are a total of 11 backup request commands, which are 8 bytes in size and have the same structure and are composed of five fields (note that the fields and fields are essentially the same, it is composed of binary strings.
The number in the brackets indicates the number of bytes. The first letter is BM, B, and W, indicating the bitmap, byte, and 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 request commands
= 10 user-defined commands
= 11 reserved value
D4d3d2d1d0 = 00000 the receiver is a device
= 00001 the recipient is a device
= 00010 the receiver is the endpoint
= 00011 the recipient is another receiver
= Other values are retained.
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 that the command code here must be consistent
When other fields are used in combination, it can be said that the command code is the core of the Standard Request command code, because these command codes determine 11 USB standard request commands ):
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 features of the command recipient
3) 5 set_address: used to allocate addresses to devices
4) 6 get_descriptor: used by the host to obtain the specific descriptor of the device.
5) 7 set_descriptor: Modify the descriptor related to the device, or add a new descriptor.
6) 8. get_configuration: used to obtain the configuration value of the current device on the host (note the same as above)
7) 9 set_configuration: used to indicate the required configuration of the device.
8) 10 get_interface: used to obtain the ID of an interface descriptor.
9) 11 set_interface: used by the host to require the device to use a descriptor to describe the interface
10) 12 synch_frame: Used to set and report the Synchronization Frame of an endpoint.
The above 11 commands are really as long as a piece of cloth. Please read the books. I will not talk about it here. controlling transmission is the focus of USB, these 11 commands are the focus of control over transmission, so these 11 commands are the top priority. This is clear, and USB is even a beginner.

Question 8: In standard USB request commands, we often see descriptor. What is this?
Back
A 8: descriptor is a complete data structure. It can be programmed and stored in a USB device by C language. It is used to describe all attributes of a USB device.
USB hosts require devices to send this information through a series of commands. It is used to pass information to the host through command operations in the Q & A section, so that the host can know what functions and attributes the device has.
Which type of device is used, how much bandwidth is used, what type of transmission mode is used, and the size of the data volume? Only after the host determines the information can the device start to work, so the descriptor is also a very important part.
Good understanding. 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 descriptors above. A device has only one device descriptor, while a device descriptor can contain multiple configuration descriptors, and a configuration descriptor can contain multiple interface descriptors, when an interface uses several endpoints, there are several endpoints descriptor. 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, 0x12
Byte bdescriptortype; // descriptor type number, 0x01
Word bcdusb; // USB Version
Byte bdeviceclass; // The device code assigned by USB, 0x01 ~ 0xfe is a standard device type, and 0xff is a vendor-defined type.
// 0x00 is not defined in the device descriptor, such as hid
Byte bdevicesubclass; // The Sub-class code assigned by USB. Same as above, the value is specified and allocated by USB.
Byte bdeviceprotocl; // protocol code of the device allocated by USB, same as above
Byte bmaxpacketsize0; // The maximum package size of the endpoint 0
Word idvendor; // vendor ID
Word idproduct; // product ID
Word bcddevice; // device factory id
Byte imanufacturer; // The index that describes the vendor string
Byte iproduct; // index that describes the product string
Byte iserialnumber; // index that describes the device serial number string
Byte bnumconfiguration; // The number of possible configurations

}

2. Configuration Descriptor
Struct _ configuration_descriptor_struct
{
Byte blength; // The number of bytes of the device descriptor, 0x12
Byte bdescriptortype; // descriptor type number, 0x01
Word wtotallength; // configure the size of all the quantities returned
Byte bnuminterface; // number of interfaces supported by this configuration
Byte bconfigurationvale; // parameter value required by the set_configuration command
Byte iconfiguration; // the index value of the string that describes the configuration
Byte bmattribute; // Power Supply Mode Selection
Byte maxpower; // 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, 0x12
Byte bdescriptortype; // descriptor type number, 0x01
Byte somedescriptor [36]; // unicode encoded string
}

4. Interface Descriptor
Struct _ interface_descriptor_struct
{
Byte blength; // The number of bytes of the device descriptor, 0x12
Byte bdescriptortype; // descriptor type number, 0x01
Byte binterfacenunber; // Interface ID
Byte balternatesetting; // backup 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; // protocol followed by the interface
Byte iinterface; // string index value that describes this interface
}
5. endpoint Descriptor
Struct _ endpoin_descriptor_struct
{
Byte blength; // The number of bytes of the device descriptor, 0x12
Byte bdescriptortype; // descriptor type number, 0x01
Byte bendpointaddress; // endpoint address and input/output attributes
Byte bmattribute; // transmission type attribute of the endpoint
Word wmaxpacketsize; // The maximum package size received and sent by the endpoint
Byte binterval; // interval at which the host queries the endpoint
}

After understanding the above eight problems, you can go to the next step of USB learning.

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.