Inside of Fatima

Bootloader for ATmega8-board

ATmega8-board has been successfully used for a long time (Ferda and Fatima, Gerda, Daisy and other). This article describes how I made the avr109 bootloader (Application note, original source code) run for this board. If you do not intent to use the bootloader with ATmega8-board, I suggest to read this text and then to adapt the original source to your needs yourself. Do not use my version in this case.

Why a bootloader?

The experience from the Eurobot 2007 Czech finals has shown, that there is a new problem becoming stronger and stronger - the microprocessor programmers. Most of the programmers we have been using are designed for parallel port. Parallel ports are becoming a rare commodity, though. Most new laptops are not equipped with them. In past, I overcame this problem by soldering a serial-port programmer (DASA), used together with a USB-to-serial converter. In present, I have troubles using this combination with FreeBSD 6.2 and avrdude 5.2 (uisp is no longer distributed with FreeBSD). Altogether, there is no way to flash a new program to my ATmega8-boards for me. Bootloaders give me an opportunity to pre-flash the ATmega8-board in the convenience of a desktop computer at home and simply upload a new program via the serial line, if needed on the battlefield.

Why avr109?

First of all, my requirement for a bootloader is that I can use it with any operating system I meet nowadays (FreeBSD, Linux, Windows). This removes bootloaders such as MegaLoad from the list of candidates. There are actually probably only STK500 and avr109-based bootloaders left. Having troubles with making any STK500-compatible bootloader run, I tried the avr109 bootloader. After few trials it works for me (TM).

How to make it work?

Programmer

First of all, a hardware programmer is still necessary to upload the bootloader to the processor. If you do not posses one, get it.

Configuration and compilation

I have been using the "Gcc" branch of the bootloader compiled with avr-gcc and avr-libc. The original code is configured for the ATmega128 microprocessor. It is easy, though, to change this configuration. The source distribution contains a file named preprocessor.xls designed exactly for this purpose (Warning: macros involved; Disclaimer: it can be used with OpenOffice). Following the advice on the first tab, it is very straightforward to change the device to _ATMEGA8 and the boot size to _B1024 on the second tab. The older ATmega8-boards came pre-set to the speed of 1MHz, hence the CPU_FREQ should be set to 1000000 in this case. Newer boards are set to 8MHz, thus the value 8000000 in this case. I have troubles using a baudrate higher than 4800 bauds. I suggest to save yourself from troubles by using this baudrate too. I do not feel any big speed limitation when uploading a new program using the bootloader. The macros in the sheet fill all the other defines for the file defines.h. Just copy the generated content to the defines.h file. Typing make in the source directory should compile the bootloader with a requested configuration.

Upload to the microprocessor

I cannot help you much with the upload of the new bootloader to your ATmega8-board, because the method is specific for your particular combination of hardware and software. Check the Makefile for the pre-specified method using avrdude. I had just changed AVRDUDE_PROGRAMMER to dasa (the type of my programmer) and AVRDUDE_PORT to /dev/ttyUSB0 (because I use the USB-to-serial converter), then I typed make program and waited until it finished.

Setting the ATmega8 fuses

The microprocessor needs to know, that there is some bootloader involved. It also needs to know, where to find it. The first fact is signaled by setting the least significant bit of the high fuse (BOOTRST) to 0 (from the default value 1). This makes the processor aware of the bootloader. The bootloader location is defined through its size specified in the two preceding bits (BOOTSZ0 and BOOTSZ1). For the booloader size I suggested in the configuration, they should both contain zeros. This combination gives me a complete value of high fuse 0xD8. The following command changes the high fuse byte to the requested value (with dasa programmer and /dev/ttyUSB0 again):

avrdude -p m8 -c dasa -P /dev/ttyUSB0 -U hfuse:w:0xD8:m

If you do not know or want to change the board speed (e.g. from 1MHz to 8MHz), check the datasheet (or arduino) for the CKSEL0 and CKSEL1 bits of the low fuse byte.

Uploading a new program to the microprocessor using the bootloader

Pull the PD4 pin down first (press the connected button, plug the starting cable in or whatever) and switch the board on afterwards. The bootloader should be waiting for instructions now. I use the following command to upload a new program:

avrdude -p m8 -c avr109 -b 4800 -P /dev/ttyUSB0 -U program.hex

Modifications

In the original version of the avr109 bootloader, the bootloader starts, checks the PD4 pin and if pulled low, waits for a new program to be uploaded. If the pin is not pulled low, the execution jumps to the begining of the program in the chip. Luckily, we use PD4 as a starting cable input in our Eurobot competing robots. For this reason I did not change this configuration in the preprocess.xls file. If I plug the starting cable in before the board is started, the bootloader waits for a new program. If I start the board before the starting cable is plugged in, the "real" program is executed (and it can wait for the starting cable to be plugged in).

Furthermore, I had troubles with the bootloader (the baudrate). For this reason, I fully understand other people adding different LED signalizations to their bootloader. I did the same. ATmega8-board comes with a LED specificaly added to the PB0 pin for this purpose (the red one). I added a status LED signalization to the bootloader code. And this is why I urge you not to use my version of bootloader for other boards than ATmega8-board. You can find my changes by looking up the status led string in the main.c file. When the bootloader starts, the red LED is switched on (as opposed to blinking when the normal program executes). When the bootloader is over, it switches it of (and it starts blinking if the new program executes correctly).

Download