Channels ▼

Al Williams

Dr. Dobb's Bloggers

Penultimate USB

April 13, 2012

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.
       if(!HIDRxHandleBusy(USBOutHandle))        
    		{
//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) {
    InitializeSystem();
    while(1)
    {
    // Check bus status and service USB interrupts.
        USBDeviceTasks(); 
        ProcessIO();
    } //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.

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.
 


Video