Bootloader for ATmega163

This page describes how to make a boot-loader for the Atmel ATmega163. The bootloader described here is written in assembly language, because I had no access to a C-compiler for AVR when I wrote it. Unfortunately the assembly language also means that it will most likely need some tweaking to get it working with other Atmel μControllers. In my opinion it is probably not worth the effort, since it is now possible to use a port of the GCC-compiler. On Windows you might want to take a look at WinAVR. I recently discovered that a guy named Martin Thomas wrote a AVR910 compatible bootloader in C. AVR910 is the protocol used by avrprog.exe, and is the one my bootloader (described on this page) uses as well. But Martin's bootloader seems to support a wide range of Atmel ATmega controllers. So I guess this page mostly remains for my own and other peoples reference.

Fixed a bug that meant the bootloader would never use autoincrement. Thanks to Frank, who also reported the bootloader to work with ATmega8, just by changing the device IDs. Cool.

Using this bootloader, you can update the software in your ATmega163-based system without having to use a programming board. You will need the programming board once to burn the bootloader into the boot-section of the μController in the first place. Once the bootloader is in place, you can use it to update the software in the system. The only requirements are that the UART can be connected to a PC, and that the external reset-pin can be activated.

To load the bootloader into the μController in the first place, you should use a programming board. If you use the SMD versions of the controllers, you might need access to the SPI-pins for this initial programming (including setting the fuses), or you will need some reliable connections to the programming pins before mounting the μController.

To get the bootloader working, start AVRstudio and create a new project. Select "Atmel AVR Assembler" as the project type, choose a name for the project, and don't set it to create an initial project file. Now select ATmega163 as the target device, and you are ready to go.

Add the file avrboot.asm to the project by right-clicking on the "Assembler"-folder in the workspace, and selecting "Add existing file". The file should automagically be set as the assembler entry-file, otherwise, right-click on the file and set it.

Modify the source to fit your needs. If you are going to be using AvrProg.exe to update the system, you should leave the baud-rate at 19.200. In this case all you should need to modify is the CLOCK_FREQ to match the frequency of your oscillator crystal (in Hz).

Now compile the bootloader (F7). If all goes well, you should now have a avrboot.hex-file. If not, and you are using AVR-studio 3.xx, go to project-settings and make sure AVRstudio is set to generate Intel HEX-files. Burn the bootloader into the μController.

You should now set the fuses to allow the μController to start the bootloader. Start the programming util and select the "Fuses"-tab. Now check "Boot Flash section size=256 words Boot start address=$1F00". Check "Boot Reset vector Enabled (default address=$0000)". You should probably also enable the brownout detection, and set BOD level to 4.0V, since the flash could be incorrectly written below this voltage. Select the appropriate clock settings (probably CKSEL=1101 Ceramic Resonator/External Clock slowly rising power).

Set the lock-bits. At least the bootsector should be protected from accidental updates by setting the "Boot Loader Protection Mode 3 LPM and SPM prohibited in Boot Loader Section". Be sure not to prevent writing of the Application Section, since this will prevent the bootloader from doing its work. Read-mode should also be allowed to enable the verification feature of AVRprog.exe.

Now, with the bootloader installed, try powering up the μController. This version of the bootloader is triggered by the external reset pin (since this saves an I/O-pin on the μController). By reading the I/O-register MCUSR, the controller is able to find out what caused the reset. This also means that the user application should not rely on nor change the value of this register, when using the bootloader. To start the bootloader, connect reset to 0V briefly. This will trigger the bootsector, which will look for communication on the UART. If nothin is received within 5 seconds, normal startup will proceed (ie. the reset-vector will be called). If however a character is received within 5 seconds, the bootsector will enter programming mode. This means that AVRprog.exe should be started within the 5 second timeout after an external reset. AVRprog is bundled with AVR Studio.

Hopefully AVRprog detects the "programmer", and you are ready to write your HEX-file to the controller. In this version of the bootloader, programming of the lock bits has been disabled, to avoid accidently disallowing further updates. If this feature is enabled, be aware that the fuses cannot be reset without a chip erase, which can only be done with a real programmer. The fuses and lock bits can still be read by the bootloader, so you can make sure everything is alright.

Things to remember when using this version of the bootloader

Suggestions for further improvements:

Last updated: 2005.10.31