14.1 USB, An Introduction

Your Ad Here

Whenever someone asks me to explain the subject of USB, I am petrified. The protocol is an absolute monster. And as if this was not bad enough, specialty devices such as HID or Audio class devices have their own communication rules on top of the vanilla USB protocol. I would say that the USB protocol is akin to the US tax code; it is very bloated and overly complex. However the difference is that USB actually works quite well, whereas the USB tax code… well that’s another subject matter all together. So the question is: how do you tackle a subject as broad as USB?

Setting the Scope

After years of playing around with USB, I still don’t think I fully understand how the thing works. The seminal literature on the subject matter, surprisingly is NOT the USB standard 2.0 which is available here, but a book called “USB Complete” by Jan Axelson. It is now in its 4th edition, and I’m sure Ms. Axelson can attest that every time the USB-IF creates a new standard, he gets a whole bunch of extra book sales. All in all, it is a pretty good book, but not perfect. I feel that it lacks some full examples from design to implementation. After reading the book, I still needed a lot of research on my own before I was capable of making a working device. Ms. Axelson’s website is a wealth of information on the subject of USB and is required viewing for all interested on the subject of USB.

With that out of the way, we need a baseline on what to expect in this tutorial. First, the tutorial is by no means a COMPLETE overview of the USB implementations with the PIC24 series of devices. I do not understand half the stuff on USB, and I have never tried doing complicated stuff such as USB audio or USB host. However, I do have plenty of experience in making USB human interface devices (HIDs). At the time of the writing of this article, I work for a company that makes joysticks as part of their product line. You can imagine that we deal with HIDs on a regular basis. The truth of the matter is that an HID will satisfy the majority of all USB device requirements. Moreover, the real joy of HID type devices is that drivers on the host side is already implemented regardless of whether you are on Windows, Mac or Linux (half the work is already done before you even started!).

The Implementation Approach

I’m sure none of my readers want to hear me babble about USB. As with most of you, when I’m reading a technical article, I just want to get my information and move on. So, here’s the gist of USB in one short sentence: the protocol is a complex mess and don’t expect to understand it.

Fortunately, you don’t have to understand USB with the USB software stacks offered by most microcontroller vendors. This is exactly the implementation approach that we will take to make our own USB device. I would still recommend reading Ms. Axelson’s book, as some of the concepts may include references to terminology used in the USB protocol. Microchip developed a USB software stack across their entire microcontroller lines when they released the USB based PIC18s a few years ago. The USB stack has become relatively stable over the years, and is very mature as this point in time. I will attempt to make a custom USB device by taking one of Microchip’s examples, and modifying the code. At that point, any good engineer will be able to take the same approach and create their own custom devices. Only the PIC24F series of controllers are used in the examples as no PIC24H controllers support USB.

The USB stack is included in the Microchip Application Library. You will need to install the library before proceeding any further. The library can be found here.

A Look at the Microchip USB Framework

After installing the library, take a look at its contents. There is a huge list of premade USB projects already. You just need to make slight modifications to these projects to make them your own.
The folder “../[Microchip application library]/Mircrochip/USB/” contains the actual USB stack. The other directories whose names begin with “Device” contain just the USB project and implementation files that use the USB stack (eg. ../[library]/USB/Device – HID – Joystick/). That’s all we need to know about the application library for now.

The USB Enumeration

Enumeration is the process by which a host accepts a USB connection from a device. When you plug device into a Windows based computer for the first time, it has to install some drivers. If a driver is found, it will make a noise saying that a device has been successfully connected to the host. Once this has occurred, you will be able to find the device under Device Manager.

Your Ad Here

Enumeration is by far the hardest part of implementing USB. So many things must occur just right and if one of them goes wrong, the enumeration fails. You’ll end up with an improperly configured device.

The USB HID Class

The HID Class specification document describes a set of devices used to interact with humans. Mice, keyboards and joysticks all fit into this category. The HID class is unique in that the driver on the host side is widely adopted and very flexible. This group of devices has a report descriptor that gets read during enumeration. This report descriptor contains important information about how the data is read from the device. The host reads the descriptor and loads the appropriate driver configuration required for the device to function. When you plug a joystick into a computer, how does it know that the device is a joystick? How does the computer know the number of buttons to map or the number of axis supported by the device? All of this information is contained in the report descriptors.

Your Ad Here

The beauty of the system is that the mapping information is contained within the device (more specifically, within the report descriptors). This allows the host drivers to be extremely flexible in the range of acceptable devices. Needless to say, your imagination is the limit when it comes to creating USB HID devices, and you’ll never have to write a drivers on the host side.

Some USB HID Tools

There are several tools that any USB developer must learn to use. If we are to create custom HID devices, then the following tools are essential. The first is the SimpleHIDWrite, which allows you to read and write simple descriptor packets to a USB HID device.

The second is the HID Descriptor tool, which allows you to construct descriptors that describe how your reports will be formatted.

Get your typing fingers ready, we are going on a crazy programming binge and creating some funky custom devices.

Table of Contents

4 Responses to 14.1 USB, An Introduction

  1. Sebastian says:

    I’m looking forward to the next part. Thumbs up. :)

  2. Carl Hill says:

    Great stuff!

    By the way, I’m pretty sure that Jan Axelson is a woman, with Jan short for Janet.

  3. jliu83 says:

    Really!? I just wiki-ed it and it is true. I guess you learn new something everyday. You go through engineering school and engineering conferences and pretty much everyone involved is a guy so you assume these authors are men. And then boom, reality hits you: there are women in this field after all.

    -J

  4. Rong111y says:

    I like this: “the protocol is a complex mess and don’t expect to understand it.” LOL ….

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>