USB (Universal Serial Bus)
by Denis Pelli
USB
has become the standard way to connect external devices to computers. The
alternatives — PCI Bus, Firewire, CardBus (PCCard), Ethernet, and BlueTooth — have
their uses, but aren't nearly as popular. USB 1.1 devices run at data rates
up to 1.5 MB/s; USB 2.0 devices run at data rates up to 60 MB/s. USB 2.0
ports accept both kinds of device. The USB implementers
forum has cleverly designed standard command vocabularies for broad categories
of use — e.g. human
interface device (HID), audio, video — which they call "classes",
allowing manufacturers to produce plug-and-play devices that can be supported
across platforms by a standard driver on each platform. Windows, Mac OS X,
and Linux provide standard USB drivers that comply with the HID and other
USB classes, so the device manufacturer doesn't have to provide any computer
software at all, just the USB-class-compliant firmware in the device. Mario
Kleiner recommends the wikipedia
one-page introduction to USB.
In July 2006 Apple published a very helpful article, with code, giving
a good overview of USB programming under Mac OS X. "Building
customized user client drivers for USB devices." Anyone doing Mac
OS USB development should also be aware of the helpful discussions
on Apple's usb mailing
list.
Controlling USB devices from Matlab seems very promising. It requires a mex file to connect our Matlab code to the generic USB driver provided by the operating system. Initially I thought that the more-specific HID-class within USB would be preferred, but, as discussed below, plain USB may actually be easier to work with than HID.
The Mac OS X version of the Psychtoolbox includes PsychHID.mexmac, which supports the HID (human interface device) USB class, allowing your MATLAB program to talk to any HID-compliant USB device. For more information about PsychHID and Psychtoolbox support of USB, type "help PsychHardware" in MATLAB. (Here's a pre-release copy of the recently updated Psychtoolbox/PsychHardware/Contents.m page that will be included in all Psychtoolbox releases after 12 April 2005. If you have an older release, you can safely update just this page now.)

The Daq
Toolbox provides a comprehensive set of 29 functions to control
the USB-1208FS
data acquisition device. This daq costs $150 and offers "50
kHz" input and output 12-bit sampling of analog voltages (8 in,
2 out) and 16 digital i/o lines. It's the size of a wallet and is powered
through its USB cable. We have complete control of it from within Matlab,
via the PsychHID extension. All these functions do their work by calling PsychHID.mexmac,
which has been enhanced to send and receive HID reports to control the
USB-1208FS and other HID-compliant devices. The software is free; you
can download it now from the Daq Toolbox page.
HID-class USB devices. "The Human Interface Device (HID) class
is one of several device classes that the USB standard describes. Mac OS X
developers can use the HID Manager to access HID-class devices, such as joysticks
and non-Apple displays." (Source: Apple's
HID page.) We list below some HID-compliant devices. We should be able
to talk to them using the Psychtoolbox PsychHID function. It appears that
many chip and product manufacturers are quickly adopting this standard, but
it seems to be advertised primarily as a USB chip feature to reduce the product
manufacturer's software development cost and is not usually advertised as
a feature to consumers, which makes it hard to discover whether a particular
product supports HID. In most cases I ascertained HID-compliance by sending
email to the device manufacturer's technical support. For more information
on HID, see the HID
for Linux page and Jan Axelson's
HID page.
help PsychHID % OSX
help PsychHardware
Most
keyboards, mice, trackballs, joysticks (Saitek),
and gamepads (e.g. Gravis, Logitech, Saitek)
are HID-compliant. Itamar Kahn writes, "I wanted to bring to your attention
a family of HID-compliant devices that are likely to work with PsychHID.
The X-keys devices are compatible with
Windows and Mac OS X. We recently purchased a USB Switch interface that maps
TTL pulses to keyboard/mouse responses. I still need to thoroughly test it
to figure out its latencies but it seems to work well."
I wanted a device that would enable my computer to measure and control analog voltages. This is called a DAQ, short for "data acquisition device". Searching the web for "USB DAQ", I found two that are HID-compliant. There are lots of new USB DAQs, but only a few are HID-compliant. (In most cases I had to contact technical support in order to ascertain whether the device is HID-compliant.) Only HID-compliant DAQs can be controlled through PsychHID. Frans Cornelissen noted that audio breakout boxes are closely related, so I searched for "USB audio" too.
Based on that search, I bought (25 February 2005) the Measurement Computing USB-1208FS. Technical support at Measurement Computing provided me with a USB-HID firmware specification for the USB-1208FS that allowed me to program it through the PsychHID extension. It's now working, and the resulting Daq Toolbox (the enhanced PsychHID and the M files supporting the USB-1208FS) will be included in the 1.06 release of OSX Psychtoolbox. You can download it now from the Daq Toolbox page.
As noted above, I originally thought HID-compliance would be the way to go, but a letter from LabJack tech support (quoted below) suggests that HID offers no advantage over simply using the standard USB "bulk" protocol to transmit/receive a block of data. It turns out that, after selecting the device, communication with any DAQ boils down to sending a block of data and receiving a block of data. Programming the USB-1208FS consists solely of sending and receiving HID reports. The LabJack people tell me that the USB "bulk" protocol is faster, and no harder to use, concluding: "We do not plan to use HID on any future devices. The performance is too limiting, and the HID drivers (provided by Microsoft) are not particularly efficient. LabJack and our customers have found that basic bulk endpoints are actually easier to handle on Mac and Linux."
Thus someone may wish to write a new mex file, perhaps called "PsychUSB", that would use the USB bulk protocol to send and receive a block of data. It would support all USB DAQs for which the manufacturers will supply the necessary USB programming specifications (i.e. what the blocks of data sent and received should contain). This would implement generic USB access just once (for each platform) as "PsychUSB", a C-code mex file that calls the computer operating system's USB driver. Subsequently anyone can write portable Matlab M code to support each new USB device. (This is the path Allen pioneered for HID, applied to plain USB.)
To support each new USB device (HID-compliant or not), the key requirement will be availability of a manual or a sample software source file that spells out how to control the device using USB protocols, i.e. what should be in the blocks of data that are sent and received. Some device manufacturers (e.g. LabJack and Measurement Computing) happily provide this information. Others (e.g. IOTech) refuse because it's "proprietary".
USB SPEED. Apple Engineer Rhoads Hollowell noted on the USB forum that not all Macs are USB High-Speed capable. Look in the USB pane of Applications:Utilities:System Profiler. If you do not see "USB High-Speed Bus" then your computer does not provide High-Speed USB (up to 480 Mb/s) and will run at "Full-Speed" USB (up to 12 Mb/s) instead. The 17" G4 PowerBook does provide High-Speed USB.
Further reading:
Jan Axelson's USB
Complete (2nd Ed.)
gets
rave reviews. I found it helpful, but it's written for a developer of
a USB device, not a host programmer.
Pangea
Software's Ultimate Game Programming Guide for Mac OS X
has
a chapter on the Mac OS X HID Manager.
Apple's USB forum. Apple Engineers answer questions about how to write USB and HID drivers.
TABLE 1. Useful USB devices that could be controlled by our Matlab programs. Do you know of one that isn't listed here? Please report it to the forum. The Mac Music web site is a good resource for USB Audio. In evaluating whether these DAQs meet your needs, consider attaching several to your computer. They're cheap.
| DAQ. USB, HID-compliant. Measurement Computing USB-1208FS, released January 2005, sells for $149. 8 analog input channels, 12-bit resolution, 50 kS/s, two D/A outputs, and 16 digital IO lines, with signals brought out to screw terminals. It doesn't come with any Mac software, but it's a USB 2.0 device fully controlled through the HID-class USB driver, so we use PsychHID to control it. Now fully supported by the Daq Toolbox for OS X MATLAB. (The 16-bit model USB-1608FS costs $399.) According to their technical support, all of their USB-xxxx devices are fully controlled through HID when used on Windows computers. The older and slower USB-1208LS is virtually identical to the LabJack U12. Dr. Warren Jasper (at North Carolina State University) provides Linux drivers for the USB-1208LS, and will soon provide them for the USB-1208FS and USB-1608FS as well. | |
DAQ. USB, HID-compliant. The LabJack U12, released January 2002, sells for $119 (minus 10% educational discount). 8 single-ended or 4 differential 12-bit analog inputs, gains of 1, 2, 4, 5, 8, 10, 16, or 20 V/V, 1.2 kSamples/s, 2 analog outputs, 20 digital i/o (up to 50 Hz), 32-bit counter. USB 1.1, HID-compliant. The manufacturer doesn't provide any Macintosh support, but they have an active section of Mac users in their user forum, providing a free driver with complete source code in C (message titled "OS X 1.1 Users bits and source"). "The U12 is HID 1.1 compliant. Protocol information can be found on our Linux, Mac, or PocketPC forums." (Source: LabJack technical support.) The $399 16-bit model UE9 has
USB 2.0 and ethernet interfaces but is not HID-compliant. "The
UE9 does not use the HID class. Instead is uses simple Bulk endpoints,
and the low-level protocol is fully spelled out in the UE9
User's Guide |
|
DAQ. USB, but not HID-compliant. None of the National Instruments DAQ devices (e.g. USB-6008) are HID-compliant. The only way to communicate with them is with the NI-DAQmx Base driver. (Source: National Instruments technical support.) Furthermore, the C software can't control all the parameters relevant to analog data acquisition (according to their development engineers as reported at the National Instruments forum). For full control you must use their LabView application. |
|
![]() |
DAQ. USB, but not HID-compliant. None of the Data Translation ECON series DAQs are HID-compliant. E.g. Data Translation DT9812-10V for $299. USB 2.0, 16SE/8DI 12-bit analog input, input range ±1.25, 2.5, 5, 10 V, at 40 kS/s, 16 DIO at 50 Hz. LabView compatible. |
| Audio interface. USB, HID- and Audio-compliant. BRIDGECO Audio breakout box SDK (BEBOB - USB2.0) No price. "USB HID Device Class. The Human Interface Device Class support allows users to easily control the device, as well as allowing the device to control applications on the PC or Mac. The USB Audio Device Class support enables true plug-and-play with the Windows XP and Mac OS X operating systems, using the USB audio drivers built into the OS." [They don't seem to have a retail product. They are selling their chips and software to manufacturers. I include this here as a placeholder, documenting the trend, until someone can locate a consumer product that we can order. HID-compliance is great, making it compatible with PsychHID. However, compliance with the USB Audio class is also attractive. I think the Mac and Win operating systems automatically recognize and use for sound any device that is USB-Audio compliant. Thus any software that controls sound now will automatically use the new device.] | |
| Audio interface. USB, probably Audio-compliant. Edirol/Roland
UA-1EX 24-bit/96-kHz USB Audio Interface $79. The specs, packaging,
and price are attractive. I don't know whether it's HID-compliant.
I think that it is USB-audio-compliant and that the Mac and Win operating
systems automatically recognize this and use it for audio, so one could
achieve this high level of performance (24-bit stereo sampled at 96
kHz, in and out) using the existing sound interfaces we already have
in Matlab: Snd, SOUND, AUDIOPLAYER, and AUDIORECORDER. Note that audio
digitizers usually incorporate a high-pass filter rejecting inaudible
frequencies below 20(?) Hz. http://www.rolandus.com/products/productdetails.aspx?ObjectId=743 http://www.academicsuperstore.com/market/marketdisp.html?PartNo=747199 |
|
| Audio interface. USB, probably Audio-compliant. M-AUDIO
transit $80."Small enough to fit in your pocket, Transit brings
hi-resolution 24-bit/96-kHz recording and playback to any USB-compatible
computer." http://www.minidisco.com/transit.html |
|
| Ploytec sells low-latency USB audio drivers for Macs (7 ms) and Windows (4 ms). |
visitors
since 15 February 2005.
David
Brainard, Denis Pelli & Allen
Ingling.
psychtoolbox@yahoogroups.com
Updated