Bus Pirate v4 and Pickit2 (bootloader and firmware updates)

From DP

Jump to: navigation , search

Contents

Get a Pickit 2

This link is not pointing to a particular seller, so it shouldn't be considered as advertising from my side. This link just gives a list of sources - examples of where you could find this tool

If you don't have a Pickit 2 you could get it with free shipping included just for $7.98 - $10.15 (bigger price with ZIF socket) - https://www.aliexpress.com/wholesale?minPrice=7&maxPrice=&isBigSale=n&isFreeShip=y&isFavorite=n&isRtl=n&isLocalReturn=n&isMobileExclusive=n&shipFromCountry=&shipCompanies=&SearchText=pickit+2&CatId=0&g=y

It costs just a fraction of Bus Pirate v4 price and makes the firmware updates much more convenient (+ you could install and re-install the bootloaders without any risk of bricking, can always recover)

Obtain the necessary .hex files

There are two ways of getting the firmware/bootloader .hex files:

If you are downloading, it is important to check the file checksums! For example:

sha1sum ./package_latest_18092016.zip
8a256ee9916906ee6cff7716e3fab3b19b81f09f
unzip package_latest_18092016.zip
cd ./Bus_Pirate/
sha1sum ./package_latest/BPv3/*
cat ./package_latest/BPv3/SHA1_checksums.txt
sha1sum ./package_latest/BPv4/bootloader/*
cat ./package_latest/BPv4/bootloader/SHA1_checksums.txt
sha1sum ./package_latest/BPv4/combined/*
cat ./package_latest/BPv4/combined/SHA1_checksums.txt
sha1sum ./package_latest/BPv4/firmware/*
cat ./package_latest/BPv4/firmware/SHA1_checksums.txt


About the optimization levels:

- 0 - Do not optimize. The compiler’s goal is to reduce the cost of compilation and to make debugging produce the expected results
- 1 - Optimize. Optimizing compilation takes somewhat longer, and a lot more host memory for a large function. The compiler tries to reduce code size and execution time
- 2 - Optimize even more. The compiler performs nearly all supported optimizations that do not involve a space-speed trade-off
- s - Optimize yet more favoring size (superset of O2)
- 3 - Optimize yet more favoring speed (superset of O2)

Even on level 3 my BPv4 passes a self test successfully, no errors. So you could try a stronger optimization first, and then - if it is causing you some issues - you tell us about them on Github and meanwhile could use a lighter optimization version (such as level 1)

Prepare the .hex file flashing software

pk2cmd

If you are on Linux (or Mac):

git clone https://github.com/psmay/pk2cmd
cd ./pk2cmd/pk2cmd/
make linux


If you are on Windows:

git clone https://github.com/protomouse/pk2cmd

Then you open .sln project file in Visual Studio and build it

MPLAB X IPE

Alternatively you could use MPLAB X IPE tool. It comes together with MPLAB X and is cross-platform, however it is closed source - and pk2cmd alternative is more lightweight than IPE.

Firmware and Bootloader upgrade plan

Here is a plan of what we are going to do:

1) Connect a debugger to ICSP header
2) Connect a debugger to computer
3) Erase the whole PIC24FJ256GB106 memory
4) Flash a bootloader .hex file
5) Flash a firmware .hex file
6) Disconnect a debugger from computer
7) Disconnect a debugger from ICSP header


Please note: disconnect a Bus Pirate from USB power before connecting a debugger to ICSP header, because if they are both connected at the same time this could cause overvoltage (5V + 3.3V) (I will remove this warning if it is not true)

Connect a Pickit 2 to ICSP of Bus Pirate

If you have ordered a Bus Pirate from SeeedStudio (instead of making it by yourself) most likely its' ICSP header is not soldered - no pins, only 5 holes!

You have two options:

  • 1) Solder 5 pins - unacceptable for the people with unreliable soldering skills!
  • 2) Use the following trick:
With Pickit2 (and probably with ICD3) there comes a cable for connecting a debugger to your device. This cable is composite: it is "6P female <--> 6P female" with inserted long 6 pins on each end - which turns it into "6P male <--> 6P male" cable.
6th pin ("Auxiliary") is unnecessary and impossible to insert (no 6th hole), so I just temporarily removed this pin from device's end of cable - which turned this end into "5P male".
Now, when I need to flash a firmware or debug, I just insert these 5 pins into ICSP header holes and then holding a device's end of cable together with BP4 - with one hand - aligned in such way so that there is a constant reliable contact between these 5 pins and ICSP header hole inner parts.
Advantage of this approach is that you don't need to solder and actually could connect to ICSP on BP4 easier and quicker than if there were the pins. Disadvantages:
1) one of your hands is busy
2) using a keyboard with one hand is slower
3) if your hand becomes too tired and some pin accidentally disconnects from hole's inner part, you'll get a failure and need to repeat the process
To avoid these disadvantages you could improvise a reliable BPv4 holder


Connect this cable correctly: MCLR pins of debugger and BPv4 should match!

Erasing and flashing of PIC24FJ256GB106

It is quite obvious how to do this under MPLAB X IPE, so I will provide the instructions for pk2cmd under Linux.

Go to a directory with pk2cmd executable and get the root rights (for convenience) :

sudo su
cd ./pk2cmd/pk2cmd/


Launch pk2cmd command below to erase the whole internal memory:

./pk2cmd /PPIC24FJ256GB106 -E


Why erasing is important?

Right now your BP4 contains old bootloader + old firmware, so if you only load the newer firmware it will be placed on top of the older firmware. Depending on the new and old firmware's addressing:
[*] some "old code" will be overwritten by "new code"
[*] some "new code" will be placed to the locations where there were no "old code" before
[*] some "old code" could remain in places that were not overwritten by "new code"
In a case of weird firmware failure, if the program counter jumps out of the "new code" areas, it is better if it meets a bunch of FFFFFFFF's instead of some instructions from "old code" remains that it will try to process. That is why I think it is good idea to start clean and erase everything first!


Now you have 3 possible ways of upgrading:

flash only a bootloader

./pk2cmd /PPIC24FJ256GB106 -M -F/YOURPATH/Bus_Pirate/package_latest/BPv4/bootloader/BOOTLOADER.hex

Then you install a firmware in a standard way: pirate-loader.

However, this method is really time consuming: you need to re-connect stuff, short PGC-PGD pins with a small piece of metal wire (or a jumper if you soldered the ICSP pins), and possibly encounter the errors! With external programmer you do it with just 1 try and always successfully!

flash both bootloader and firmware (separate .hex files)

./pk2cmd /PPIC24FJ256GB106 -M -F/YOURPATH/Bus_Pirate/package_latest/BPv4/bootloader/BOOTLOADER.hex
./pk2cmd /PPIC24FJ256GB106 -M -F/YOURPATH/Bus_Pirate/package_latest/BPv4/firmware/FIRMWARE.hex


Note: FIRMWARE.hex will not overwrite a BOOTLOADER.hex. To understand why, please look at the description of .hex file which is Microchip INHX32 format:

.hex file contains not just data but also the addresses where to load this data, plus checksums and other stuff. The memory maps of the sections inside BOOTLOADER.hex and FIRMWARE.hex are not overlapping, so they will not overwrite each other while flashing, and actually it is not important in which order you would be flashing them


flash both bootloader and firmware (combined .hex file)

You can always make a full backup of PPIC24FJ256GB106 internal memory:

./pk2cmd /PPIC24FJ256GB106 -GF/YOURPATH/Bus_Pirate/package_latest/BPv4/combined/COMBINED.hex


If this backup contains both bootloader and firmware, you could call it a "combined" hex file. It is convenient to install a bootloader and firmware of the same optimization, and then instantly make a complete backup of PIC24FJ256GB106 internal program memory (it is 256 KB, but because of hex file structure these 256 KB turn into 963,2 KB). I have made my own combined builds in this way.

Although such builds could take more time to install, they are convenient to use because just 1 command instead of 2. Example of how to restore it:

./pk2cmd /PPIC24FJ256GB106 -M -F/YOURPATH/Bus_Pirate/package_latest/BPv4/combined/COMBINED.hex


Test your Bus Pirate

Instructions for Linux:

install minicom terminal app, then connect to your Bus Pirate and run a self test:

sudo minicom -D /dev/ttyACM0 -b 115200 -8 -o
HiZ> ~