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: The Adventure Begins

March 27, 2012

I've been tiptoeing around building a real USB device for the last few weeks. Last week, I described a circuit with a Microchip 18F2550 and mentioned the tools I planned to use. After a frustrating week, I'm happy to report I have something working for both Linux and Windows. It is a simple HID example with a switch, an LED, and an analog input. But it's a start!

The project turned out to be pretty big, so while I am going to attach at least the first version of the USB code for download, it may take a few more blogs to explain all the details. The actual details aren't the difficult part. The relationship between all the parts is what makes it tough.

There's four major parts that all need to work together to make this USB device work:

  1. The Microchip USB library that does all the real work.
  2. Some code that uses the USB library, as well as doing whatever it is your device is supposed to do (in this case, handle the analog and digital I/O).
  3. A set of data structures that describe your device to the host computer.
  4. Code on the host computer to communicate with the device.

When you install the Microchip application library, you will get a large number of subdirectories with code for many different things, not just USB. You also get a lot of example code (again, not all related to USB). Just navigating it can be a little daunting.

In my case, I installed the library in /opt/microchip/applib (I'm on Linux, but the layout is the same on Windows). From the root directory, the USB subdirectory contains a lot of example code.

However, the real code is in the Microchip/USB directory (note this is relative to the install root, so in my case the directory is /opt/microchip/applib/Microchip/USB). Here you'll find the core USB library code needed, along with some other USB-related software (for example, code for creating a USB host or USB OTG peripherals). There are also specific subdirectories for particular configurations. For this project, the file of interest is HID Device Driver/usb_function_hid.c.

I created an 18F2550 project in the usual way using MPLAB-X (there's a video tutorial on DDJ TV). Then I created a logical folder in the source files called USB Stack. In that folder, I added the usb_device.c file and the HID Device Driver/usb_function_hid.c file from the library directories.

The main part of my project has two source files — newmain.c, which is the main file and usb_descriptors.c, which contains code related to the descriptors — the data structures that describe the device to the host computer.

The library also expects two header files: usb_config.h and HardwareProfile.h.

Confused yet? Here's a quick summary:

  • usb_device.c — Main Microchip USB stack file
  • usb_function_hid.c — Microchip USB stack specific to an HID device
  • newmain.c — Device-specific code (user-supplied)
  • usb_descriptors.c — Data sent to host device to describe the device
  • HardwareProfile.h — File that configures the Microchip library for specific hardware
  • usb_config.h — Selects options for the Microchip library

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.