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:
- has not standart starting vector address
- has not standart interrupt vector adress
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:
- What is a boot loader, and how would I develop one? It is really brief and exhausting forum reply you have to see.
- how such a bootloader works in principle detaily described in Microchip's article AN851 AppNote for a FLASH Bootloader for PIC16 and PIC18 Devices and AN1388 AppNote for PIC32 Bootloader.
2. PIC bootloader types
There are several bootloader types available on internet and not just for USB. You could find:
- USB bootloader (uses PIC's USB interface to get program into PIC)
- COM/serial bootloader (uses PIC's USART interface)
- some special types (like SD card bootloader, TCP bootloader)
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:
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:
- use relocated entry points (RESET and interrupt),
- not use RB4 pin (initializes USB HID bootloader),
- not use D0 pin on PIC18F4550 or PIC18F4455 (LED signalizing USB HID bootloader activity).
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:
- MPLAB X IDE v1.95 for Windows released on 30.10.2013
- MPLAB C for PIC18 v3.46 in LITE mode released on 3.5.2013
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:
- part
\microchip_solutions_v2013-06-15\USB\Device - Bootloaders\HID\Firmware - PIC18 Non-J\of Microchip Libraries for Applications version 2013-06-15 Windows,
here is USB HID bootloader HEX fileUSB Device - HID - HID Bootloader - C18 - PIC18F4550.hexyou have to upload to PIC18F2550 using COM / serial port programmer or LPT / parallel port programmer - user applications template working/compatible with Microchip's USB HID bootloader including compiled
PIC18F2550_Application_template_RKA.hexfile generated by MPLAB C for PIC18 v3.46 in LITE mode, that you can upload into PIC using USB programmer
If you found these information useful, please consider to:
in USD
(Article created 19.12.2013)