Open Source Framework for USB Generic HID devices based on the PIC18F and Windows
If you’ve dabbled with PIC18F microcontrollers and the USB Generic HID standard before (perhaps you’ve even tried my Building a PIC18F USB device project) then you will have noticed that there is a lot of complexity in supporting USB on both the PIC18F and the Windows host-side of things.
Getting beyond the basic steps of reading a switch and flashing an LED (how many projects have you built to flash LEDs?!) is actually quite a large task with a steep learning-curve.
To make things easier on the hobbyist who wishes to delve into more exciting projects, I’ve developed a framework for producing USB devices which covers both the Windows host-side application development and the PIC18F firmware itself. The framework consists of a Visual Studio C# class library (which handles all of the intricacy of dealing with the Windows specific SDKs and operating system requirements), a windows reference application (which demonstrates how to use the class library and acts as a testing front-end for the library), a simple USB hardware reference design and (last but not least!) a PIC18F4550 firmware which communicates with the class library.
In essence this means that you can rapidly develop and test a USB device and the Windows host-application with a minimum of USB Generic HID protocol knowledge. The class library gives you a very simple interface to the USB device from C# and the firmware serves as an example of how to create the specific software needed on the PIC for your device design.
The framework includes the ability to pass commands and responses to and from the USB device as well as bi-directional bulk-transfer of data allowing you to build more complex data-capture applications which require more information to be passed quickly from host to device and vice-versa.
In this article I will go over each element of the framework and show how you can use it to develop your own creative USB devices.
For the impatient, here is a quick YouTube video of the framework in action. The LCD driver example shown in the video can be downloaded below:
Version 3 of the USB library and firmware is now available (2011-10-16). I’ve made several major and minor enhancements to the library as well as some bug fixing. Here is a list of the enhancements and changes:
- Firmware is now targeted for the Microchip C18 compiler (and is no longer compatible with Hi-Tech PICC18)
- Microchip’s Application Library (containing the USB stack) is now separated from the framework code and is installed standalone
- The firmware now supports version 2.9a of the USB stack
- Support for USB bootloaders is now included (lightly tested at the moment)
- Both interrupt driven USB polling and non-interrupt driven USB polling are now supported
- Debug stream now supports dynamic text so you can place live variables and other information into the in-built debug functions
- Open-source firmware code now separated correctly from Microchip stack code
- General code clean-up with better (and more concise) comments and notes
- Host Reference Application
- Fixed memory leakage issue in non-Managed code (readRawReportFromDevice method)
- Changed the method used for registering to receive device event notifications. This is now handled by the library and does not need a WndProc override in the user code
- General code clean-up
In addition I’ve also updated all of the circuit diagrams and included a new schematic for the PIC18F2550.
Please ensure that you specify which version of the USB framework you are using when asking for help in the forums.
The reference hardware is extremely simple and can be built on a breadboard if required. I’ve made the pin-out of the hardware identical to the PIC DEM FS USB board available from Microchip to allow you to buy a reference board (if you want to!), you can also use my clone of the Microchip board – the PIC USB Development Board. If you don’t have the time/money/inclination to make a reference board here is a circuit schematic of the minimum required hardware for the reference board for both the PIC18F4550 and PIC18F2550 microcontrollers:
PIC18F4550 Circuit Schematic
The next picture shows the reference hardware built on an experimenter’s breadboard (using the PIC18F4550):
The reference hardware provides 4 LEDs which are used to show the status of the USB device as it processes the various requests that can be sent to it from the Windows host-application. The 4 LEDs (from left-to-right) mean:
- Power is on – device is ready
- USB enumerated – device is ready to communicate
- Success indicator (this flashes as commands are correctly processed)
- Failure indicator (this flashes if a command was not correctly processed)
The board provides all you need to add in your own devices and circuity as you require, you can even remove the LEDs, the only part the firmware truly requires is the USB port and its associated hardware.
EasyEDA: Ideas for Circuit Design, Innovation for Electronics Access
Free Circuit Design: Schematic – Simulation – PCB Layout – Gerber Viewer
Cheapest PCB Prototype: Only $2 for 10pcs 10×10cm PCBs, 24 hours Quick Turn, DHL Delivery in 3 Days