How to use the PICkit2 programmer under Linux

I’ve been using the Microchip PICkit2 development programmer for a long time now, under Windows and Linux :), it’s a very good piece of hardware and I think the recommended companion for hobby PIC micro controller developers.

These days I had to install the software for Ubuntu Lucid (32 bits) at work, and I think I’ll list the required steps to have it working so that I don’t forget them ;), and maybe it can be of help.

  1. Download pk2cmd source code for Linux/Mac OS X (it’s a command line application), you can get the code from the official PICkit2 page under the Linux and Mac OS X Software section at the bottom of the page or using this link: pk2cmdv1.20LinuxMacSource.tar.gz
  2. Uncompress the file and cd to the directory
    $ tar xzvf pk2cmdv1.20LinuxMacSource.tar.gz
    $ cd pk2cmdv1.20LinuxMacSource
  3. Make sure you have libusb development files:
    $ sudo aptitude install libusb-dev
  4. Build the application for linux (this is for Linux kernels 2.6, for older kernels and/other systems (FreeBSD, Mac) check the ReadmeMakefile.txt file.)
    $ make linux
  5. Make install as root
    $ sudo make install
  6. The device definitions file is copied to /usr/share/pk2/ directory, in order to be able to access it this directory should be in your path. To add it, edit your .bashrc file (in your home directory) and add these lines at the beginning:
    PATH=$PATH:/usr/share/pk2/
    export PATH
  7. Test your installation:
    $ pk2cmd -?v

    If PICkit2 is not present (plugged in), you get:

    Executable Version:    1.20.00
    Device File Version:   1.55.00
    OS Firmware Version:   PICkit 2 not found
     
    Operation Succeeded

    If PICkit is plugged:

    Executable Version:    1.20.00
    Device File Version:   1.55.00
    OS Firmware Version:   2.32.00
     
    Operation Succeeded

For instructions on how to use pk2cmd execute it using the -?h parameter. As an example, if you want to program a sample.hex file to a pic16f84a you’d execute:

$ pk2cmd -ppic16f84a -fsample.hex -m

The output would be

PICkit 2 Program Report
3-7-2010, 9:55:24
Device Type: PIC16F84A
 
Program Succeeded.

That’s it. If you get an error message indicating that PK2DeviceFile.dat file can not be found, confirm whether /usr/share/pk2/ directory is in your path.

I have worked successfully with several micro controllers (12F508, 12F629, 12F683, 16F84, 16F628, 16F88, 18F2550 and 18F4550), under Ubuntu 32 and 64 bits without any major problem (sometime ago I read about some issues working under Ubuntu 64 bits, but I tried and didn’t find any).

9 thoughts on “How to use the PICkit2 programmer under Linux”

  1. I tried to make this application on Ubuntu Lucid but this error message apears:

    $make linux
    make TARGET=linux
    make[1]: se ingresa al directorio «/home/becario/Escritorio/programando_PIC_en_Ubuntu/pk2cmdv1.20LinuxMacSource»
    g++ -Wall -D_GNU_SOURCE -O2 -I/usr/local/include -DLINUX -DUSE_DETACH -DCLAIM_USB -o pk2usbcommon.o -c pk2usbcommon.cpp
    In file included from pk2usbcommon.cpp:32:
    pk2usb.h:49:26: error: usb.h: No existe el fichero o el directorio
    pk2usbcommon.cpp: In member function ‘void CUsbhidioc::CloseReport()’:
    pk2usbcommon.cpp:648: error: ‘usb_release_interface’ was not declared in this scope
    make[1]: *** [pk2usbcommon.o] Error 1
    make[1]: se sale del directorio «/home/becario/Escritorio/Programando_PIC_en_Ubuntu/pk2cmdv1.20LinuxMacSource»
    make: *** [linux] Error 2

    I don know how to correct this error!!!!

  2. Are you sure you have the libusb dev package?

    You can install it by running this on a terminal:

    $ sudo aptitude install libusb-dev
  3. Excelente.. funciona:

    LinuxMint
    Release 13 (maya) 64-bitKernel
    Linux 3.2.0-23-generic
    GNOME 3.4.1
    Memory:5.8 GiB
    Processor:Intel® Core™2 Duo CPU P8700 @ 2.53GHz × 2

    Exactamente como lo tienes.
    Solo tuve que crear:
    gedit /home/martin/.bashrc

    Probar con: pk2cmd -?v
    Executable Version: 1.20.00
    Device File Version: 1.55.00
    OS Firmware Version: 2.32.00

    Hasta aqui todo ok…

    gracias.
    Querétaro, México

  4. Hello !

    I’ve just bought my Pickit 2 and I want to compile it to include a custom PK2DeviceFile.dat file which enables my PIC18F46K22. This file works without problem with the GUI pickit2 programmer on windows, but when I compile pk2cmd on linux, here is my error :

    pk2cmd -?v
    PK2DeviceFile.dat device file requires an update of pk2cmd.

    Executable Version: 1.20.00
    Device File Version: not found
    OS Firmware Version: 2.32.00

    What can I do ?

    Thanks a lot for this great guide !

  5. Hello I get this error “illegal instruction”

    root@ip2a:/home/test/pk2cmdv1-20Linux2-6/2/pk2cmdv1-20Linux2-6# ./pk2cmd -M -PPIC16F1827 -A3.3 -Ftest.hex
    Illegal instruction
    root@ip2a:/home/test/pk2cmdv1-20Linux2-6/2/pk2cmdv1-20Linux2-6#

    Any help will be appreciated

Leave a Reply