Difference between revisions of "USB bootloading"

From Mech
Jump to navigationJump to search
(by Greg McGlynn)
m (by Greg McGlynn)
Line 38: Line 38:
* Compile your program with the two lines mentioned above.
* Compile your program with the two lines mentioned above.
* Fire up PDFSUSB.exe.
* Fire up PDFSUSB.exe.
* Put your PIC into bootloading mode. You should now be able to select "PIC DEM FS 0" from the PDFSUSB.exe drop-down list.
* Put your PIC into bootloading mode. You should now be able to select "PICDEM FS USB 0" from the PDFSUSB.exe drop-down list.
* Click "Load Hex File" and go find the hex file that you created when you compiled your program.
* Click "Load Hex File" and go find the hex file that you created when you compiled your program.
* Click "Program Device" to load your program.
* Click "Program Device" to load your program.

Revision as of 10:59, 21 April 2009

This article assumes you are using Windows.

Bootloading allows you to reprogram your PIC without the need for an expensive hardware programmer like an ICD.

Bootloading works like this:

  • You compile your program on the computer, which generates a file with a .hex extension. This is the compiled code for your program, which you want to get onto the PIC.
  • You connect the PIC to the computer, in this case with a simple USB cable.
  • You fire up a PC program that takes your hex file and sends it to the PIC.
  • Sitting on the PIC there is a small program called a bootloader. It takes the hex file that is coming from the computer and writes it into memory.
  • You reset the PIC, and your program starts running!


USB Bootloading with the PIC 18f4553

USB bootloading is bootloading over a USB cable. To do this you need to be working with a PIC that talks USB, like an 18f4553. Then you need to have a bootloader on the PIC. The only way to put a bootloader in place is with a hardware programmer like an ICD, but you only need to do this once: afterwards, all you'll need is a regular USB cable. See here for how to connect a USB cable to a PIC.

Loading the bootloader

Here is a USB bootloader hex file that works for the 18f4553 with a 20Mhz oscillator: File:FW B 20.hex. It comes from http://www.schmalzhaus.com/UBW , where you can find the source it was built from, if you're interested. To load this precompiled hex file onto the PIC using the CCS ICD, use the icd.exe program in the C:\Program Files\PICC directory. Now the bootloader is itself loaded.

The PC software

There needs to be a program on the PC that sends your hex files to bootloader on the PIC. This bootloader uses a program from the Microchip USB Framework. You can download the framework installer here. It will create some folders in the C:\ directory. The loader program is at C:\MCHPFSUSB\Pc\Pdfsusb\PDFSUSB.exe.

Required circuitry

You need some sort of button or switch hooked up to pin RC2 so that it is normally high. If you reset the PIC and pin RC2 is high, the bootloader will run whatever program you've loaded onto it. But if RC2 is low, because you're pressing the button, the bootloader will run, allowing you to load a new program onto the PIC. You don't need to keep holding the button down, C2 just has to be low at the time the PIC resets. So whenever you want to put the PIC into bootloader mode, press and hold the reset button, then press your bootloading button, then release the reset button, then release the bootloading button. You can attach an LED to pin RC1 and the bootloader will flash it when it runs.

Installing Drivers

When you first connect the PIC to the PC and make the bootloader run, Windows will need to install a driver. Point it to "C:\MCHPFSUSB\Pc\MCHPUSB Driver\Release" and it will install the right driver. Now you're all set up.

Compiling for a bootloader

A bootloader takes up part of the available program memory, and you need to take this into account when you write your program. Generally, the bootloader takes up a small chunk of memory at the beginning of space available for program memory. You need to tell the compiler to place your code just after the bootloader. Our bootloader takes up the memory from 0x000 to 0x7FF (the first two kilobytes of program memory). Your program therefore needs to start at address 0x800 and not touch the memory from 0x000 to 0x7FF. You can accomplish this by adding the following two lines at the top of your code:

#build (reset=0x800, interrupt=0x808)  //code starts right after the bootloader
#org 0, 0x7FF {}                       //don't overwrite the bootloader

Bootloading

Now that everything's set up, here are the steps to load your program onto your PIC:

  • Compile your program with the two lines mentioned above.
  • Fire up PDFSUSB.exe.
  • Put your PIC into bootloading mode. You should now be able to select "PICDEM FS USB 0" from the PDFSUSB.exe drop-down list.
  • Click "Load Hex File" and go find the hex file that you created when you compiled your program.
  • Click "Program Device" to load your program.
  • Reset your PIC and your program should run.

See also

USB communication with PC

Wireless PIC bootloading