## Makefile to generate demo binary files from the one and only 
## source file blinky.c

###########
## make all
###########
## The output is organized in one new direktory named $(BDIR).
## $(BDIR) will contain 
##      blinky.c 
##      Makefile 
##      Target dependent files will reside 
##      in $(BDIR)/<TARGET> for target <TARGET>.
##
## For a list if supported tarets see $(TARGETS).
##
## Target dependent output is:
## Binaries: 
##      Intel HEX (*.hex)
##      SREC      (*.srec)
##      Binary    (*.bin)
##      elf       (*elf)
## Assembly/Disassembly:
##      Assembly output of avr-gcc (*.s)
##      Disassembly of *.elf       (*.lss)

############
## make arch
############
## Build the archives from $(BDIR)
##      $(ARCH).tar.gz
##      $(ARCH).zip
## They will contain all stuff you generated with "make all".

#############
## make clean
#############
## Recursively remove ./(BDIR) and the two archives
##

PRG            = blinky
OPTIMIZE       = -Os
# DEFS           = -DF_CPU=1000000

CC             = avr-gcc
OBJCOPY        = avr-objcopy
OBJDUMP        = avr-objdump

MAKEFILE       = Makefile

CFLAGS        = -g -Wall -fverbose-asm -fno-common $(OPTIMIZE) $(DEFS)
TARGETS        =  attiny2313 attiny24 attiny44 attiny84 \
						at90s2313 at90s8515 at90s8535 \
						atmega8 atmega88 atmega48 atmega168 atmega8535 atmega8515 \
						atmega162 atmega169 atmega16 \
						atmega32 \
						atmega64 atmega644 \
						atmega128 

.PHONY: all clean $(TARGETS) cp_files arch

BDIR   = ./blinky
BASE   = $(PRG)-b1
ARCH   = $(PRG)

all: clean $(TARGETS) cp_files

cp_files:
	cp $(PRG).c    $(BDIR)
	cp $(MAKEFILE) $(BDIR)

$(TARGETS): $(PRG).c $(MAKEFILE)
	mkdir -p $(BDIR)/$@
	cd $(BDIR)/$@ && $(CC) $(CFLAGS) -mmcu=$@ ../../$< -o $(BASE)_$@.elf
	cd $(BDIR)/$@ && $(CC) $(CFLAGS) -mmcu=$@ -S ../../$< -o $(BASE)_$@.s
	cd $(BDIR)/$@ && $(OBJDUMP) -d $(BASE)_$@.elf > $(BASE)_$@.lss
	cd $(BDIR)/$@ && $(OBJCOPY) -j .data -j.text -O ihex   $(BASE)_$@.elf $(BASE)_$@.hex
	cd $(BDIR)/$@ && $(OBJCOPY) -j .data -j.text -O binary $(BASE)_$@.elf $(BASE)_$@.bin
	cd $(BDIR)/$@ && $(OBJCOPY) -j .data -j.text -O srec   $(BASE)_$@.elf $(BASE)_$@.srec

arch:
	tar cfz $(ARCH).tar.gz  $(BDIR)
	jar cfM $(ARCH).zip     $(BDIR)

clean:
	rm -rf $(BDIR) ./$(ARCH).tar.gz ./$(ARCH).zip
