You are here
What is a bootloader ? Why do I need it ?
A bootloader is a PIC program used to upload code directly to the PIC memory, without having to deal with a PIC programmer.
A bootloader lives in microcontroller's memory. When powered, it is launched and it tries to contact a host, typically a PC host, and gets some code. This code is then recorded to PIC program memory. The way bootloader reaches host is typically through a serial link or USB.
- PIC must be self-writable (self-programming): it must be able to write some code into its own memory while running
- PIC must handle serial or USB communications
- there should be enough memory to write user program
When using a bootloader, there's actually two programs within your PIC: the bootloader itself and user's program. So, how does the PIC knows which to run ? Usually, bootloader lives near the end of PIC memory. User program, when compiled, adds a special instruction which tells the PIC to jump to bootloader program. This instruction is executed at the very beginning1. When bootloader is launched, it waits for some instructions coming from PC host. This is where user's program is sent, bootloader writing bytes of code at the beginning of memory. If no instructions was received for a given amount of time, it timeouts and jump back to the beginning of PIC memory. User's program is executed.
Why do you need this anyway ? There's no actual need, you can safely skip this part and continue to use PIC programmers. Some even prefer this way, as there are common pitfalls when using bootloaders, pitfalls you may face some day or another... So, does it worth it ?
Using a bootloader can simplify your life. For instance, you may not always have your PIC programmer around, you may not even own one. You may have access to a programmer, at a friend's or your robotic club's. Will you have to go there every time you need to program your PIC ? And possibly loose friends ?... You'd better want to program a bootloader once, then go back home and safely have fun by programming your PIC using your serial port or USB.
In addition, as there's no need for high voltage programming, as when using a PIC programmer, there's no need for yet another power supply. Programming is easier, and usually also faster (programmers can be slow). And because bootloaders use common communication links, you could be able to program your PIC through the air, using for instance a bluetooth-to-serial adapter.
How to choose a bootloader ?
- Tiny Bootloader: this is a famous one, probably because it claims to be the smallest (less than 100 words, or 200 bytes), leaving plenty of space for user's program. Tinybld uses a serial link to communicate with PC host, you'll thus need to have a proper serial module connected to Jaluino. One interesting feature is it can reset PIC using RTS line. You don't even have to press reset button to get your PIC programmed, making programming cycles really fast.
- PDFUSB bootloader: coming from Microchip PICDEM FS USB board, this one uses USB connection. No need for a serial module. It is also much bigger, closed to 2KB. While this sounds really heavy, remember you'll still have 30KB memory remaining. This is usually enough... This bootloader has been implemented by Albert Faber, using jalv2 and jallib. This is a 100% Jal bootloader! When launched, bootloader identifies itself to PC host, using USB protocol
Which one to choose ? There's no definite answer, you may need to switch between both, depending on your project. The only restriction is about serial: if you don't have any serial module, you'll clearly have to use PDFUSB...
The following matrix sums up main features for both bootloaders, this may help you make your choice.
|Size||100 words, or 200 bytes||~2000 bytes|
|Timeout||1 second||~10 seconds|
|Serial adapter needed||Yes||No|
|Can reset PIC||Yes, via RTS||No|
Tinybld installation and configuration
In order to get a fully functional Tiny bootloader, you'll need to act on several parts of Jaluino environment.
First, you need to program the bootloader itself. Follow instruction in previous Blink a LED section, and upload Tinybootloader HEX file bootloaders/tinybl/18f4550/tinybld18f4550_20mhz_external_48mhz_pll_115k.hex available in downloaded archive, or from Tiny booloader files SVN repository.
If you want to use Reset via RTS feature, you also need to put a jumper on J7, on Jaluino mainboard. This jumper must be put on the two most left connectors. Please refer Jaluino User Guide, section Hardware Setup. If you don't use this (leave J7 without any jumper, or jumper on the right connectors to activate RTS line), you'll need to push Reset button each you upload a program.
Finally, Jaluino IDE needs to be configured. Because serial link will be used to upload program to the microcontroller, you need to specify serial port and baudrate. Please refer to Jaluino IDE configuration and serial setup. If you didn't change any parameter in bootloader, you'll use 115200 bauds as speed. Serial port should be setup accordingly to your system.
Compilation and Upload default commands should also be set. You'll avoid common pitfalls, like compiling with wrong parameters. Open Jaluino IDE settings, and click on "General" tab. This is where you associate commands with file types. Select "Jalv2" file type, and choose "Jaluino with Tiny/18F" command as default. Just close the window in order your new settings to be saved.
Proceed the same about produced HEX files, uploaded to Jaluino. Select "Hex" file type, and choose "Tiny bootloader"
Except if you forced and specified other options in IDE, this setup will be used by default.
Let's try this by uploading our "Blink a LED" sample with Tinybld. Select appropriate sample following guidelines detailed in previous section. Make sure "Jaluino with Tiny/18F" is your compilation command, and click on "Compile". Jaluino IDE has generated appropriate command line, generated HEX file is ready to be uploaded.
Sample is ready to be uploaded, make sure "Tiny Bootloader" is selected in upload commands, and press "Upload" button. If Reset-via-RTS feature has been enabled, Jaluino will automatically reset and Tinybld2 will send program. If not, just press and release Reset button.
Once uploaded, your program is immediatly executed, you should see your LED blinking.
PDFUSB installation and configuration
Closed to what done with Tiny bootloader, using PDFUSB requires you to act both on hardware and software sides.
First program the bootloader itself. HEX file can be found in archive, as bootloaders/pdfusb/18f4550_usb_bootloader_autostart.hex. You can also retrieve it from Jaluino SVN repository. Follow instructions detailed in Blink a LED section, in order to upload this file using a programmer.
Once done, you should configure default commands in Jaluino IDE, both for compilation and upload steps. Open Jaluino settings window, and click on "General" tab. Select "Jalv2" file type, and choose "Jaluino with PDFUSB" as default command.
Proceed the same with default upload command. Select "Hex" file type, and choose "PDFUSB bootloader" as default command. Close window to save new settings.
PDFUSB is now configured. As it doesn't use a serial link, there's no for serial configuration. Remember PDFUSB can't reset a PIC running user's program: in order to upload a HEX file, you'll need to push Reset button.
Now, let's blink again our LED, this time using PDFUSB bootloader. Refer to Blink a LED section and open sample. Ensure "Jaluino with PDFUSB" is selected as compilation command, and click on "Compile" button. Notice the generated command line has changed and includes appropriate parameters.
"PDFUSB Bootloader" should be selected in upload commands. Click on "Upload" button. Jaluino needs to reset to activate bootloader, so press Reset push button on board. Wait for a while, you should get PDFUSB output, claiming it has flashed your program. If you get an error, try again by pushing and maintaining Reset button, then click on "Upload", then release Reset2.
Using -x option, PDFUSB automatically resets and run user's program. You should see your LED blinking as soon as program was flashed. Note the next time Jaluino is turned on, you'll need to wait PDFUSB timeout (approx. 10 seconds) before your program is executed.
- Registers and fuses with Tinybld: when writing a program, you usually specify clock speed and type (internal, crystal, resonator...) and other parameters like selecting which pin carry PWM signals, watchdog, etc3.... These should have impacts on PIC registers, but when using a bootloader, these registers won't get programmed as you may think, as bootloader is the one which first has defined these. What you specified in your code isn't actually what was programmed ! For instance, Tinybld by default has been configure to run @48MHz. If you use another speed within your code, it won't be considered4.
- Registers and fuses with PDFUSB: programs aimed to be used with this bootloader must be compiled with no-fuse option. Should you miss this option, registers will get overwritten and will break bootloader... You'll need to program it again.
- Compilation options: beside -no-fuse for PDFUSB, you also need to specify how "far" it should jump. -loader18 4 with Tinybld, -loader18 2048 with PDFUSB. Don't forget it ! Because if you do, bootloader will never get executed first anymore. You'll have to program it again.
- Serial baudrate: when using Tinybld coming with Jaluino, bootloader expects to communicate through a serial link @115200 bds. This is bootloader's speed, not user program's speed. Bootloader's and user program's speeds are independent, you can choose the one you need on your own program5.
This sounds scary, isn't ? Luckily, using Jaluino IDE default compilation commands, "Jaluino with Tiny/18F" and "Jaluino with PDFUSB", will ensure you're using correct options.
And, at the end, breaking a bootloader isn't that bad, you'll have a good reason to visit your friends !