# GCC-AVR standard Makefile part 2
# Volker Oth (c) 1999


#set right cpu type
ifeq ($(MCU),-2313)
	CPFLAGS += -mmcu=at90s2313
        AVRMEGA = 0
else
ifeq ($(MCU),-2323)
	CPFLAGS += -mmcu=at90s2323
        AVRMEGA = 0
else
ifeq ($(MCU),-2333 )
	CPFLAGS += -mmcu=at90s2333
        AVRMEGA = 0
else
ifeq ($(MCU),-2343)
	CPFLAGS += -mmcu=at90s2343
        AVRMEGA = 0
else
ifeq ($(MCU),-4433)
	CPFLAGS += -mmcu=at90s4433
        AVRMEGA = 0
else
ifeq ($(MCU),-4414)
	CPFLAGS += -mmcu=at90s4414
        AVRMEGA = 0
else
ifeq ($(MCU),-4334)
	CPFLAGS += -mmcu=at90s4334
        AVRMEGA = 0
else
ifeq ($(MCU),-8515)
	CPFLAGS += -mmcu=at90s8515
        AVRMEGA = 0
else
ifeq ($(MCU),-8535)
	CPFLAGS += -mmcu=at90s8535
        AVRMEGA = 0
else
ifeq ($(MCU),-tiny22)
	CPFLAGS += -mmcu=attiny22
        AVRMEGA = 0
else
ifeq ($(MCU),-mega103)
	CPFLAGS	+= -minit-stack=0xfff -mmcu=atmega103
        AVRMEGA = 1
else
ifeq ($(MCU),-mega603)
	CPFLAGS	+= -minit-stack=0xfff -mmcu=atmega603
        AVRMEGA = 1
endif
endif
endif
endif
endif
endif
endif
endif
endif
endif
endif
endif

	LIB += $(LIBDIR)/crt1$(MCU).o $(LIBDIR)/crtn.o

ifeq ($(AVRMEGA),1)
	LIB += $(LIBDIR)/libgcc-mega.o $(LIBDIR)/libm-mega/fplib.o $(LIBDIR)/libc-mega/string.o
        LIB += $(LIBDIR)/libc-mega/malloc.o $(LIBDIR)/libc-mega/ctype.o $(LIBDIR)/libc-mega/eeprom.o
else
	LIB += $(LIBDIR)/libgcc.o $(LIBDIR)/libm/fplib.o $(LIBDIR)/libc/string.o
        LIB += $(LIBDIR)/libc/malloc.o $(LIBDIR)/libc/ctype.o $(LIBDIR)/libc/eeprom.o
endif

#define all project specific object files
	OBJ	= $(ASRC:.s=.o) $(SRC:.c=.o) 

		  
#this defines the aim of the make process
#all:	$(TRG).out $(TRG).rom
all:	$(TRG).rom

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

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


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


#link: instructions to create output files from object files
$(TRG).rom: $(OBJ)
	$(AS) $(LIB) $(OBJ) $(LDFLAGS) --multiple-output-files --motorola -I$(INCDIR) -o $(@:.rom=)
	$(RN) $(TRG).fla $(TRG).rom
	$(AS) $(LIB) $(OBJ) $(LDFLAGS) --multiple-output-files --intel -I$(INCDIR) -o $(TRG)
	$(RN) $(TRG).fla $(TRG).hex
#	$(AVR)/bin/goisp $(TRG).avr
	goisp $(TRG).avr

#link: instructions to create output file from object files
#$(TRG).out: $(OBJ)
#	$(AS) $(LIB) $(OBJ) $(LDFLAGS) -I$(INCDIR) -o $@


#make instructions to create ROM file from output
#%.rom : %.out $(OBJ)
#	$(BIN) $<


#make instruction to delete created files
clean:
	$(RM) $(OBJ)
	$(RM) $(SRC:.c=.s)
	$(RM) $(SRC:.c=.lst)
	$(RM) $(TRG).out 
	$(RM) $(TRG).rom
	$(RM) $(TRG).eep
	$(RM) $(TRG).lnk
	$(RM) *.bak
	$(RM) *.log