USB: The Adventure Begins

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

Leave a Reply

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

*