Tiny PIC bootloader
Tiny Bootloader
I consider firmware programs should be as small and as fast as possible. In the bootloader case all the hard work can be moved to PC software, allowing a wider range of options and easier upgrades to the software, while the firmware can implement only the basic functions.
Features of the firmware
- Size of only 100 words; (both versions, for 16F and for 18F occupy less than 100 words);
- Can write flash, eeprom and configuration bytes(18F);
- On reset, waits 1 second for a message from the PC, if not received, launch user application;
- Developed initially for: 16F876A, 16F877A, 18F252; should work (with modifications) on most 16F and 18F PICs;
- Reported to work with 16F873, 16F873A, 16F876, 16F877, 18F452, 18F2620, 18F8720, 18F8680, 18F4320, 18F1x20, 16F88; 18F2620;
- The .asm file can be easily modified and adapted for any frequency (or baudrate);
Features of the PC software
- Can upload programs into flash (in current version eeprom and cfg bytes can be modified only from command line);
- Works with both PIC 16F and 18F types; automatically detects PIC type, model, HEXcontent;
- Remembers last settings;
- In case of errors, performs retransmissions or tries to resynchronize with pic;
- The communication settings are editable so you can write any COM number or desired baud;
- If a filename is specified as a command line parameter, it will automatically try to write it;
- Several features can be activated from "Options", others by writing the command in the log window. Start typing "help" or "?" on an empty line:
? help ontop keep the window on top of the others logdetails lists detailed stuff happening inside (used only for debugging) clearallflash clears all flash except the bootloader (actually it fills all flash with FFh) writeeeprom <addr> <dat> writes the byte <dat> at the address <addr> (numbers can be either dec or hex)
eg: writeeeprom 0 b6h - writes B6h in the first eeprom locationwriteconfig <adrLow> <dat> only for 18F; writes the configuration byte <dat> at the address 300000h+<addrLow>
eg: writeconfig 3 FFh - activates WDT
What do you need
- MPASM installed
- Know how to use MPASM IDE (or Mpasmwin.exe) to modify and assemble an asm file.
- You must have the PIC (pins TX,RX) connected to the serial port of the PC (pins Rx,Tx), usually using a MAX232 level converter:
How to use
- The program must meet this requirement: it must have in the first 4words of memory a GOTO to the start of the main program. (this is what the majority of the bootloaders require). So in assembler it should look something like this:
PIC16F PIC16F PIC18F org 0
;clrf STATUS
clrf PCLATH
goto Mainorg 0
;clrf STATUS
pagesel Main
goto Mainorg 0
goto Main
- If you use a compiler, search the help for a directive that allows coexistence with a bootloader;
- Modify "xtal", "baud", "_??_OSC" to what you need; assemble it (for example with .\MPLAB\MCHIP_Tools\Mpasmwin.exe); write the HEX it with a PIC programmer; put the PIC on you board and connect it to the PC serial port.
- You can keep the TinyBootloader interface opened, browse or enter the name of the hex file, select the COM port and baud rate, then click WriteFlash, or double-click the tray icon, and after that reset (or power-up) the PIC.
- If all things are set up properly the program will be loaded into the pic and executed.
- When you press "WriteFlash" the application will read and analyze the hex file to determine if it's a 16F or 18F code, and to determine the size, eeprom and config data.
- Then it will ping the serial port until there is a response from the PIC or until the timeout will expire.
- On the other side, the PIC (after reset), it will wait around 1s to receive something from the PC. This timeout value can be adjusted from "movlw xtal/2000000+1", but decreasing this timeout too much will cause later communication to fail.
- A warning will be issued if: a goto is not found in the first 4 instructions or if PIC and HEX files do not match.
All critics and suggestions are welcome at:
General info about Bootloaders
A bootloader is a program that stays in the microcontroller and communicates with the PC (usually through the serial interface). The bootlader receives a user program from the PC and writes it in the flash memory, then launches this program in execution. Bootloaders can only be used with those microcontrollers that can write their eeprom through software. The bootloader itself must be written into the flash memory with an external burner. In order for the bootloader to be launched after each reset, a "goto bootloader" instruction must exist somewhere in the first 4 instructions; it is the job of the bootloader to reallocate the first 4 instructions of the user program to another location and execute them when the bootloader exits.
(Some of the) Available bootloaders (as reported by Google) in May, 2003:
Bootloader Name / Author | Supported models | Size(words) | Comments |
---|---|---|---|
From Microchip | 16F,18F | 1000 | uses Hyperterminal to upload hex files |
From MicrochipC | 16F,16F*A,18F | 256/2000 | +ok |
WLoader Wouter van Ooijen | 16f877 | 1000 | +does not use the UART, +the serial interface use only one I/O pin |
ZPL Wouter van Ooijen | 18F | 384 | +unusual method using mclr: uses zero I/O pins ! |
KarlLunt | 16f87x | 512 | -activation on input pin -derived from Microchip boot877.asm (uses Hyperterminal) |
PICLOADER Rick Farmer | PIC16F87x | 2000 | -program must start at 0x3; +password (uses Hyperterminal) |
bootload | PIC16F877 | 800 | -written in C -command line DOS program |
theByteFactory | 16F877 | 1000 | -written in C (uses Hyperterminal) |
Jolt Martin Dubuc | 18F | 256 | -user code and interrupt vectors need to be relocated; Java GUI, +auto detect baud |
? HI-TECH Software | 16F87x | 256 | -written in C |
PIC downloader Petr Kolomaznik | 16F876 | 256 | -is rewritten and modified from HI-TECH +Windows interface |
Ivar Johnsrud | 18Fxx2/18Fxx8 | 360 | -bootloader based on HiTech's -downloader based on Petr Kolomaznik's |
B Bootloader | PIC16F87x, PIC16F87xA | 340 | -called only by user application -written in C +Linux uploader |
SGupta | 16f876 | 256 | |
I put mine here, for comparison: | |||
Tiny | 16F876A, 18F252, ... | 100 | +details above |
I did it mainly because:
1. Some bootloaders I used previously had some unpleasant bugs or didn't support the devices I had.
2. I wanted to do it small. Actually it can be done even smaller :) but I like the size of 100;