PC USB Host to a PIC HID Interfacing - Development by Example: Hardware and Protocol Stack.

 

In the beginning, there was DOS, LPT1 and the parallel printer port for purposes of hardware evaluation and interfacing.  However, the “evolution” of personal computers has seen these simple ports mutate into the Universal Serial Bus.  What was once an evening’s work for a capable experimenter has become a major, multi-skilled month-long task for the uninitiated. 

 

To minimise the effort required to get a first pass at a simple PC Host to Device test card functional, we take a “Cook-Book” approach to describing the assembly of hardware, firmware and software snippets needed to create a simple Human Interface Device project - we are going to focus on “what” you have to do rather than “how” or “why”.  We will embed a USB Protocol Stack into the test card and download supporting PC based software, for an initial “reference design” which should be sufficient to demonstrate bi-directional communication between the test card and a host PC, via a USB cable connecting the two. 

 

 

Overview

Hardware

Testcard Checks

Programming the PIC

Installing the Bootloader

Re-developing the PIC code

Checking the Device USB Stack and PIC code

Re-development of the PC GUI

FootNote: OSI Layers and the USB Protocol Stacks

 

 

Overview

 

Once constructed the testcard may be checked by programming it via the In-Circuit Serial Programme interface using MicroChip’s own PICkit 2 Debug Express module, or similar.  The embedded code we will use is a modified version of MicroChip’s “Hello world!” provided with their 44-Pin Demo Board, which comes with their PICkit 2 module.  The PICkit 2 module (and its 44-Pin Demo board) may be purchased from MicroChipDirect.com.  The “Hello world!” code will need to be modified to redirect RD0 to RB0 (we use only one LED not the eight of their card) since the PIC18F2550 we use, has less I/O pins than the PIC18F4550 of their solution.  Editing the code can be done using MicroChip’s MPLAB Integrated Development Environment.

 

Next we will bootload the USB code (from the MicroChip website), reboot to activate the “Hello World!” code (modified to reside above the bootloader) then re-programme the PIC18F2550 via the USB interface (rather than the PICkit 2 module) and then check we haven’t broken anything. 

 

Once the bootloader has been installed, we add a second USB HID protocol stack above the bootloader to allow us to communicate with the PIC micro when in “normal” mode.  We will then use the MicroChip HID demonstration executable to control the firmware we will install in our PIC18F2550.  This section steps through the editing needed to alter the demo code from the example PIC18F4550 to our chosen PIC18F2550.  The impatient may skip this and download the resultant executable referenced at the end of the section.

 

Once the hardware and firmware are in place we turn to redeveloping the GUI software and look at how it communicates through the USB ports on the PC. 

 


Hardware

 

The UK edition of Everyday Practical Electronics, November 2008 carried a promotion for Microchip’s with the PICkit  2 Debug Express module and their 44-Pin Demo Board  (using a “non-USB” 44-pin PIC16F887) which got us to thinking how we might address the PC–USB–testcard issue.  To make such a part USB capable in a straight-forward manner we need to use one of the generic PIC18Fxxxx USB parts and  for other project reasons we chose the PIC18F2550, which unfortunately is physically incompatible with MicroChip’s standard demo board, the PICDEM FSUSB, so had to create our own.  An example of what is required can be seen in the CREATE USB Interface as described by Dan Overholt:

 

             

 

 

Here we implement a version of CUI using the PIC18F2550 (the 28pin version of the PIC18F4550 on the CUI) modified to enable us to program the USB boot-loader into a blank PIC18F2550 device and set it into its Single Supply Programming mode: 

 

              

 

The internal LVP bit must be set to allow the device to be able to be re-programmed over the USB  using the Low Voltage or single Supply Programming option and this bit can only be set using the “High Voltage Programming” mode via the ICSP socket (the PICkit 2 module will apply 13V to the Vpp/MCLR pin) and will be set by the boot-load routine.  Thereafter, the device maybe programmed over the USB input.  The USB firmware so loaded is write-protected to prevent accidental overwriting and the start address for subsequent embedded code is shifted from &000 to &800, above the USB firmware.

 

 NOTE: an IC socket is useful, as it allows us to leave the IC out when testing the card to ensure the supplies are correctly connected before the IC is plugged in.  Also note that the crystal can be any frequency up to 48MHz for the card and PIC to function, but should be a 20MHz device if you are intending to load the MicroChip Bootloader code (as we intend later in this note) as their USB Bootloader routines all assume a 20MHz external Crystal by default.

 

Testcard Checks

 

With the IC out of the socket, use a DVM set to “ohms” and check between pins 19 and 20 on the IC to check that the supply is not short-circuited.  Check pin 20 connects to pin 1 of the USB connector and pin 2 of the ICSP connector.  Similarly check that pin 19 of the IC connects to pin 4 of the USB connector and pin 1 of the ICSP connector.  Check that the “+” lead of the 1uF electrolytic cap connects to pin 20.

 

Plug the USB cable into the card and then the PC.  Use a DVM on “volts” to check that 5V appears across pins 19 and 20 (+ve or red lead to pin 20).  Check the voltage at pin 1, then press the RST button and hold, checking that the voltage at pin 1 goes to zero.  Release.  Check  the voltage at pin 26, press the S2 button and hold, check the voltage drops to zero.  Release.

 

Remove the USB input, plug the IC in (check that pin1 is away from the ICSP connector and the USB socket) then connect an in-circuit programmer (such as the PICkit 2 module) to the ICSP connector.  Note that the triangle indicating pin 1 on the module should be closest to the LED, ie pin 1 of the ICSP connector goes to pin  1 of the IC and via a diode to the RST switch and the 4k7 pull-up resistor.

 

Boot up the PICkit 2 Application on the PC, find the “Hello World” hex file in the MicroChip directory tree (you may need to compile this first), import it and program the PIC.  Remember to select the PIC18F family – the PICkit 2 application should recognise that the 18F2550 is present.  Write the hex file to the PIC, as you program the PIC, notice that the voltage at pin 1 of the IC goes to 13V.  Note that the LED will not light as the default “hello world” file has not been set up to address RB0.  However the application should report that the PIC programmed correctly, thus:

 

 

If you have an oscilloscope available check the waveform at Pins 9 and 10 using a 10M x10 probe: it should be a near “rail-to-rail” waveform at the frequency of the crystal (default is 20MHz).  If not, then a DVM on volts should read about “half-rail” on DC.  You may (depending on the bandwidth your DVM) be able to get a low level of AC voltage on either of the pins.

 

Exit the PICkit 2 Programmer application.

 

This concludes the initial set up checks.  Leave the PICkit 2 module connected to the PC, with the testcard plugged into it.

 

Programming the PIC

 

If you haven’t already done so, download the Microchip MPLAB IDE software, install it, then download MPLAB C Compiler for PIC18 MCUs (you will need it later) and install it, check all boxes when you install it.

 

Open the MPLAB IDE application,  select Project, Project Wizard.  Select PIC18F2550 as Device.  Accept entries until you get to Create New Project File, then browse to your selected project path (I used C:\Documents\Projects\MicroChip\MPLAB_IDE\... ) then give your project file a name (I used “HelloWorld”).  Click through to complete the wizard (don’t add any files) returning to the MPLAB IDE workspace.

 

Click on View and enable Project.  Go to Programmer and pick the one you are using (I used the PICkit 2 module) .  Click on File, New then paste the following text:

 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

 

#include <p18F2550.inc>

      

       CONFIG WDT   = OFF   ; disable watchdog timer

       CONFIG MCLRE = ON    ; MCLEAR Pin enabled, RE3 input pin disabled

       CONFIG DEBUG = ON    ; Enable Debug Mode

       CONFIG LVP   = OFF   ; Single Supply (Low-Voltage) programming disabled (necessary for debugging)

       CONFIG FOSC  = HS    ; HS Oscillator, external XTAL

       CONFIG PBADEN= OFF   ; PORTB<4:0> set to Dig I/O on reset

      

       org 0                ; start code at 0

             

Delay1 res 1;reserve 1 byte for the variable Delay1

Delay2 res 1;reserve 1 byte for the variable Delay2

 

Start:

       CLRF PORTB           ; Initialise PORTB, clear output latches

       CLRF TRISB,0         ; Clear RB0 data direction ie, make it an output

      

       CLRF Delay1          ; Clear Delay1  ie set to 0

       CLRF Delay2          ; Clear Delay2  ie set to 0

MainLoop:

       BTG       PORTB,RB0 ; Toggle PORT B PIN 0 (pin 21 for the PIC18F2550)

Delay:

       NOP NOP NOP NOP NOP  ; waste time

       NOP NOP NOP NOP NOP  ; waste time

       NOP NOP NOP NOP NOP  ; waste time

 

       DECFSZ    Delay1,1   ; Decrement File (Delay1) by 1, skip next instruction (GOTO) if Delay1 is 0 

       GOTO      Delay      ; The Inner loop takes 15 instructions * 256 loops = 3840 instructions

       DECFSZ    Delay2,1   ; The outer loop takes and additional 3 instructions per lap * 256 loops

       GOTO      Delay      ; (3840+3) * 256 = 983808 instructions @ 20MHz (ie 5 MIPS) = ~ 197ms.

 

       GOTO      MainLoop   ; Do it again...

      

       end

 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

 

 

then paste it into the text box then select File, Save as …  navigate to your project area and save it as projectname.asm (my example was HelloWorld.asm).  Right click on source files (in the Project Window) select add files then navigate to the above assembler file.

 

Go to Project, Build all.  The build should run to completion without errors producing a window thus:

 

 

Select Programmer then Program, the Busy LED (on the PICkit 2 module) should flash.  The LED on our testcard should also flash once.

 

Put a DVM on volts on pin1 the MCLR pin, it should be low – the PIC is being held in reset mode.

 

Select Programmer again, then select Release from Reset.  The voltage on pin 1 should go to Vdd (~5V) and the LED should begin to flash.

 

Exit The MPLAB IDE application.  The test card will power down.

 

Boot up the PICkit 2 application.  Select Vdd On - the LED flashes (very fast as the original code was intended for a 4MHz clock not 20MHze) select /MCLR - the LED goes off.  Check pin 1 with the DVM, deselect /MCLR to see the voltage rise (and the LED flashes again).  Now press the RST button and hold it down – the LED goes off and the voltage on pin 1 drops. 

 

NOTE that this is different to using Hold in Reset and Release from Reset in the MPLAB IDE application –  when using the MPLAB IDE application, the voltage at pin1 is controlled by the PIC (and is held high/low, internally) so over-rides the RST button’s ability to control the pin due to the isolation diode on the testcard.

 

Last of all, remove the test card from the PICkit 2 module and plug in the USB cable to the PC.  The LED should flash and held off when the RST button is held down.

 

Our “HelloWorld” program is now installed into the PIC and all is well with the hardware!  Next we will turn our attention to installing the USB firmware which comes from MicroChip in the form of a “Bootloader”.

 

Installing the Bootloader

 

A Bootloader allows you to “burn” other programs into the microcontroller flash memory and run them. The one we are going to use here is located in the lower 2048 bytes of PIC’s Flash memory located at 0x0000 (address zero) which means that it is resident at the addresses used by the PIC’s reset and interrupt vectors, therefore the Bootloader must be redefined to point to the user application, so imposing some limitations on the user application requiring additional changes to that application’s source code, which for example, must contain a copy of the bootloader at that location in its source code.  To work with the bootloader, the “user mode” application must be linked using a special command file for MPLINK. The necessary linker command files may be found in the bootloader installation package within the Microchip Applications Libraries download which includes the Memory Disk Drive (MDD) stack and Graphics Library.

 

 

MicroChip’s Full-Speed USB distribution (MCHPFSUSB Framework: Application Libraries) comes with two separate USB bootloader example applications (and corresponding firmware projects) being: “HID Bootloader” and “MCHPUSB Bootloader”.  For our purposes we are going to use the HID Bootloader in it’s “two USB Stacks approach” where the bootloader firmware will contain the code needed for self programming (the first stack) as well as all of the code necessary to enumerate as a HID class USB device (and uses the second stack).  The selection of the HID class of USB device obviates the need to develop a USB driver as most PCs ship with the HID class driver already installed.

 

Unpack the downloaded zip file and unpack into your chosen installation path.  In order to use the bootloader, you need to program a device with the bootloader firmware … however this next step can be ignored if your device comes with the USB bootloader preloaded, if not then locate a file called: “USB Device - HID - HID Bootloader - C18 - PIC18F4550.hex” to found in a directory path something like: “C:\Program Files\Microchip\USB\USB Device - Bootloaders\HID – Bootloader\”.  You can download the copy I used, here.  Plug the PICkit 2 module into the PC and the test card, run up the MPLAB IDE application, import the .hex file.  Go to Configure/Select Device, choose the PIC18F2550 device then go to Programmer/Select Programmer and select the PICkit 2 module.  Notice that you could also use the PICkit 2 application directly to import the .hex file, then programme the PIC18F2550. The MPLAB IDE Output window should report that the device is present and that PICkit 2 is ready.  Go to Programmer/Program  the hex file for the bootloader should then be loaded into the 18F2550 device and MPLAB IDE will report the same.

 

Remove the PICkit 2 module from the test card and the PC.  Plug in the USB cable to both.  If Windows reports an “Unknown Device” then (for WinXP) go to Control Panel, System, Hardware, Device Manager, Universal Serial Bus Controllers, then select the Unknown Devices, then right click on “Devices” then select Properties and look at the Devices Properties ID.  If the VID=0000 and PID=0000, then you have found the problem the testcard is failing to enumerate correctly …

 

Hold the S2 button down, press the RST button then release it, then release the S2 button.  The PC should now report Found new Hardware” then install it as a HID.  If you get an “Unknown Device” report, then the most likely cause will be an incorrect oscillator frequency.  Check that the oscillator is running at 20MHz,  even with an incorrect crystal in the HID bootloader will be functional and the PIC will “talk” to the PC over the USB cable, but will not “enumerate” correctly. 

 

A quick check on the hardware with a DVM on pin 14 (VUSB) should reveal the presence of the 3.3V regulated voltage for the USB interface: Pin 16 (D+) should be near 3V and  Pin 15 (D-) should be at 0V, indicating to the controller (the PC) that this (testcard) is a low speed device.  Check that the oscillator frequency is 20MHz.

 

 

Even if seemingly nothing happened when the device was plugged in, its is still worth taking a look in Device Manager/Human Interface Devices under the now present “USB Human Interface Device” – to see that the device did enumerate correctly and is ready to use.  Confirm that you get a message like USB\VID_04D8&PID_003C\..... under the Details tab for Device Instance Id entry:

 

 

  Notice that this matches the PID and VID the HIDBootLoader will be expecting to see.

 

Locate “HIDBootLoader.exe” and run the application (Note you will need MicroSoft’s .NET Framework version 2.0 or later installed if you are running WinXP in order to run the HIDBootLoader application, Vista or later comes with the framework installed).  In my setup, the HID BootLoader application is filed at:

 

C:\Program Files\Microchip\USB\USB Device - Bootloaders\HID – Bootloader\HIDBootLoader.exe

 

If you have not done so, hold down S2 button then press and release the RST button, keeping the S2 button depressed, wait a second, then release S2.

 

Open the HIDbootloader executable to see:

 

 

Click on the [Read Device] button to get a “Read Complete” message. 

 

This confirms that you have successfully installed the bootloader into the PIC and the application has got a response from it. 

 

Rebuild the above “HelloWorld.asm” file altering it as follows to remove the configuration statements and redirct the code’s start address, a revised version may be downloaded here: HelloWorld2.asm.  Remember to select “none” as the programmer before building the hex file. 

 

On the MicroChip USB HID Bootloader application select [Open Hex File]  to load the rebuilt .hex file, hit [Program/Verify] which will become active once you load the hex file.  Hit [Reset Device] to exit the PIC Bootloader routine and you will see the message “Device Successfully Reset” followed by “Device Removed”.  The red LED should now be flashing at a much slower rate than for our first HelloWorld program.

 

A useful utility for examining what USB devices your PC has attached to may be downloaded here: USBdeview.zip.  This utility is particularly useful as it also tells you about devices which have been connected in the past.  Confirm that VID=048d and PID=003c for both instances of  your “HID USB Bootloader” devices.

 

There is also another useful utility USBview in the Windows Design Kit which we will download in the next section, however to get us started you can download a precompiled version from here.  Extract the executable and boot it up, plug in the testcard, press and hold down S1 then hit and release RST, then releasing S1.  You should get a window that looks like this:

 

 

If nothing appears to happen, you may need to set “AutoRefresh” (under Options), then “Refresh” (under File) to see your device.  Press reset (the testcard should disconnect) and the USBview window should now show “NoDeviceConnected” (which for my example above, is [Port1] ). 

 

This concludes the installation and checking of the BootLoader and have arrived at a point equal to the conclusion of the CUI project upon which this project is based.  A zipped copy of the above bootloader files maybe downloaded here:  BootLoader.zip.

 

Notice that we have to put the PIC into its “bootloader” mode to be able to “connect” to the card.    We have yet to add the second USB stack which allow us to communicate with the application (yet to be created and installed) when the card is plugged in:

 

Connect the test card through its USB port to the PC.  Locate the file called “USB Device - HID - Simple Custom Demo - C18 - PICDEM FSUSB - HID Bootload.hex” in C:\Program Files\Microchip\USB\USB Device - HID - Simple Custom Demo\  then install it into the PIC via the HID Bootloader utility.   Press reset and you should then get a “New Hardware Found” icon, followed a little while later by a “your hardware is ready to use” message in the system tray.  Notice that flashing led is now extinguished.  Use USBview to check that the USB stack in the PIC is now responding – set Auto Refresh, then unplug the testcard (you should see NoDeviceConnected) wait a few seconds, then plug it back in again to see that it continues responds to the USBview utility (ie you get DeviceConnected and can see its PID and VID).

 

Re-developing the PIC code

 

Locate the directory called “Generic HID - Firmware” (under C:\Program Files\Microchip\USB\USB Device - HID - Custom Demos \) and copy the following files into your project area – I used:

 

      C:\Documents\Projects\MicroChip\HIDdemo\PICfirmware\

 

You only need to copy these files over:

 

      USB Device - HID - Simple Custom Demo - C18 - Low Pin Count USB Development Kit.mcw

      USB Device - HID - Simple Custom Demo - C18 - Low Pin Count USB Development Kit.mcp

      rm18f4550 - HID Bootload.lkr

      usb_config.h

      HardwareProfile.h

      main.c

      usb_descriptors.c

 

You can rename the workspace definition (projectname.mcw) and project binary (projectname.mcp) files to names of your choice – I used HIDdemo.mcw (and ~.mcp).  Open HIDdemo.mcw with a text editor and change corresponding lines (near the beginning of the file) to be as follows:

 

            dir_tmp=.\Objects\HIDdemo

 

      dir_inc=..\..\HIDdemo\PICfirmware

and:

      dir_lib=.\Objects;.\Library

 

 

Save the file then close the text editor.  Notice that we have removed the references : “C:\MCC18\h;..\..\Microchip\Include;” and “C:\mcc18\lib” which point to the device object library used by the C compiler and the usb definition files.  We will have to copy these over to the project file (which is still referenced by dir_inc) leaving the original files for later reference.  Making this change allows us to archive all the files used in the project including those library files used by the project when we make that archive.  This allows us to recover the project as “built” at a later date, even after any subsequent update of the Application Framework and the Library files.

 

Copy:   c018i.o

 

from:    C:\MCC18\lib\

 

into:      C:\Documents\Projects\MicroChip\HIDdemo\PICfirmware\Objects\

 

copy:    clib.lib

            p18f4550.lib

      p18F2550.lib

 

from:    C:\MCC18\lib\

 

 into:     C:\Documents\Projects\MicroChip\HIDdemo\PICfirmware\Library\

 

 

Locate the directory:

 

      C:\Program Files\Microchip\USB\USB Device - HID - Custom Demos\Generic HID – Firmware\

 

And copy the files:

 

      usb_device.c

      usb_function_hid.c

 

      usb.h

      usb_ch9.h

      usb_common.h

      usb_device.h

      usb_function_hid.h

      usb_hal.h

      usb_hal_pic18.h

 

into a directory below your project area called \USB\ as follows:

 

      C:\Documents\Projects\MicroChip\HIDdemo\PICfirmware\USB\

 

Open one of the files and notice that they use relative path indexing thus: ./USB/ for calling files.  Next locate a directory called:

 

      C:\Program Files\Microchip\Include\

 

and copy

 

      Compiler.h

      GenericTypeDefs.h

 

To a new directory, below the main project directory, called “Common” as in:

 

      C:\Documents\Projects\MicroChip\HIDdemo\PICfirmware\Common\

 

Boot up MPLAB IDE and load the project.  Go to the the Solution Explorer window, expand “Common” and the two “USB” paths, then re-reference each of the “missing” files - right click on the file name then use “Locate Missing File”.

 

Save as “yourproject name” (I used “HIDdemo”).  Your Solution Explorer window should look like:

 

 

Select “None” as programmer (leave the device as PIC18F4550) then “Build all” - you will get a load of warnings followed by an error message “unable to locate ‘HardwareProfile - PICDEM FSUSB.h’ ”.

 

The missing file may be found in:

 

      C:\Program Files\Microchip\USB\USB Device - HID - Custom Demos\Generic HID - Firmware

 

Copy it over to the project directory, then “build” the project again - you should get the “BUILD SUCCEEDED” message.   Go to Configure, Select Device - scroll through and select PIC18F2550, build the project again to get the “Demo board not defined.” error message. 

 

Open HardwareProfile.h file for editing, copy the lines:

 

       #elif defined(__18F14K50)

            #include "HardwareProfile - Low Pin Count USB Development Kit.h"

 

then paste immediately below those lines, editing them to:

 

        #elif defined(__18F2550)                                  //added for PIC18F2550testcard

                  #ifndef DEMO_BOARD                              //added for PIC18F2550testcard

                        #define DEMO_BOARD PIC18F2550testcard     //added for PIC18F2550testcard

                  #endif                                          //added for PIC18F2550testcard

            #include "HardwareProfile - PIC18F2550testcard.h"     //added for PIC18F2550testcard

 

In the project directory, copy and paste the file:

 

      HardwareProfile - PICDEM FSUSB.h

 

Rename it to:

 

      HardwareProfile - PIC18F2550testcard.h

 

Open that file and locate the lines:

 

      #define DEMO_BOARD PICDEM_FS_USB

      #define PICDEM_FS_USB

and:

      #ifndef HARDWARE_PROFILE_PICDEM_FSUSB_H

      #define HARDWARE_PROFILE_PICDEM_FSUSB_H

 

change them to:

 

      #define DEMO_BOARD PIC18F2550testcard

      #define PIC18F2550testcard

and:

      #ifndef HARDWARE_PROFILE_PIC18F2550_TESTCARD_H

      #define HARDWARE_PROFILE_PIC18F2550_TESTCARD_H

 

then save the file.  “Build” the project – you will now get the error “processor types do not agree across all input files”.  Copy rm18f4550 - HID Bootload.lkr and rename it to rm18f2550 - HID Bootload.lkr.  Open rm18f2550 - HID Bootload.lkr and edit the line:

 

      FILES p18f4550.lib

To be:

            FILES p18f2550.lib

 

Save the file, then in the Solution Explorer window remove the Linker Script entry for rm18f4550 and add a link to rm18f2550.  Rebuild the project – it should now run successfully to the “BUILD SUCCEEDED” message.

 

So, far, so good – we can now produce a HIDdemo.hex file which can be programmed into the PIC18F2550testcard - try it … use USBview to verify it (remember to set “auto refresh” , then “refresh”, then) press RST on the test card to see it drop the connection to the PC, then re-establish it).

 

Re-open the “HardwareProfile - PIC18F2550testcard.h” file for editing, locate the lines:

 

      #define mInitAllLEDs()      LATD &= 0xF0; TRISD &= 0xF0;

   

      #define mLED_1              LATDbits.LATD0

      #define mLED_2              LATDbits.LATD1

      #define mLED_3              LATDbits.LATD2

      #define mLED_4              LATDbits.LATD3

and:

            #endif  //HARDWARE_PROFILE_PICDEM_FSUSB_H

 

then change them to:

 

      #define mInitAllLEDs()      LATB &= 0xF0; TRISB &= 0xF0;

   

      #define mLED_1              LATBbits.LATB0

      #define mLED_2              LATBbits.LATB1

      #define mLED_3              LATBbits.LATB2

      #define mLED_4              LATBbits.LATB3

and:

            #endif  //HARDWARE_PROFILE_ PIC18F2550_TESTCARD_H

 

Build the project again, then program the resultant HIDdemo.hex file into the test card. 

 

Checking the Device USB Stack and PIC code

 

Go to:

            C:\Program Files\Microchip\USB\USB Device - HID - Custom Demos

Locate:

            GenericHIDSimpleDemo.exe

 

Copy it over into the project area under a new directory branch:

 

            C:\Documents\Projects\MicroChip\HIDdemo\PCsoftware

 

And rename it to prevent it being accidentally over written (I called it HIDdemo.exe).  Boot it up, check the testcard is plugged ito the PC’s USB port – the red led on RB0 will start flashing after you press RST to disconnect from the HID Bootloader, or directly if you have just plogged it in.  Click on [Connect].  You should get a window that looks like this:

 

 

If the connection has been “made” the two buttons on the right will be active (ie the text will be black – it would be grey if the connection failed).  Click on [Get Pushbutton State] the “State: Unknown” comment will change to “State: Not Pressed” - hold down S2 on the test card and click [Get Pushbutton State] again to see that “State” is now “Pressed”.  Try clicking on [Toggle LED(s)] does the LED go off?  Click on [Toggle LED(s)]  again and the LED should come back on (but will no longer be flashing) subsequent clicks will toggle the LED off/on..

 

 

This concludes the setting up of the PIC USB code and the code for a simple application.  The process we have gone through above may seem very long winded, but it does result in a “portable” project which we can easily archive and distribute.  If you are impatient or just want to install the PIC18F2550testcard hex code, it may be downloaded here:  HIDdemo.hex.  At this point in our PIC18F2550testcard project, we are about equal to that we would have been at had we used the standard PICDEM FS USB Board with its PIC18F4550 device.  We have in the process, learned a little about programming the PIC18F2550 device using MicroChip’s Bootloader software and modifying the PIC code to use different ports. 

 

Re-development of the PC GUI

 

Next we re-develop the Graphical User Interface and develop some new code to be embedded into the PIC to allow the GUI automatically respond to  S2 and to toggle the LED when S2 is pressed.

 

Download Visual C++ Express and install.

 

Locate and download the C++ project source code for the Generic HID - Simple Demo - PC Software(confusingly, the path to it is: C:\Program Files\Microchip\USB\USB Device - HID - Simple Custom Demo) a copy of which can be downloaded as a zip file here: PCsoftware.zip which we are going to modify to create our new application for reading from and writing  to our card. Copy it into the PCsoftware directory we created above: C:\Documents\Projects\MicroChip\HIDdemo\PCsoftware.

 

Open GenericHIDSimpleDemo.vcproj by double-clicking on it.  Allow C++ to convert the project.  “Build” the project, then go down one level to the \Release\ directory and locate the GenericHIDSimpleDemo.exe executable.  Run it up and verify that it functions as HIDdemo.exe did above, so ensuring that all of the source files are present – this C++ project gives us a good starting point for re-developing the GUI.

 

[To be continued ….]

 

 

 

 

 

 


FootNote: OSI Layers and the USB Protocol Stacks

 

Both the Host and the Device USB Protocol Stacks may be mapped on to the layers of the Open System Interconnect Reference Model (sometimes referred to as the ISO OSI Seven Layer Model) thus:

 

 

OSI Layer            HOST USB Protocol Stack

                       {USB Driver Stack}

 

7  Application       USBview.exe  HIDdemo.exe

                       |             |    < File  (open, close, read, write)

6  Presentation        |     Class Libraries

                             {HIDCLASS.SYS}

                              {HIDUSB.SYS}

                       |             |                                  USER

----------------------------------------------------------------------------

                       |             |                               KERNEL

5 Session            Class Device Driver

                        {USBHUB.SYS}

                            |                  Transaction (Buffer movement)

4 Transport             USB Driver         

                        {USBD.SYS}

                             |

3 Network            Bus-specific Device Driver

                     {UHCI.SYS}    {OHCI.SYS}

                         |             |        Packet (sync, data, ACK, NAK)

2 Data Link          Host Controller Driver

                         PCI Enumerator

                               |                  Physical (diff. signalling)

1 Physical                 USB Cable

 

 

 

The default Dynamic Link Library file for Human Interface Devices is called Hid.dll and resides at:  C:\Windows\System32\).  Background for this driver may be found at: http://msdn.microsoft.com/en-us/library/ms789872.aspx.  It is worth reading the sections on Operating HID Collections, Generic HIDclass Driver Stacks and Generic HIDclass Device Stacks.

 

 

Download Windows Design Kit (WDK) (comes as a CD image file) write it to CD, then install the WDK onto your PC.  This contains the Device Driver Kit (DDK) our C++ Project files will need to call when creating the Win32 Windows Applications. Be warned, this download and installation will take a few hours to complete.

 

A useful USB Command Verifier tool for looking at USB traffic may be downloaded from the USB.org website here.