circuitcellar.com
Magazine Support   Digital Library   Products & Services   Suppliers Directory 
 
 





 

May 2004, Issue 166

USB in Embedded Design (Part 2):
HIDmaker Converts an Application


HIDmaker DEVICE OUTPUT

After you have fully defined your device, its data, and its interface, HIDmaker can work its magic. Select the environments that you want HIDmaker to generate output for (I used PicBasic Pro and Visual Basic), and in seconds you’ll receive a list of all the output files for both the peripheral and the host. You can peruse the files before exiting HIDmaker. You may download the files from the Circuit Cellar ftp site.

On the peripheral side, HIDmaker generates two main files (nine files in all for this project). First, descript.asm holds all of the descriptor tables defined from the information entered in HIDmaker. Then there is USBX10_M.bas, which is this project’s PicBasic Pro application file. The following four files are project-independent. Three  Microchip files—usb_ch9.asm, usb_defs.inc, and hidclass.asm—support the PIC USB parts. One file, usbdesc.asm, lists a number of include files specific to PicBasic Pro.

The descript.asm file, which is certainly the most important, contains all of the tables the host reads during enumeration to gain the ultimate knowledge of your device. For this particular project, it contains a device descriptor table, configuration descriptor, interface descriptor, HID descriptor, endpoint descriptor (for EndPoint1In), endpoint descriptor (for EndPoint1Out), and report descriptor. That’s not to mention the string descriptors as defined in HIDmaker (e.g., language ID, manufacturer, product name, serial number, and other option strings).

Using Microchip’s IDE and PicBasic Pro, I compiled the project files into a hex file, which is used to program a PIC16C745. Let’s take a quick look at what HIDmaker built for this simple application. 

HIDmaker creates a USB interface capable of moving defined data. In this case it does this by setting up an application to pass three data items from the host and collect two data items from the device using a USB connection. After some initialization, the application proceeds to a main loop where the USBIN command checks to see if there is any out endpoint 1 data available. If so, it retrieves and unpacks the data. Otherwise, it goes on. 

In the second part of the loop, the out endpoint 1 data items are set to test values (initially defined in the define data section of HIDmaker), and a flag is set to indicate that data is ready to send. The data is packetized and the USBOUT command is executed, which sends the data to the USB in endpoint 1 buffer if it is free. (Otherwise, it tries again.) Then it’s back to the main loop. 

Notice that this application passes data but there is no apparent check on the data. This could be easily modified in this case so that the data that comes in is used for the data that goes out. But this is not necessary because there are ways to see the data inside the PIC. At this point, if everything was done correctly, the PIC with the necessary USB circuitry will enumerate when it’s plugged into a USB port on the PC (see Figure 1). 

(Click here to enlarge)

Figure 1—The PIC16C745 microcontroller has USB support for version 1.1. The interface is simple and can be powered from the USB bus. The microcontroller can apply an internal phase-locked loop to operate at 24 MHz using an external 6-MHz crystal.