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