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

USB:

Universal Serial Bus


Ocial USB documenta6on
h8p://www.usb.org h8p://www.usb.org/developers/docs/usb_20_040908.zip (See chapter 9)

Hyde, J. (2001). USB Design by Example: A Prac6cal Guide to Building I/O Devices. Second Edi6on. Enhanced host controller specica6on
h8p://www.intel.com/technology/usb/ehcispec.htm

Example homebrew USB HID device


h8p://www.create.ucsb.edu/%7Edano/CUI/

Microchip (h8p://www.microchip.com)
pic18f4550 microcontroller, pickit2 programmer, C compiler and MPLAB IDE PICDEM development board (no hardware programmer needed; bootloader enables ashing microcontroller rmware through USB)

Linux & USB


Host sobware on Linux
h8p://libusb.wiki.sourceforge.net/ h8p://libhid.alioth.debian.org/

My notes
h8p://www.cprince.com/PubRes/Hardware/LinuxUSB/

Linux Device Driver book


h8p://lwn.net/Kernel/LDD3/ (freely available) Chapter 13 is on USB

Linux device driver kit


h8p://kernel.org/pub/linux/kernel/people/gregkh/ddk/

usbmouse.c & hid-core.c Linux device drivers


h8p://www.cprince.com/courses/cs5631/lectures/USB
2

Outline
RS-232 serial communica6on Wiring, power & hardware congura6on USB organiza6on Congura6ons & endpoints Transac6on types Bus data communica6on Control transfers Interrupt transfers Hardware development Some Linux host sobware
Devices classes Interrupts & Interrupts

Packets, frames, microframes Data rates

RS-232 Serial
Bytes are shibed out onto wire, for serial transmission of bits Transmission is point to point. A serial port connects to one single other serial port via wires Bits are received serially at receiving end, and shibed back into byte form Communica6on is bidirec6onal and full-duplex RS-232 communica6on protocol is just about sending byte data serially
4

Chapter 13, LDD3


USB was originally created to replace a wide range of slow and dierent busesthe parallel, serial and keyboard connec6ons with a single bus type that all devices could connect to (p. 327)
serial means standard serial or RS-232

Wiring & Power for USB <= 2.0


Four wire connectors Two wires: + & - 5V power
No wire for clock Physical protocol is asynchronous

Two other wires: communica6on protocol


Usually driven in an6-phase

Devices start using 100mA power During congura6on, can request use of up to 500mA
(Dierent wiring for USB 3)
6

Host & Device (Gadget)


Host controller

h8p://www.intel.com/technology/usb/ehcispec.htm (Enhanced host controller descrip6on)

USB Organiza6on: Device Classes


USB devices (gadgets) can be programmed so they t in one of several device classes
h8p://www.usb.org/developers/devclass_docs/

These include
HID: Human Interface Device
E.g., Mouse, joys6ck

CDC: Communica6on Device Class


Part of an architecture [for] telecommunica6on and medium-speed networking services (p. 250, Hyde, 2001)

PID: Physical Interface Device


devices that require real-6me physical feedback. The main focus for the deni6on is for the use of hap6c devices and the implementa6on of force feedback systems. (usb.org devclass doc)

MSC: Mass Storage Class

Such devices can oben be controlled by generic class device drivers If a device ts neatly into one or more of these predened classes, then [with appropriate exis6ng support from the opera6ng system], you dont need [an addi6onal] device driver (p. 10, Hyde, 2001)

HID on Linux
sta6c char *hid_types[] = {"Device", "Pointer", "Mouse", "Device", "Joys6ck, "Gamepad", "Keyboard", "Keypad", "Mul6-Axis Controller"}; From:
/usr/src/linux-source-2.6.22/drivers/hid/usbhid/hid-core.c

Ubuntu Linux

USB Protocol Basics - 1


Device level part of protocol is not hardware interrupt based USB devices are polled by host computer
The bus is very simple at the technological level, as its a single- master implementa6on in which the host computer polls the various peripheral devices (Ch 13, p. 1, Linux Device Drivers).

However, host controllers use hardware interrupts (presumably, to interact with host)
E.g., see h8p://www.gelato.unsw.edu.au/lxr/source/ drivers/usb/host/uhci-hcd.c
10

USB Protocol Basics - 2


Host periodically asks each device if it wants to communicate
Polls as oben as once every (micro)frame, but for many devices (e.g., HID), once every 10 frames (100 Hz) may be sucient A typical packet size of 20 bits (4 bit PID + 7 bit device address + 4 bit endpoint address + 5 bit CRC) is sent to poll a device USB device can never start sending data without rst being asked to by a host controller

11

USB Protocol Basics - 3


Packets intended for devices and sent on the bus have a device address & endpoint address
126 possible devices per host controller

Device address indicates a par6cular gadget connected to the host


Endpoint indicates a logical data loca6on on the gadget

12

Congura6ons & Endpoints


Each device can have mul6ple congura6ons
Only a single congura6on is ac6ve at one 6me

Each congura6on has a control endpoint and a collec6on of interfaces


Each interface has a collec6on of IN & OUT endpoints Interfaces run concurrently

Each congura6on of a device has a collec6on of endpoints


IN endpoints: Gadget to host data transfer OUT endpoints: Host to gadget data transfer
13

Endpoint types (LDD3, p. 329-330)


Control, Interrupt, Bulk, Isochronous Control
Used to allow access to dierent parts of the USB device Used for conguring device, retrieving info about device, sending commands to device, retrieving status reports Every USB device has a control endpoint endpoint 0, used to congure device at inser6on 6me transfers guaranteed by the USB protocol to always have enough reserved bandwidth to make it through to the device (p. 329)

Interrupt
These endpoints transfer small amounts of data at a xed rate every 6me the USB host asks the device for data; primary transport method for USB keyboards and mice Also, guaranteed to make it through
14

Endpoint types - 2
Bulk
Transfer large amounts of data Used for devices that need to transfer data with no data loss Transfers not guaranteed to make it through the bus in a specic amount of 6me

Isochronous
Also transfer large amounts of data, but data not guaranteed to make it through Used with devices that can handle data loss E.g., audio and video devices
15

Endpoint Transfer Types


Devices can request a xed bandwidth for data transfers (p. 327, LDD3)
Transfer types are specied for each endpoint

Interrupt transfer type does not mean hardware interrupts Interrupt and control transfers: Always guaranteed to make it through on 6me

16

Figure 1-8, Hyde (2001)

17

Low-level Data Transmission

PID = Packet ID h8p://www.beyondlogic.org/usbnutshell/usb3.htm

18

Packet types
Token
indicates the endpoint number to be used for sending/receiving data

Data
Size limit is 1024 bytes

Handshake
ACK, NAK, STALL (requires host interven6on)

Start of frame
h8p://www.beyondlogic.org/usbnutshell/usb3.shtml
19

Mul6ple packets are sent per frame

20

Data Rates
Low-speed & full-speed
Frame rate: 1000 Hz Low-speed: 1.5 Mbps
1,200 bits per frame (prac6cally)

Full speed: 12 Mbps


12,000 bits per frame (prac6cally)

High speed: 480 Mbps (USB 2.0)


Microframe rate: 8000 Hz 53,000 bits per microframe (prac6cally)

Super speed (USB 3.0): approx. 4.8 Gbit/s


21

Control Transfers
Used to control the gadget Endpoint 0 on gadget Packets sent in 2 or 3 phases
Setup; op6onal data phase; status phase

22

23

24

25

26

27

End!

28

29

30

Interrupt Transfers
Not really interrupts Just another USB transac6on type This transac6on type emphasizes quality & 6me (same as control), but intended for more general data transfer to and from gadget

Ch. 13, Linux Device Drivers, p. 4

31

32

Hardware Development
Whats involved?
Microcontroller Circuit in which the microcontroller will run Firmware for the microcontroller Assembler or C compiler to program rmware Programmer for microcontroller Sobware to download rmware to microcontroller Sobware for the host
33

Example Circuit

h8p://www.create.ucsb.edu/%7Edano/CUI/MediaInterfaceCircuit.jpg

34

PICDEM Full Speed USB (Microchip corp).


35

Linux
tail f /var/log/messages Host sobware for HID libusb
h8p://libusb.wiki.sourceforge.net/

libhid
h8p://libhid.alioth.debian.org/ test_libhid.c

Commands
lsusb
E.g., lsusb -d 04d8:0000 vvv

lshid

My notes on HID hardware development & Linux host sobware


h8p://www.cprince.com/PubRes/Hardware/LinuxUSB
36

Ch. 13, Linux Device Drivers, p. 9

37

<< Snip Material omi8ed>>

<< Snip Material omi8ed>>

<< Snip Material omi8ed>>

38

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