Вы находитесь на странице: 1из 31

USB BASICS

Covering: WWH
Why USB?
motivation, advantages wrt other protocols

What is USB?
Architecture, bus and other characteristics

How the USB works?


Protocols, descriptors and a lot of other stuff

World before USB


PS/2 connectors, RS-232

connectors, Centronics interfaces, SCSI interfaces, and many more Lots of different connector types The connectors and cables were expensive Most supported only single, pointto-point connections If you ran out of connectors you had to add expansion cards You had to shut down the system to attach or detach devices You had to restart the system after installing a device driver

USB rises
USB-IF Formation: Compaq, DEC, IBM, Intel, Microsoft, NEC,

and Nortel. In1994. Aim: To make it easier to connect external devices to computers and simplify their software configuration USB 1.0 was released in January 1996 with number of glitches USB 1.1 was released in September 1998 fixing the earlier issues and was widely adopted. Low/Full speed functions with speed of 1.5/12 Mb/s resp.(Two different standards UHCI and OHCI) The USB 2.0 specification was released in April 2000 and has become the most successful PC interface ever. High speed functions upto 480Mb/s speed and also supports USB 1.1. (Only one standard EHCI) In 2008, the USB specification was expanded with USB 3.0. Superspeed functions upto 5Gb/s speed and also supports USB 2.0&1.1 (One standard xHCI)

USB saves world (almost!)


One interface for many devices Automatic configuration

Easy to connect
Convenient cables Wireless option Hot pluggable No user setting

Inexpensive
Power saving Multiple speeds Reliable Versatile (4 transfer types and 4 speeds to make interface feasible for

many type of devices) Limitations: distance, broadcasting, peer to peer communication (partial solution: On The Go/OTG extension)

Why USB?
motivation, advantages wrt other protocols

What is USB?
Architecture, bus and other characteristics

How the USB works?


Protocols, descriptors and a lot of other stuff

USB from system viewpoint


The USB is a polled bus USB System: consists of three discrete parts: the USB device(s), the USB interconnect, and the USB host. USB devices: either a hub or a function Hubs: provide additional attachment points Functions: provide capabilities to the system. compound devices: several functions and an embedded hub in one physical package USB interconnect: provides a connection from the USB device(s) to the USB host a tiered star topology maximum of seven tiers including root hub

USB Host: USB system contains only one USB host. The host interfaces with the USB interconnect via a host controller. The host includes an embedded hub called the root hub which provides one or more attachment points, or ports.

USB from BUS viewpoint


a shielded cable containing 4 wires
GND VBUS-carries a nominal 5V supply, which may be used

by a device for power. D+ and D- form a twisted pair responsible for carrying a differential data signal (data is encoded using NRZI & is bit stuffed to ensure adequate transitions in the data stream)

Transceivers

Upstream End

Downstream End

Each line can be driven low individually, or a differential data signal can be applied. The maximum 'high' level is 3.3V. When receiving, individual receivers on each line are able to detect single ended signals There is also a differential receiver for reliable reception of data

Speed Identification
A 1.5k resistor pulls one of the lines up

to a 3.3V supply derived from VBUS. This is on D- for a low speed device, and on D+ for a full speed device A high speed device will initially present itself as a full speed device with the pull-up resistor on D+ and then it will do a high speed chirp. Pulled up state is the idle state of bus when a device is attached to port. This state is called J state. K state is opposite polarity to J state

J STATE TABLE
D+ D-

Low speed 0 1

High Speed 1 0

Line states

SE0

Why USB?
motivation, advantages wrt other protocols

What is USB?
Architecture, bus and other characteristics

How the USB works?


Protocols, descriptors and a lot of other stuff

Basic Concepts and Data Flow

Basic Concepts

Endpoints:

source and/or sink of data. up to 16 OUT and 16 IN endpoints. OUT - from host to device. IN - from device to host. Endpoint 0 OUT and endpoint 0 IN are used for controlling the device.(Also called Default Control Pipe)

Pipes:

Associates a buffer on the host with an endpoint on a device. Stream pipes: impose no structure on the data being transferred, always uni-directional Message pipes: impose some structure on the data being transferred, bi-directional- however data may predominantly transfer in one direction Data flow in one pipe is independent of data flow in any other pipe. Most pipes are available after a device has been configured, however the default control pipe always exists after a USB device is powered and has received a bus reset.

Packets: Could be thought of as the smallest element of data transmission First byte: Packet Identifier (PID) - helps to identify type of packet 17 different PID values This byte needs to be recognized quickly and so is not included in

any CRC checks. It therefore has its own validity check


PID0 PID1 PID2 PID3 ~PID0 ~PID1 ~PID2 ~PID3

Four different packet formats based on PID 1. Token Packet: Used for SETUP, OUT and IN Sync PID 8 bits ADDR 7 bits ENDP 4 bits CRC5 5 bits EOP

2. Data Packet: Used for DATA0, DATA1, DATA2 and MDATA Sync PID 8 bits DATA (0-1024)*8 CRC16 16 bits EOP

3. Handshake Packet: Used for ACK, NAK, STALL and NYET packets Sync PID EOP

8 bits

4.

SOF Packet: sent every 1ms on full speed links and 125s on high speed links. Sync PID 8 bits Frame No. 11 bits CRC5 5 bits EOP

Transactions
Is a sequence of upto three packets which performs a simple but

secure transfer of data. OUT transaction:

comprises two or three sequential packets

IN transaction:
comprises two or three sequential packets

SETUP Transaction:

comprises three sequential packets data payload is exactly 8 bytes long

Data Flow
Four different ways to transfer data on a USB bus. Each has its

own purposes and characteristics. 1. Bulk Transfers

designed to transfer large amounts of data error-free delivery no guarantee of bandwidth max packet size is 8, 16, 32 or 64 at full Speed and 512 for high speed

2.

Interrupt Transfers
host will only fetch one packet, at an interval specified in the endpoint descriptor size from 1 to 8 bytes at low speed, from 1 to 64 at full speed or up to 1024 bytes at high speed.
Similar for Interrupt transfer

3.

Control Transfer:
bi-directional transfer which uses both an IN and an OUT endpoint divided into three stages: SETUP stage carries 8 bytes, defines the request DATA stage is optional. If present, it always starts with a transaction containing a DATA1 The STATUS stage is a transaction containing a zero-length DATA1 packet. If the DATA stage was IN then the STATUS stage is OUT, and vice versa. max packet size for the data stage is 8 bytes at low speed, 8, 16, 32 or 64 at full Speed and 64 for high speed. guaranteed bandwidth error-free delivery is not guaranteed. main purpose- applications where it is important to maintain the data flow, but not so important if some data gets missed or corrupted. may contain up to 1023 bytes at full speed, or up to 1024 at high speed

4.

Isochronous Transfers:

Distribution of Bus Access


USB divides bus access time into fixed length segment to ensure synchronization. For low- or full-speed buses, segment = 1ms units, called frames. For a high-speed bus, segment =125 s units, called microframes. The host controller issues a start-of-frame (SOF) packet at the beginning of every (micro)frame. The remainder of the (micro)frame is available for the host controller to carry out transactions

Host controller may rearrange transactions to make better use of the available bandwidth two transactions through the same pipe must occur in the correct order, but the transactions of two separate transfers may be reordered at the host controller's discretion

Bus Time Rationing


For full- or low- speed buses:
Upto 10% of a frame for bulk transfers No more than 90% of a frame for periodic (isochronous and

interrupt) transfers. The host must not issue more than 1 transaction in a single frame for a specific isochronous endpoint.

For a high-speed bus:


Upto 20% of a microframe for bulk transfers. No more than 80% of a frame for periodic (isochronous and

interrupt) transfers. The host must not issue more than 1 transaction in a single microframe for a specific isochronous endpoint unless it is a highspeed, high-bandwidth endpoint. Split transaction bus access time is allocated from the 80% of the microframe guaranteed to periodic transfers.

Attaching device to host and making it communicate

Device Framework
The device contains a number of

descriptors which help to define what the device is capable of. Device can have more than one configuration, though only one at a time, and to change configuration the whole device would have to stop functioning. A device can have one or more interfaces. Each interface can have a number of endpoints and represents a functional unit belonging to a particular class. It is also possible to have alternative versions of an interface. It is possible to switch an interface to an alternate while the device remains configured

USB Device States: USB device framework defines a set of states that

are visible to both the host and the device. Those visible states are the following
Attached - Immediately after the USB device is attached to the USB

system Powered - After device has been attached to the bus and the VBUS line is applied to the device .
Self-Powered: Devices draw power from an external power source Bus-Powered: Devices draw power solely from the USB up to 100mA.

Default after receiving a bus reset. Device is addressable at the default address of 0. Address - After the host assigns device an address via the default

control pipe. Configured - after the host examines its possible configurations and selects one Suspended -When no traffic is observed on the bus for a period of 3 millisecond, a USB device enters this state, characterized by its low power consumption.

Enumeration

When a device is attached to a USB port:


The hub notifies the host. Device state-powered. The host waits to allow power to stabilize at the device. Then host issues a reset

signal to the device. (Host will reset only one device at a time, so there is no danger of 2 devices responding to address 0.) Device enters the default state. The host sends a request to endpoint 0 of device address 0 to find out its maximum packet size by using the Get Descriptor (Device) command. It then sends a Set Address request, with a unique address to the device at address 0. After the request is completed, the device assumes the new address. Host will now begin to quiz the device for as many details as it feels it needs. Some requests involved here are:

Get Device Descriptor, Get Configuration Descriptor, Get String Descriptor

At this moment the device is in an addressed but unconfigured state, and is only allowed to respond to standard requests. Once the host feels it has a clear enough picture of what the device is, it will load a suitable device driver. The device driver will then select a configuration for the device, by sending a Set Configuration request to the device The device is now in the configured state, and can start working as the device it was designed to be. From now on it may respond to device specific requests, in addition to the standard requests which it must continue to support.

Offset

Field

Size

Value

Description

D7 Data direction 0 - Host-to-device 1 - Device-to-host D6:5 Type = Standard8 bytes SETUP transaction in control transfer 0 conveys 1 = Class define the request from the host. 2 = Vendor bmRequestType 1 Bitmap 1. GET_DESCRIPTOR 3 = Reserved D4:0 Recipient This request will be the first that will be received after 0 = Device USB reset. = Interface Used to know the max packet length 1 in use by the 2 = Endpoint control endpoint which is available in the 8th byte of 3 = Other the device descriptor. 4-31 = Reserved

More About Descriptors

1 2 4 6

2.
1. 2.

Device Descriptor bRequest

Value

Specific Request

most likely be the first one fetched by Use the varies host according to wValue 2 Value request All descriptors start with a single byte specifying the descriptor's length, and this is always followed a Index or Use varies by according to wIndex 2 single byte defining the descriptor Offset type. request
wLength 2 Count Number of bytes to transfer if there is a data stage

SET_ADDRESS a simple, outward direction request in a control transfer with no data stage. The only useful information carried in the SETUP packet is the required address. 4. Get Descriptor (Configuration) Request returns:
3.
Configuration Descriptor

Interface Descriptor
Endpoint Descriptor OTG Descriptor Class-specific Descriptors

Vendor-specific Descriptors

Of Off Off set set 0 10 0 1 2 1 2

2 3
4 4 35 5 6

6 7 4 7

8 68

Field Siz Value Description Field Size Value Description Field Size Value Description 1 Number Size of this descriptor in bytes bLength Configuration Descriptor bLength 1 Constant Number ENDPOINT Size of this descriptor in bytes Numbe bDescriptorType 1 descriptor type (= 5) The wTotalLegth tells the host how many bytes are bLength 1 Size of this descriptor in bytes r bDescriptorTyp 1in this Constant INTERFACE descriptor type contained descriptor and 0all the descriptors D7: Direction, = OUT, 1= IN, (= 4) e bEndpointAddress 1 Endpoint bDescriptorTyp Consta D6-D4: Set to 0, D3-D0: Endpoint number 1 CONFIGURATION descriptor type (= 2) which follows. e nt bInterfaceNumb Number identifying this interface. Zero-based 1 Number D1:0 Transfer Type er value. Interface Descriptor Numbe Total number of bytes in this descriptor and all wTotalLength 2 00 = Control ,01 = Isochronous r the following descriptors. bAlternateSettin Value used to select this alternate setting for By defining class, subclass and protocol in the 1 the Number 10 = Bulk, 11 = Interrupt g this interface. bNumInterface Number of interfaces supported by this D3:2 Type with interface,1it is Numbe possible toSynchronisation have interfaces s r configuration of endpoints used by this interface. 00Number = No Synchronisation, 01 = Asynchronous, bNumEndpoints 1 Number different classes in the sameinclude device control endpoint 0. 10Doesn't = Adaptive, 11 = Synchronous bConfiguration Numbe Value used by Set Configuration to select this bmAttributes 1 Bitmap D5:4 Usage Type Endpoint Descriptor Value r configuration Class code assigned by USB-IF 0000h = Data isstring a endpoint reserved value Index of descriptor describing bInterfaceClass 1 Class Get Descriptor (String): optional iConfiguration 1 Index 01FFh = Feedback endpoint means vendor-defined class configuration - set to 0 if no string = Implicit feedback Data endpoint other value must be a class code Manufacturer String 10Any D7: Must be set to 1 11 = Reserved bInterfaceSubCl Product String D6: Self-powered 1 SubClass SubClass Code assigned by USB-IF D7:6 Reserved bmAttributes 1 Bitmap ass D5: Remote Wakeup Serial Number StringMaximum packet size this endpoint can send or bInterfaceProto wMaxPacketSize 2 Number D4...D0: Set to 0 1 Protocol receive Protocol Code assigned by USB-IF col Maximum current drawn by device in this bMaxPower 1 mA Index for of string descriptor describing interface bInterval 1 Interval polling endpoint for data transfers. In units of 2mA. iInterface 1 Number Index configuration. - set to 0 if no string

SET_CONFIGURATION Host loads a driver for the device based on the VID/PID

combination in the device descriptor, or on the standard class defined Feature there orSelector in an interface descriptor. Recipient driver may also ask for the same or different information using Get Descriptor requests. ENDPOINT_HALT Endpoint Eventually it will decide to configure the device using the DEVICE_REMOTE_WAKEUP Device SET_CONFIGURATION request TEST_MODE Device SET_FEATURE -CLEAR_FEATURE This pair of requests is used to control a small number of onB_HNP_ENABLE Device off features on a device, an interface or an endpoint A_HNP_SUPPORT Device SET_INTERFACE A_ALT_HNP_SUPPORT Device Once a device has been configured the host may use Set Interface to select an alternative interface to a particular default interface

HID device Interface example(mouse)


What happens when a device is attached to usb port

Knowing the capabilities of device

Data Transfer

Communication Channels possible in Device

Inside each transaction

Вам также может понравиться