A Linux command-line utility for the PICkit3
I've been using PIC microcontrollers for quite long now. I first started
with a simple JDM
programmer made from a couple of transistors and diodes, which
plugged into a serial port. I built the
programmer on solderless breadboard, and just left it there. It
worked just fine with all the PICs I used, and worked with the
Picprog
utility under Linux. The magic command line to burn the file
"mudcontrol.hex" onto e.g. a PIC16F876 device, with
the programmer on serial port /dev/ttyS1
looks something like this:
picprog --erase --burn -i mudcontrol.hex -d pic16f876 -p /dev/ttyS1 Unfortunately the JDM programmer was not supposed to work well with a USB serial port adapter, due to voltage level and timing issues. |
|
As serial ports were becoming something of an endangered species, I wanted
to get a USB programmer.
I briefly used a usbpicprog device,
which appealed to me because it was fully open source in hardware,
firmware as well as host software. The
hardware can be easily built at home (though you do need some
PIC programmer to initially load its firmware—once that is done,
updates can be done directly over the USB), but it was cheap enough that
I decided to support the project by ordering mine pre-built from
their web shop. The host software was sleek and functional, but there were some small issues (probably fixed since then) which left me looking for another solution. One of these days I ought to update its firmware and software and take a look at how it's doing. The project looks a bit quiet, but the list of implemented devices (not all tested, though) is impressive. In addition to the GUI, it also has a command line interface. The magic command might be (I haven't tested this): usbpicprog -p PIC16F876 --erase --write -f mudcontrol.hex |
|
Finally I pounced on a good
deal on a PICkit2 on eBay. It
worked from a USB port and could program
an even bigger pile of PIC devices than the JDM or (at the time)
usbpicprog. With the
pk2cmd
utility under Linux it was a pleasure to use directly from
the command line. (I couldn't find it on
Microchip's
website any more, but there are archived copies all over the Net.) This
simple and stupid program just worked, first time, every time. The magic
command line is:
pk2cmd -PPIC16F876 -Fmudcontrol.hex -MPICkit2 used to work under MPLAB X, although it was not "officially" supported. The latest version does not support it at all—Microchip definitely wants you to invest in the newer (but flakier) PICkit3. I did, but I'm still keeping the PICkit2! I like the pk2cmd command-line utility better, and overall the PICkit2 seems the most reliable of all my programmers. (Oh, speaking of MPLAB X, how in hell is it even possible that its installed size under /opt/microchip is seventeen fscking gigabytes??? Talk about an overbloated shitpile! I mean, for comparison, the entire /usr directory of my Debian installation takes less than 8 GB total, with all office, graphics, programming, multimedia, 2d CAD, 3d CAD, electronics design, planetarium and other software and their libraries included!!! I mean WTF???) |
|
The PICkit2 is just a programmer, but the PICkit3 was touted
as being a debugger as well. Also, it supports some devices
I was interested in (e.g. the PIC12F1840) which the
PICkit2 did not support. Unfortunately there's no official
command-line utility for PICkit3 (though I managed to
make my own which works—sorta), so you are kind of
being forced to use the more cumbersome MPLAB X IDE or IPE. So
far I have not used its debug features, but programming certainly does
work as advertized. Other than the inconvenience of having to use it through MPLAB X, the PICkit3 seems nearly as good as the PICkit2. I don't understand the holy wars surrounding the relative merits of the two, but Microchip, please make a proper Linux command-line tool like pk2cmd for PICkit3! (Yes, mdb.sh can be scripted, but it has its problems. See below for my attempt at creating a "pk3cmd" utility.) Some people claim that the PICkit3 requires you to externally power the target device, but that is bull (see below). By the way, the Microchip DM164135 "ONE PIC" demo kit includes a PICkit3 programmer. Farnell was selling the kit way cheaper than the programmer alone... |
|
Well, the PICkit3 can in fact provide Vdd to the target,
without any modifications! This just happens to be disabled by default. To
enable the feature from the MPLAB X IDE:
Or, if you're using the MPLAB X IPE instead of the full-blown IDE:
With the 2.00 version of MPLAB X I noticed the following
bugs in mdb.sh:
Powering the target device from the PICkit3
Some people have trouble with the PICkit3 because it is not providing
power (Vdd) to the target device. This results in the target device not
being found by the programmer, and thus programming fails. The
MPLAB X IDE reports
"Target device was not found. You
must connect to a target device to use PICkit 3." I've
even seen hardware modifications to the PICkit3 to enable it to
provide Vdd.
Now you can simply connect the five wires (Vpp, Vdd,
Vss, PGD, PGC) from the PICkit3 to the
target device, and program away. Just make sure you don't try to
power an entire power-hungry circuit from the PICkit3, since it
is limited to about 30 mA (or something, various sources quote
different values).
A Linux command-line utility for the PICkit3
MPLAB X ships with a tool called mdb.sh, which lives
in the
/opt/microchip/mplabx_v2.00/mplab_ide/bin
directory (or wherever
you installed MPLAB X). Its documentation is in
/opt/microchip/mplabx_v2.00/docs/MDBUserGuide.pdf.
It is a rudimentary and very buggy
text-based tool for using the PICkit3 and other devices.
This page details how some people have had success scripting
it, but I never got it to work properly that way.
So I wrote an expect script to talk to
mdb.sh.
The magic command line to erase and program a PIC16F876
device from the file "mudcontrol.hex" is:
pk3cmd PIC16F876 mudcontrol.hexIt works most of the time, at least for me. Your mileage may vary. Note that it does very little, if any, error checking. If something really screwy occurs and you have to kill it with Ctrl-C, the PICkit3 may be left in some weird state that you can fix by unplugging and reconnecting its USB cable.