Sunday, June 10, 2007

PICkit 2 under Linux Mini-Howto

Edit: please take note this Mini-How to is obsolete now. Please go to Microchip's PICkit 2 website to download pk2cmd codes. It now supports Linux and Mac OS X along with Windows.

PICkit 2 (http://www.microchip.com/pickit2) is a nice little USB programmer from Microchip. It now supports many Flash MCUs from Microchip. As of now, most of the PIC12F/16F/18F 8-bit PIC MCUs and PIC24/dsPIC30/dsPIC33 16bit MCUs are supported (http://ww1.microchip.com/downloads/en/DeviceDoc/PICkit%202%20Readme%20v2-30.txt).

Microchip has kindly published the source codes for PICkit 2 firmware and the standardalone PC application (http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1960).

PICkit 2 works under Linux thanks to the efforts from several developers. The easiest to start with is pk2 from Jeff Post (http://home.pacbell.net/theposts/picmicro/). pk2-2.04 only supports version 1.x (up to V1.21) firmware. The upcoming pk2-3.x will support version 2.x firmware.

The following is the simple procedure to get pk2-2.04 built under Linux.
1) Make sure you have the necessary compiler (gcc/make) and libusb development package. Normally they are already included in the distribution.

2) Download pk2-2.04. Apply the follow patch for the file pk2usb.c.

//#if HAVE_LIBUSB_INTERRUPT_MODE
// latest libusb: interrupt mode, works with all kernels
# define PICKIT_USB(direction) usb_interrupt_##direction //only keep this line
//#else
// older libusb: bulk mode, will only work with older kernels
//# define PICKIT_USB(direction) usb_bulk_##direction
//#endif

3) Modify the default Makefile. Normally you should be using kernel version Linux 2.6.x and you should use the following option.
# ----- Use the following for Linux kernel 2.6.x: -----
CFLAGS=$(OPTS) -I$(LIBUSB)/include -DLINUX -DUSE_DETACH -DCLAIM_USB

4) Built pk2 with the command "make"

5) Test out pk2. Normally you need to run as root if you have not setup the proper hotplug/udev rules.

6) If it is okay, set up the hotplug/udev rules. This will depend on your distribution. The following is a quick start for you.
http://piklab.sourceforge.net/wiki/index.php/Port_connection_problems
You can read the bundled usbhotplug.txt by Linus Walleij for more information about udev/hotplug.

7) If you encounter problems with pk2, try to ask question in pickit-devel mailing list (http://groups.google.com/group/pickit-devel). Take note that you should ask general questions about PICkit 2 in Microchip Forum (http://forum.microchip.com/tt.aspx?forumid=15). There are some good information about PICkit 2 in the Microchip Forum. In paticular, if you encounter USB emuneration problem under Linux, try the hardware changes detailed in the post (http://forum.microchip.com/tm.aspx?m=245280). Only Step 2 is necessary.

8) How to help out?
From Jeff Post: http://home.pacbell.net/theposts/picmicro/helping.html

Note on 10-April-2009: it is now better to use pk2cmd at Microchip PICkit 2 websites.
http://www.microchip.com/pickit2



5 comments:

Darrell said...

Xiaofan, I was real excited to see this post. But, alas, I think my issues are more fundamental.

Is there a particular location to put the pk2 source code? The reason is that I have tried both FC6 and FC7 and keeping running into issues of not finding certain .h files in the search path. Probably incorrectly, I add another -I option which fixes one "no such .h file" only to create another. So, I add another -I for that one, and so on. This surely is incorrect. It would seem that most folks just type "make" and things progress.

For example, the first one I encounter is not finding usb.h. I find it at /usr/src/kernels/2.6.20-1.2962.fc6-i686/include/linux so add a -I for this path. Next compile attempt then it seems that it can't find compiler.h which is in the directoty above where I found usb.h. So, I add a -I option for that. Clearly, this is nuts. What am I missing that is so fundamental here? Why would Fedora be so different from Ubuntu when it comes to compiling the source?

So, this is where I can use some help. Seems that "make" is a pretty big leap! It's probably obvious stuff for you experts but not for all of us ... some hints would be greatly appreciated.

Xiaofan Chen said...

This bas been solved in pickit-devel Google group. You need to havelibusb (and libusb-devel) installed.

Xiaofan Chen said...

pk2cmd Linux port is now the preferred application to get PICkit 2 to work under Linux.

Mario Xerxes Castelán Castro said...

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

April 6th 2010 in mcuee.blogspot.com article comments "PICkit 2 under
Linux Mini-Howto".

>Microchip has kindly published the source codes

Did you read the copyright notice?.

The microchip source code is propietary, they don't allows you to do
*anyting* with it. I wouldn't call it kindly.

Plus, You are talking about the GNU/Linux OS. Linux is only the
kernel.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEAREIAAYFAku73awACgkQZ4DA0TLic4guVACfZyfEIYWVtrlOEph7rVE3UtFr
6dkAn0RR0UuwjIS917FnD3J/J1bgBZhA
=Xc3w
-----END PGP SIGNATURE-----

Xiaofan said...

To Mario: you have your interpretation of the license. To me it is good enough for most users unless you are a GPL purist. I am an OS neutral guy.

And I do not like to use GNU/Linux as Linux is the more often used generic name for both the kernel and the distros.

You are of course free to refer it as GNU/Linux.