# Tools and directories
CC		= avr-gcc
AS		= avr-gcc -x assembler-with-cpp	
RM		= rm -f
RN		= mv
BIN		= avr-objcopy
INCDIR	= .
LIBDIR	= $(AVR)\avr\lib
SHELL   = $(AVR)\bin\sh.exe
FORMAT  = srec
############################################################################################
	
# CPU type
#MCU = atmega128
#MCU = atmega32
MCU = atmega644

#F_CPU = 4000000
#F_CPU = 6000000
#F_CPU = 8000000
#F_CPU = 11059200
F_CPU = 16000000
#F_CPU = 20000000

# Target
TRG	= main

# Assembler source files
#ASRC    = delay.s

# C-source files
# !!! Warning !!! Upercase/Lowercase letters in filenames have to be typed EXACTLY
SRC	= paul.c bitmap.c draw.c t6963.c savebmp.c savetext.c readbmp.c \
          ../dos.c ../fat.c ../dir.c ../find_x.c ../mmc_spi.c ../printf.c ../serial.c \
          $(TRG).c


# Libraries 
LIB = 

# Compiler flags
CPFLAGS	=  -g -Os -fregmove -fforce-addr -ffunction-sections -Wall -Wstrict-prototypes -Wa,-ahlms=$(<:.c=.lst)

# Assembler flags
ASFLAGS = -Wa,-gstabs

# Linker flags
LDFLAGS = -Wl,--defsym,__init_wdtcr__=0x07,--defsym,__init_mcucr__=0x80,-Map=$(TRG).map,--cref,-lm,--gc-sections


############################################################################################
############################################################################################

#define all project specific object files
	OBJ	= $(ASRC:.s=.o) $(SRC:.c=.o)
	CPFLAGS += -mmcu=$(MCU) -DF_CPU=$(F_CPU)
	ASFLAGS += -mmcu=$(MCU)
	LDFLAGS += -mmcu=$(MCU)

	  
#this defines the aims of the make process
all:	$(TRG).elf $(TRG).hex $(TRG).bin


#compile: instructions to create assembler and/or object files from C source
%o : %c 
	$(CC) -c $(CPFLAGS) -I$(INCDIR) $< -o $@

%s : %c
	$(CC) -S $(CPFLAGS) -I$(INCDIR) $< -o $@

#assemble: instructions to create object file from assembler files
%o : %s
	$(AS) -c $(ASFLAGS) -I$(INCDIR) $< -o $@


#link: instructions to create elf output file from object files
%elf: $(OBJ)
	$(CC) $(OBJ) $(LIB) $(LDFLAGS) -o $@

#create avrobj file from elf output file
%obj: %elf
	$(BIN) -O avrobj $< $@

#create bin (ihex, srec) file from elf output file
%rom: %elf
	$(BIN) -O $(FORMAT) $< $@

%hex: %elf
	$(BIN) -O ihex $< $@

%bin: %elf
	$(BIN) -O binary $< $@


	avr-size $(TRG).elf


#make instruction to delete created files
clean:
	$(RM) $(OBJ)
	$(RM) $(TRG).map
	$(RM) $(TRG).elf
	$(RM) $(TRG).obj
#	$(RM) $(TRG).hex
#	$(RM) $(TRG).bin
	$(RM) *.bak
	$(RM) *.lst
	$(RM) ../*.lst
	$(RM) *.?_sym

clean_before:
	$(RM) $(TRG).hex
	$(RM) $(TRG).bin
	$(RM) *.err

############################################################################################
############################################################################################

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

#$(TRG).o	: $(TRG).c types.h
