# 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 # ok
#MCU = atmega163 # did not work, why ?
#MCU = atmega323 # did not work, why ?
#MCU = atmega161  # ok
MCU = atmega168  # ok
#MCU = atmega8  # ok, but remove serial.c for this
#MCU = atmega32 # ok

#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
SRC	= playmp3.c buttons.c rc5.c vs1001.c display.c ../serial.c printf.c ../compact.c ../mmc_spi.c ../dir.c ../fat.c ../dos.c ../readraw.c ../find_x.c ../drivefree.c ../lcd.c $(TRG).c
#SRC	= playmp3.c buttons.c rc5.c vs1001.c display.c printf.c ../compact.c ../mmc_spi.c ../dir.c ../fat.c ../dos.c ../readraw.c ../find_x.c ../drivefree.c ../lcd.c $(TRG).c


# Libraries 
LIB = 

# Compiler flags
CPFLAGS	=  -g -Os -mcall-prologues -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,--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
