USB Basic Knowledge

Source: Internet
Author: User
Tags ack

Important key words for USB:

1, endpoint: A data buffer located on the USB device or host, used to store and send various USB data, each endpoint has a unique address, there are different transmission characteristics (such as input endpoint, output endpoint, configuration endpoint, bulk transfer endpoint)

2, Frame: Time concept, in the USB, a frame is 1MS, it is a separate unit, contains a series of bus action, USB 1 frames are divided into several, each part is a USB transmission action.

3, upstream, downstream: The device to the host for the upstream, the host to the device for the downstream

Let's start with a question and answer form.

Question one: What is the structure of the USB transmission line?

Answer one: A USB transmission line is composed of ground, power cord, d+, D-Four lines, d+ and D-is a differential input line, it uses 3.3V voltage (note Oh, unlike CMOS 5V level), and the power and ground can provide the device with 5 V voltage, The maximum current is 500MA (can be set in the programming, as for the hardware implementation mechanism, do not care about it).

Question two: How the data is transmitted within the USB transmission line

Answer two: The data in the USB cable transmission is from low to high send.

Question three: the USB encoding scheme?

Answer three: USB uses the non-zero inversion to transmit data, when the transmission line of differential data input 0 o'clock on the counter, input 1 o'clock to maintain the original value, in order to ensure the accuracy of the signal transmission, when a packet on the USB bus, the transfer device will be bit inserted * * * (i.e., inserting a 0 after each 6 consecutive 1 in the data stream), forcing the NRZI code to change. That's OK, these are handled by specialized hardware.

Question four: What is the data format of USB?

Answer four: As with the other, USB data is made up of binary digital strings, the first number of numbers to form a domain (there are seven), the domain to form the package, the package to form the transaction (in, out, SETUP), the transaction finally constitutes the transmission (interrupt transmission, parallel transmission, bulk transmission and control transmission). The following is a brief introduction to domains, packages, transactions, transports, and note the relationship between them.

(a) domain: is the smallest unit of USB data, composed of several bits (as to how many bits are determined by the specific domain), the domain can be divided into seven types:

1, synchronization domain (sync), eight bit, value fixed to 0000 0001, for local clock and input synchronization

2, identification domain (PID), consists of four-bit identifier + four-bit identifier anti-code, indicating the type and format of the package, this is a very important part, here can be calculated, the USB identification code has 16 kinds, the specific classification please see question five.

3, address domain (ADDR): seven-bit address, which represents the address of the device on the host, address 000 0000 is named 0 address, is the first time any device connected to the host, in the host configuration, the default address before enumeration, so you can know why a USB host can only connect 127 devices reason.

4, endpoint domain (ENDP), four-bit, it is possible that a USB device has a maximum number of endpoints of 16.

5, Frame Number field (FRAM), 11 bits, each frame has a specific frame number, frame number domain maximum capacity 0x800, for synchronous transmission is important (synchronous transmission is one of four transmission types, see below).

6, Data: Length is 0~1023 bytes, in different transport types, the data field length is different, but must be the length of the whole number of bytes

7, check the domain (CRC): The token package and the packet (for the classification of the package please see below) The non-PID domain to verify a method, CRC check in the communication application is very broad, is a good calibration method, as to the specific calibration method here is not much to say, please consult the relevant information, It is important to note that the division of the CRC is a modulo 2 operation, different from the division in the 10 binary.

(b) Package: There are four types of packets consisting of the domain, namely token package, packet, handshake and special package, the first three are important packages, different package domain structure is different, introduced as follows

1, token package: can be divided into input package, output package, set package and frame start package (Note that the input package here is used to set the input command, the output package is used to set the output command, rather than the number of data)

The format of the input package, output package, and provisioning package is the same:

SYNC+PID+ADDR+ENDP+CRC5 (five-bit check code)

(The above abbreviation explanation please see above domain introduction, PID code's specific definition see question five)

Format of the Frame starter package:

Sync+pid+11 bit FRAM+CRC5 (five-bit check code)

2, packet: Divided into DATA0 package and DATA1 package, when the USB send data, when the length of the data sent more than the corresponding endpoint capacity, you need to divide the packet into several packets, sent in batches, DATA0 packet and DATA1 packet alternately sent, that is, if the first packet is DATA0, The second packet is DATA1. However, there are exceptions, in synchronous transmissions (one of the four types of transport), all packets are DATA0, in the following format:

sync+pid+0~1023 bytes +crc16

3, handshake Package: the most simple structure of the package, the format is as follows

Sync+pid

(Note that each package has a different type, USB1.1 a total of 10 kinds of packages, see question five)

(c) Transactions: There are in the transaction, out transaction and Setup transaction three major transactions, each transaction consists of token package, packet, handshake package Three stages, where the use of the stage means that because these packets are sent in a certain chronological order, the three stages of the transaction are as follows:

1. Token package phase: Start a transaction for input, output, or Setup

2. Packet stage: Send the corresponding data by input and output

3, Handshake packet Stage: The return of data reception, in the synchronous transmission in and out transactions do not have this stage, this is more special.

The three types of transactions are as follows (the following are three stages to illustrate a transaction):

1. In transaction:

Token package phase-the host sends a PID input packet to the device, notifying the device to send data to the host;

Packet phase--the device will react three depending on the situation (note: The packet stage also does not always transmit data, depending on the transmission situation will also advance into the handshake packet stage)

1) The device endpoint is normal, the device sends the packet into the host (DATA0 and DATA1 alternately);

2) The device is busy, unable to send a packet to the host sent Nak invalid packet, in the transaction early end, to the next in transaction to continue;

3) The corresponding device endpoint is forbidden, send the error packet stall packet, the transaction will end prematurely, the bus enters the idle state.

Handshake packet Phase-the host sends an ACK packet to the device after it receives the data correctly.

2. Out transaction:

Token package phase-the host sends a PID output packet to the device, notifying the device to receive data;

Packet phase-relatively simple, is the main opportunity device to send data, DATA0 and DATA1 alternating

Handshake Package Phase--the equipment will react according to three kinds of conditions

1) The device endpoint receives correctly, the device returns an ACK to the master, notifies the host to send new data, and if the packet has a CRC checksum error, no handshake information will be returned;

2) The device is busy, unable to send packets to the host sent Nak invalid packet, notify the host to send data again;

3) The corresponding device endpoint is forbidden, send the error packet stall packet, the transaction ends prematurely, the bus goes directly to the idle state.

3. Setut Affairs:

Token package phase-the host sends a PID output packet to the device, notifying the device to receive data;

Packet phase-simple, is the main opportunity device to send data, note that there is only a fixed 8-byte DATA0 package, this 8 bytes of content is the standard USB device request command (a total of 11, see question Seven)

Handshake Package Phase-when the device receives command information from the host, it returns an ACK, and then the bus enters an idle state and prepares the next transmission (usually a transport consisting of an in or out transaction after the setup transaction)

(iv) Transmission: Transport by the Out, in, setup transactions in which the transaction consists of four types of transmission, interrupt transmission, batch transmission, synchronous transmission, control transmission, in which interrupt transmission and bulk transfer structure, synchronous transmission has the simplest structure, and control transmission is the most important and most complex transmission.

1, interrupt transmission: From the out transaction and in transactions, for the keyboard, mouse and other HID device data transmission

2, Bulk transmission: By the out transaction and in transaction composition, for the large-capacity data transmission, no fixed transmission rate, also does not occupy the bandwidth, when the bus is busy, the USB will prioritize other types of data transmission, and temporarily stop the bulk transfer.

3. Synchronous transmission: Consists of out transactions and in transactions, there are two special places, first, there is no return packet phase in the in and out transactions of synchronous transmission; second, all packets in the packet phase are DATA0

4, Control transmission: The most important is also the most complex transmission, control transmission consists of three stages (initial setup stage, optional data stage, state information step), each stage can be regarded as a transmission, that is, the control transmission is actually composed of three transmission, used in the first USB device added to the host, The host can control the transmission to exchange information, device address and read the device descriptor, so that the host to identify the device and install the appropriate driver, which is a concern for every USB developer.

1. Initial setup steps: A transport consisting of a set transaction

2. Optional data step: is a transport consisting of in or out transaction, this step is optional, to see whether the initial setup step requires read/write data (determined by standard request command sent by the packet phase of the set transaction)

3. Status information step: As the name implies, this step is to get state information, composed of in or out transaction composition of the transmission, but note here in and out transactions and before the int and out transactions have two points different:

1) transmission in the opposite direction, usually in indicates that the device sends data to the host, out indicates that the host sends data to the device, where in indicates that the host sends data to the device, and out indicates that the device sends data to the host, which is to combine with optional data steps;

2) In this step, packets in the packet phase are 0 lengths, i.e. SYNC+PID+CRC16

In addition to the above two points there is a difference, the other is the same, here is not much to say

(think: How should these transfer modes be set in real-world? )

Question five: What is the identification code?

Answer five: As the above-mentioned identification code consists of four bits of data, so can represent 16 kinds of identification code, in the USB1.1 specification, only 10 kinds of identification code, USB2.0 use 16 kinds of identification code, the function of the identification code is used to explain the properties of the package, the identification code is associated with the package, First of all, a brief introduction of the type of packet, packet is divided into tokens, data, handshake packets and special package four (see question seven for specific classification), the identification code has the following 16 kinds:

Token Package:

The 0x01 output (out) initiates a direction for the host-to-device transmission, and contains the device address and label

The 0x09 input (in) initiates a device-to-host transmission and includes the device address and label

0x05 frame Start (SOF) represents the beginning of a frame and contains the corresponding frame number

0x0d Setup (Setup) initiates a control transfer for the host to initialize the device

Data package:

0x03 even packets (DATA0),

0X0B-Odd Packet (DATA1)

Handshake Package:

0x02 Confirm receipt of error-correct packets (ACK)

0x0a invalid, receive (send) the correct busy to receive (send) information

0X0E error, endpoint is forbidden or does not support control pipeline request

Special packet 0x0C preamble, data transfer for low-speed devices that initiate downstream ports

Question six: How does the USB host identify the USB device?

Answer six: When the USB device plug in the host, the host through a series of actions to enumerate the device configuration (configuration is a state of the enumeration, state represents a temporary state), which are as follows:

1, Access State (attached): equipment access to the host, the host through the detection signal line level change to discover the device access;

2, the supply State (Powered): is to power the equipment, divided into the device when the default power supply value, after the configuration phase of the power supply value (according to the maximum value of data, programmable settings)

3, default: USB before being configured, through the default address 0 to communicate with the host;

4, Address: After the configuration, the USB device is reset, it can be assigned to the host by its unique address to communicate with the host, this state is the address State;

5, configuration (configured): Through a variety of standard USB request commands to obtain a variety of device information, and the device of a certain information to change or set.

6, suspended state (Suspended): Bus power supply equipment in the 3MS without bus * * *, that is, the USB bus is idle, the device will automatically enter the suspended state, after entering the suspended state, the total current consumption of not more than 280UA.

Question seven: What exactly is the standard USB device request command that was mentioned in answer four just now?

Answer seven: The standard USB device request command is used in the control transmission in the "Initial setup step" in the packet phase (that is, DATA0, composed of eight bytes), please look back to the content of question and answer four. The standard USB device request command has a total of 11, the size is 8 bytes, has the same structure, consists of 5 fields (the field is the data part of the standard request command), the structure is as follows (the number in parentheses represents the number of bytes, the first letter bm,b,w the bitmap, the byte, the Double byte):

Bmrequesttype (1) +brequest (1) +wvalue (2) +windex (2) +wlength (2)

The meanings of each field are as follows:

1, bmrequesttype:d7d6d5d4d3d2d1d0

D7=0 Host to Device

=1 equipment to the host;

d6d5=00 Standard Request Command

= 01 Class Request command

=10 user-defined commands

=11 Reserved values

d4d3d2d1d0=00000 Receiver for device

=00001 Receiver for device

=00010 Receiver for Endpoint

=00011 Receiver for other recipients

= Other values Reserved

2, brequest: Request command code, in the standard USB command, each command has defined the number, the value of the number is the value of the field, the number and command name is as follows (note that the command code here is used in conjunction with other fields, it can be said that the command code is the core of the standard request command code, It is because of these command codes that the 11 USB standard request commands are determined:

0) 0 Get_status: Used to return the status of a specific recipient

1) 1 Clear_feature: To remove or prohibit certain features of the receiver

2) 3 Set_feature: Some features to enable or activate the command receiver

3) 5 set_address: Used to assign an address to the device

4) 6 Get_des Criptor: Specific descriptor for host acquisition device

5) 7 Set_des criptor: Modify the relevant descriptor in the device, or add a new descriptor

6) 8 Get_configuration: The configuration value of the current device for the host acquisition device (note the difference above)

7) 9 set_configuration: Configuration for the requirements used by the host to indicate the device

8) Get_interface: Used to get the current interface descriptor number

9) One set_interface: For the host requires the device to describe the interface with a descriptor

Synch_frame: Synchronization frame for device setup and reporting of one endpoint

The above 11 commands to understand really have a piece of cloth so long, please go to read it, here is not much to say, control transmission is the center of gravity of the USB, and these 11 commands is the center of gravity of the control transmission, so these 11 commands is the weight of the heavy, this makes clear, USB even is the introduction.

Question eight: In the standard USB request command, you will often see des criptor, what is this?

Answer eight: Des Criptor is a descriptor, is a complete data structure, can be implemented by programming in the C language, and stored in a USB device to describe all the properties of a USB device, the USB host through a series of commands to request the device to send this information. Its role is to pass information such as the command in the question and answer section, so that the host knows what the device has, what kind of equipment, how much bandwidth to use, which type of transmission and the size of the data, only the host determines the information, the device can really start to work, So the descriptor is also a very important part, to be well mastered. There are 5 standard descriptors, and USB defines the numbers for these descriptors:

Device Descriptor

2--Configuration Descriptor

3--Character Descriptor

4--Interface Descriptor

5--Endpoint Descriptor

There is a certain relationship between the above descriptors, a device has only one device descriptor, and a device descriptor can contain multiple configuration descriptors, and a configuration descriptor can contain multiple interface descriptors, one interface uses several endpoints, and there are several endpoint descriptors. This descriptor is made up of a certain field, respectively, as follows:

1. Device Descriptor

struct _device_des criptor_struct

{

BYTE blength; The size of the byte number of the device descriptor, which is 0x12

BYTE Bdes Criptortype; Descriptor type number, for 0x01

WORD Bcdusb; USB Version number

BYTE Bdeviceclass; USB assigned device class code, 0X01~0XFE for standard device class, 0xFF for vendor custom type

0X00 is not defined in the device descriptor, such as HID

BYTE Bdevicesubclass; USB assigned subclass code, IBID., value specified by USB and assigned by

BYTE Bdeviceprotocl; USB-assigned Device protocol code, IBID.

BYTE bMaxPacketSize0; The maximum package size for endpoint 0

WORD Idvendor; Vendor number

WORD idproduct; Product number

WORD Bcddevice; Equipment Factory Number

BYTE Imanufacturer; Index describing the vendor string

BYTE iproduct; Index describing the product string

BYTE Iserialnumber; Index describing the device serial number string

BYTE bnumconfiguration; Number of possible configurations

}

2. Configuration Descriptor

struct _configuration_des criptor_struct

{

BYTE blength; The size of the byte number of the device descriptor, which is 0x12

BYTE Bdes Criptortype; Descriptor type number, for 0x01

WORD wtotallength; The size of all the quantities returned by the configuration

BYTE Bnuminterface; The number of interfaces supported by this configuration

BYTE Bconfigurationvale; parameter values required by the set_configuration command

BYTE iconfiguration; The index value of the string that describes the configuration

BYTE Bmattribute; Selection of power supply modes

BYTE Maxpower; Maximum current that the device extracts from the bus

}

3. Character Descriptor

struct _string_des criptor_struct

{

BYTE blength; The size of the byte number of the device descriptor, which is 0x12

BYTE Bdes Criptortype; Descriptor type number, for 0x01

BYTE Somedes criptor[36]; Unicode-encoded strings

}

4. Interface Descriptor

struct _interface_des criptor_struct

{

BYTE blength; The size of the byte number of the device descriptor, which is 0x12

BYTE Bdes Criptortype; Descriptor type number, for 0x01

BYTE Binterfacenunber; The number of the interface

BYTE balternatesetting;//Alternate Interface Descriptor number

BYTE bnumendpoints; This interface uses end points, not including endpoint 0

BYTE Binterfaceclass; Interface type

BYTE binterfacesubclass;//Interface Sub-type

The protocol that is followed by the BYTE binterfaceprotocol;//interface

BYTE IInterface; A string index value that describes the interface

}

5. Endpoint Descriptor

struct _endpoin_des criptor_struct

{

BYTE blength; The size of the byte number of the device descriptor, which is 0x12

BYTE Bdes Criptortype; Descriptor type number, for 0x01

BYTE bendpointaddress; Endpoint address and input and output properties

BYTE Bmattribute; Transport type properties for endpoints

WORD wmaxpacketsize; The maximum packet size of the end registration, the hair

BYTE Binterval; Time interval for host query endpoint

}

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


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

(GO) USB basics

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.