#
# Targets:
#	<none>		generate flash file
#	install		generate flash
#	download	download flash
#	check		shows md5 and sha1 sum of uploaded code
#	clean		remove obj- and temporary files
#
# change PROJNAME for new projects
# add your object files to OBJS
#

#-------------------------------------------------------------------------
# project specific things
# change these definitions for new projects
#-------------------------------------------------------------------------

PROJNAME = main
OBJS = main.o sha1.o hmac.o trunc.o #usbdrv/usbdrvasm.o usbdrv/usbdrv.o

# target architecture
MCU	= atmega168
#MCU	= attiny45
#-------------------------------------------------------------------------
# macros for the tools
#-------------------------------------------------------------------------

BAUD	= 19200

BINFORMAT = ihex

# programmer format, this depends on programmer or bootloader you're using
#PROGFORMAT = dasa2
#PROGFORMAT = dapa
#PROGFORMAT = stk200
PROGFORMAT = usbtiny
#PROGFORMAT = stk500v2

# programmer-device-file
#IFCTYPE	= /dev/ttyS0
#IFCTYPE	= /dev/ttyS1
#IFCTYPE	= /dev/ttyACM0
#IFCTYPE 	= /dev/ttyACM1
IFCTYPE	= /dev/ttyUSB0
#IFCTYPE	= /dev/ttyUSB1

# Tools
AS	= avr-as
ASLD	= avr-gcc -x assembler
LD	= avr-ld 
CC	= avr-gcc 
OBJCOPY = avr-objcopy
#PROGR	= uisp
#PROGR	= at16prog
#PROGR  = lboot
PROGR = avrdude

INC=-Iusbdrv
OPTLEVEL=s

# Flags
CFLAGS=-I. $(INC) -g -mmcu=$(MCU) -O$(OPTLEVEL) \
	-DF_CPU=16500000	                        \
#    -mcall-prologues                            \
#    -mtiny-stack                                \
#    -fno-jump-tables                            \
#    -fno-inline-functions                       \
#    -fno-inline-small-functions                 \
#    -fno-split-wide-types                       \
#	-fpack-struct                               \
#    -fshort-enums                               \
#	-funsigned-bitfields                        \
#    -funsigned-char                             \
#	-Wall -Wstrict-prototypes                   \
#    -Wl,--gc-sections                           \
#    -Wl,--print-gc-sections                     \
#    -ffunction-sections -fdata-sections         \
#	-Wa,-ahlms=$(firstword                      \
#	    $(filter %.lst, $(<:.c=.lst)))
ASMFLAGS =-I. $(INC) -mmcu=$(MCU)               \
	-x assembler-with-cpp                       \
	-DF_CPU=16500000	                        \
	-Wa,-gstabs,-ahlms=$(firstword              \
		$(<:.S=.lst) $(<.s=.lst))

LDFLAGS	= -Wl,-Map=$*.map -mmcu=$(MCU)
OCFLAGS	= -O $(BINFORMAT)
# at16prog
#PRFLAGS	= $(IFCTYPE) -dprog=$(PROGFORMAT) -dpart=$(MCU)
# avrdude
PRFLAGS		= -F -p $(MCU) -P $(IFCTYPE) -c $(PROGFORMAT)  -b $(BAUD) 
#lboot
#PRFLAGS 		= -d $(IFCTYPE) -b $(BAUD)

#-------------------------------------------------------------------------
# the targets
#-------------------------------------------------------------------------

all: $(PROJNAME).hex $(PROJNAME).elf $(OBJS)

%.hex:	%.elf
	$(OBJCOPY) $(OCFLAGS) $< $@
	@chmod ugo-x $@

%.elf:	$(OBJS)
	$(CC) $(CFLAGS)  $(OBJS) $(LIBS) $(LDFLAGS) -o $@

%.o:	%.c
	$(CC) $(CFLAGS) -Wa,-a=$*.list -c -o $@ $<

# object from asm
.S.o :
	$(CC) $(ASMFLAGS) -c $< -o $@


install: $(PROJNAME).hex
	$(PROGR) $(PRFLAGS) -p $(PROJNAME).hex
#$(PROGR) $(PRFLAGS)  -U flash:w:$(PROJNAME).hex
	
download: $(PROJNAME).down.hex
	echo ""
	
$(PROJNAME).down.hex:
	$(PROGR) $(PRFLAGS) -U flash:r:$(PROJNAME).down.hex:i
	
check:$(PROJNAME).down.hex $(PROJNAME).hex
	$(PROGR) $(PRFLAGS) -v $(PROJNAME).hex

size: $(PROJNAME).hex
	avr-size $(PROJNAME).elf
	
clean:
	rm -f *.hex *.elf *.map *.list *.o *~ *.lst
	rm -f usbdrv/*.o usbdrv/*.map usbdrv/*.list usbdrv/*~ usbdrv/*.lst

test: test.c sha1.c hmac.c trunc.c
	gcc test.c sha1.c hmac.c trunc.c -o test

