Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 594 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
594
Dung lượng
6,85 MB
Nội dung
USB Complete EverythingYouNeedtoDevelopCustomUSBPeripherals Third Edition Jan Axelson Lakeview Research LLC Madison, WI 53704 USBComplete:EverythingYouNeedtoDevelopUSB Peripherals, Third Edition by Jan Axelson Copyright 1999-2005 by Janet L. Axelson All rights reserved. No part of the contents of this book, except the program code, may be reproduced or transmitted in any form or by any means without the written permis- sion of the publisher. The program code may be stored and executed in a computer sys- tem and may be incorporated into computer programs developed by the reader. The information, computer programs, schematic diagrams, documentation, and other material in this book are provided “as is,” without warranty of any kind, expressed or implied, including without limitation any warranty concerning the accuracy, adequacy, or completeness of the material or the results obtained from using the material. Neither the publisher nor the author shall be responsible for any claims attributable to errors, omissions, or other inaccuracies in the material in this book. In no event shall the pub- lisher or author be liable for direct, indirect, special, incidental, or consequential dam- ages in connection with, or arising out of, the construction, performance, or other use of the materials contained herein. Many of the products and company names mentioned herein are the trademarks of their respective holders. PICMicro is a registered trademark of Microchip Technology Inc. in the U.S.A. and other countries. Published by Lakeview Research LLC, 5310 Chinook Ln., Madison WI 53704 On the web at www.Lvr.com Distributed by Independent Publishers Group (www.ipgbook.com). Cover by Rattray Design. Cover Photo by Bill Bilsley Photography. Index by Julie Kawabata. 14 13 12 11 10 9 8 7 6 5 4 3 2 1 PDF e-book edition ISBN13 978-1-931448-03-1 ISBN10 1-931448-03-5 Contents iii Contents Introduction xv 1. USB Basics 1 What USB Can Do 2 Benefits for Users 2 Benefits for Developers 6 Beyond the Hype 10 Evolution of an Interface 13 Original USB 14 USB 2.0 15 USB On-The-Go 16 Wireless USB 16 USB versus IEEE-1394 16 USB versus Ethernet 17 Bus Components 17 Topology 18 Defining Terms 19 Contents iv Division of Labor 22 The Host’s Duties 22 The Peripheral’s Duties 25 What about Speed? 28 Developing a Device 29 Elements in the Link 29 Tools for Developing 30 Steps in Developing a Project 30 2. Inside USB Transfers 33 Transfer Basics 34 Enumeration Communications 34 Application Communications 34 Managing Data on the Bus 35 Host Speed and Bus Speed 36 Elements of a Transfer 37 Device Endpoints: the Source and Sink of Data 38 Pipes: Connecting Endpoints to the Host 40 Types of Transfers 40 Stream and Message Pipes 42 Initiating a Transfer 43 Transactions: the Building Blocks of a Transfer 44 Transaction Phases 45 Ensuring that Transfers Are Successful 51 Handshaking 51 Reporting the Status of Control Transfers 55 Error Checking 56 3. A Transfer Type for Every Purpose 61 Control Transfers 61 Availability 62 Structure 62 Data Size 66 Speed 66 Detecting and Handling Errors 68 Bulk Transfers 68 Availability 69 Structure 69 Data Size 69 Speed 71 Detecting and Handling Errors 71 Contents v Interrupt Transfers 72 Availability 72 Structure 72 Data Size 73 Speed 73 Detecting and Handling Errors 75 Isochronous Transfers 76 Availability 76 Structure 76 Data Size 79 Speed 79 Detecting and Handling Errors 80 More about Time-critical Transfers 80 Bus Bandwidth 81 Device Capabilities 81 Host Capabilities 82 Host Latencies 83 4. Enumeration: How the Host Learns about Devices 85 The Process 86 Enumeration Steps 87 Enumerating a Hub 91 Device Removal 92 Tips for Successful Enumeration 92 Descriptors 93 Types of Descriptors 94 Device Descriptor 96 Device_qualifier Descriptor 99 Configuration Descriptor 101 Other_speed_configuration Descriptor 103 Interface Association Descriptor 103 Interface Descriptor 106 Endpoint Descriptor 108 String Descriptor 112 Other Standard Descriptors 113 The Microsoft OS Descriptor 114 Descriptors in 2.0-compliant Devices 114 Making 1.x Descriptors 2.0-compliant 114 Detecting the Speed of a Dual-Speed Device 116 Contents vi 5. Control Transfers: Structured Requests for Critical Data 117 Elements of a Control Transfer 117 Setup Stage 118 Data Stage 120 Status Stage 122 Handling Errors 124 Device Firmware 125 The Requests 127 Get_Status 129 Clear_Feature 130 Set_Feature 131 Set_Address 132 Get_Descriptor 133 Set_Descriptor 134 Get_Configuration 135 Set_Configuration 136 Get_Interface 137 Set_Interface 138 Synch_Frame 139 Other Control Requests 140 Class-specific Requests 140 Vendor-specific Requests 140 6. Chip Choices 141 Components of a USB Device 142 The USB Controller 143 Other Device Components 145 Simplifying Device Development 148 Device Requirements 149 Chip Documentation 150 Driver Choices 151 Debugging Tools 151 Controllers with Embedded CPUs 154 Microchip PIC18F4550 156 Cypress EZ-USB 157 Cypress enCoRe II 163 Freescale MC68HC908JB16 164 Freescale MCF5482 ColdFire 164 Contents vii Controllers that Interface to External CPUs 165 National Semiconductor USBN9603 165 Philips Semiconductors ISP1181B 167 Philips Semiconductors ISP1581 168 PLX Technology NET2272 169 FTDI Chip FT232BM and FT245BM 170 7. Device Classes 177 About Classes 177 Device Working Groups 178 Elements of a Class Specification 178 Defined Classes 181 Audio 181 Chip/Smart Card Interface 189 Communication Devices: Modems and Networks 191 Content Security 198 Device Firmware Upgrade 200 Human Interface 203 IrDA Bridge 206 Mass Storage 208 Printers 213 Still Image Capture: Cameras and Scanners 217 Test and Measurement 220 Video 221 Implementing Non-standard Functions 226 Standard or Custom Driver? 226 Converting from RS-232 227 Converting from the Parallel Port 229 PC-to-PC Communications 229 Using a Generic Driver 231 8. How the Host Communicates 233 Device Driver Basics 233 Insulating Applications from the Details 234 Options for USB Devices 235 User and Kernel Modes 235 WDM Drivers 237 Layered Drivers 238 Communication Flow 243 More Examples 246 Contents viii Creating a Custom Driver 247 Writing a Driver from Scratch 247 Using a Driver Toolkit 248 Using GUIDs 249 Device Setup GUIDs 250 Device Interface GUIDs 251 9. Matching a Driver to a Device 253 Using the Device Manager 253 Viewing Devices 254 Property Pages 257 Device Information in the Registry 257 The Hardware Key 258 The Class Key 259 The Driver Key 260 The Service Key 262 Inside INF Files 262 Syntax 265 Sections 266 Using Device Identification Strings 272 Finding a Match 274 Do YouNeedto Provide an INF File? 276 Tools and Diagnostic Aids 277 Tips for Using INF Files 277 What the User Sees 279 10. Detecting Devices 281 A Brief Guide to Calling API Functions 281 Managed and Unmanaged Code 282 Documentation 284 Using Visual C++ .NET 284 Using Visual Basic .NET 286 Finding Your Device 291 Obtaining the Device Interface GUID 292 Requesting a Pointer to a Device Information Set 293 Identifying a Device Interface 295 Requesting a Structure Containing the Device Path Name 298 Extracting the Device Path Name 301 Closing Communications 302 Contents ix Obtaining a Handle 303 Requesting a Communications Handle 303 Closing the Handle 306 Detecting Attachment and Removal 306 About Device Notifications 307 Registering for Device Notifications 307 Capturing Device Change Messages 311 Reading Device Change Messages 312 Retrieving the Device Path Name in the Message 314 Stopping Device Notifications 317 11. Human Interface Devices: Using Control and Interrupt Transfers 319 What is a HID? 320 Hardware Requirements 321 Firmware Requirements 323 Identifying a Device as a HID 323 The HID Interface 326 HID Class Descriptor 326 Report Descriptors 328 HID-specific Requests 330 Get_Report 332 Get_Idle 333 Get_Protocol 334 Set_Report 335 Set_Idle 336 Set_Protocol 337 Transferring Data 338 About the Example Code 338 Sending Reports via Interrupt Transfers 340 Receiving Reports via Interrupt Transfers 343 Sending Reports via Control Transfers 345 Receiving Reports via Control Transfers 347 12. Human Interface Devices: Reports 351 Report Structure 351 Using the HID Descriptor Tool 352 Control and Data Item Values 354 Item Types 354 [...]... Corrections To find out more about developing USB devices and the software that communicates with them, I invite youto visit my USB Central page at Lakeview Research’s Web site (www.Lvr.com) You ll find code examples and links to articles, products, tools, and other information related to developing USB devices If you have a suggestion, code, or other information that you d like me to post or link to, let... programming and for small-scale developers, the needto obtain a Vendor ID Protocol Complexity A USB peripheral is an intelligent device that knows how to respond to requests and other events on the bus Chips vary in how much firmware support they require to perform USB communications In most cases, to program a USB peripheral, you needto know a fair amount about the USB s protocols, or rules for exchanging... getting a design up and running With USB, you have plenty of help via the USB Implementers Forum, Inc (USB- IF) and its Web site (www .usb. org) The USB- IF is the non-profit corporation founded by the companies that developed the USB specification The USB- IF’s mission is to support the advancement and adoption of USB technology To that end, the USB- IF offers information, tools, and tests The information includes... devices that have RS-232 interfaces are available, however If you want to use a USB peripheral with a PC that doesn’t support USB, a solution is to add USB capabilities to the PC To do so, you ll need to add USB host-controller hardware and install an operating system that supports USB The hardware is available on expansion cards that plug into a PCI slot or on a replacement motherboard The Windows edition... restate everything in the specifications Instead, the focus is on what you ll need to know to enable your devices to communicate efficiently and reliably • How can I decide if my device should use a USB interface? USB isn’t the best choice for every application Find out whether your design should use USB or another interface The chances are good that you will choose USB, however, and if so, you ll learn... the bus Can I connect other USBperipheralsto my device? Find out how to use USB On-The-Go to enable your device to act as a limited-capability host that can access other USBperipherals How can I ensure that my device will communicate without problems? At the device, writing bugfree firmware requires understanding what your device must do to meet the requirements of the USB specifications At the host... forum where developers can discuss USB- related topics The tools provided by the USB- IF include software and hardware to help in developing and testing products The support for testing includes developing compliance tests to verify proper operation and holding compliance workshops where developers can have their products tested and earn the rights for their devices to display the USB logo USB Complete... user to insert a disk with driver software, but other than that, installation is automatic There’s no need to restart the system before using the peripheral Easy to connect With USB, there’s no need to open the computer’s enclosure to add an expansion card for each peripheral A typical PC has four or more USB ports You can expand the number of ports by adding hubs with additional ports Easy cables USB. .. connect to a different PC and pass data between the PCs 10 USB Complete USB Basics Broadcasting USB provides no way to send a message simultaneously to multiple devices on the bus The host must send the message to each device individually If you must have broadcasting ability, use IEEE-1394 or Ethernet Legacy Hardware Older (“legacy”) computers and peripherals don’t have USB ports If you want to connect... drives, and video An external USB 2.0 hub must support all three speeds Other USB 2.0 devices can support low, full, or high speed or a combination USB 2.0 is backwards compatible with USB 1.1 In other words, USB 2.0 peripherals can use the same connectors and cables as 1.x peripherals, and a USB 2.0 peripheral works when connected to a PC that supports USB 1.x or 2.0 To use high speed, a high-speed-capable . USB Complete Everything You Need to Develop Custom USB Peripherals Third Edition Jan Axelson Lakeview Research LLC Madison, WI 53704 USB Complete: Everything. designs. To develop a device with a USB interface, you need to know something about how the interface works, what tasks your device firmware must per- form to