SOURCE  = Testprogramm_Loesung.c

#
# These are tool-specific options. You will not have to
# change these for your assignment.
#
CC  = avr-gcc
SIZE  = avr-size
OBJDUMP = avr-objdump
OBJCOPY = avr-objcopy
AVRDUDE = avrdude

MCU  = attiny84
MHZ = 8000000

# AVRDUDE_PROG  = avrispmkII

# Dragon ISP with clock fix ("bad signature" fix)
AVRDUDE_PROG  = dragon_isp -B10
AVRDUDE_PORT = usb
AVRDUDE_PART = t84

#### Do not change anything below this line! ####
CFLAGS = -Os -mmcu=$(MCU) -DF_CPU=$(MHZ) -Wall
ELF = $(addsuffix .elf, $(basename $(SOURCE)))
HEX = $(addsuffix .hex, $(basename $(SOURCE)))


AVRDUDE_FLAGS = -p $(AVRDUDE_PART) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROG) -q -q

.PHONY: compile
compile: $(SOURCE)
 @echo Compiling $(SOURCE)...
 @$(CC) $(CFLAGS) -o $(ELF) $(SOURCE)
 @echo Generating programming file...
 @$(OBJCOPY) -O ihex -R .eeprom -R .fuse $(ELF) $(HEX)
 @echo done. Size requirements:
 @$(SIZE) $(ELF)
 @echo Now go ahead and \'make install\'.

.PHONY: install
install: compile
 @echo Writing fuses...
 @$(AVRDUDE) $(AVRDUDE_FLAGS) `$(OBJDUMP) -s -d -j .fuse $(ELF) | tail -n1 | perl -ne 'if(/(\w\w)\W(\w\w)\W(\w\w)/) {print "-U lfuse:w:0x$$1:m -U hfuse:w:0x$$2:m -U efuse:w:0x$$3:m";}'`
 @echo -n Waiting for device to reset...3
 @sleep 1
 @echo -n ...2
 @sleep 1
 @echo -n ...1
 @sleep 1
 @echo
 @echo Programming flash memory...
 @$(AVRDUDE) $(AVRDUDE_FLAGS) -U flash:w:$(HEX)
 @echo done.

.PHONY: clean
clean:
 rm -rf $(HEX) $(ELF)