###############################################################################
# Project: AVRUSBboard / USB bootloader for ATmega8/168/328
###############################################################################
# Webpage: http://www.dunweber.com/docs/elektronik/AVRUSBboard/
# Author:  Christian Dnweber
# Date:    2011-01-22 11:37:00 (GMT+1)
# Credits: The open source USB bootloader BootloadHID is
#          used and has been modified slightly for this
#          project. Find the original bootloader here:
#          http://www.obdev.at/products/vusb/bootloadhid.html
# Changes: Fuse bits modified + easy adaptable for ATmega168: See lines 37 and 51.
#          For an ATmega328 you have to make some changes.
# Tip:     On Windows use Build.bat as an easy way of running the makefile.

###############################################################################
# Creator of the bootloader:
###############################################################################
# Name: Makefile
# Project: bootloadHID
# Author: Christian Starkjohann
# Creation Date: 2007-03-19
# Tabsize: 4
# Copyright: (c) 2007 by OBJECTIVE DEVELOPMENT Software GmbH
# License: GNU GPL v2 (see License.txt)
# This Revision: $Id: Makefile 684 2008-10-22 18:43:39Z cs $

###############################################################################
# To-do and Settings:
###############################################################################
# Configure the following variables according to your AVR. The examples below
# is for an ATmega8 and ATmega168 (by setting DEVICE = ATmega168).
# The following must be done just once for each new device:
#     make fuse    # to set the clock generator, boot section size etc.
#     make flash   # to load the boot loader into flash
#     make lock    # to protect the boot loader from overwriting

DEVICE = atmega8
BOOTLOADER_ADDRESS = 1800
F_CPU = 12000000
FUSEH = 0xC0
FUSEL = 0xFF
LOCK = 0x2F
INCLUDE = usbdrv
TARGET = bootloader.main
OUTPUT = bootloader.atmega8

# Program device using AVRdude
UPLOADER = usbasp
UPLOADER_DEVICE = m8

# Just set DEVICE = atmega168 above and the rest is handled automatically
ifeq ($(DEVICE),atmega168)
	BOOTLOADER_ADDRESS = 3800
	FUSEH = 0xD7
	FUSEL = 0xFF
	FUSEX = 0xF8
	OUTPUT = bootloader.atmega168
	UPLOADER_DEVICE = m168
endif

# Fuse high byte:
# 0xC0 = 1 1 0 0   0 0 0 0 <-- BOOTRST (boot reset vector at 0x1800)
#        ^ ^ ^ ^   ^ ^ ^------ BOOTSZ0
#        | | | |   | +-------- BOOTSZ1
#        | | | |   + --------- EESAVE (preserve EEPROM over chip erase)
#        | | | +-------------- CKOPT (full output swing)
#        | | +---------------- SPIEN (allow serial programming)
#        | +------------------ WDTON (Watch Dog Timer not always on)
#        +-------------------- RSTDISBL (1=reset pin enabled: MUST for ICSP to work! | 0=reset pin disabled: normal I/O)

# Fuse low byte:
# 0xFF = 1 1 1 1   1 1 1 1
#        ^ ^ \ /   \--+--/
#        | |  |       +------- CKSEL 3..0 (external clock >8 M crystal)
#        | |  +--------------- SUT 1..0 (crystal osc, longest possible start-up)
#        | +------------------ BODEN (Brown-out detector disabled)
#        +-------------------- BODLEVEL (2.7 V)
# Originally low fuse byte was: FUSEL = 0x9F

#
# Extended fuse bits (ATmega168):
# 0xF8 = 1 1 1 1   1 0 0 0
#

# Lock bits:
# 0xEF = 1 1 1 0   1 1 1 1
#        \ / \ /   \ / \ /
#         |   |     |   +----- LB2/LB1 (no lock)
#         |   |     +--------- BLB02/BLB01 (Application Section)
#         |   +--------------- BLB12/BLB11 (Boot Loader Section: SPM is not allowed to write to the Boot Loader section)
#         +------------------- NA (reserved bits)
###############################################################################

AVRDUDE = avrdude -c $(UPLOADER) -p $(UPLOADER_DEVICE)

LDFLAGS = -Wl,--section-start=.text=$(BOOTLOADER_ADDRESS)

COMPILE = avr-gcc -Wall -Os -I$(INCLUDE) -I. -mmcu=$(DEVICE) -DF_CPU=$(F_CPU) -DDEBUG_LEVEL=0 # -DTEST_MODE
# NEVER compile the final product with debugging! Any debug output will
# distort timing so that the specs can't be met.

OBJECTS = $(TARGET).o $(INCLUDE)/usbdrvasm.o $(INCLUDE)/oddebug.o

# symbolic targets:
all:	$(OUTPUT).hex
	rm -f $(TARGET).bin $(TARGET).o

.c.o:
	$(COMPILE) -c $< -o $@

.S.o:
	$(COMPILE) -x assembler-with-cpp -c $< -o $@
# "-x assembler-with-cpp" should not be necessary since this is the default
# file type for the .S (with capital S) extension. However, upper case
# characters are not always preserved on Windows. To ensure WinAVR
# compatibility define the file type manually.

.c.s:
	$(COMPILE) -S $< -o $@

flash:
	$(AVRDUDE) -U flash:w:$(OUTPUT).hex:i

readflash:
	$(AVRDUDE) -U flash:r:read.hex:i

fuse:
ifeq ($(DEVICE),atmega8)
	$(AVRDUDE) -U hfuse:w:$(FUSEH):m -U lfuse:w:$(FUSEL):m
endif
ifeq ($(DEVICE),atmega168)
	$(AVRDUDE) -U hfuse:w:$(FUSEH):m -U lfuse:w:$(FUSEL):m -u -V -U efuse:w:$(FUSEX):m
endif

lock:
	$(AVRDUDE) -U lock:w:$(LOCK):m

read_fuses:
	$(AVRDUDE) --rd_fuses

clean:
	rm -f $(OUTPUT).hex $(TARGET).bin *.o $(INCLUDE)/*.o $(TARGET).s $(INCLUDE)/oddebug.s $(INCLUDE)/usbdrv.s

# file targets:
$(TARGET).bin:	$(OBJECTS)
	$(COMPILE) -o $(TARGET).bin $(OBJECTS) $(LDFLAGS)

$(OUTPUT).hex:	$(TARGET).bin
	rm -f $(OUTPUT).hex $(TARGET).eep.hex usbdrv/usbdrvasm.o usbdrv/oddebug.o
	avr-objcopy -j .text -j .data -O ihex $(TARGET).bin $(OUTPUT).hex
	avr-size $(TARGET).bin

disasm:	$(TARGET).bin
	avr-objdump -d $(TARGET).bin

cpp:
	$(COMPILE) -E $(TARGET).c
