7UGUCPF.KOKVU USB is a likely solution any time you want to use a computer to communicate with an external device.. When a user connects a USB device to a PC, the operating system detect
Trang 1At the start of each code example, a sidehead indicates the programming lan-guage:
The NET code is compatible with the NET Framework Version 2.0 and later
Example applications are available for free download from www.Lvr.com.
5KFGJGCF 2TQITCOOKPI.CPIWCIG 2TQXKFGT
VB Visual Basic NET Microsoft VC# Visual C# NET Microsoft PBP PICBASIC PRO microEngineering Labs, Inc.
C18 MPLAB C compiler for
PIC18 CPUs
Microchip Technology Inc.
Trang 2xxi
#DDTGXKCVKQPU
This book uses the abbreviations and symbols below to express quantities and units:
/WNVKRNKGTU
'NGEVTKECN
6KOG
5[ODQN &GUETKRVKQP /WNVKRNKGT
p pico 10-12
n nano 10-9
µ micro 10-6
m milli 10-3
K kilo 210 (1024)
M mega 106 or 2 20 depending on context
G giga 109 or 2 30 depending on context
5[ODQN &GUETKRVKQP
A ampere
F farad
V volt
5[ODQN &GUETKRVKQP
s second
Hz Hertz (cycles per second)
Trang 3&CVC
0WODGT5[UVGOU Binary values have a trailing subscript “b” Example: 10100011b An exception is when it’s clear from the context that the values are binary
Example: Set bits 6 5 to 01
Hexadecimal values have a trailing “h” Example: A3h.
All other values are decimal Example: 163.
5[ODQN &GUETKRVKQP
in inch
ft foot
m meter
5[ODQN &GUETKRVKQP
B byte bps bits per second
Trang 4xxiii
#EMPQYNGFIGOGPVU
USB is much too big a topic to write about without help I have many people to thank
My technical reviewers provided feedback that helped make the book as com-plete and accurate as possible With that said, every error in this book is mine and mine alone A big thanks to Paul E Berg, Greg Burk, Robert Dunstan, John Garney, Bill Jacobus, Kosta Koeman, and Matt Leptich
Others I want to thank for their support are Phyllis Brown of J Gordon Elec-tronic Design, Michael DeVault of DeVaSys Embedded Systems, Traci Donnell
of the USB-IF, David Flowers of Microchip Technology, Inc., Laurent Guin-nard of Ellisys, Tim Harvey of CWAV, Inc., Blake Henry of Bitwise Systems,
John Hyde of usb-by-example.com, Rahman Ismail and Jeff Ravencraft of Intel
Corporation, Dr Bob Miller of Trace Systems, Inc., and Jeff Schmoyer of microEngineering Labs, Inc
For their help with the previous editions this edition builds on, thanks to Joshua Buergel, Gary Crowell, Fred Dart, Wendy Dee, Lucio DiJasio, Keith Dingwall, Dave Dowler, Mike Fahrion, David Goll, John M Goodman, Lane Hauck, David James, Christer Johansson, Geert Knapen, Alan Lowne, Jon Lueker, Brad Markisohn, Rich Moran, Bob Nathan, Walter Oney, Amar Rajan, Marc Reinig, Rawin Rojvanit, Glenn M Roberts, Robert Severson, Craig R Smith, and Dave Wright
I hope you find the book useful and welcome your comments at jan@Lvr.com.
Trang 6
1
75$$CUKEU
At over two billion new installed units per year, USB is the most successful per-sonal-computer interface ever Every recent PC has USB ports that can connect
to keyboards, mice, game controllers, scanners, cameras, printers, drives, and more USB is reliable, fast, versatile, power-conserving, inexpensive, and sup-ported by major operating systems USB 3.0’s new SuperSpeed bus means USB
is likely to continue to dominate as the interface of choice for an ever-expand-ing selection of peripherals
This chapter introduces USB, including its advantages and limits, some history about the interface and recent enhancements to it, and a look at what’s involved
in designing and programming a device with a USB interface
7UGUCPF.KOKVU
USB is a likely solution any time you want to use a computer to communicate with an external device Internal devices, such as fingerprint readers, can use USB as well The interface is suitable for mass-produced, consumer devices as well as specialized, small-volume products and one-of-a-kind projects
Trang 7Chapter 1
To be successful, an interface has to please two audiences: the users who want to use the devices and the developers who design the hardware and write the code that communicates with the devices USB has features to please both groups
$GPGHKVUHQT7UGTU
From the user’s perspective, the benefits of USB are ease of use, fast and reliable data transfers, low cost, and power conservation Table 1-1 compares USB with other interfaces
'CU[VQ7UG
Ease of use was a major design goal for USB, and the result is an interface that’s
a pleasure to use for many reasons:
One interface for many devices USB is versatile enough for just about any
standard PC peripheral function Instead of having a different connector and cable type for each peripheral function, one interface serves many
Automatic configuration When a user connects a USB device to a PC, the
operating system detects the device and loads the appropriate software driver The first time the device connects, the operating system may prompt the user to insert a disc with driver software, but other than that, installation is automatic Users don’t need to reboot before using the device
Easy to connect A typical PC has multiple USB ports, and hubs make it easy
to add ports without opening up the PC
Convenient cables USB connectors are small and compact compared to
con-nectors used by other interfaces such as RS-232 To ensure reliable operation, the USB specification defines electrical requirements for cables A cable seg-ment can be as long as 5 m depending on bus speed With hubs, again depend-ing on bus speed, a device can be as far as 30 m from its host PC
Wireless options USB originated as a wired interface, but technologies are
now available for wireless communications with USB devices
Hot pluggable Users can connect and disconnect a USB device whenever they
want, whether or not the system and device are powered, without damaging the
Trang 8USB Basics
3
Table 1-1: USB is more flexible than other interfaces, which often target a
specific use.
+PVGTHCEG 6[RG 0WODGTQH
&GXKEGU &KUVCPEG 5RGGF 6[RKECN7UG
USB 3.0 dual simplex
serial
127 (per bus) 9 (typical)
(up to 49 with 5 hubs)
5 G Mass storage,
video
USB 2.0 half duplex
serial
127 (per bus) 16 (98 ft
with 5 hubs)
1.5M, 12M, 480M
Keyboard, mouse, drive, speakers, printer, camera
eSATA serial 2 (port
multiplier supports 16)
Ethernet serial 1024 1600 10G General network
communications IEEE-1394b
(FireWire 800)
serial 64 300 3.2G Video, mass
storage IEEE-488
(GPIB)
parallel 15 60 8M Instrumentation
I 2 C synchronous
serial
40 18 3.4M Microcontroller
communications Microwire synchronous
serial
8 10 2M Microcontroller
communications MIDI serial current
loop
2 (more with flow-through mode)
50 31.5k Music, show
control
Parallel Printer
Port
parallel 2 (8 with
daisy-chain support)
10–30 8M Printers,
scanners, disk drives
RS-232
(EIA/TIA-232)
asynchronous serial
2 50–100 20k (115k
with some hardware)
Modem, mouse, instrumentation
RS-485
(TIA/EIA-485)
asynchronous serial
32 unit loads (some chips allow up to
256 devices)
4000 10M Data acquisition
and control systems
SPI synchronous
serial
8 10 2.1M Microcontroller
communications
Trang 9Chapter 1
No power supply required (sometimes) The USB interface includes
power-supply and ground lines that provide a nominal +5V from the PC or a hub A device that requires up to 500 mA (USB 2.0) or 900 mA (USB 3.0) can draw all of its power from the bus instead of using a dedicated power supply In contrast, devices that use other interfaces may have to provide a power supply inside the device or an external supply
/WNVKRNG5RGGFU
USB supports four bus speeds: SuperSpeed at 5 Gbps, high speed at 480 Mbps, full speed at 12 Mbps, and low speed at 1.5 Mbps SuperSpeed requires a USB 3.0 host controller in the host PC USB 2.0 host controllers support low, full, and high speeds
The bus speeds describe the rate that information travels on the bus In addi-tion to applicaaddi-tion data, the bus must carry status, control, and error-checking information Plus, multiple devices can share a bus Thus, the data throughput for an individual device’s data is less than the bus speed The USB protocols support data transfers at around 400 MB/s for SuperSpeed, 53 MB/s for high speed, 1.2 MB/s for full speed, and 800 B/s for low speed Hardware and soft-ware limitations can result in lower real-world rates, however
The USB 1.0 specification defined low and full speeds Full speed was intended for most peripherals that had been using RS-232 (serial) and parallel ports Full-speed data-transfer rates are comparable to the speeds of these earlier inter-faces Mice tend to use low speed because the less stringent cable requirements allow flexible cables Low-speed devices may have lower manufacturing cost due
in part to cheaper cables High speed became an option with the release of USB 2.0, and USB 3.0 defined SuperSpeed
4GNKCDNG
USB’s reliability is due to both the hardware and the protocols The hardware specifications for USB drivers, receivers, and cables ensure an electrically quiet interface that eliminates most noise that could cause data errors The USB pro-tocols enable detecting errors in received data and notifying the sender so it can
Trang 10USB Basics
5
+PGZRGPUKXG
Because the host computer provides most of the intelligence to control the interface, components for USB devices are inexpensive A device with a USB interface is likely to cost the same or less than an equivalent device with a differ-ent interface
2QYGT5CXKPI
Power-saving circuits and protocols reduce a device’s power consumption while keeping the device ready to communicate when needed Reducing power con-sumption saves money, helps the environment, and for battery-powered devices, allows a longer time between recharges
$GPGHKVUHQT&GXGNQRGTU
Many of the user advantages described above also make things easier for opers For example, USB’s cable standards and error checking mean that devel-opers don’t have to worry about specifying cable characteristics or providing error checking in software
Other advantages help the hardware designers who select components and design the circuits in devices and the programmers who write firmware embed-ded in the devices and software to communicate with devices
The benefits result from the flexibility built into the USB protocol, the support
in the controller chips and operating system, and the support available from the USB Implementers Forum
8GTUCVKNG
USB’s four transfer types and four speeds make the interface feasible for many types of peripherals USB has transfer types suited for exchanging large and small blocks of data, with and without time constraints For data that can’t tol-erate delays, USB can guarantee bandwidth These abilities are especially wel-come under Windows where accessing peripherals in real time is often a challenge Although the operating system, device drivers, and application soft-ware can introduce unavoidable delays, USB makes it as easy as possible to achieve transfers that are close to real time even on desktop systems
Unlike other interfaces, USB doesn’t assign specific functions to signal lines or make other assumptions about how the system will use the interface For exam-ple, the status and control lines on the PC’s parallel port were defined with the