Sunday, April 27, 2008

pk2cmd Linux Port under Fedora Core 6

Here is a mini-howto to get pk2cmd Linux port to work under Fedora Core 6. It should work for Fedora Core 5 and Fedora 7 as well. Hopefully it also works for Fedora 8. I have never used Fedora 8 since it failed to boot on my machine because of the conflict with FreeBSD.
http://mcuee.blogspot.com/2007/11/new-fc8-install-hangs-at-message.html

1) Download pk2cmdLinux from Jeff Post's website.
Download link: http://home.pacbell.net/theposts/picmicro/

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

As of this writing, the version is 0.8.

2) Preparation to get pk2cmd working is easy under FC6.

You need to have g++ and make. You also need to install libusb-devel package. You can use yum (yum install libusb-devel) as root to install this package.

3) Edit the Makefile and then build it using make.

You need to edit the Makefile to to define the proper CFLAGs.
FC6 is usingLinux kernel 2.6.x and support udev rules. So we will use the following option.

# ----- Use the following for Linux kernel 2.6.x: -----
CFLAGS=$(OPTS) -I$(LIBUSB)/include -DLINUX -DUSE_DETACH -DCLAIM_USB

Now we can build pk2cmdLinux. Just type "make".

4) Test it as root and it should just work.

5) In order to run pk2cmdLinux as a normal user, we need to set up udev rules.

FC6 uses udev with PAM. You can read the file 'usbhotplug.txt'bundled with the pk2cmdLinux source package for more details. A more up-to-date document is presented in the Gphto project website:
http://www.gphoto.org/doc/manual/permissions-usb.html

Firstly you need to create the file/etc/udev/rules.d/26-microchip.rules with thefollowing content.

#PICKit2SUBSYSTEM=="usb_device", ACTION=="add", SYSFS{idVendor}=="04d8",SYSFS{idProduct}=="0033", SYMLINK+="pickit2-%k"

Secondly, you need to create the file/etc/security/console.perms.d/pickit2.perms
with thefollowing content:

=/dev/pickit2* 0600 0600 root

6) After this unplug and then plug PICkit 2 back in the USB port andit should work.
If not, reboot the machine.

Now we can test pk2cmdLinux under FC6. Here are some examples.

[mcuee@localhost pk2cmdLinux-0.8]$ ./pk2cmd -?V
Executable Version: 1.01.00 (Linux/Mac port 0.8)
Device File Version: 1.42.00OS
Firmware Version: 2.30.01
Operation Succeeded

[mcuee@localhost pk2cmdLinux-0.8]$ ./pk2cmd -PPIC16F690 -I
Device ID = 1403
Operation Succeeded

[mcuee@localhost pk2cmdLinux-0.8]$ ./pk2cmd -PPIC16F690 -GFlowpindemo.hex
Read successfully.
Operation Succeeded

[mcuee@localhost pk2cmdLinux-0.8]$ ./pk2cmd -PPIC16F690 -Y -Flowpindemo.hex
PICkit 2 Verify Report
27-4-2008, 11:49:37
Device Type: PIC16F690
Verify Succeeded.
Operation Succeeded

[mcuee@localhost pk2cmdLinux-0.8]$ ./pk2cmd -PPIC16F690 -T
Operation Succeeded

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

USB PIC related open source projects

Updates to be found in Microchip Forum:
http://www.microchip.com/forums/tm.aspx?m=235427

1. PUF using sdcc : http://vasco.gforge.enseeiht.fr/

"PUF is a USB application framework dedicated to Linux (on the host side) and to the PIC 18F4550 family of microcontrollers (on the device side). PUF includes: a bootloader that can flash the PIC application through the USB, a PIC demo and its corresponding application on the host side, and docker, the host-side flash programming utility.

2. PIC18F4550 USB/MSD sourceforge, Apache License V2.0
http://sourceforge.net/projects/pic18fusb

3) GPLed USB stack with SDCC
http://forum.microchip.com/tm.aspx?m=170553
http://www.nutsvolts.com/%7Edownloads/USB.zip

4) PICDEM FS USB bootloader for Linux/Windows using libusb
http://www.internetking.org/fsusb/
http://forum.microchip.com/tm.aspx?m=106426

5) PICDEM FS USB demo appliation under Linux/Windows using libusb
http://www.varxec.net/picdem_fs_usb/

6) PIC18F2455/4455 Assembler USB Firmware (MPASM) From Bminch
http://forum.microchip.com/tm.aspx?m=89669
http://pe.ece.olin.edu/ece/projects.html

7) MPLAB C18 based PIC USB firmware from Bminch (simpler than Microchip's)
http://forum.microchip.com/tm.aspx?m=125158
http://pe.ece.olin.edu/ece/projects.html

8) Microchip USB C18 Firmware framework ported to SDCC
http://www.efn.org/~rick/work/MCHPFSUSB/

9) USB Midi (using gpasm and sdcc). It is said that the firmware is also based on the Microchip Firmware Framework.
http://www.ucapps.de/mbhp_usb_pic.html

javax usb under Ubuntu 7.10


I have tried to build javaxusb from the CVS source under Ubuntu 7.10. I can
build javax-usb and java-usb-ri without a problem with the Java 6.0 package
included in Ubuntu. But I could not build the package javax-usb-ri-linux.

mcuee@ubuntu710:~/Desktop/build/javaxusb
/javax-usb-ri-linux$ ant jars
Buildfile: build.xml

compile:
[echo] Compiling class files.
[echo] Compiling JNI library.
[javah] Error: A required super class
com.ibm.jusb.os.AbstractUsbServices could not be found.

BUILD FAILED
/home/mcuee/Desktop/build/javaxusb/javax-usb-ri-linux/build.xml:114:
compilation failed

Total time: 1 second

Luckily, Google found this and it works for me.
http://www.mail-archive.com/javax-usb-devel@lists.sourceforge.net/msg00101.html

mcuee@ubuntu710:~/Desktop/build/javaxusb/javax-usb-ri-linux$
javah -d jni -classpath src:$CLASSPATH com.ibm.jusb.os.linux.JavaxUsb
(This will produce the file com_ibm_jusb_os_linux_JavaxUsb.h in the jni directory)
mcuee@ubuntu710:~/Desktop/build/javaxusb/javax-usb-ri-linux$ cd jni/
mcuee@ubuntu710:~/Desktop/build/javaxusb/javax-usb-ri-linux/jni$
export JAVA_HOME=/usr/lib/jvm/java-6-sun
mcuee@ubuntu710:~/Desktop/build/javaxusb/javax-usb-ri-linux/jni$ make all
(This will create the file libJavaxUsb.so)
mcuee@ubuntu710:~/Desktop/build/javaxusb/javax-usb-ri-linux/jni$
mcuee@ubuntu710:~/Desktop/build/javaxusb/javax-usb-ri-linux/jni$ cd ..
mcuee@ubuntu710:~/Desktop/build/javaxusb/javax-usb-ri-linux$ gedit build.xml
Remove the following part.
.JavaxUsb">
destdir="${jsr80.linux.jni.src.dir}"
verbose="${jsr80.linux.verbose}"
classpath="${java.class.path}:${jsr80.linux.src.dir}"/>

}" failonerror="yes">
.library}"/>

mcuee@ubuntu710:~/Desktop/build/javaxusb/javax-usb-ri-linux$ ant jars
Buildfile: build.xml

compile:
[echo] Compiling class files.
[echo] Compiling JNI library.

jars:
[echo] Creating JAR files.
[copy] Copying 1 file to
/home/mcuee/Desktop/build/javaxusb/javax-usb-ri-linux/lib

BUILD SUCCESSFUL
Total time: 0 seconds

After this, I can build the examples and they seem to run fine.