We have been asked about how you can program an application code file and a separate bootloader into an AVR microcontroller using a Kanda programmer. It isn’t possible to program two separate files but it is simple enough to combine them into one file.
The first thing to do is to assemble the bootloader code and see what size the final code is in words. This tells us what boot block size we will need. Most AVR devices have a separate boot block section, and we can set its size, but the choice of sizes varies between different AVR microcontrollers.
The next step is open the datasheet for our particular AVR and search for Boot Size Configuration table. Look for the boot block size needed to fit our bootloader code in, and note
- Setting for BOOTSZ1 BOOTSZ0 Fuses
- Number of pages
- Boot Reset Address or start of Boot section
The next step is to combine our application code and bootloader in the assembler. As the boot section is at the end of the AVR device, we need to put an ORG directive at the end of our application code with the address set to the Boot Reset Address. Depending on the assembler, we may need to convert this address into bytes (x2).
Next, place an Include directive after the ORG and include the bootloader code file. When we assemble it, we get one file with application code and bootloader included at correct address ready for ISP programmer.
The last step is to set the boot block size to the correct setting. This is done with BOOTSZ1:0 fuses in the programmer. Set them to the value we noted down, or to that number of pages, or the required size in words, whichever you find easiest.
Thats it, the bootloader will now be programmed into your AVR.
For more details, look at this file, Guide to Adding Bootloader Code
Bootloader in C Code
The above description is for files written in assembler, so what about C code? The simplest approach is to create your application code and bootloader code as two separate files, and combine the two output hex files.
The application code is compiled as normal, but you do need “Create Hex File” checked in Configuration options. The bootloader needs to be compiled with its start address set to Boot Reset Address in words. This is the same address as the assembler example.
To do this in WinAVR, go to Configuration > Memory Settings tab, and click on Add button. In the small window that appears, set memory type to flash, name to .text and address to Boot Reset Address in words eg 0xE00 or 0xFC00. Now compile it as usual and it will start at correct location.
Now we need to combine the two output hex files. The simplest way to do this is to open your application hex file in notepad or similar, scroll to the end and remove the last line. This will be :00000001FF, which is the End of File Marker. Now paste in your bootloader code at the end of the remaining application code and save it.
Now you can just use a normal programmer with one file, but remember to set Boot Size fuses to correct value.