# makfile configuration
NAME       = project
CPU        = atmega32
#F_CPU     = 11059200
F_CPU      =  3500000
#         2..4MHz (     )
OBJECTS    = main.o speaker2.o uart_out.o

INCLUDEDIR = -I C:\avr\gcc\AVR\include


OPTIMIZE   = s -fno-gcse 
OPTIMIZE   += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
#OPTIMIZE  += -Wstrict-prototypes -fno-exceptions -mno-tablejump
#OPTIMIZE  += -mtiny-stack

ASFLAGS    = -mmcu=${CPU} -x assembler-with-cpp -D_GNU_ASSEMBLER_ -c 
CFLAGS     = -mmcu=${CPU} -O${OPTIMIZE} -Wall -g 
CFLAGS    +=-DF_CPU=${F_CPU}UL
CFLAGS    += -std=gnu99
CFLAGS    += $(INCLUDEDIR)



#+8KB extSRAM for mega8515
#EXTMEMOPTS = -Wl,--section-start,.data=0x800260,--defsym=__heap_end=0x80225f
#EXTMEMOPTS =  -Wl,-Tdata,0x801100
#EXTMEMOPTS  =
LDFLAGS     = -Wl,-Map=$(NAME).map
LDFLAGS    += $(EXTMEMOPTS)
#LDFLAGS   += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)



#switch the compiler (for the internal make rules)
CC              = avr-gcc
AS              = avr-gcc

#OBJDIR = .\OUT

.PHONY: all FORCE clean download download-jtag download-bsl dist
#all should be the first target. it's built when make is runwithout args
all: ${NAME}.elf ${NAME}.hex ${NAME}.lst

#confgigure the next line if you want to use the serial download
download: download-jtag
#download: download-bsl

#additional rules for files
${NAME}.elf: ${OBJECTS}
	@echo -----[project]-----
	${CC} -mmcu=${CPU} -o $@ -O${OPTIMIZE} ${OBJECTS} ${LDFLAGS}
	@echo -------------------

${NAME}.hex: ${NAME}.elf
	avr-objcopy -O ihex $^ $@

${NAME}.lst: ${NAME}.elf
	avr-objdump -dSwxz $^ >$@
#	avr-size -x -t ${OBJECTS} -B >mod_size.lst
	avr-size -A -x ${OBJECTS}  >mod_size.lst

download-jtag: all
	avr-jtag -e ${NAME}.elf

download-bsl: all
	avr-bsl -e ${NAME}.elf

clean:
	rm -f ${NAME}.elf ${NAME}.o ${NAME}.lst ${OBJECTS}

#backup archive
dist:
	tar czf dist.tgz *.c *.h *.txt makefile

#dummy target as dependecy if something has to be build everytime
FORCE:

#project dependencies
#hard.h:     hard_io.h makefile
#hard.c:     hard.h hard_io.h makefile

main.o:      makefile main.c 
speaker2.o:   makefile speaker2.c
uart_out.o:  makefile uart_out.c