Penultimate USB

The last several weeks, I’ve been talking about building a USB device using a Microchip PIC 18F2550. You can still find all the source code and the related links at the very first of this series.

All the code I’ve mentioned previously was related to just describing the USB device. In my opinion, that’s actually the hard part. The working code is anti-climatic. That’s partially true because it doesn’t have much to do to start with. It also helps that the Microchip examples give you a good place to start. The code is in the newmain.c source file.

A lot of the code in this file has absolutely nothing to do with USB. For example, read_analog just reads the A/D converter. Some of the other functions like UserInit handle some non-USB items but also set up things in the USB stack as well. There are a few initialization functions, including InitializeSystem, which sets up the USB stack (among other things) with a call to USBDeviceInit.

The main part of the USB-related code, however, is in ProcessIO. The logic is simple enough:

  1. Check to make sure the USB stack is ready to go and not suspended.
  2. Check for any data from the host.
  3. If data is available, do a switch statement to process it.

Note that this is a polling technique, which for this device is fine. You can, however, build software that responds to USB requests via an interrupt, if you prefer. Here’s an excerpt from the code:

  // Process any I/O including USB I/O
void ProcessIO(void)
   if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return;
//Check if data was received from the host.
//Look at the data the host sent, to see what kind of 
// application-specific command it sent.
            switch(ReceivedDataBuffer[0])               {
         case 0x1:  // command 1, set LED
 . . .

Since the USB logic is polled, you have to call it periodically from your main program (as well the USB stack’s USBDeviceTasks function). Also, if you look at the very end, ProcessIO rearms the USB receiver to wait for the next packet from the host computer. As you’d expect, that’s pretty important!

The main function is quite simple (especially if you strip out some of the more verbose comments):

void main(void) {
    // Check bus status and service USB interrupts.
    } //end while

That’s really about it. Of course, a real device might have more non-USB things going on as well.

The rest of the file is pretty much a copy of the Microchip examples. There are quite a few callbacks that are mostly placeholders for USB events you might want to handle. For example, you’ll get a call if the host wants to suspend your device or wake it up. The only callback that is really significant in this example is USBCBInitEP, which initializes your end point. This is the function that enables the end point and primes the receiver to get the first packet from the host. That packet will pass through ProcessIO, and ProcessIO will reprime the receiver, as I mentioned before.

These callbacks, by the way, don’t happen by magic. In reality, the stack calls one function: USER_USB_CALLBACK_EVENT_HANDLER (at the very end of the file). There a switch statement calls the individual callback functions. It would be easy to remove unused callback functions and delete them from the switch statement.

After all the hassle of setting up the configuration, this seems pretty simple I think. Of course, you can make it more complicated by handling interrupts, different types of pipes, suspend and resume, and other features. But as it is, you have a simple base for a practical USB device.

There’s one more topic to discuss: How do you talk to the device from a host PC? There are a few answers to that, and I will wrap up this series next time with a look at some different ways to easily create the host software.