Bootloader for PIC18F4550

Bootloading is a way of burning code into the Microcontroller without removing it from the application circuitry. This is achieved by running a program called Bootloader whenever it is necessary. The bootloader is like an OS which starts by enabling a port pin during reset. PIC18F4550 is a Microcontroller from Microchip with onboard USB module. In this tutorial, we are discussing how to use the bootloader for PIC18f4550.

What is Bootloader

The bootloader is a program which helps to program the microcontroller device without using an external programmer. It is possible to burn the hex code even if the PC doesn’t have a serial port or any other tools.

Required tools for Bootloader are

  1. USB cable to connect board to the PC
  2. A microcontroller with already flashed bootloader program

Download Bootloader, tools, and sample program from code libary section Or browse it online on GitHub.

Install Bootloader for PIC18F4550

For installing bootloader program, we need an external programmer. This is a one time process which doesn’t need to repeat. Use any programmer like PicKit3 or serial programmer or ICD3 to flash the provided bootloader program “MCHPUSB Bootloader PIC18F4550 Family.hex” which is available in the provided bootloader folder.

Install Bootloader driver

In order to install Bootloader driver, we need to enter into the Boot mode. Keep pressing the reset button while connecting the USB cable to the port. This will enter to the boot mode and you can see below status in Taskbar.        task bar - new device found 

The driver installation will fail because Windows doesn’t have the driver files by default. So we need to point to the required files to install it manually.

To install, take Device Manager (from Control panel). There you can see a section Other Devices. The Unknown Device which is listed under is our required device.

device manager- device not recognized

Right click on the Unknown Device and select Update Driver Software.

Device manager - install the device driver

From the window, select Browse my computer for driver software.

Device manager - install it from the directory

Use the browse button to point to the bootloader\MCHPUSB Driver\Release. Press Next. Windows may warn you that the driver is not from verified source. Select Install it anyway.

device manager4

Next window will show the driver installing status. After a while, Windows would complete the installation.

Device manager - Installed

Now you can examine the Device manager screen, under Custom USB Devices, there is our Microchip Custom USB Device.

Device manager - verify installation

Once the Bootloader is installed, start programming your projects.

Writing Program for bootloader loaded microcontroller

Writing code for a bootloader is similar to what we do in normal cases except some minor changes. The main difference comes where the user program starts from the address 0x800. So the reset vector should be at 0x800. In a normal case, reset is in 0x00 and interrupt vectors are in 0x08 and 0x18. So they need to be remapped.

Requirements

  1. Reset vector should be in the address 0x800
  2. Interrupt vectors should be in the address lines of 0x808 and  0x818

Reset vector remapping

You need to add a code offset of 0x800 for the project. In order to do that, take project properties and select xc8 linker. From the Options Categories dropdown menu, select Additional options. Add 800 corresponding to the codeoffset box.

Reset vector remapping in Microchip PIC: Bootloader for PIC18F4550

Interrupt vector remapping

To remap the interrupt vector, add the code in source file just below the header file.

/* --- BEGIN: changes required for bootloader --------- */
#define PROG_START 0x800        //application memory origin
#asm
PSECT intcode
        goto PROG_START+0x8
PSECT intcodelo
        goto PROG_START+0x18
#endasm

interrupt vector mapping

Burning new programs using bootloader

Boot into the bootloader mode by keeping the reset key pressed when connecting the USB cable. Now open the PDFSUSB.exe from the Pdfsusb folder. Select PICDEM FS USB 0(Boot) as a device.

Now Load the hex file using the button. If the loaded code starts from a random memory than 0x800, then you need to add a line to your hex file. Check below picture, the red circled text is  4262E20, which is the starting address.

Wrong starting memory address during burning

Add: 020000040000FA in the beginning of the hex file and reload the file (there is a column sign in front of the address). Now it will start from the 0x800 address. After programming the device, disconnect the USB cable and turn ON the board to start executing your program.