Programming a PIC32 using a USBASP
----------------------------------

To do this requires 2 USBASP (aka USBISP) boards, because new firmware
needs writing to a board (you can save the existing firmware and restore
it if you wish).  You need to fit a jumper (JP2) to the board being
changed to let its code be written.

You will also need some resistors, as the USBASP is 5V logic and the
PIC32 is 3V3.

The boards I bought were the cheapest on ebay (about $3) and each
included a 10-way ribbon cable.  Quite handy as I used one to a circuit
with the resistors on a breadboard and the other for reprogramming.

1. Reprogramming a USBASP

1(a) Using AVRDUDE - should just work but is more to install.

Install the AVRDUDE program.  It's a command-line utility with slightly
odd syntax and quite verbose, but easy to use and it "just worked".

Connect the self-programming jumper (JP2) on the board to be changed
(the slave; the other will be master), then daisy-chain the boads (plug
them together using the 10-way cable).

Look closely at the CPU chip on the boards.  You'll need it shortly.
Mine was ATMEGA8A, which is an atmega8.

Plug the master board (the one without JP2 shorted) into a USB port.
You should be able to see it in Windows Device Manager (or the like) or
with Linux lsusb (or in syslog / with dmesg).  Mine was VID 16C0
PID 5DC, which looks to be more or less standard.

Open a command prompt (I guess as Administrator on Windows).  On Linux
you can either make a udev rule (they're on the net) to allow access as
a normal user or just use sudo / change tobe root.

If you want to save the firmware, use:
    avrdude -p xxx -P usb -c usbasp -U flash:r:usbasp.hex:i
where xxx is the CPU type (atmega8 for me).
Yes, you need the colons (:), now you know why I said odd syntax.

You should see quite a bit of output and it should create the file (in
this case usbasp.hex).

To write the new firmware use:
    avrdude -p xxx -P usb -c usbasp -U flash:w:pic32asp.hex:i
again, xxx is the CPU on your board (atmega8 for me).

If all goes well, unplug from the USB, separate the boards, remove the
JP2 jumper - and make sure you know which board is which!

1(b) Using progasp.exe - much easier (it's test code)

Plug the boards together like 1(a) and then at an Administrator command
prompt use:
    progasp -e pic32asp.hex

Unplug the boards as in 1(a).

If it doesn't work (mainly you'll know by trying #2 below), AVRDUDE is
the fix.


2. Programming a PIC32

The basic idea is to connect wires from the USBASP, via voltage dividers,
to the PIC32's ICSP pins (and GND).

The pins on the USBASP connector are 1 (old use MOSI), 5 (RESET),
7 (old use SCK) & 10 (GND).

Be sure to connect pin 10 (GND) to the PIC32 board's GND.

You will need to power the PIC32 board somehow; the USBASP will get its
power from the USB port.

The easy wires are for RESET (MCLR/) and PGC as they're unidirectional.
I used 2K2 and 3K3 resistors:
  USBASP pin ---- 2K2 ---- X ---- 3K3 ---- GND
where X goes to the PIC32 pin.  Do this for each of the PIC32's MCLR/,
PGC and PGD (which needs an extra resistor, as below).

Now, PGD is bidirectional and doesn't really output enough volts as a
logic high (its VOH is the same as the ATMEGA's VIH) so I also used a
10K pullup from PGD to 3V3.

Power your PIC32 board and plug the USBASP into a USB port.

As with AVRDUDE, you need a command prompt with the ability to access
USB hardware (so, Administrator on Windows, and either udev rule or as
root on Linux).  Run the pic32asp program (pic32asp.exe on Windows)
with a command line like:
    pic32asp -e myfile.hex
(or ./pic32asp if you're somewhere not in your PATH)
and so long as it can find the USBASP it will erase (-e) then program
the PIC32.

You can use:
    pic32asp -h
to get help.


Good luck!

John Skelton
