Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

Al Williams

Dr. Dobb's Bloggers

USB Clickity Clack

March 02, 2012

If you read this space regularly, you know I've been on a mission lately to build up some simple tools to work with USB on embedded systems. This week, I got some motivation from an unexpected source: a keyboard made for gaming enthusiasts.

I don't think of myself as especially old fashioned. However, I do have affection for several old technologies (after all, I do collect slide rules). One of the things I really love is the old-style "clickity clack" mechanical computer keyboards. I don't know if it is the placebo effect or not, but my typing speed goes up by about 20% when I have a mechanical keyboard.

The problem is, the world has changed since these keyboards had their hay day. The two mechanical keyboards I own are getting pretty shabby. Even when you clean them up, they need an adapter to hook into a PS/2 port (they use the big old-style keyboard connectors) and they don't have things like Windows keys and media control keys.

I wind up swapping every few months. I get frustrated with the big heavy old keyboard, switch it for something new, and then get sick of that in (usually) a few weeks and switch back. You can buy mechanical keyboards still, but they are typically expensive and I haven't really found one I liked well enough to make me blow that much money.

The other day, however, I was in the local computer store and saw a RAZER Blackwidow keyboard. It had a great mechanical feel to it and was about US$80. The only downside is it was billed as a "gamer keyboard" so the style of it isn't going to fit into a Wall Street office. Still, for a gamer keyboard it isn't too loud and I thought it would work fine in my office at home.

When I got it home I still appreciated the feel, but I had forgot one little detail. All the keyboard's special keys relied on the Windows software supplied with the keyboard. I don't use Windows, so naturally I was a little miffed, if not surprised.

On the other hand, a keyboard is just a USB human interface device (HID) and I have been worrying with USB interfaces lately. So it turned into a project. Naturally, there aren't any specs for the keyboard that I could find at that level of detail. However, a quick search turned up someone who had reverse engineered what the Windows software sends to the keyboard to enable the keys and wrote a program to turn it on.

Problem solved, right? Well, sort of. First, the program was in Haskell. I write in a lot of different languages, but Haskell isn't one of them. Second, I really wanted to look at the host side of USB programming and this was as good of an excuse as any.

When you build up a USB device, you need to have some software on the host computer to interact with it. In the last few weeks, I've talked about using USB to RS232 chips, in which case you just treat the thing like a plain old serial port. At the other extreme would be a completely custom USB device with your own drivers for whatever operating systems you wanted to support.

There is, however, a middle ground. The USB standard defines several types of devices with preset behaviors. This is why you can plug in a USB mouse or keyboard or camera or disk drive and expect it to work with no real effort. While any of these could be useful, many embedded systems (at least the ones where you might have used a serial port in the past) can use the USB HID (Human Interface Device) drivers. Normally, a HID is a mouse or a keyboard (or a VR glove, or a graphics tablet, or something similar). However, there's no reason you can't define a HID device that accepts and produces data — not all HID devices really talk to a human. Your program can use the HID drivers to query and command the remote system.

In this case, the embedded system of interest isn't one I designed, it is the microcontroller inside the Blackwidow keyboard. All USB devices have a vendor ID and a product ID. They can also have a serial number. These three things uniquely identify a device on the bus. On Linux, the lsusb command will show you data about all connected devices. Here's the output from my keyboard:

Bus 001 Device 032: ID 1532:010e Razer USA, Ltd

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.