Difference between revisions of "USB bootloading"

From Mech
Jump to navigationJump to search
(by Greg McGlynn)
m (by Greg McGlynn)
Line 9: Line 9:
* 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.
* 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!
* You reset the PIC, and your program starts running!



== USB Bootloading with the PIC 18f4553 ==
== USB Bootloading with the PIC 18f4553 ==

Revision as of 17:45, 20 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: 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 (some location).

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 (some location) 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 do 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

So you've compiled your program and want to load it on to your PIC. Fire up (some location).exe. Put your PIC into bootloading mode. You should now be able to select (some string) from the (some string).exe drop-down list. Now click "Load Hex File" and go find the hex file that you created when you compiled your program. Then click "Program Device" to load your program. Reset your PIC and your program should run.

See also

USB communication with PC