Flashing AVR via a Bootloader.

Since I didn't get a USB programmer right away, and flashing via the COM port was slow and inconvenient, I had to find a way out of the situation, and it was to use a bootloader. A bootloader is a program that receives firmware over the selected interface, for example uart, and writes it into the microcontroller.

One of my previous articles described how to flash an STM32 using its bootloader, but on STM32 the bootloader is programmed into the chip at the factory during manufacturing, whereas here we'll have to flash it ourselves. Here's how it looks in general terms: we take that bootloader and flash it into the microcontroller, and that's it, we won't need the programmer anymore; then, using a special program, we connect to the bootloader over uart or RS485 and send it the firmware we want to flash into the MCU, and it does the rest itself.

First you need to choose a bootloader; chip45boot2 is suitable for AVR; below is the list of controllers it currently supports.
Flashing AVR via a Bootloader.

Download the bootloader hex for our microcontroller and the program here.
Flashing AVR via a Bootloader.

Now that we have all the necessary tools, we can begin. In the bootloader folder, find the hex file with the name of our MCU (mine is the Atmega16) and flash it, having first set the BOOTSZ and BOOTRST fuses.
Flashing AVR via a Bootloader.

Flashing AVR via a Bootloader.

BOOTSZ determines the size of the bootloader, and BOOTRST determines where the MCU will start after a reset.

In my Atmel Studio the fuses look like this.
Flashing AVR via a Bootloader.

But in other programs the fuses may be inverted. To find out whether this is the case, you can read the current fuse values and look at SPIEN and JTAGEN, the bits that enable flashing the MCU via SPI and JTAG; if you managed to connect to the MCU, it was surely via one of these interfaces. By default they are set and should be checked; if the fuse of the interface you connected through is not checked, the fuses must be inverted.

Now after a reset the MCU will start from the boot section where the bootloader is located, and it in turn will wait to be sent the firmware it should write into the MCU. A certain amount of time, a few seconds, is allotted for waiting; if no one contacts it during this time, the MCU proceeds to execute the main program. Let's connect to it over uart; for this we'll need a usb-uart adapter: the adapter's RX pin is connected to the microcontroller's TX pin, and the adapter's TX pin to the microcontroller's RX pin.
Flashing AVR via a Bootloader.

Open the chip45boot2 program, select the COM port our usb-uart adapter was assigned to (the speed is detected automatically), reset the MCU and click Connect to Bootloader. If the connection is successful, the Status indicator will change to Connected and turn bright green.
Flashing AVR via a Bootloader.

Now select the hex and/or eeprom file and click Program Flash and/or Program Eeprom. That's it, the flashing procedure is finished. If you accidentally flash the MCU with a programmer, the bootloader will be erased, and you'll have to repeat the procedure to restore it.

There is another way to get into the boot section: the table above shows that with a bootloader size of 1024 words, the boot area starts at address $1C00, and you can start executing the program from this address using a function pointer.


#define F_CPU 8000000UL
#define Boot_Address 0x1C00

#include <avr/io.h>
#include <avr/interrupt.h>

void (*Goto_Boot)(void);
ISR(USART_RXC_vect)
{

	if (UDR == 'b')
	{
		Goto_Boot = (void(*)())Boot_Address;
		Goto_Boot();
	}
}

int main(void)
{
	UCSRB = (1<<RXEN)|(1<<TXEN)|(1<<RXCIE)|(0<<UDRIE);
	UCSRC = (1<<URSEL)|(1<<UCSZ0)|(1<<UCSZ1);
	UBRRL = 0x33;
	sei();
    while(1)
    {

    }
}

And the same in assembly

.cseg
 .org $0
 rjmp Reset
 .org $016
 rjmp  RX_Complete

 Reset:
	ldi R16, low(RAMEND)
	out SPL,R16
	ldi R16, high(RAMEND)
	out SPH,R16

	ldi R16,(1<<RXEN)|(1<<TXEN)|(1<<RXCIE)|(0<<UDRIE)
	out UCSRB,R16

	ldi R16,(1<<URSEL)|(1<<UCSZ0)|(1<<UCSZ1)
	out UCSRC,R16

	ldi R16, $33
	out UBRRL,R16 ;set the baud rate to 9600
	sei

 main:
 rjmp main


 RX_Complete:
	in R16, UDR

	cpi R16, 'b'
	breq Goto_Boot
 reti

 Goto_Boot:
	ldi ZH, $1C
	ldi ZL, $00
	icall
 ret

Flashing AVR via a Bootloader.

Now you don't need to reset the MCU to connect to the bootloader; it's enough to just send it the letter b.