Thursday, April 24, 2008

pk2cmd Linux port under Ubuntu

Edit in 09/2009: new download link
http://www.microchip.com/pickit2

Download link:
http://home.pacbell.net/theposts/picmicro/

As of this writing, the version is 0.8. I've tested the following under Ubuntu 6.06 and 7.10. It should work for 7.04 as well. For Ubuntu 5.04 and maybe 5.10, you should use hotplug rules instead of udev rules. But I no longer use these older versions.

How to build it from the downloaded source? You need to have gcc, g++, make (sudo apt-get install build-essential) and libusb-dev (sudo apt-get install libusb-dev).

You will also need to edit the Makefile and choose the following option.
# ----- Use the following for Linux kernel 2.6.x: -----
CFLAGS=$(OPTS) -I$(LIBUSB)/include -DLINUX -DUSE_DETACH -DCLAIM_USB

After that, just type "make" and you should have pk2cmd Linux port built.

You will need to set up proper udev rules to run pk2cmd as a normal user and not using sudo.

More detailed steps.
1) Create a group called microchip, add your user name to it.
GUI method: System --> Administration --> Users and Groups -->
Manage Groups --> Add Group
Group name : microchip
Group member: add your user name (mine is "mcuee").

To confirm that this is a success.
mcuee@ubuntu710:~$ groups mcuee
mcuee : mcuee adm dialout cdrom floppy audio dip video plugdev scanner
lpadmin admin netdev powerdev microchip

Take note that the output has "microchip" in it.

The command line way of doing this.
mcuee@ubuntu710:~$ sudo groupadd microchip
mcuee@ubuntu710:~$ sudo gpasswd -a mcuee microchip
(Replace mcuee with your username linuxdev).
mcuee@ubuntu710:~$ groups mcuee
mcuee : mcuee adm dialout cdrom floppy audio dip video plugdev scanner lpadmin admin netdev powerdev microchip

2) Create the udev rule 026_microchip.rules in the directory
/etc/udev/rules.d. You can use gedit.
mcuee@ubuntu710:~$ sudo gedit /etc/udev/rules.d/026_microchip.rules
Here is my udev rules for PICKit 2 and some other Microchip USB tools.

Edit: please follow Peter Restall's comments and use ATTRS{} instead of SYSFS{}. SYSFS{} is deprecated in later version of udev.

mcuee@ubuntu710:/etc/udev/rules.d$ cat 026_microchip.rules
#PICKit 1
SYSFS{idVendor}=="04d8", SYSFS{idProduct}=="0032", MODE="0660",
GROUP="microchip"
#PICKit 2
SYSFS{idVendor}=="04d8", SYSFS{idProduct}=="0033", MODE="0660",
GROUP="microchip"

#ICD2
SYSFS{idVendor}=="04d8", SYSFS{idProduct}=="8000", MODE="0660",
GROUP="microchip"
#ICD21
SYSFS{idVendor}=="04d8", SYSFS{idProduct}=="8001", MODE="0660",
GROUP="microchip"

#PICDEM FS USB Bootload
SYSFS{idVendor}=="04d8", SYSFS{idProduct}=="000b", MODE="0660",
GROUP="microchip"
#PICDEM FS USB Demo
SYSFS{idVendor}=="04d8", SYSFS{idProduct}=="000c", MODE="0660",
GROUP="microchip"
#PICDEM FS USB CDC-ACM
SYSFS{idVendor}=="04d8", SYSFS{idProduct}=="000a", MODE="0660",
GROUP="microchip"

3) Unplug and plug PICkit 2 and check if the udev rules work or not. If not, you may reboot and check again. Actually a better way is to restart udev and then unplug and plug in PICkit 2.

mcuee@ubuntu710:~$ sudo /etc/init.d/udev restart
* Loading additional hardware drivers... [ OK ]

mcuee@ubuntu710:~$ lsusb
Bus 003 Device 001: ID 0000:0000
Bus 002 Device 001: ID 0000:0000
Bus 001 Device 003: ID 04d8:0033 Microchip Technology, Inc.
Bus 001 Device 004: ID 04d8:000c Microchip Technology, Inc.
Bus 001 Device 001: ID 0000:0000

mcuee@ubuntu710:~$ ls -la /dev/bus/usb/001
total 0
drwxr-xr-x 2 root root 100 2008-04-24 15:26 .
drwxr-xr-x 5 root root 100 2008-04-24 15:26 ..
crw-rw-r-- 1 root root 189, 0 2008-04-24 15:26 001
crw-rw-r-- 1 root microchip 189, 2 2008-04-24 15:26 003
crw-rw-r-- 1 root microchip 189, 3 2008-04-24 15:26 004

Now you can see that PICkit 2 is in Bus 001 and it belongs
to root user but group member of microchip have read
and write access to it.

4) now you should be able to run pk2cmd as normal user.
mcuee@ubuntu710:~/Desktop/build/pk2cmd/pk2cmdLinux-0.8$./pk2cmd -PPIC16F690 -I
Device ID = 1403

Operation Succeeded

17 comments:

phill said...

I just purchased my ICD2. I wasn't sure what the differences were between ICD2 and ICD21 for the microchip.rules file. If you run lusb, it will give a report like: Bus 002 Device 004: ID 04d8:8001 Microchip Technology, Inc.

You can see from this what you need to write in your rules file.

Xiaofan Chen said...

MPLAB ICD2 has two modes, one is the bootloader mode and the other is the normal mode. They use the same VID but two different PIDs. That is why we need two entries.

Phill said...

Hmm, any idea which number is what mode? Do you know of any resources that describe this further?

Xiaofan Chen said...

I believe 0x8001 is the normal mode and 0x8000 is the bootloader mode.

You can check Piklab project for more details.

You can also check this long thread.
http://www.edaboard.com/ftopic161641.html

Cantareus said...

I have my PICkit 2 working with Ubuntu 8.04. I wouldn't have been able to do it without this page. Thank you!

Xiaofan Chen said...

You are welcome!

Chunk (not really) said...

Is pk2cmd specific to the PICkit 2? I have an ICD2 that I've set up following the directions here, its listed in lsusb and /dev/bus/usb, with the proper permissions, but pk2cmd simply says "no PICkit2 found." Do I need different compile options?

Xiaofan Chen said...

Yes pk2cmd is specific to PICKit 2. It will not work with ICD 2.

hoyungh said...

I am just starting out with linux mint
I am a newby so pls be kind
I followed the instructions on this page and was able to get to step 4 before I failed. For step 4 pk2cmd -p - i
I got no Pickit 2 found

since I did get
Bus 002 Device 008: ID 04d8:0033 Microchip Technology, Inc
for lsusb what could be wrong?

hoyungh said...

Sorry. Ignore my earlier comments. Your instructions on this blog work.

I used latest version of pk2cmd from microchip

When I switched to the version on Cannasoftware site everything worked

Xiaofan Chen said...

Recently there is one similar report that the released binary does not work under Ubuntu 7.10.

The solution is for him to build the binary by himself. And that works.

http://groups.google.com/group/pickit-devel/browse_thread/thread/a09072f222898e65

The version on Cannasoftware is a bit old.

David E. Grayson said...

I was experiencing the "No PICkit 2 found" problem in Ubuntu Linux and I fixed it byt adding the following line to the top of pk2usb.cpp:
#define HAVE_LIBUSB_INTERRUPT_MODE 1

See http://groups.google.com/group/pickit-devel/browse_thread/thread/a09072f222898e65/fd0bb174944087b8 for details.

Pete Restall said...

I'm using Ubuntu 8.04 (Hardy Heron) and it looks like my /etc/udev/rules.d is a slightly different build/version from the one used in Xiaofan's original post.

There's a README file in the directory giving the correct naming conventions for the rule files; I called mine 50-microchip.rules rather than 026_microchip.rules - and the changes were automatically picked up when the file was written to disk (no daemon restarts, etc.)

Also, the SYSFS{...} commands inside the rules file needed to be changed to ATTRS{...} to get the nodes created under the proper group, otherwise they just stayed owned by root:root.

Thanks for the information, Xiaofan.

Yves McDonald said...

Thanks to Xiaofan and Pete, I got my PICKit working with Ubuntu 9.04 (Jaunty Jackalope), using new naming convention for rule files and changing SYSFS to ATTRS. Kind of arcane words to me... but it worked.

At the time of writing this, the Piklab IDE doesn't work well with PICKit firmware v 2.32 (sigh) but I can live with pk2cmd, perhaps invoking it from the IDE...

Cheers!

Xiaofan said...

Please follow Peter Restall's comments on the udev rule.

M said...

I suppose the firmware listed under software for Windows (at M-chips Picki2 site) should work even if it's installed from Linux. Since I refuse to take anything for granted, I ask.

Is my assumption correct?

Xiaofan said...

To M: yes that is correct.