# Simple Makefile Volker Oth (c) 1999
# edited by AVRfreaks.net nov.2001

#----- DigiSpark

########### change these lines according to your project ##################

CC             = avr-gcc
CXX = $(CC)

#put the name of the target mcu here (at90s8515, at90s8535, attiny22, atmega603 etc.)
MCU = attiny85

#put the name of the target file here (without extension)
TRG	 = DigiUSB

#put your C sourcefiles here 
OBJ = usbdrv.o usbdrvasm.o osccal.o main.o

#assembler flags
ASFLAGS =  -gstabs -mmcu=$(MCU)  -DF_CPU=16500000L -DDEBUG_LEVEL=0

###### dependencies, add any dependencies you need here ###################

OPTIMIZE	= -Os
CFLAGS		= -g -Wall $(OPTIMIZE) -mmcu=$(MCU) $(DEFS) -I/usr/local/avr/include/ -DF_CPU=16500000L -DDEBUG_LEVEL=0 -fdata-sections -ffunction-sections -ffreestanding -fno-schedule-insns2 -fno-tree-scev-cprop -fno-split-wide-types 
CXXFLAGS        = $(CFLAGS) 
LDFLAGS       = -Wl,-Map,$(TRG).map,--relax,--gc-sections
OBJCOPY        = avr-objcopy
OBJDUMP        = avr-objdump

all:	$(TRG).hex $(TRG).lst

# $(TRG).o : $(TRG).c
# 	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^

# ir.o:	ir.c
# 	avr-gcc -g -Wall -O3 -mmcu=attiny85    -c -o ir.o ir.c

%.hex:	%.elf
	$(OBJCOPY) -j .text -j .data -O ihex $< $@

$(TRG).elf:	$(OBJ)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^

%.lst:	%.elf
	$(OBJDUMP) -h -S $< > $@

size:
	avr-size $(TRG).elf
	echo -n "STACK="; echo "ibase=16; 025F-" `avr-nm $(TRG).elf | grep " _end" | perl -ne 's/\s.+//; tr/[a-z]/[A-Z]/; print'` "+800000" | bc

clean:
	touch dummy.o
	rm *.o
	if [ -e $(TRG).hex ]; then rm $(TRG).hex; fi
	if [ -e $(TRG).map ]; then rm *.map; fi
	if [ -e $(TRG).lst ]; then rm *.lst; fi
	if [ -e $(TRG).elf ]; then rm *.elf; fi
	touch \#dummyfile
	touch dummyfile~
	rm \#* *~
	perl -e 'use Cwd; $$_=getcwd(); /(\d+)/; print "#define VERSION \"$$1\"\n"' > version.h

write: $(TRG).hex
	micronucleus --run DigiUSB.hex

lock:
	avrdude -V -B 5 -p t85 -c avrispv2 -P usb -U lock:w:0xfc:m

#avrdude -B 5 -p t85 -c avrispv2 -P usb -U flash:r:file.hex:i

#avrdude USB
#avrdude -B 5 -p t85 -c avrispv2 -P usb -U flash:w:file.hex

#eeprom_r:
#	avrdude -E noreset -y -p t85 -c bsd -U eeprom:r:$(TRG)_eeprom.hex:i
#
#eeprom_w: $(TRG)_eeprom.hex
#	avrdude -E noreset -y -p t85 -c bsd -U eeprom:w:$(TRG)_eeprom.hex:i

# fuse:
#	avrdude -E noreset -y -p t85 -c bsd -U hfuse:w:0xdf:m

# вٻ(RC 1MHz)
#	avrdude -E noreset -y -p t85 -c bsd -U lfuse:w:0x62:m
# RC 8MHz
#	avrdude -E noreset -y -p t85 -c bsd -U lfuse:w:0xe2:m
# å 4MHz
#	avrdude -E noreset -y -p t85 -c bsd -U lfuse:w:0xdc:m
# PLL 16MHz
#	avrdude -E noreset -y -p t85 -c bsd -U lfuse:w:0xf1:m
# WDT ͭ(Ѥʤ)
# avrdude -E noreset -y -p t85 -c bsd -U hfuse:w:0xcf:m
# BOD ͭ(2.7V)
#	avrdude -E noreset -y -p t85 -c bsd -U hfuse:w:0xdd:m

