====================================================================================================
Optiboot Bootloader for Arduino and AVR
====================================================================================================

https://github.com/Optiboot/optiboot
https://github.com/Optiboot/optiboot/wiki/CompilingOptiboot

----------------------------------------------------------------------------------------------------
Optiboot and all its files are not part of the eCxx library and are not covered by eCxx licenses.

Optiboot is licensed under the terms of the GNU General Public License version 2 or later with
"Bootloader Exception". Please refer to Optiboot's LICENSE file for more details.
----------------------------------------------------------------------------------------------------

This Optiboot bootloader source code is included here in case someone needs a lightweight and fast
bootloader for their AVRs.


====================================================================================================
Usage Examples
====================================================================================================

Some examples use non-default GCC and AVRDUDE paths and configurations. In this case, please adjust
them as needed.


---------------------------------------------
Fuse setting for ATmega8A 16 Mhz for Optiboot
---------------------------------------------

Low      fuse : <none>         (0xBF)
High     fuse : SPIEN, BOOTRST (0xDC)
Unlock   bits :                 0x3F

External crystal oscillator; frequency 8.0+ MHz; start-up time 16K CK + 64 mS
Boot reset vector enabled (size 256 words, boot start address $0F00)
Serial program downloading (SPI) enabled

Brown-out detection level at 2.7 V


---------------------------------------------------------------------
Fuse setting for ATmega328P within Arduino LilyPad 8 Mhz for Optiboot
---------------------------------------------------------------------

Low      fuse : <none>         (0xFF)
High     fuse : SPIEN, BOOTRST (0xDE)
Extended fuse : BODLEVEL1      (0x05)
Unlock   bits :                 0x3F

External crystal oscillator; frequency 8.0+ MHz; start-up time 16K CK / 14 CK + 65 mS
Boot reset vector enabled (size 256 words, boot start address $3F00)
Serial program downloading (SPI) enabled

Brown-out detection level at 2.7 V


-----------------------------------------------------------------------
Fuse setting for ATmega328P within Arduino Pro Mini 16 Mhz for Optiboot
-----------------------------------------------------------------------

Low      fuse : <none>         (0xFF)
High     fuse : SPIEN, BOOTRST (0xDE)
Extended fuse : BODLEVEL1      (0x05)
Unlock   bits :                 0x3F

External crystal oscillator; frequency 8.0+ MHz; start-up time 16K CK / 14 CK + 65 mS
Boot reset vector enabled (size 256 words, boot start address $3F00)
Serial program downloading (SPI) enabled

Brown-out detection level at 2.7 V


--------------------------------------------------------------------------
Fuse setting for ATmega328PB within Arduino Nano Clone 16 Mhz for Optiboot
--------------------------------------------------------------------------

Low      fuse : <none>         (0xFF)
High     fuse : SPIEN, BOOTRST (0xDE)
Extended fuse : BODLEVEL1      (0xFD)
Unlock   bits :                 0x3F

External crystal oscillator; frequency 8.0+ MHz; start-up time 16K CK / 14 CK + 65 mS
Boot reset vector enabled (size 256 words, boot start address $3F00)
Serial program downloading (SPI) enabled

Enable clock failure detection
Brown-out detection level at 2.7 V


------------------------------------------------
Fuse setting for ATmega1284P 20 Mhz for Optiboot
------------------------------------------------

Low      fuse : <none>         (0xFF)
High     fuse : SPIEN, BOOTRST (0xDE)
Extended fuse : BODLEVEL1      (0xFD)
Unlock   bits :                 0x3F

External crystal oscillator; frequency 8.0+ MHz; start-up time 16K CK + 65 mS
Boot reset vector enabled (size 512 words, boot start address $FE00)
Serial program downloading (SPI) enabled

Brown-out detection level at 2.7 V


------------------------
Read fuses and lock bits
------------------------

avrdude -c usbasp -B 1 -p atmega8 -U lfuse:r:-:h -U hfuse:r:-:h -U lock:r:-:h

avrdude -c usbasp -B 1 -p atmega328p -U lfuse:r:-:h -U hfuse:r:-:h -U efuse:r:-:h -U lock:r:-:h

avrdude -c usbasp -B 1 -p atmega1284p -U lfuse:r:-:h -U hfuse:r:-:h -U efuse:r:-:h -U lock:r:-:h

export AVRDUDEROOT=~/.arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17
export AVRDUDEPROG=$AVRDUDEROOT/bin/avrdude
export AVRDUDECONF=$AVRDUDEROOT/etc/avrdude.conf
$AVRDUDEPROG -C $AVRDUDECONF -c usbasp -B 1 -p atmega328pb -U lfuse:r:-:h -U hfuse:r:-:h -U efuse:r:-:h -U lock:r:-:h


-------------------------
Write fuses and lock bits
-------------------------

avrdude -c usbasp -B 1 -p atmega8 -e -U lfuse:w:0xBF:m -U hfuse:w:0xDC:m -U lock:w:0x3F:m

avrdude -c usbasp -B 1 -p atmega328p -e -U lfuse:w:0xFF:m -U hfuse:w:0xDE:m -U efuse:w:0x05:m -U lock:w:0x3F:m

avrdude -c usbasp -B 1 -p atmega1284p -e -U lfuse:w:0xFF:m -U hfuse:w:0xDE:m -U efuse:w:0xFD:m -U lock:w:0x3F:m

export AVRDUDEROOT=~/.arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17
export AVRDUDEPROG=$AVRDUDEROOT/bin/avrdude
export AVRDUDECONF=$AVRDUDEROOT/etc/avrdude.conf
$AVRDUDEPROG -C $AVRDUDECONF -c usbasp -B 1 -p atmega328pb -e -U lfuse:w:0xFF:m -U hfuse:w:0xDE:m -U efuse:w:0xFD:m -U lock:w:0x3F:m


--------------
Build Optiboot
--------------

make atmega8 AVR_FREQ=16000000UL UART=0 BAUD_RATE=115200 LED=B5 LED_START_FLASHES=3 LED_DATA_FLASH=1

make atmega328 AVR_FREQ=8000000UL UART=0 BAUD_RATE=57600 LED=B5 LED_START_FLASHES=3 LED_DATA_FLASH=1

make atmega328 AVR_FREQ=16000000UL UART=0 BAUD_RATE=115200 LED=B5 LED_START_FLASHES=3 LED_DATA_FLASH=1

make atmega1284 AVR_FREQ=20000000UL UART=0 BAUD_RATE=115200 LED=B5 LED_START_FLASHES=3 LED_DATA_FLASH=1

export DFPDIR="~/Atmel.ATmega_DFP.1.5.362"
export GCCROOT='~/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino5/bin/'
export OPTIMIZE='-B$DFPDIR/gcc/dev/atmega328pb -I$DFPDIR/include -Os -fno-split-wide-types -mrelax'
make atmega328pb AVR_FREQ=16000000UL UART=0 BAUD_RATE=115200 LED=B5 LED_START_FLASHES=3 LED_DATA_FLASH=1 GCCROOT="$GCCROOT" OPTIMIZE="$OPTIMIZE"

NOTE : Please refer to eCxx's main README file about how to download and extract AVR DFP.


-------------
Burn Optiboot
-------------

avrdude -c usbasp -B 1 -p atmega8 -e -V -U flash:w:optiboot_atmega8.hex

avrdude -c usbasp -B 1 -p atmega328p -e -V -U flash:w:optiboot_atmega328.hex

avrdude -c usbasp -B 1 -p atmega1284p -e -V -U flash:w:optiboot_atmega1284p.hex

export AVRDUDEROOT=~/.arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17
export AVRDUDEPROG=$AVRDUDEROOT/bin/avrdude
export AVRDUDECONF=$AVRDUDEROOT/etc/avrdude.conf
$AVRDUDEPROG -C $AVRDUDECONF -c usbasp -B 1 -p atmega328pb -e -V -U flash:w:optiboot_atmega328pb_UART0_115200_16000000UL.hex


-------------------
Upload user program
-------------------

avrdude -c arduino -P /dev/ttyUSB0 -b 115200 -p atmega8 -D -U flash:w:my_program.hex:i

avrdude -c arduino -P /dev/ttyUSB0 -b 57600 -p atmega328p -D -U flash:w:my_program.hex:i

avrdude -c arduino -P /dev/ttyUSB0 -b 115200 -p atmega328p -D -U flash:w:my_program.hex:i

avrdude -c arduino -P /dev/ttyUSB0 -b 115200 -p atmega1284p -D -U flash:w:my_program.hex:i

export AVRDUDEROOT=~/.arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17
export AVRDUDEPROG=$AVRDUDEROOT/bin/avrdude
export AVRDUDECONF=$AVRDUDEROOT/etc/avrdude.conf
$AVRDUDEPROG -C $AVRDUDECONF -c arduino -P /dev/ttyUSB0 -b 115200 -p atmega328pb -D -U flash:w:my_program.hex:i


====================================================================================================
