# Name: Makefile
# Project: bootloadHID
# Author: Christian Starkjohann
# Creation Date: 2007-03-19
# Tabsize: 8
# Copyright: (c) 2007 by OBJECTIVE DEVELOPMENT Software GmbH
# License: GNU GPL v2 (see License.txt)

###############################################################################
# Configure the following variables according to your AVR. The example below
# is for an ATMega8. Program the device with
#     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
F_CPU = 12800000
FUSEH = 0x90
FUSEL = 0x84
# Fuse high byte:
# 0x80 = 1 0 0 1   0 0 0 0 <-- BOOTRST (boot reset vector at 0x1800)
#        ^ ^ ^ ^   ^ ^ ^------ BOOTSZ0
#        | | | |   | +-------- BOOTSZ1
#        | | | |   + --------- EESAVE (preserve EEPROM over chip erase)
#        | | | +-------------- CKOPT (not used)
#        | | +---------------- SPIEN (allow serial programming)
#        | +------------------ WDTON (WDT always on)
#        +-------------------- RSTDISBL (reset pin is enabled)
# Fuse low byte:
# 0x84 = 1 0 0 0   0 1 0 0
#        ^ ^ \ /   \--+--/
#        | |  |       +------- CKSEL 3..0 (internal RC oscillator at 8 MHz)
#        | |  +--------------- SUT 1..0 (minimum start-up time = 0.5 s)
#        | +------------------ BODEN (BrownOut Detector enabled)
#        +-------------------- BODLEVEL (2.7V)

###############################################################################

PROJECT = bootloader
USBDRV  = ../../usbdrv
AVRDUDE = avrdude -c pony-stk200 -P lpt1 -p $(DEVICE) -E noreset

CFLAGS  += -Wall -Os -I$(USBDRV) -I. -mmcu=$(DEVICE) -DF_CPU=$(F_CPU)
LDFLAGS += -nostartfiles

OBJECTS = $(PROJECT).o usbdrvasm.o

# symbolic targets:
all:	$(PROJECT).hex $(PROJECT).lst

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

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

fuse:
	$(AVRDUDE) -U hfuse:w:$(FUSEH):m -U lfuse:w:$(FUSEL):m

lock:
	$(AVRDUDE) -U lock:w:0x2F:m
	
# all three programming options in one invocation of avrdude
program:
	$(AVRDUDE) -U hfuse:w:$(FUSEH):m -U lfuse:w:$(FUSEL):m -U flash:w:bootloader.hex:i -U lock:w:0x2F:m

clean:
	rm -f $(PROJECT).lst $(PROJECT).hex $(PROJECT).elf *.o

$(PROJECT).o: main.c makefile
	avr-gcc -c main.c -o $@ $(CFLAGS)

usbdrvasm.o: $(USBDRV)/usbdrvasm.S makefile
	avr-gcc -c $(USBDRV)/usbdrvasm.S -o $@ $(CFLAGS)

$(PROJECT).elf:	$(OBJECTS)
	avr-gcc -o $@ $(OBJECTS) $(LDFLAGS)

$(PROJECT).hex: $(PROJECT).elf
	rm -f $@
	avr-objcopy -j .text -j .data -O ihex $< $@
	avr-size $<

$(PROJECT).lst:	$(PROJECT).elf
	avr-objdump -d $< > $@
