PIC Microcontroller Projects

USB Bootloader

Introduction

In this article I will show how to use and/or modify Microchip's MCHPUSB
Bootloader Firmware in your own projects.
The bootloader can be used for the all USB PIC devices (PIC18F4550, PIC18F4455,
PIC18F2550, PIC18F2455, PIC18F4553, PIC18F4458, PIC18F2553, PIC18F2458).

The information provided here is based on Microchip-USB-Framework version 2.7.

The bootloader firmware (incl. source code) can be found in the installation
directory in the following subdirectory:
USB Device - Bootloaders\Vendor Class - MCHPUSB Bootloader\Bootloader -
Firmware for PIC18F4550 Family Devices.
This bootloader was designed to be used with the PICDEM FS USB DEMONSTRATION
BOARD from Microchip (PIC18F4550).

The bootloader starts after Power-On or
Reset. The bootloader checks if pin RB4 is low or high:

RB4 = low (0V): PIC starts in bootloader mode

RB4 = high (5V): PIC starts user applicatiob

The current status of the bootloader is displayed via 4 LEDs which are connected
to RD0-RD3.

How to compile the USB-Bootloader from Microchip

Since the bootloader is designed for the Microchip demo board, the configuration
of the bootloader possibly will not fit for your project. E.g. you want to use another
pin for bootloader mode entry or you do not want to spend 4 pins/LED's for
displaying the current status.

Fortunately, the source code of the bootloader firmware is available. It is
written in C language. It can be compiled with Microchip's C18-Compiler, even
with the free version of the compiler.

In the directory USB Device - Bootloaders\Vendor Class - MCHPUSB Bootloader\Bootloader -
Firmware for PIC18F4550 Family Devices you find the project file
MCHPUSB.mcp which has to be opened with MPLAB. In the project file the
path settings point to the default C18 compiler directory c:\mcc18. If you have
installed the compiler in a different directroy, you have to modify the path
settings in MPLAB.
To do this, select "Project - Build options - Project" from the MPLAB menu after
opening the project file MCHPUSB.mcp. In this dialog go into the "Directories"
tab and change the modify the

Include Search Path

Library Search Path

Linker-Script Search Path

according to your C18 installation path.

Now you can build the project via "Project - Build All".
If you use the free version of the C18 compiler, the linker will fail since the
generated code exceeds the boot block size.

To fit the bootloader into the bootblock, we can stirp unnecessary functions
from the source code, like code for showing the status of the bootloader via
4 different LEDs. Here the code parts which can be removed without loosing
bootloader functionality:

Now again try to build the project via "Project - Build All" as described earlier.

Basic USB Bootloader Circuit

Here the basic USB bootloader circuit. JP1 controls the bootloader entry. If JP1
is closed, the PIC will start in bootloader mode. Please note that the status of
JP1 is only checked after the PIC has been reset. I.e. to enter the bootloader
mode, first JP1 has to be closed. After that the PIC has to be reset.

Flash the USB-Bootloader into the PIC

If all the above steps were successful, you now have your bootloader HEX file
MCHPUSB.hex which can be flashed into the PIC. Make sure that the config bits
are set properly and match your hardware, e.g. PLLDIV fits to your oscillator
frequency.

Now, just load the HEX file into PICPgm and flash it.

USB driver installation for the bootloader

Now Windows should detect a new USB device. If it askes for the driver, choose
the driver location manually and navigate to the driver directory which is located
in the Microchip's Applications Library installation directory in the subdirectory
USB Tools\MCHPUSB Custom Driver\MCHPUSB Driver\Release.

Bootloader PC Software - PDFSUSB.exe

The bootloader PC software PDFSUSB.exe is located in the directory
USB Tools\Pdfsusb. The tool itself is selfexplaining.

Unfortunately, I had some problems with this tool when trying to load HEX files
generated with the CCS compiler. The start address of the firmware shown in
PDFSUSB.exe was some ugly address like 0x2C44340 instead of 0x800 which is the
firmware start address.

After some investigations I found that PDFSUSB.exe has a problem
if the following line in the first line in the HEX file is missing (initialization
of Upper Linear Base Address):

:020000040000FA

I.e. to solve the problem insert :020000040000FA before the first line in
the HEX file.

Bootloader PC Software - USB Bootloader

I have also put a copy of the application onto my webspace, so if the above link
is not working, you can get the bootloader PC Software
here.

Application Software for usage of the Bootloader

Programs which shall be flashed with the bootloader need to be adapted since
the bootloader uses the memory area 0x000 to 0x7FF. This area is usually used
by the application software itself. So the following changes are required:

reserve boot block area (0x000-0x7FF)

map reset vector from 0x000 to 0x800

map interrupt vector from 0x008/0x018 to 0x808/0x818

How this can be done is depending on the compiler which is used for generation
of the HEX file.

I have written a LED blinking demo application which can be loaded into the PIC
with the bootloader. You can download it here:
18f4550_app4bootloader_mplab.zip.

CCS compiler

For the CCS compiler it is a little bit simpler:

...
/* ------------------------------------------------------------------------- *//* map reset vector and interrupt vector *//* 0x000-0x7FF is used by the bootloader. The bootloader maps the original *//* reset vector (0x000) to 0x800 and the interrupt vector (0x008) to 0x808. *//* ------------------------------------------------------------------------- */
#build (reset=0x800, interrupt=0x808)
/* ------------------------------------------------------------------------- *//* reserve boot block area *//* This memory range is used by the bootloader, so the application must not *//* use this area. *//* ------------------------------------------------------------------------- */
#org 0, 0x7FF {}
...

HI-TECH C Compiler

For the HI-TECH C compiler the reset vector can be changed via the compiler option
--CODEOFFSET. For the MCHPUSB bootloader we have to set the option to
--CODEOFFSET=0x0800
If you use the HI-TIDE IDE, the option can be specified in the project properties
(Project => Properties => C/C++ Build => Settings => Advanced => Command Line Options):