Basic USB - Using Microchip Stack and C#.Net - Hardware PDF Print E-mail
User Rating: / 98
PoorBest 
Written by Mat   
Sunday, 26 February 2006
Article Index
Hardware
Hardware
PIC Software
Windows Drivers
Application Code

PIC Software

The example PIC code discussed in this article can be found below, or in the Complete Code file at the start of the article.

PIC USB demo Code PIC USB demo Code (79.26 KB) 

Extract the zip file into C:, this should create a folder called "c:\picusbdemo\" containing all the project files, if you choose to extract it to a different directory be careful setting up the include directories in the project configuration settings.

Compiling the USB firmware 

To compile the PIC source code you will require MPLAB and Microchip's C18 Compiler. Free versions of both are avaiable from the links provided (student version for C18). The code has been sucessfully compiled on MPLAB v7.31 and C18 V3.00, however it should compile on more recent version if these are available. 

To open the project open MCHPUSB.mcw in MPLAB. Before compiling the code, please ensure that the correct device is selected. This is done by clicking 'Configure','Select Device...' then selecting PIC18F4550 as the device.

It is probably a good idea to check the include directories even if you have extracted the files to the correct location. To do this click 'Project','Build options','Project'. Ensure that the 'Output Directory','Intermediates Directory','Include Path' and 'Linker-Script Path' all point to the location of the extracted files. The 'Library Path' should point to the location of the C18 library files, these by default are located in "C:\mcc18\lib", and will need adjusting to your setup.

You should now be ready to compile the source code, press ctrl+F10 to start the build. All being well the code should have compiled sucessfully, however if it is unsucessful please go back and check all the project and include paths are setup correctly. (Any other strange errors, please let me know!)

You should now be able to test the code, to do this program the hardware using your favorate PIC programmer, and connect it to your test PC (warning check the hardware first, there is a risk of causing damage to the PC), all being well the PC should request for the drivers for a "PIC18F4550 Family Device", you can now either skip to the Driver section or keep reading if you want to understand how to add in extra functionality to the device.

Modifying the USB Firmware 

The two main sections of the USB firmware to be concerned about when modifying the code are the initialisation routines and the main switch statement, which responds to usb commands sent from the C# application.

If you require and further initialisation routines these should be inserted into the UserInit function in user.c, currently only mInitAllLEDs is called which defines the output pins for the led's.

The other main section to modify is the service request switch statement, this is located in the ServiceRequests function in user.c, however before modifying this a new enum value should be defined for CMD, located in user.h. To do this simple add a new command name and associate it to an available id (0-256), in the CMD enum.

After adding the new command to user.h, you need to modify user.c to respond to the new command. In order for the pic to respond to the new command it must be added to the switch statement found in ServiceRequests. When a new command is added to the switch statement it is important to respond to it in the correct manner. There are two possible ways to communicate with the application, either responding with data back or simply just an achknowledge response. The later is easiest to implement so we shall deal with this first.

Essentially the PIC accepts a usb data packet stored in the variable datapacket, which is a byte array (64 bytes long), byte 0 represents the command sent by the application (e.g 0x00 - READ_VERSION) and byte 1 the transmission length (if a none fixed length is used), the remaining bytes can be arbitarily selected depending on the format of the request, and used accordingly. This gives a data format as follows.

<Command><length><data><data><data><data>...... 

When you have finished dealing with the request, counter should be set to 0x01 to indicate that only the command will be sent back to the application as an acknowledgement.

However this on its own is not a very useful system, as the pic has no method for transmitting data back to the application and therefore a format for sending data back must be defined. If the command you are programming requires data to be sent back to the pc, then the following format must be constructed within datapacket before exiting the switch statement. Byte 0 should be left alone and represent the command, as per the request, byte 1 should represent the length of the packet to be transmitted back, and the following bytes may be used for data. This should give the same format as is used for sending bytes to the PIC by the application. You should then set counter to the packet total packet length so the pic knows how much of the buffer to send.

Now you should be able to add your own commands and extra functionality to the PIC, give it a go! If you have any issues leave a message in the forums.

It is possible to change parameters such as the amount of current which may be drawn from the USB port and modifying the descriptors of the device so that the device name and description found in Windows represent your device and not the generic Microchip setup, however I shall leave this for another more advanced article!



Last Updated ( Thursday, 06 April 2006 )
 
< Prev