Embedding USB
Lane Hauck
Cypress Semiconductor
Agenda
Why USB?
USB Advantages
USB Basics
• With embedded system emphasis
An Implementation Roadmap
USB On The Go
An Embedded Host Example
Conclusion
Embedding USB Embedded Systems Conference, March 2002 1
Agenda
Why USB?
USB Advantages
USB Basics
• With embedded system emphasis
An Implementation Roadmap
USB On The Go
An Embedded Host Example
Conclusion
Embedding USB Embedded Systems Conference, March 2002 2
Why USB?
No other way to talk to so many things
• Input devices
– Mice, keyboards, joysticks
• Memory devices
– Disk drives
– Memory cards
• Fingerprint scanners
It‘s standard
• USB Implementers Forum, Inc.
• Plugfests
Embedding USB Embedded Systems Conference, March 2002 3
Agenda
Why USB?
USB Advantages
USB Basics
• With embedded system emphasis
An Implementation Roadmap
USB On The Go
An Embedded Host Example
Conclusion
Embedding USB Embedded Systems Conference, March 2002 4
USB Advantages
Many standardized, low cost devices
are available
Connectors and cables are standard
USB devices are compliance-tested to
insure compatibility and uniform
performance
Using hubs, multiple USB devices can
share one connector
USB devices can tell you about their
capabilities
Embedding USB Embedded Systems Conference, March 2002 5
USB Advantages
(Cont’d)
USB provides device power
USB devices can:
• Be put to sleep
• Be awakened
• Provide a system wakeup call
USB protocol takes care of low-level
details
• Flow control
• Error checking
You decide how complex to make the
firmware
Embedding USB Embedded Systems Conference, March 2002 6
Agenda
Why USB?
USB Advantages
USB Basics
• With embedded system emphasis
An Implementation Roadmap
USB On The Go
An Embedded Host Example
Conclusion
Embedding USB Embedded Systems Conference, March 2002 7
USB Basics
with embedded system emphasis
Speeds
Cables
Topology
Endpoints
Transfer Types
Handshakes
Embedding USB Embedded Systems Conference, March 2002 8
USB Basics
cont’d
Hubs
Power Management
Device Classes
Host Chips & Drivers
Protocol
Embedding USB Embedded Systems Conference, March 2002 9
Speeds
USB 1.0 and 1.1
• Low speed is 1.5 Mbits/sec
– Packet size is limited to 8 bytes
– Transfer type limited
– Don‘t want to wait slowly
• Full speed is 12 Mbits/sec
USB 2.0
• ―Hi-speed‖ is 480 Mbits/sec
USB 1.x retro-named ―Classic‖ USB
12 Mb/s is great for embedded systems
Embedding USB Embedded Systems Conference, March 2002 10
Cables
Shielded 4-wire cables
• Vbus = 4.4-5.25 volts
• GND
• D+ and D-
Reflected-wave signaling
• Low power buffers
5 meter limit
• Hubs extend effective cable length
Cables are standard, plentiful and cheap
Embedding USB Embedded Systems Conference, March 2002 11
Cables
1 foot cable
Driver
Receiver
Embedding USB Embedded Systems Conference, March 2002 12
Cables
2 meter cable
Driver
Receiver
Embedding USB Embedded Systems Conference, March 2002 13
Cables
5 meter cable
Driver
Receiver
Embedding USB Embedded Systems Conference, March 2002 14
Topology
tiered star
PC
USB Host
Root Hub
P
Hub
P P
P
Hub
P P
P
One USB connector can support up to 127
devices
Embedding USB Embedded Systems Conference, March 2002 15
Endpoints
Endpoints are logical channels inside a
peripheral device
In hardware they‘re addressable
buffers (FIFOS)
• Every host transfer includes a 4-bit
endpoint number and a direction bit
CONTROL endpoint 0 is mandatory
All others are up to the design
Endpoints are handy for separating
data types
Embedding USB Embedded Systems Conference, March 2002 16
Transfer Types
four types to suit data requirements
Control
• Mission-critical, highest error protection
• Used to interrogate, configure and control
devices
Bulk
Interrupt
Isochronous
BULK is easiest to use and the most flexible
Embedding USB Embedded Systems Conference, March 2002 17
Transfer Types
anatomy of a USB frame
1 msec frame
...
SOF
SOF
Video Audio Mouse Control Printer Printer
Isochronous Interrupt Control Bulk
• 12MHz = 1.5MB/s or 1500 bytes/ms (full speed)
• Isochronous/Interrupt traffic have guaranteed bandwidth
• Control traffic is ―best-effort‖
• Bulk uses what is left
Embedding USB Embedded Systems Conference, March 2002 18
Handshakes
ACK means ―success‖
NAK means ―try again‖
No response means error
Handshakes used for
• CONTROL
• BULK
• INTERRUPT
You don’t need to implement flow control
Embedding USB Embedded Systems Conference, March 2002 19
Handshakes
a CONTROL Transfer
H H P
S D C
SETUP
A E C
E A 8 bytes R A H is Host
D N R
T T Setup C C
D D C P is Peripheral
U A Data 1 K
R P 5
P 0 6
Token Packet Data Packet H/S Pkt
H P H
D C
DATA
A E C
A R A
I D N R Payload
T C C
N D D C Data
A 1 K
R P 5
1 6
Token Packet Data Packet H/S Pkt
H H P H H P
HANDSHAKE
D C D C
A E C A E C
O A R N O A R A
D N R D N R
U T C A U T C C
D D C D D C
T A 1 K T A 1 K
R P 5 R P 5
1 6 1 6
Token Packet Data Pkt H/S Pkt Token Packet Data Pkt H/S Pkt
(Operation not completed) (Operation completed)
Embedding USB Embedded Systems Conference, March 2002 20
Hubs
PC PC
USB USB USB USB
Hub
Device Device Device
USB USB USB USB
Device Device
Embedding USB Embedded Systems Conference, March 2002 21
Hubs
power
PC
USB USB
500 500
Hub
Device
USB USB USB USB
100 100
Device Device
Embedding USB Embedded Systems Conference, March 2002 22
Hubs
more power
PC Wall Wart
USB USB
500 500
Hub
Device
USB USB USB USB
500 500
Device Device
Embedding USB Embedded Systems Conference, March 2002 23
Hubs
more devices
PC Wall Wart
USB USB
500 500
Hub
Device
USB USB USB USB USB USB USB
500 500 500
Device Device Device
Easy way to expand and power devices
Embedding USB Embedded Systems Conference, March 2002 24
Power Management
USB power consumption is carefully
controlled
Host determines power needs of all attached
devices
• Only configures those within power limits
USB devices must enter low power mode
when host signals ‗suspend‘
• 500 microamps
USB devices can signal a remote wakeup
Built-in mechanism prevents peripherals from
drawing too much power from your system
Embedding USB Embedded Systems Conference, March 2002 25
Device Classes
USB-sanctioned method to extend the
specification to device classes
Classes are devices with similar
characteristics
Huge advantage—the driver is built into
the OS
• Not written and maintained by YOU
Immense simplification of the software effort if the
OS supports your device
Embedding USB Embedded Systems Conference, March 2002 26
Protocol
USB architects went well beyond the
electrical interface
• Remember IrDa?
Protocol layer insures interoperability
Compliance testing (plugfests) enforce the
protocol
USB protocol is extensible
• Device classes
• Fully custom ‗vendor‘ designs
Device standardization makes the embedded
software task far easier than writing custom drivers
Embedding USB Embedded Systems Conference, March 2002 27
Protocol
A large part of the embedded system
design is understanding the protocol
How much do you need to support?
• How many devices
• How many device types
• How many hub layers
Embedded USB is by necessity a
subset of full PC-based USB
Efforts are underway to simplify the software
task (more about this)
Embedding USB Embedded Systems Conference, March 2002 28
Protocol
the eight bytes in a SETUP packet
Byte Field Meaning
0 bmRequestType Request type
1 bRequest The actual request
2 wValueL Varies by request
3 wValueH
4 wIndexL Varies by request
5 wIndexH
6 wLengthL Number of data bytes
7 wLengthH
Embedding USB Embedded Systems Conference, March 2002 29
Protocol
Chapter 9 Requests
bmRequestType
X 0 0 X X X X X X 0 1 X X X X X X 1 0 X X X X X X 1 1 X X X X X
Standard Class Vendor STALL
bRequest
00 GET_STATUS Custom
Programming
01 CLR_FEATURE
03 SET_FEATURE
05 SET_ADDRESS
06 GET_DESCRIPT0R wValueH
01 DEVICE
07 SET_DESCRIPT0R
02 CONFIGURATION
08 GET_C0NFIG
03 STRING
09 SET_CONFIG
0A GET_INTERFACE
0B SET_INTERFACE
0C SYNC_FRAME
other STALL
other STALL
Embedding USB Embedded Systems Conference, March 2002 30
Protocol
added HID Class Requests
bmRequestType
X 0 0 X X X X X X 0 1 X X X X X X 1 0 X X X X X X 1 1 X X X X X
Standard Class Vendor STALL
bRequest bRequest
00 GET_STATUS Custom
01 GET_REPORT Programming
01 CLR_FEATURE 02 GET_IDLE
03 SET_FEATURE 03 GET_PROTOCOL
05 SET_ADDRESS 09 SET_REPORT
06 GET_DESCRIPT0R 0A SET_IDLE
wValueH
01 DEVICE
07 SET_DESCRIPT0R 0B SET_PROTOCOL
02 CONFIGURATION
08 GET_C0NFIG other STALL
03 STRING
09 SET_CONFIG
21
0A HID
GET_INTERFACE
22 REPORT
0B SET_INTERFACE
23 PHYSICAL
0C SYNC_FRAME
other STALL
other STALL
Embedding USB Embedded Systems Conference, March 2002 31
Host Chips & Drivers
PC‘s use OCHI/UHCI host controllers
• Usually part of a ‗Southbridge‘ chip
• Driver software is part of the OS
– Class drivers
– USBD.sys
– HAL: OHCI.sys or UHCI.sys
Embedded systems use different,
proprietary host controllers
• Hardware abstraction layer is custom
Embedding USB Embedded Systems Conference, March 2002 32
Host Chips & Drivers
Application Application
Class Driver
IRP
USB Driver
Host Controller Driver
Transaction List
Host Controller Host Controller
PC Simple Embedded
Embedding USB Embedded Systems Conference, March 2002 33
Host Chips & Drivers
PC implementation
Apps or class drivers create IO
Application Request Packets (IRP)
USB Driver
Class Driver
• Processes IRP‘s
IRP • Manages CONTROL pipe
• Division of duties between USBD
USB Driver and HCD is not defined
Host Controller Driver (HCD)
Host Controller Driver
• Provides abstraction
– Of host controller registers
Transaction List
– Of data transfer specifics
• HCD creates a transaction list
Host Controller – Schedule of transfers
Host Controller turns transaction
list into packets
Embedding USB Embedded Systems Conference, March 2002 34
Host Chips & Drivers
why the PC architecture is complex
App needs no knowledge of
Application
• Which host controller is installed
Class Driver
• USB signaling details
• Bus utilization details
IRP
• Transaction lists
USB Driver App talks to buffers in your device
using ―pipes‖
Host Controller Driver
Most of the HCD/HC complexity is
Transaction List due to the flexible nature of USB
• 127 devices
Host Controller • Devices appear and disappear at
random
• Bandwidth must be fairly shared
between devices
Embedding USB Embedded Systems Conference, March 2002 35
Host Chips & Drivers
simplest embedded hardware
Looks simple Application
The hardware is simple
But software is ―low-level‖
• Detect USB device attachment
• Detect speed
• Enumerate devices
• Suspend and resume devices
• Manage error recovery
• Generate USB resets
Host Controller
• Conduct transfers
Simple Embedded
Embedding USB Embedded Systems Conference, March 2002 36
Host Chips & Drivers
RTOS provides a ‘middle’ solution
• Examples Application • Host Controller
– WinCE vendor may
– Linux provide a small
Class Driver
– VxWorks driver to make
the chip look
like something
RTOS with a
USB the RTOS
"Stack"
supports
Host Controller
Embedding USB Embedded Systems Conference, March 2002 37
Agenda
Why USB?
USB Advantages
USB Basics
• With embedded system emphasis
An Implementation Roadmap
USB On The Go
An Embedded Host Example
Conclusion
Embedding USB Embedded Systems Conference, March 2002 38
An implementation
roadmap
Pick a host controller
For simple interface (a few simple devices)
• Write custom firmware
• Use chip supplier tools
– Compilers, debuggers
– Sample code
For more general interface
• Use an RTOS with USB support
• Chip supplier usually provides hardware drivers
• Check the supported device class list
Embedding USB Embedded Systems Conference, March 2002 39
Agenda
Why USB?
USB Advantages
USB Basics
• With embedded system emphasis
An Implementation Roadmap
USB On The Go
An Embedded Host Example
Conclusion
Embedding USB Embedded Systems Conference, March 2002 40
USB On The Go
goals
Connect two peripherals together
• PC is not required (but still supported)
Allow peripherals to awaken each other
• Session Request Protocol (SRP)
Allow devices to exchange the host role
• Host Negotiation Protocol (HNP)
New OTG devices can tap into the
existing 900 million USB devices
• Forecast: 1 billion devices by 2003
Embedding USB Embedded Systems Conference, March 2002 41
USB On The Go
marketing foil
Embedding USB Embedded Systems Conference, March 2002 42
USB On The Go
hardware details
Defines a new connector and cable
• ―Mini-AB‖ receptacle
• Mini-A to Mini-B cable
• Cable establishes the default host
– A-Device is the default host
– Host turns on VBUS
Dual-role USB devices:
• Sometimes a peripheral, sometimes a host
• Must use the new AB connector
• Provide limited host capability
• Supply 8 mA (min) on VBUS
• Operate at full speed (high speed optional)
Embedding USB Embedded Systems Conference, March 2002 43
USB On The Go
the best news may be on the software side
Impossible for a small battery operated
device to support everything a PC does
Solution: a Targeted Device List
• Simplifies and standardizes device classes
• Example: OTG printer class
This effort is underway in the OTG Device
Working Group
Moves complexity from your application code and
drivers into the Operating System
Embedding USB Embedded Systems Conference, March 2002 44
Agenda
Why USB?
USB Advantages
USB Basics
• With embedded system emphasis
An Implementation Roadmap
USB On The Go
An Embedded Host Example
Conclusion
Embedding USB Embedded Systems Conference, March 2002 45
USB peripheral dev tools
Serial
Develop--Debug PC USB
Test USB
Full-speed USB
Development Board
Embedding USB Embedded Systems Conference, March 2002 46
Simple embedded host
Serial
Develop--Debug PC USB
Test Host
Transfers
Add-on board
contains USB host
plus 4-port hub
Full-speed USB
Development Board
Embedding USB Embedded Systems Conference, March 2002 47
Simple embedded host
Embedding USB Embedded Systems Conference, March 2002 48
Simple embedded host
Embedding USB Embedded Systems Conference, March 2002 49
Simple embedded host
Embedding USB Embedded Systems Conference, March 2002 50
Simple embedded host
Example:
BTC Keyboard + 2-port Hub with
embedded Fingerprint Scanner
– Addr1 = 2 port Hub
– Addr2 = Fingerprint Scanner
– Addr3 = Keyboard
Fuji Digital Still Camera
– Addr4 = Mass Storage Class (MSC)
Mitsumi Mouse
– Addr5 = HID Class
– Data being read through EP1
Embedding USB Embedded Systems Conference, March 2002 51
Simple embedded host
(live example)
Embedding USB Embedded Systems Conference, March 2002 52
Conclusion
Embedded host chips are here now
Software is the main effort
Point solution is easy
More complex solution requires an
RTOS with USB support
• Third-party choices
• Some classes are supported
USB OTG aims to provide simplified
and standardized host drivers
Embedding USB Embedded Systems Conference, March 2002 53