USB bootloading

From Mech
Jump to: navigation, search

This article assumes you are using Windows. The provided code is intended to be run on a PIC18F4550 or 4553 with a 20Mhz oscillator.

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 is bootloading over a USB cable. To do this you need to be working with a PIC that talks USB, like a PIC 18f4550. 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.

Note: if you're going to load programs onto a PIC with a bootloader, you need to put some special instructions in the PIC code to make it work. See the "Compiling for a bootloader" section.


CDC Bootloader

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 , where you can find the source it was built from, if you're interested. The bootloader is a slightly modified version of one supplied by Microchip; the changes allow the compiled hex file to fit within the first 0x800 bytes program memory. 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. These pins can be easily changed in the bootloader source code.

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.


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.

HID Bootloader

A HID (Human Interface Device) bootloader has the advantage that it doesn't require any driver installation on the programming computer. Once the bootloader is programmed onto the PIC and the PIC is plugged into the PC, your computer should be able to handle the rest of the setup automatically. A HID bootloader is supplied with the Microchip USB Framework. This is a slightly modified version of the original HID bootloader project from Microchip. The original project is designed to fit within the first 0x1000 bytes of program memory, but due to changes in the compiler will no longer compile into that space; the modified version goes int he first 0x1100 bytes.

PIC software

You can download the PIC hex file here: Media:HID_Bootloader_PIC18_Non_J.hex.

You can download the full MPLAB project for the PIC program here:

PC software

You can download the PC program here (it comes as part of the Microchip USB Framework): Unzip the downloaded .zip file to get the HIDBootLoader.exe executable. Running this requires a sufficiently recent version of the .NET Framework, which you may not have if you are running XP or earlier versions of Windows. You can download a sufficiently recent version here (the install takes a while): .NET Framework download.

When the program is running it should automatically detect when you attach the PIC (if the PIC has been properly programmed). To program the PIC click "Open Hex File," and browse your compiled hex file. Then click "Program/Verify" and the bootloader should, after a few seconds, report that it has successfully erase, programmed, and verified the PIC. After that you can reset the PIC and it should run your program.

Required circuitry

Like the CDC bootloader, you must have a button wired up to indicate when the PIC should start up in bootloader mode. This bootloader looks at pin RC4; if RC4 is low on reset, the bootloader starts, otherwise the user program starts. The bootloader will use pins D0 and D1 to indicate connection state: leds hooked up to these pins will flash alternately when the connection has been established with the PC. Again, these pins can easily be changed in the bootloader source code.

Note that the first time you plug the PIC into the PC and start it up in bootloader mode, it will take a few seconds for the PC to initialize everything before the lights start flashing, but after that the process should be pretty fast.

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 (rather than overwriting it). The CDC bootloader takes up the memory from 0x000 to 0x7FF (the first two kilobytes of program memory), while the HID bootloader goes from 0x0000 to 0x10FF (these are hexadecimal numbers). Your program therefore needs to start at address 0x800 or 0x1100, depending on which bootloader you're using, and not touch the memory from 0x000 to 0x7FF.

With CCS you can accomplish this by adding the following two lines at the top of your code:

CDC bootloader with CCS:

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

HID bootloader with CCS:

#build (reset=0x1100, interrupt=0x1108)  //code starts right after the bootloader
#org 0, 0x10FF {}                       //don't overwrite the bootloader

MPLAB/C18: With MPLAB/C18 the required code is a bit longer. So as not to clutter up this page, it's on a separate one: see Compiling for a bootloader in MPLAB.

See also

USB communication with PC

Wireless PIC bootloading

Personal tools