Basic knowledge about USB

Source: Internet
Author: User

Important USB keywords:

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 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 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 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, 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, batch transmission: The data0 package and the data1 package are sent alternately. That is, if the first packet is data0, the second 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

 

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 regarded as a transmission, that is to say, the control transmission is actually composed of three transmissions. It is used to exchange information through control transmission after the USB device is first added to the host, device address and device descriptor, so that the host identifies the device and installs the corresponding driverProgramThis is a concern for 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)Opposite Transmission DirectionGenerally, 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 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 *** work ?)

 

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 identification codes:

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 Packets:

0x03 even data packets (data0 ),

0x0b odd data packet (data1)

Handshake package:

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 0x0c 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 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: 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)

 

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 commandCodeIn 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 should be used with other fields. It can be said that the command code is the core of the Standard Request command code, 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_des criptor: used by the host to obtain the specific descriptor of the device.

5) 7 set_des criptor: 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 des criptor. What is this?

Answer 8: des criptor, a descriptor, is a complete data structure that can be programmed and stored in a USB device by C language. It describes 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 -- 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_des criptor_struct

{

Byte blength; // The number of bytes of the device descriptor, 0x12

Byte BDEs criptortype; // 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_des criptor_struct

{

Byte blength; // The number of bytes of the device descriptor, 0x12

Byte BDEs criptortype; // 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_des criptor_struct

{

Byte blength; // The number of bytes of the device descriptor, 0x12

Byte BDEs criptortype; // descriptor type number, 0x01

Byte somedes criptor [36]; // unicode encoded string

}

 

4. Interface Descriptor

Struct _ interface_des criptor_struct

{

Byte blength; // The number of bytes of the device descriptor, 0x12

Byte BDEs criptortype; // 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_des criptor_struct

{

Byte blength; // The number of bytes of the device descriptor, 0x12

Byte BDEs criptortype; // 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.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/menuconfig/archive/2009/07/13/4344915.aspx

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.