October 11, 20001
2 Firmware for USB 2.0 Ryan Augustin Netchip Technology, Inc
October 11, Agenda USB 2.0 device-side programming w New USB 2.0 features: a programmer’s glossary w Choosing a chip from a programmer’s perspective w Performance: fast transfers, race conditions w Development aids: – Standard tools (USB analyzer) – Manufacturer’s Tools and support w Your “Rev 1” board: Tips, hints, steps to success
October 11, USB 2.0 Glossary, Features – For the Chip Programmer Glossary w CHIRP- Device/host: Who’s 2.0, who’s 1.1? w PING- Better way to NAK OUT packets w Micro-frame- 8 micro-frames per frame w?-w?-w?-w?- USB 2.0 features for chip programmers w 480 Mbit/s- 40 times faster than USB 1.1 w Bulk/Int: 512- Bulk and Interrupt MaxPkt now 512 w ISO: ISO MaxPkt now 1024 (up to 3 per micro frame)
October 11, USB 2.0 Max Packet Size 1024 (1,2 or 3 per µframe) 1023 (one per frame) ISO 64 8, 16, 32, 64 CONTROL (bMaxPacketSize0) 512 / , 16, 32, 64 BULK / INTERRUPT USB 2.0 USB 1.1 wMaxPacketSize
October 11, Choosing a Chip – Programmers Perspective Get involved choosing the chip w Understand the hardware designer’s restrictions w Understand your task w Can the chip help you and the HW designer? – Pitfalls: e.g. The “endianism” problem w Can the chip maker help? – Development kits – Support policy
October 11, Choosing a Chip – Transfers and Hardware w Programmed I/O (Firmware transfer loop): – Transfer Rate: One pass of a “for” loop – End conditions: u Packet level: How much overhead? u Transfer level: ZLPs and dribble bytes – Write samples (Surprise!) w DMA – End conditions w Deep FIFOs – The “buzz” – Double depth (2 x 512 byte) minimum – FIFO allotment
October 11, Transfer Firmware – Programmed I/O w IN and OUT transfer firmware: – Interrupt handlers – Arbitrary length transfers (zero to a zillion+1) – Fast! (any type: Iso, Bulk, Interrupt, Control) w Critical considerations: – End of Transfer handling: Short Packets and ZLPs – Handling asynchronous nature of the USB – “Dribble” bytes (on 16 and 32 bit CPUs)
October 11, Transfer Firmware – IN Transfer Usb_Interrupt(): For arbitrary length IN transfer w Read IntStat – Which endpoint interrupted? w Write EpStat – Re-arm endpoint interrupt w Read EpStat – Check for Short Packet u (Short packet? Finish and return) w Read EpCount – Get available FIFO space w Copy – “for” loop w Return from Interrupt u Deep FIFOs
October 11, Transfer Firmware – OUT Transfer Usb_Interrupt(): For arbitrary length OUT transfer w Read IntStat – Which endpoint interrupted? w Write EpStat – Re-arm endpoint interrupt w Read EpCount– Get FIFO count (in bytes) w Read EpStat– Check for Short Packet u (Short Packet? Re-read EpCount) w Copy – “for” loop (32-bit) w Return from Interrupt u Short Packet (ZLP) detection u NAK OUTs after Short Packet
October 11, Transfer Firmware – Summary w Write critical transfer code to find weak spots – ZLPs – Dribble bytes – Race conditions w See for yourself: Get a development kit
October 11, Development Kits USB 2.0 Host USB 2.0 “Device” USB Cable NET2290 Development Kit for a USB 2.0 device PCI card with NET2290 Plugged into Windows PC
October 11, Development Kits – Highlights w Goals of the kit: – Show the chip in action – Provide a good HW platform for learning firmware – Provide “Ready to port” sample source code w Allows development without hardware: – Develop your firmware without hardware – Develop host drivers and apps without hardware – Prove concepts without hardware w Includes host-side and device-side tools
October 11, Development Kits – Device-Side Software Tools USB device-side monitor application w Interactive and intuitive w Comprehensive command set w Full control of: – Chip register content – Transfer content (fill, copy, compare, …) – Transfer size – EP0: Standard, Vendor Specific; Read and Write w Multiple commands w Can you repeat that?
October 11, Development Kits – Host-Side Software Tools USB Host-Side Monitor Application w Interactive and intuitive w Comprehensive command set w Full control over transfers: – Transfer content (fill, copy, compare, …) – Transfer size – EP0: Standard, Vendor Specific; Read and Write w Multiple commands w Can you repeat that? w Loopback tests
October 11, Your New USB 2.0 Device – Rev 1: Prototype Board w Relaxed board dimensions w Header pins w Diagnostic serial port w Room for emulator pod (if applicable) w Board layout and USB signal quality
October 11, Your New USB 2.0 Device Development Environment A good development environment is priceless w Tools: – USB bus analyzer – USB host-side program w printf() w History() – Minimal run-time penalty – Simple: It stores a text key and few parameters u Example History(“TxPk”, ExpectLen, GotLen, pBuf); – Call History() anywhere in your code – Make a HISTORY() macro (add or remove at compile time) – Examine History memory after problem occurs
October 11, Your New USB 2.0 Device – History() Module typedef struct _HISTORY_BUF { char* Text; Arg1; Arg2; Arg3; } HISTORY_BUF #define HISTORY_ENTRIES 1000// Number of historical events static HISTORY_BUF HistoBuf[HISTORY_ENTRIES];// History is recorded here static HistoryCount = 0;// Current historical event void History(char * Text, Arg1, Arg2, Arg3) { memcpy(&HistoBuf[HistoryCount].Text, Text, 4);// Text key (E.g. “TxCo” HistoBuf[HistoryCount].Arg1 = Arg1; HistoBuf[HistoryCount].Arg2 = Arg2; HistoBuf[HistoryCount].Arg3 = Arg3; HistoryCount++;// Prepare for next event if (HistoryCount >= HISTORY_ENTRIES) { if (HistoryCount >= HISTORY_ENTRIES) { HistoryCount = 0;// History rewrites itself } memset(&HistoBuf[HistoryCount], 0, sizeof(HISTORY_BUF));// “Easy to spot” terminator }
October 11, Your New USB 2.0 Device – Bringing up the Board Take small steps! w Prove register access w Prove interrupt subsystem w Vendor Specific enumeration w Basic IN and OUT transfers w Loopback transfers w Special transfers w Special enumeration
October 11, Your New USB 2.0 Device – Enumeration w Everything has to work at the same time w You can’t single step (except PDK) w Things that can cause enumeration failures: – Set Address request (gotcha!) – Signal quality – Descriptor content … and plenty more! w Use Vendor Specific enumeration first
October 11, Your New USB 2.0 Device: - Bringing up the Board Take small steps! w Prove register access w Prove interrupt subsystem w Vendor Specific enumeration w Basic IN and OUT transfers w Loopback transfers w Special transfers (Control, ISO, Interrupt) w Special enumeration (for class devices) Debugger “Gotcha”
October 11, NET2290 – A Few Special Features w Built-in Standard Request handlers “Automatic Enumeration” Class devices must handle Get Configuration w Self configuring FIFOs w Ready signal on FIFO access … and more!
October 11, ? ? Question and Answers ? ? ? ? ? ? ? ? ? ? ? ? ? ?
October 11, Thank You! From Ryan Augustin and… NetChip Technology, Inc.