1、USB Interfacing,Lane Hauck ,Prerequesites,To get the most from this course, you should have an EZ-USB Development System. This system includes a Development Board and the Keil software development tools. Most chapters include example code and simple programming assignments.To complete the assignment
2、s, you must be able to write 8051 firmware using the C language. To develop code and run the USB designs, you must have access to a PC equipped with a USB port, and running Windows 98Me or Windows 2000.”,Agenda,USB history USB Demo Mouse, Speakers, Camera USB Basics: 10 USB Facts 8051 Primer EZ-USB
3、Architecture Onion introduction USB Resources,USB Founding Companies,USB Demo,Plug in some peripherals Mouse Speakers Camera Notice that they are: “Hot plugged” Powered by the USB cable,Question: How did the PC know what was plugged in and which driver to load?,USB Basics,Cables Four wires: Vbus, GN
4、D, D+, D Vbus: 5V nom, 4.4V minimum (no wall warts!) Cables are 5 meters MAX Two connector types, A and B Prevents illegal topologies USB extension cables are ILLEGAL PC has a “root hub” with 1 or 2 ports Each port supplies 5 “unit loads” or 500ma Hubs expand number of devices to 126 7-bit address =
5、 128 root hub reserved addr 0 Hubs can be bus or self-powered 100/500ma per downstream port,10 USB Facts,Fact 1 USB is not simple,Outside, it is simple The “rich user experience” requires some inner complexity Even though it replaces serial and parallel ports, its not a drop-in replacement Its elect
6、rically simple, but a whole protocol layer is added,Fact 2 The Host (PC) initiates all transfers,Devices respond to host requests Direction: OUT is host-to-device Direction: IN is device-to-host USB is NOT peer-to-peer,USB Topology (a),Device,Device,PC,USB,USB,USB Topology (b),PC,USB,USB,“Tiered Sta
7、r”,Hub,USB,USB,USB,USB,PC,USB,USB,Device,Device,Device,Device,Device,Fact 3 USB supports three speeds,Low speed is 1.5Mb/s Mice Keyboards Full speed is 12Mb/s Modems Cameras Speakers Printers High speed is 480 Mb/s USB 2.0 is coming,Fact 4 An Endpoint Is an Addressable FIFO,USB Spec: “a source or si
8、nk of data” CONTROL endpoint is bi-directional Others are uni-directional Four address bits plus a direction bit selects between 32 buffers (FIFOs) Different USB chips support Various numbers of endpoints Various buffer sizes,Fact 5 USB Supports Four Transfer Types,Bulk Guaranteed accuracy, but deli
9、very time is variable Best for “bursty” data Isochronous Guaranteed delivery time, but accuracy is not guaranteed Control Enumeration and device control Interrupt Predictable polling time,Fact 6 USB Transfers Occur in 1ms Frames,Host sends “SOF” (Start Of Frame) token every 1ms Host schedules packet
10、s inside frames. Isochronous data FIFOS “ping-pong” every SOF,Anatomy of a USB Frame,12MHz = 1.5MB/s or 1500 bytes/ms Isochronous/Interrupt traffic have guaranteed bandwidth Control traffic is “best-effort” Bulk uses what is left Actual scheduling order depends on host controller UHCI, OHCI,Video,Au
11、dio,Mouse,Control,Printer,Printer,1 msec frame,SOF,.,Isochronous,Interrupt,Control,Bulk,SOF,Fact 7 USB Data Travels in Packets,Identified by “Packet ID” (PID) Token packet tells whats coming Data packets deliver bytes Handshake packets report success or otherwise,USB Packets,Y,N,C,D,a,t,a,A,C,K,S,Y,
12、N,C,H/S Pkt,Data Packet,O,U,T,A,D,D,R,C,R,C,5,S,Y,N,C,E,N,D,P,Token Packet,I,N,A,D,D,R,C,R,C,5,S,Y,N,C,E,N,D,P,D,A,T,A,1,C,R,C,1,6,S,Y,N,C,A,C,K,S,Y,N,C,Data Packet,H/S Pkt,Token Packet,Status Stage,The SIE (Serial Interface Engine),Serial,Interface,Engine,(SIE),D+,Bytes,USB,Transceiver,D+,Fact 8 Bu
13、lk/Ctl/Int Transfers Have Error Recovery,Success = ACK token Error = No response (timeout) Busy = NAK token (try again later),Fact 9 Erroneous or Missing ISO Data Is Lost,No handshakes or retries Individual application must handle errors,Fact 10 USB Projects Require a Significant Code Development Ef
14、fort,Device side USB houskeeping firmware Application firmware Host side Driver (maybe) Application software The EZ-USB Development kit supplies help for both sides, especially the device side,Fact 11 (OK, 11 facts) Conform to a standard USB device class, and you wont need to write a driver.,BIG inc
15、entive Spend effort making firmware class-compliant In Win 98 HID (Human Interface Device) Speakers Comm Additional Win 2000 and Millennium devices Mass storage Still image Printer,8051 Primer,8051 Primer Assy lang access to Internal Registers,128 GP Registers,00,7F,Direct Addressing,Indirect Addres
16、sing,8051 Primer C access to Internal Registers,128 GP Registers,00,7F,Direct Addressing,Indirect Addressing,8051 Primer Assy lang access to 8051 External Memory,Data,Program,8051 Primer C access to 8051 External Memory,Data,Program,char code dig=3,5,7; num = dig2;,8051 Primer EZ-USB Internal RAM,8
17、Kilobytes RAM,FFFF,0000,8 Kilobytes RAM,FFFF,0000,7FFF,7FFF,8051 Primer EZ-USB COMBINES internal 8K Program & Data RAM,8 Kilobytes RAM,0000,RD#,PSEN#,7FFF,8051 Primer EZ-USB Control Registers and Endpoint buffers Assembly language access,0000,RD#,PSEN#,7FFF,7B40,All EZ-USB control and status registe
18、rs and endpoint buffers are here. Access them using the data pointer and movx instruction. Example: mov dptr,#OUT2BC mov a,#35 movx dptr,a,0000,RD#,PSEN#,7FFF,7B40,Xdata char OUT2BC _at_ 0x7FC9; . OUT2BC = 45;,8051 Primer EZ-USB Control Registers and Endpoint buffers C access,EZ-USB Architecture,Lea
19、rning USB The Conventional Way,USB Learning Continuum,Practice,Theory,Start,Finish,time,USB Learning Continuum,Practice,Theory,Learning USB Using the EZ-USB Chip,Start,Development Kit Contents,EZ-USB development board Prototyping board EZ-USB development software Control panel Keil tool set Assemble
20、r C compiler Limited (4 Kilobyte) code size EZ-USB Technical Reference Manual Anderson book Necessary cables,Development Flow,Bulk IN Transfer,xdata volatile char IN2BUF64 _at_ 0x7E00; xdata volatile char IN2BC _at_ 0x7FB9; xdata volatile char IN2CS _at_ 0x7FB8; #define bBSY 0x02char j,count; main()
21、 for (j=0; j64; j+) / fill IN2 buffer with incrementing countIN2BUFj=j; IN2BC = 64; / arm the first IN2 transfer count = 0; while (1) / foreverwhile (IN2CS / arm the next IN2 transfer by loading byte count ,Bulk OUT Transfer,main() while (1) while (OUT2CS / arm the next OUT2 transfer (BC=x) ,Assembl
22、y code: A Handy 8051 Macro,LDREG MACRO ad,val ; Load register with valmov a,valmov dptr,#admovx dptr,aENDM,Bulk IN Transfer (Assy Code),start: mov dptr,#IN2BUFmov r7,#64 ; fill EP2IN buffer with 64 bytes fill: mov a,r7movx dptr,ainc dptrdjnz r7,fill ; load decrementing counter starting w. 64mov coun
23、t,#0 ; count the IN transfersLDREG IN2BC,#64 ; load the IN2 Byte Count Register loop: JBL IN2CS,BSY,IN2 ; check the busy bit sjmp loop IN2: inc count ; IN packet counterLDREG IN2BUF,count ; put count into first buffer byteLDREG IN2BC,#64 ; this arms the IN transfer wait for next IN to complete,The U
24、SB Onion,USB Resources: Books,Devices EZ-USB Technical Reference Manual Anderson, USB System Architecture Both are included in your development kit Axelson, USB Complete, Second Edition Class text Very easy to read Examples using Cypress low-speed family Shows how to use Visual BASIC and HID driver,
25、USB Resources: Books,Devices (contd) Hyde, USB Design by Example Some host side detail USB Hardware and Software (Annabooks) Encyclopedic, dry, very complete Windows Drivers (including USB) Oney, Programming the Microsoft Windows Driver Model Cant, Writing Windows WDM Device Drivers,USB Resources: I
26、nternet,www.usb.org USB Specifications Draft docs Bulletin board Product information incl. For general questions,Homework Questions,What are the goals of USB? Can two computers be connected through their USB ports? Explain. Explain the differences between a serial port and a USB port. Why does USB use two different connector types? Where does a USB hub get its power? Why is there a USB low speed mode? Describe two scenarios where a USB device might not be recognized.,