The fourth talk on peripheral modules of microcontroller, basic concepts of USB bus

Publisher:创客1992Latest update time:2023-03-14 Source: zhihu Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
  1. USB overview

USB devices are now a very commonly used interface, and its plug-and-play feature brings great convenience to people. In embedded applications, USB is often used as an interface to communicate with the host computer, and is also used to store data through U disks. USB can be divided into low-speed, full-speed and high-speed devices according to communication speed. In our applications, low speed and full speed are the most common. Here we give a brief introduction to USB from the physical layer to the protocol layer. The principle of high-speed USB is the same. After understanding the working principles of low-speed and full-speed devices, it is easier to understand high-speed devices. We will not discuss it here.

Low Speed ​​(Low Speed, 1.5Mbps): keyboard, mouse, stylus

Full Speed ​​(12Mbps): Audio

High Speed ​​(High Speed, 480Mbps): Video

The USB protocol is open and can be downloaded from the official website download.

2. Host, Device

The picture above is a typical connection diagram of a USB full-speed host and device. The host must have external power supply capability. In the picture, you can see the microcontroller as the host. One pin is used to control the transistor or MOSFET and provide 5V power to the USB port. There are two ways to power the device: one is to get it from the 5V provided by the host through the USB bus, such as our commonly used U disk; the other is to get it from another power source, and the power cord from the host to the device does not need to be connected. If the device's 1.5k pull-up resistor is added to D-, then the device will be recognized by the host as a low-speed device. The pull-up of high-speed devices is added to D+ like that of full-speed devices, and needs to be further distinguished by software protocols.

Devices that are powered through the USB bus are divided into two configurations:

Low-power devices: Maximum current does not exceed 100mA

High-power devices: The enumeration phase after the device is first connected does not exceed 100mA, and the maximum after configuration is completed does not exceed 500mA.

All USB communications are initiated by the host. When the host detects that a device is connected, it will first ask the device to report itself to see what capabilities the device has, including the maximum current, and then the host will perform corresponding operations based on the reported description. This process is called bootstrapping (Enumeration). Devices declare their capabilities through descriptors, including:

Device Descriptor

Configuration Descriptor

Interface Descriptor

Endpoint Descriptor

String Descriptor

Endpoint is the basic unit of USB communication. Each USB device contains several endpoints. The data sent by the host will eventually reach an endpoint according to the device address and endpoint address. When the host obtains the data, it also issues a read data command to an endpoint. The endpoint then sends the data stored in its own buffer to the host.

Above the endpoints is a logical organization. Multiple endpoints can be grouped into one interface, and multiple interfaces can be grouped into one configuration. A device can have multiple configurations.

3. USB physical layer

(USB Specification 2.0)

1 red Vbus(5V)

2 white D-

3 Green D+

4 black GND

Some USB interfaces will have an extra ID line to support OTG (On The Go). The two ends of the line that supports OTG are different. When one end is plugged into an OTG device, the identification pin ID of the device interface will be pulled low. After the device recognizes that its ID is pulled low, it will enter the host state (Host) and connect the line. The device ID on the other end is not pulled down and enters the device state (Device) by default. After that, master-slave switching can be done through soft protocol. However, there are not too many centralized applications of this kind. It is common for one device to be used as either a host or a device.

USB uses a differential transmission mode and has two data lines D+ and D-.
Differential 1 : D+ > VOH(min) (2.8V) and D- < VOL(max)(0.3V)
Differential 0 : D- > VOH and D+ < VOL

J state: Differential 0 for low-speed USB, Differential 1 for full-speed USB
K state: Differential 1 for low-speed USB, Differential 0 for full-speed USB

In addition, by pulling D+ and D- as single-ended signals low and high, some special states can be expressed.

SE0 state (Single Ended 0) : D+ low, D- low
SE1 state (Single Ended 1): D+ high, D- high

Reset signal: D+ and D- < VOL for >= 10ms
The host will send a Reset signal to set the device to the default unconfigured state before communicating with the device. That is, the host pulls down the two signal lines (SE0 state) and maintains it for 10ms.

You may be a little dizzy when you see this, but it doesn't matter. You will be even more dizzy if you look at the USB protocol.

We must not fall into this pit and be unable to get out. Just like we never trigger a start signal or pull out an end signal when using a serial port, the processing of these physical layer signal states is completely handled by the chip-integrated USB The controller handles it. Moreover, it is also necessary to provide a USB software protocol stack. It is unrealistic for users to completely figure out all the details by themselves. However, just like driving a car, if you have a deeper understanding of the principles of a car, you will be able to fully utilize the performance of the car.

Continuing, in addition to the above status, there are also:

Idle State, Resume State, Start of Packet, End of Packet, Disconnect, Connect.

4.Packet

Packet is the most basic unit of USB communication.

SOP: Start Of Packet, the flag changes from idle state to data packet sending.

SYNC: Synchronization segment, used for clock synchronization of USB devices.

PID: Packet Identifier. There are many types, which will be explained in detail below.

Address: Device and endpoint addresses. A host can attach multiple devices, and the host will assign different addresses to each device.

Frame Number: Frame number, incremented by 1 for each frame sent, and becomes 0 when it reaches 7FFFH.

Data: data segment.

CRC: Checksum.

EOP: End Of Packet.

Through different PIDs, data packets are divided into 4 major categories, and each major category contains some subcategories:

Token OUT, IN, SETUP, SOF

Data (Data) DATA0, DATA1

Handshake (Handshake) ACK, NAK, STALL, NYET

Special package (Special) PRE, ERR


5.Transaction

A Transaction always starts with the host issuing a token to the device. Again, all USB communication processes are initiated by the host. The three types of tokens divide Transaction into three categories:

OUT: The host sends data to the device.

IN: The host obtains data from the device.

SETUP: The host sets up the device.

OUT and IN in the USB protocol must be viewed from the perspective of the host. The following are typical examples of obtaining and sending data:

For every Transaction, Token is always required, and data segments and handshakes depend on the situation. For example, in the previous example, when the host issues an IN token to obtain data, if the device does not have the data ready, it can return NAK to end the Transaction.


6. Transfer

Okay, with the above, everything seems to be ready. But if you think about it further, there are still some problems that are difficult to solve. what? For example, what is the specified length of the DATA data segment? How often should the host initiate communication?

A USB host is allowed to mount multiple devices, and these devices vary widely: for example, a mouse needs to respond quickly after pressing a button and send location information to the host. The amount of data is very small, while a USB flash drive needs to transmit a large amount of data. The data. What should I do if the USB flash drive is transmitting data when I click the mouse?

In order to solve the above problems, USB first specifies four transmission types:

Control Transfers:  Mainly used to set up the device when it is first connected to the host. There is also the usual management of equipment status. It requires bidirectional data transfer.

Bulk Data Transfers:  Mainly used for scenarios where the volume is large but the transfer time is not strict. For example, USB flash drive.

Interrupt Data Transfers:  Scenarios that require timely and accurate transmission of information. Interrupt transfers are always one-way. Such as the mouse.

Isochronous Data Transfers:  Generally require a relatively fixed bandwidth, the delay is short and relatively certain. The transmission is one-way, and data does not need to be retransmitted after an error. Such as USB camera.

Then, in order to solve the problem of timely response of the device, USB issues a SOF token every 1ms (high-speed USB is every 125us), followed by the token for synchronization type transmission, followed by interrupt type, control type and batch data transmission. type. Within each Frame, Isochronous, Interrupt and Control will guarantee a certain bandwidth. The Bulk type has the lowest transmission priority and does not necessarily get bandwidth for data transmission in every frame.

A Transfer consists of one or more Transactions. For example, a control transmission can be composed of Setup, IN, OUT and other Transactions. Packet and Transaction are not allowed to be interrupted in the middle, and multiple Transactions of Transfer can be transmitted multiple times.


7. Summary

We gave a brief introduction to the physical layer and protocol layer of USB. In the following article, we will take a look at how USB works through practical examples, and discuss some issues that many engineers often overlook or are not aware of.



Reference address:The fourth talk on peripheral modules of microcontroller, basic concepts of USB bus

Previous article:What stages have I gone through from scratch to independent development of a microcontroller?
Next article:The third talk on peripheral modules of single-chip microcomputer, CAN bus

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号