PIC USB HID bootloader (from Microchip)

1 PIC bootloader principles

Just briefly what it does. PIC's code FLASH memory is organizationally splitted into 2 areas. First one is permanently used by bootloader itself - it is code which cooperates with programming software to get your user code into PIC. Second part (rest of code FLASH memory) is available for your code. That means, smaller the bootloader is, bigger your code might be.

Every bootloader redirects vectors (RESET and interrupt) to itself. Than (if bootloader is not active) they are redirected by bootloader to your code. This specialty means your's user code:

And that is the reason, why you could not use any HEX code available on internet with bootloader - because it will not work.

If you would like to know more, see:

 

2 PIC bootloader types

There are several bootloader types available on internet and not just for USB. You could find:

If you are interested more on these ones, you can continue reading on Microchip's forum topic Free Bootloader for PIC.

From my point of view it is good for first experiments to start with something already tested, proven that works, and ideally available from manufacturer.

 

3 PIC USB HID bootloader

All criterias mentioned in sentence above are met by USB bootloader from Microchip available at Microchip Libraries for Applications. I have used version 2013-06-15 Windows. All source code references in this section are to sources located in \microchip_solutions_v2013-06-15\USB\Device - Bootloaders\HID\Firmware - PIC18 Non-J\ of these libraries.

All necesarry you will find summarized in one archive at the bottom of page in Downloads section.

As described in principles, these are bootloader redirections:

(main.c)
//Constants
#define REMAPPED_APPLICATION_RESET_VECTOR 0x1000
#define REMAPPED_APPLICATION_HIGH_ISR_VECTOR 0x1008
#define REMAPPED_APPLICATION_LOW_ISR_VECTOR 0x1018

If you would like, you can activate bootloader programatically from your user application, jush jump to adress:

(main.c)
#define BOOTLOADER_ABSOLUTE_ENTRY_ADDRESS 0x001C

Here you can see how code FLASH memory is organized:

USB HID bootloader code FLASH map
Figure 1: USB HID bootloader code FLASH map

Red wire shows how your user program can hand control over bootloader.
Green wire shows how your user program gets control since PIC is powered on (when RB4 is connected to VCC/+5V).

To activate bootloader by hardware you have to connect RB4 to GND:

(io_cfg.h)
#elif defined(PIC18F4550_PICDEM_FS_USB) //Based on PIC18F4550, for PIC18F45K50 based board, see farther below
     ...
/** S W I T C H *****************************************************/
#define mInitAllSwitches()   {mInitSwitch2();}
#define mInitSwitch2()        {ADCON1 = 0x0F;}
#define sw2                        PORTBbits.RB4
#define mDeInitSwitch2()    {ADCON1 = 0x07;}

In case you have PIC18F4550 or PIC18F4455, which has also gate D, you can see bootloader activity indicated by LED on pin D0:

(io_cfg.h)
/** L E D ***********************************************************/
#define mInitAllLEDs()        LATD &= 0xFE; TRISD &= 0xFE;
#define mLED_1                 LATDbits.LATD0
#define mLED_1_On()        mLED_1 = 1;
#define mLED_1_Off()        mLED_1 = 0;
#define mLED_1_Toggle()   mLED_1 = !mLED_1;

But nothing happens on PIC18F2550 or PIC18F2455, because these do not have gate D.

 

4 PIC user app code template

In your user application you have to reflect all requirements emerged by USB HID bootloader. In case of Microchip's bootloader you have to:

I have tried several available compilers available for PIC but only one created working binary compatible with bootloader. Seems compilers (like HI-TECH C for PIC18, MPLAB XC8) do have problem with entry points rellocation except MPLAB C Compiler for PIC18 MCUs.
With MPLAB® X Integrated Development Environment (IDE) it is very easy to use.
I have been successful with USB bootloader compatible application generated using:

There is very useful article with detailed decripton what and how has to be modified to get application working with USB Bootloader for PIC18F2550. There is complete application template attached, too, which I have used to get my first working application uploadable by USB HID bootloader.
There has to be only changed parts that are relevat to Microchip's USB HID bootloader relocation addresses. This tempate uses 0x1100 base address, we need 0x1000 base address.

Modified template with 0x1000 base address you can find for download below. You have just to import it to your MPLAB IDE and modify code as you need. This template just blinks LED on port RB6 and RB7, for details see my LED blinker article.

5 Downloads

Archive USB HID bootloader and application template package.zip contains:



If you found these information useful, please consider to:

This will allow me to bring more information and projects for you.