I have used these neat little micro controllers from Microchip's PIC family for various projects throughout the years using different compilers, assemblers and programmers. What I have recently used is the PICKit2. The best thing is that Microchip has released Linux drivers for it! Here is how to get it all running.
The first step is to obtain the release from
www.microchip.com. I won't even try to place a direct link here since they seam to change their website structure on a regular basis just to make it harder to find what you are looking for. However locate the downloads for the PICKit2 and scroll down a bit. The Linux and MacOS drivers should be there. Note that the PICKit
3 does NOT have drivers for anything but Windows so make sure you get a PICKit
2. The latest release when I am writing this is
1.20. Microchip is kind and releases pre-compiled binaries for those who do not want to build from source but I will go through the building steps here to get it all covered. The release archive name is
pk2cmdv1.20LinuxMacSource.tar.gz and the procedure is quite straight forward. Unpack, Compile. Install. There is no configuration step prior to compilation but the development libraries for usb-dev is required for the compile. Please note that I have only tested this under Debian Squeeze. EDIT 2014-06-19: Works fine under Debian Wheezy as well.
Install required library (you might have this installed already)
# apt-get install libusb-dev
Unpack and compile
# tar zxvf pk2cmdv1.20LinuxMacSource.tar.gz
# cd pk2cmdv1.20LinuxMacSource
# make linux
Three files are required for everything to run and they have to be copied to somewhere in your PATH.
# cp pk2cmd /usr/local/bin
# cp PK2DeviceFile.dat /usr/local/bin/
# cp PK2V023200.hex /usr/local/bin/
Verify (as root!) that you can reach your PICKit2 dongle.
# pk2cmd -?V
Executable Version: 1.20.00
Device File Version: 1.55.00
OS Firmware Version: 2.32.00
Operation Succeeded
If you get something like "
OS Firmware Version: PICkit 2 not found" then make sure that the PICKit2 is connected and that you are running as root.
# lsusb | grep Microchip
Bus 003 Device 030: ID 04d8:0033 Microchip Technology, Inc. PICkit2
It is a bit ugly to be required to program your chips as root. This can be fixed by adding a udev rule.
Create a new file
/etc/udev/rules.d/99-pickit2.rules containg the following:
SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device",SYSFS{idVendor}=="04d8" , SYSFS{idProduct}=="0033", MODE="0666"
It is important that you restart udev and after that reconnect the PICKit2 for the new rule to apply.
# cat /etc/udev/rules.d/99-pickit2.rules
SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device",SYSFS{idVendor}=="04d8" , SYSFS{idProduct}=="0033", MODE="0666"
# /etc/init.d/udev restart
Now you should be able to run the PICKit2 as a regular user. Programming was a bit of trial and error for me but I will present the procedure I found was working. The
pk2cmd application is used for all PICKit2 activities and it has quite a bit of arguments that can be issued. One of the most important arguments is "
-P". It tells pk2cmd which kind of PIC we are actually programming. It can also be used to auto-detect the connected PIC. I think the best way of describing pk2cmd is by a set of examples.
Run auto-detection
$ pk2cmd -P
Auto-Detect: Found part PIC16F630.
Operation Succeeded
Now we know which PIC we have connected (odds are that we knew this already).
Erase the PIC
$ pk2cmd -P PIC16F630 -X -E
Erasing Device...
Operation Succeeded
Note that the device name from the auto-detection is now placed after the
-P argument. Two new arguments is supplied now.
-X tells the PICKit2 to "
Use VPP first Program Entry Method". I get read/write errors if I do not supply this no matter how I connect my PIC's but your experience may be different.
-E tells pk2cmd to erase the connected PIC.
Program a HEX file to the PIC
$ pk2cmd -P PIC16F630 -X -M -F code.hex
PICkit 2 Program Report
16-8-2012, 10:21:05
Device Type: PIC16F630
Program Succeeded.
Operation Succeeded
Two new arguments again.
-M tells pk2cmd to actually program the PIC.
-F tells pk2cmd which file to use. The
-M argument can actually also be either
-MP,
-ME,
-MI or
-MC to program only
Program memory,
EEPROM,
ID memory or
Configuration memory respectively but for most cases you will program the entire PIC using
-M.
Verify a program
$ pk2cmd -P PIC16F630 -Y -F code.hex
PICkit 2 Verify Report
16-8-2012, 10:28:59
Device Type: PIC16F630
Verify Succeeded.
Operation Succeeded
The argument
-Y tells pk2cmd to verify the PIC's memory with the HEX file given by
-F. Again the
-Y argument can address different areas just as the
-M argument by setting either
-YP,
-YE,
-YI or
-YC to verify a specific region only.
The PICKit2 can be used to power a connected device. For example the "
PICKit2 Low Pin Count Demo Board". The argument for turning on power is
-T.
Power ON
$ pk2cmd -P PIC16F630 -T
Operation Succeeded
Power OFF
$ pk2cmd -P PIC16F630
Operation Succeeded
This was quick demonstration of the PICKit2 under Linux. Make sure to check out all the other arguments for pk2cmd.