USB device和USB host

来源:互联网 发布:淘宝客服新手工资多少 编辑:程序博客网 时间:2024/04/30 22:02

USB,英文全称:Universal Serial Bus,即通用串行总线。

常常各种USB芯片同时具有host和device两种接口。host就是主的,可以起控制作用;device(slave)就是从,是被控制的。比如PC机的USB口,可以识别U盘或者其他USB设备,起到控制的作用,就是host;而U盘是被控制的,是device(slave)。

只有一个host和一个device(slave)才能起到数据传输或者控制的作用。

而还有一种称为OTG,例如芯片ISP1761就有OTG的口,它是指它既可以做host口也可以做device(slave)口。




HOST端要做的事情:硬件和软件
host端需要的硬件是:USB host controller and a root hub with one or more USB ports.
           软件是: an operating system that enables device drivers to communicate with lower-level drivers that access the USB hardware.
一个host controller总是和一个root hub结合在一起的,(但是还是不能把两者结合成一个设备,因为他们还是有具体的不同作用的)从linux的host controller driver就可以看出,struct hc_driver中即包含了对host controller的操作,也包含了对root hub的操作。上面所说的软件也就是这个host controller driver。

1. 探测设备(detect device)
On power-up, hubs make the host aware of all attached USB devices. In a process 
called enumeration, the host determines what bus speed to use, assigns an
address, and requests additional information. After power-up, whenever a
device is removed or attached, a hub informs the host of the event, and the host
enumerates any newly attached device and removes any detached device from
its list of devices available to applications.
从这里可以知道,host controller和root hub的具体分工。root hub只是通知host controller有设备插入或断开,host controller接到通知后开始一个
叫做enumeration的过程(枚举),确定通信速率,分配地址等。建立真正的通信通道。

2. 控制数据流(manage data flow)
The host manages traffic on the bus. Multiple devices may want to transfer data
at the same time. The host controller divides the available time into intervals
and gives each transmission a portion of the available time.
During enumeration, a device’s driver requests bandwidth for transfers that
must have guaranteed timing. If the bandwidth isn’t available, the driver can
request a smaller portion of the bandwidth or wait until the requested band-
width is available. Transfers that have no guaranteed timing use the remaining
bandwidth and must wait if the bus is busy.
在数据传输方面,usb采用了分时的策略。传输带宽是在枚举的时候就已经确定好了的并且得以保证。

3. 错误检查(error checking)
When transferring data, the host adds error-checking bits. On receiving data,
the device performs calculations on the data and compares the result with
received error-checking bits. If the results don’t match, the device doesn’t
acknowledge receiving the data and the host knows it should retransmit. 
 In a similar way, the host error-checks data received from devices. 
USB also supportsa transfer type without acknowledgments for use with data such as real-time audio that tolerates errors to enable a constant transfer rate.
错误检查很容易理解,在传输数据的时候,发送方在数据中加入error-checking bits,接受方收到数据后用收到的数据计算出来的校验码和
发送方发出的校验码是否一致,不一致就不回应发送方,发送方没有接到回应就重新发送数据。usb也支持没有错误校验的传输。

4. 供电和电源管理
In addition to data wires, a USB cable has wires for a +5V supply and ground.
Some devices draw all of their power from the bus. The host provides power to
all devices on power up or attachment and works with the devices to conserve
power when possible. A high-power USB 2.0 device can draw up to 500 mA
from the bus. A high-power SuperSpeed device can draw up to 900 mA from a
USB 3.0 bus. Ports on some battery-powered hosts and hubs support only
low-power devices, which are limited to 100 mA (USB 2.0) or 150 mA (Super-
Speed). To conserve power when the bus is idle, a host can require devices to
enter a low-power state and reduce their use of bus current.

5. 交换数据
All of the above tasks support the host’s main job, which is to exchange data
with devices. In some cases, a device driver requests the host to attempt to send
or receive data at defined intervals, while in others the host communicates only
when an application or other software component requests a transfer.



DEVICE端要做的事情:
device端要做的事情分两类,一类是和HOST对应的事情,host请求,device必须给予回应。令一类就是device端自己的事情。

1.响应HOST
On power up or when a device attaches to a powered system, a device must
respond to standard requests sent by the host computer during enumeration.
The host may also send requests any time after enumeration completes.
All devices must respond to these requests, which query the capabilities and sta-
tus of the device or request the device to take other action. On receiving a
request, the device places data or status information in a buffer to send to the
host. For some requests, such as selecting a configuration, the device takes other
action in addition to responding to the host computer.
The USB specification defines requests, and a class or vendor may define addi-
tional requests. On receiving a request the device doesn’t support, the device
responds with a status code.

2. 错误检查
同HOST端类似

3. 数据传输
For most transfers where the host sends data to the
device, the device responds to each transfer attempt by sending a code that indicates whether the device accepted the data or was too busy to accept it. 
For most transfers where the device sends data to the host, the device must respond
to each attempt by returning data or a code indicating the device has no data to
send. 
数据传输分两种,HOST给DEVICE传输数据和HOST请求DEVICE发送数据。两者都有响应的应答机制(a code).
DEVICE不可以主动给HOST发送数据,只有当HOST请求其发送数据的时候才可以发送。(USB3.0中device可以发送code让host请求)

0 0