Arduino Bootloader

Arduino among others has a really nice feature… The bootloader. Once you burn it on the chip you can upload your code without the need of an external programmer.

I have started using it for my AVR projects but i came across on some issues so i compiled a COMPLETE list of what you need to do it, in order not to search again for the obvious…

Programmer used AVR910 (or compatible) with avrdude. In the avrdude commands I assume that the
programmer is AVR910, COM port=COM3 and baud rate=115200. Adjust to your programmer.

Have in mind that you might use also -x devcode=0x35 for AVR910

ATMEGA8

The default fuses of ATMega8 are (pdf and locally tested):
High Fuse:      0xd9
Low Fuse:       0xe1
Lock:           0x3f

Programming the bootloader. Chip erase (-e maybe needed in some cases)

  • Unlock the bootloader (TEST: try with -e)

avrdude -c avr910 -P com3 -b 115200 -p atmega8 -U lock:w:0xff:m
(fails with avrdude: verification error, first mismatch at byte 0x0000 0xff != 0x3f)

  • Program the fuses

avrdude -c avr910 -P com3 -b 115200 -p atmega8 -U hfuse:w:0xca:m -U lfuse:w:0xdf:m

  • Burn the bootloader (TEST: try with -D disable auto erase)

avrdude -c avr910 -P com3 -b 115200 -p atmega8 -U flash:w:ATmegaBOOT.hex

  • Lock the bootloader

avrdude -c avr910 -P com3 -b 115200 -p atmega8 -U lock:w:0xcf:m
(fails with avrdude: verification error, first mismatch at byte 0x0000 0xcf != 0x0f)

But it works….

ATMEGA168

The default fuses of ATMega168 are (pdf and locally tested):
High Fuse:      0xd9
Low Fuse:       0xe1
Lock:           0x3f

Programming the bootloader. Chip erase (-e maybe needed in some cases)

  • Unlock the bootloader

avrdude -c avr910 -P com3 -b 115200 -p atmega168 -U lock:w:0x3f:m

  • Program the fuses

avrdude -c avr910 -P com3 -b 115200 -p atmega168 -U hfuse:w:0xdd:m -U lfuse:w:0xff:m -U efuse:w:0x00:m

  • Burn the bootloader

avrdude -c avr910 -P com3 -b 115200 -p atmega168 -U flash:w:ATmegaBOOT_168_ng.hex

  • Lock the bootloader

avrdude -c avr910 -P com3 -b 115200 -p atmega168 -U lock:w:0x0f:m

And now the good part:

Once you burn the bootloader to a chip you can use this excellent program to upload any hex file to your ATmega with Arduino uploader

References

Updated: July 2013
Notes:
FUSEBITS: internal 1MHz clock, EESAVE enabled:
M8 L:0xE1 H:0xD1
M88,M168 L:0x62 H:0xD7 E:0xF9
M328 L:0x62 H:0xD1 E:0xFF

12 comments on “Arduino Bootloader
  1. geertbarents says:

    the only thing i get is ”initialization failed, rc=-1

    please help

    Grtz, Geert

  2. geertbarents says:

    @billy

    windows.
    and the command is:
    1. avrdude -p atmega8 -c usbtiny -b 115200 -U lock:w:0xff:m
    2. avrdude -p atmega8 -c usbtiny -b 115200 -U hfuse:w:0xca:m -U lfuse:w:0xdf:m
    3. avrdude -p atmega8 -c usbtiny -b 115200 -U flash:w:ATmegaBOOT.hex
    4. avrdude -p atmega8 -c usbtiny -b 115200 -U lock:w:0xcf:m

    Grtz. Geert

  3. geertbarents says:

    i made a test board for this

    connections:
    icsp atmega8 -16pu
    1 –> MISO
    2 –> none
    3 –> SCK
    4 –> MOSI
    5 –> RESET
    6 –> none

    and then for the atmega8 -16pu:
    pin 7 VCC and pin 20 AVCC to 4,5 v
    pin 8 GND and pin 22 GND to GND

  4. billy says:

    Wiring looks correct. But you are missing 6->GND

    Do you want me to send you a programmed atmega8 ?

  5. Tius says:

    I try to upload Arduino to ATMega328 with AVR910 programmer, but not success. Anyone can suggest me what software that support AVR910 and ATMega328?

  6. Prashanth says:

    Hey,
    Brilliant tutorial… Very easy to follow and straight forward..Thank you so much..i spent days on this..

  7. Stephen says:

    These fuse settings here do baffle me just a bit. I had my chips soldered to a board already and was expecting them to use the internal 8MHz clock, but once I run Step 2 to set the fuses to 0xdd, 0xff, and 0x00, I lose communication with the chip (i.e. initialization failed: rc = -1). What do these fuse settings mean, and how can I restore my chips?

    As an aside, every time I go to an “atmega fuse calculator”, it shows that unprogrammed bits are 1 and programmed bits are 0, yet when I look at the default settings the Arduino SDK uses in avrdude, they seem to set bits that are impossible to set in these calculators. Is it possible that avrdude considers programmed to be 1 and unprogrammed to be 0?

    Thanks!

  8. billy says:

    I know that is confusing:
    For the HFUSE 0xDD = SPIEN + BODLEVEl1 = 00100010

    Apply a NOT on this one and you get your 0xDD in HEX.

    What you are using for programmer ? AVR910 based ?. If you haven’t touch the serial programming HFUSE you can reset it back to defaults

  9. Stephen says:

    Hi Billy, I’m using the USBtinyISP to program the chip. After having run the command “avrdude -c usbtiny -p m168 -U hfuse:w:0xdd:m -U lfuse:w:0xff:m -U efuse:w:0x00:m”, I stopped being able to reach the chips. I’m afraid I’ll have to find a parallel programmer (i.e. not ICSP) in order to re-enable the reset pin & program the chip. Thanks!

    • billy says:

      I am surprised that this command disabled your ICSP since the fuse bits are correct.
      Try the following:
      Use a -e to do a chip erase and -B 100
      Second try it again with a 5V regulator on the chip it self. Don’t rely on the usb power. If the USBtinyISP has a slow clock option try that one also

  10. Stephen says:

    Hey Billy, it seems like I found out what the problem was. Gotta add an external clock. I whipped up a quick script that toggles an output pin on a spare Arduino at 8 MHz, and used that to drive XTAL1 & XTAL2 on the unresponsive ATmegas. I then used this to write 0xE2 back to the lfuse (to use the internal oscillator), and now I’m able to burn the bootloader. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

*