[USB] key concepts of USB

Source: Internet
Author: User

I. Advantages of USB Interface

Simple, fast, support for plug-and-play and hot swapping

2. USB communication

Host is the core of USB communication, and the common USB host is PC. Any USB data transmission must be initiated and controlled by the host. All USB peripherals can only be connected to the host. direct communication between any two peripherals or between the two hosts is not allowed. Therefore, the functions of the USB host and USB device are different.

USB Host features:

(1) provide power to peripherals through USB Interface

(2) detect and configure devices (that is, device enumeration ). For example, it must detect the connection and removal of a device, understand the features of the device, and assign an address to the device.

(3) check errors and manage data transmission. These are guaranteed by the USB interface hardware and do not need to be programmed.

(4) exchange data with peripherals Based on the set transmission mode.

USB devices have the following functions:

(1) power management. The device can be powered by a USB interface or its own power supply. The device should be suspended for low power consumption after the USB interface has no communication function for more than 3 ms.

(2) detect communications. Each device must check whether the address in the communication information package matches the address of the device. If the address does not match, the device will ignore the communication, which is automatically handled by the USB interface hardware. When the device connects to the USB interface at the beginning, use the fixed default address 0. Then, the USB host will assign an address to the device during the detection phase, and subsequent communication will follow this address.

(3) Check communication data errors. It is guaranteed by the USB interface hardware and does not need to be programmed.

(4) respond to the request. After the host detects the device connection, it sends the corresponding device request according to the USB protocol to understand the device type and capability, and configures the device (such as setting the address and configuration descriptor ), the device should be able to respond to these requests and return corresponding response data.

(5) exchange data with the Host Based on the set transmission mode.


Iii. data transmission between a USB host and a USB device currently, the USB Protocol specifies four data transmission modes:

(1) control transmission. It is mainly used for device detection and configuration by hosts.

(2) interrupted transmission. It is used to support devices that occasionally require data communication but have limited service time. Interrupted transmission is often used on devices such as keyboards, mouse, and game poles.

(3) batch transmission. It is suitable for scenarios where time is not important. Batch transmission can transmit a large amount of data without blocking the bus, because it will first execute other types of transmission to wait for the transmission time, such as for disk operations.

(4) synchronous transmission. It is suitable for transmission at a fixed rate and can tolerate occasional errors, such as real-time voice transmission.


4. device endpoints

Any data transmission is transmitted to an endpoint of a USB device (specifically a USB interface device) or sent by an endpoint of a USB device.

The endpoint can be simply understood as a cache in the USB interface device and used as a data buffer. It is managed by the corresponding control registers and status registers.

The device endpoint stores the received data or the data to be sent. The host also has a buffer for receiving and transmitting data. However, the host does not define it as an endpoint, but as the starting point for communication with the device endpoint ). A USB device may have several endpoints, each of which can be set as an output or input direction and one of the control, interrupt, batch or synchronous transmission modes. Because the host detects and configures the device through the endpoint 0 at the beginning, each device must have an endpoint 0, in addition, the transmission mode must be controlled (generally supported by USB interface devices by default ). In addition, the device rarely needs other control endpoints. The USB protocol defines 11 standard request commands, which are used to detect and configure devices in control transmission mode at the endpoint 0.


5. device descriptor

A usb host requests a series of descriptors of a USB device to obtain device information. A descriptor is a defined data structure that may contain information about the entire device or a component of the device. Host request Descriptor and device response descriptor. Currently, the USB protocol defines three types of descriptors:

(1) standard type. Provides basic information about a device. Standard descriptors include device descriptors, configuration descriptors, interface descriptors, endpoint descriptors, and string descriptors.

(2) device category-specific descriptor. Provides more detailed device information. For example, the hid Descriptor and report descriptor in the category-specific descriptor of the hid class (Human-Machine Interface Class) device can be used to describe whether the device is a mouse or a keyboard. If the mouse is used, the data of the report descriptor is the mouse buttons and the shift.

(3) vendor-specific descriptors. It is also used to provide more detailed information about the device, but it is defined by the vendor, unlike the device category-specific descriptor, which is defined by the USB specification.

6. data information package format

Packet is the basic form of data organization for USB transmission. Its significance and actual content are expressed by a series of fields. Some fields have defined keywords in the USB protocol.

The fields in the information package include:

(1) The SYNC Field is used to start and synchronize information packets. It is automatically processed by hardware.

(2) PID field, information Packet Identifier (Packet Identifier, pid), information packet has four types: Token, Data, contact and special, the four types correspond to 16 PID codes.

(3) The address field specifies the device to which the USB Host communicates with. The device address is 0 by default. The host will assign an address to the device during device detection.

(4) The endpoint field is used to specify which terminal of the USB host to communicate. As mentioned above, a device can have multiple endpoints.

(5) In the frame number field, the USB Host splits the actual data transmission on the USB bus into two frames (FRAME) or micro frames (micro frame) by time ). For devices at full speed and low speed, the host divides transmission into 1 millisecond frames, and for high-speed devices, the host divides transmission into 125 microseconds of microframes. The frame number field is used to identify a specific frame or microframe, which is automatically processed by the hardware.

(6) data field, which is the data to be transmitted.

(7) Verification field, used for data verification of information packages, which is automatically processed by hardware.


There are four types of token information packages:

(1) Out, indicating that the host outputs data to the device;

(2) In indicates that the Host reads data from the device;

(3) sof indicates the start of the frame number;

(4) setup, which is used to control the transaction settings for transmission.

VII. Transactions

The USB Protocol Specification defines a transaction as "transferring a service to an endpoint". A service here refers to a host that transmits information to a device, or the host receives information from the device. Each transmission can contain one or more transactions, and each transaction can contain one, two, or three information packets, which can be understood as the basic unit of data transmission.

Most transactions contain three information packages: token information package, data information package, and contact information package. According to the pid id of the token information package, transactions are generally divided into three types: input (in) transactions, output (out) transactions, and setup transactions.

Each transmission type (control, interrupt, batch, and synchronization) contains one or more stages, and each stage contains one or more transactions. Specifically, in control transmission, it generally includes the SET transaction stage (corresponding to the SET transaction type) and Data stage (corresponding to the input or output transaction type) and the status phase (corresponding to the input or output transaction type), and the interrupted, batch, and synchronous transmission only contains the data phase.

(1) The setup phase contains a setup transaction (consisting of a token information package, data information package, and contact information package );

(2) The data stage may consist of multiple transactions, generally, each transaction is composed of a token information package, a data information package, and a contact information package (only the transaction in the data phase of synchronous transmission does not contain a contact information package );

(3) The State stage contains a transaction, which is also composed of a token information package, data information package, and contact information package. However, the content of the data information package is empty, the status phase is only used to control transmission, to indicate whether the entire control transmission is successful.

The device driver of the USB device driver is loaded when the host learns the device information from the device descriptor, it will find the most appropriate driver to manage the communication between the host and the device. When selecting the driver, Windows will try. the information in the INF file is compared with the vendor, product ID, and version number read from the device. the inf file loads the driver. If Windows cannot find the appropriate. inf file, it will display a "Add new hardware wizard" for users to specify the driver.


8. USB device design

The USB specification defines many device types, which are expressed by different device category codes and interface category codes, such:

(1) hid (Human Interface Device) device category code is 0x00, interface category code is 0x03, hid devices include keyboards, mice, and game poles;

(2) the device category code of Mass Storage (large-capacity storage device) is also 0x00, and the interface category code is 0x09, mass Storage devices include floppy disks, hard disks, optical disks, and flash disks. Other devices include displays, communication devices, and audio devices. In the USB specification, there is also a special Vendor Specific Class device, which is used by the vendor to customize the device type. Its Interface category code is 0xff. Therefore, designers can always find a device type suitable for their own design.

When designing a USB device, first determine the category of the device, and then implement the Basic USB communication protocol and the category communication protocol of the device. For example, the USB flash disk belongs to the mass storage device. Therefore, in addition to the basic USB communication protocol, the UFI command specification in the large-capacity storage equipment specification must be implemented. Because Windows supports the mass storage protocol, the USB flash disk only needs to follow the mass storage protocol to organize data and process commands to exchange data with PCs. Generally, the complete design process of a USB device consists of four parts:

(1) Design of USB hardware interfaces;

(2) programming of device firmware;

(3) Development of PC-side device drivers;

(4) Development of PC-side device applications.

9. USB hardware interface design

The USB interface chip generally has two options:

(1) USB chip itself is a microcontroller, such as cypress EZ-USB chip series, compatible with 8051, most EZ-USB chips support the maximum number of endpoints (one control endpoint 0 and 30 additional endpoints) and all four transmission modes;

(2) the USB chip only handles USB communication, so it must be controlled by an external microcontroller, such as PDIUSBD12 of plilips, which complies with USB specifications, there are three two-way endpoints, including the default endpoint 0.

10. Controller Firmware Programming

After the USB interface chip receives data or sends data, it will interrupt. Therefore, the core of Firmware Programming is to write the interrupt service program. This task is mainly to process the data in the buffer zone of each endpoint Based on the Mark of the relevant registers. The interrupted service program can be divided into some functional modules (functions) to consider:

(1) response from the endpoint 0. When the device is plugged in with a USB interface, the host sends a series of requests to the device's endpoint 0, and the device's firmware program should be able to correctly respond to these requirements at the end point 0.

(2) Data Communication Process of other endpoints. (1) The host will be able to know the usage of the device endpoint, and then it will be able to exchange data through other endpoints in a set transmission mode.

(3) Implement the protocols and specifications that the device category complies. For example, if you want to design a USB flash drive, the firmware program of the USB flash drive must support the UFI command specification in the Mass Storage Class Specification.

11. Development of PC-side drivers

The USB driver executed in windows must comply with Microsoft-defined Win32 Driver Model (WDM ). It is a layered driver model. Microsoft provides the Windows DDK (Windows Device developer's kit) tool and VC compiler to compile the WDM driver. For details, see the relevant development guide. There are also many third-party tool software that can be used to write usb wdm drivers, such as JUNGO WinDriver
USB. Using this type of tool software requires no in-depth understanding of WDM programming details.

Design end of PC-side applications

In the Win32 system, the operating system abstracts every device as a file. The application design only requires several simple file operations for API functions, you can communicate with the driver of the device. Such Win32 functions include the following:

(1) The creatfile function is used to open a device and return a device-related handle;

(2) readfile function, used to read data from the device;

(3) writefile function, used to write data to devices;

(4) The deviceiocontrol function is used to control devices, such as changing settings;

(5) closehandle function: closes a device opened by the creatfile function. The parameter of the function is the device handle returned by the creatfile function. Other APIs related to the USB device category can be used to obtain device information, such as the device path and name. For more information, see Related Development Guide.

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.