# Simple Makefile
# Volker Oth (c) 1999
# GCC-AVR standard Makefile part 1
# Volker Oth 1/2000

##### install directory PATH
#AVR = c:/WinAVR-20071221
AVR = c:/WinAVR


###### define some variables based on the AVR base path in $(AVR) #######
 CC  = avr-gcc
 AS  = avr-gcc -x assembler-with-cpp 
 RM  = rm -f
 RN  = mv
 BIN  = avr-objcopy
 OBJDUMP = avr-objdump
 INCDIR = .
 LIBDIR = $(AVR)/avr/lib
    SHELL   = $(AVR)/bin/sh.exe

###### Pony Prog ########################################################
 PONYPROG = "c:/Program Files/ponyprog2000/ponyprog2000.exe"
# PONYPROG = "L:/slee/Lecture/2008 1/마이크로프로세서및실험/실험 2008/PonyProg2000/ponyprog2000.exe"

###### output format can be srec, ihex (avrobj is always created) #######
# FORMAT = srec
 FORMAT = ihex

####################### default compiler flags ##########################
 CPFLAGS = -g -O3 -Wall -Wstrict-prototypes -Wa,-ahlms=$(<:.c=.lst)

####################### default assembler flags #########################
 ASFLAGS = -Wa,-gstabs

########################## default linker flags #########################
 LDFLAGS = -Wl,-Map=$(TRG).map,--cref

##########################################################################
########### change this lines according to your project ##################
 
#put the name of the target mcu here (at90s8515, at90s8535, attiny22, atmega603 etc.)
 MCU = atmega128

#put the name of the target file here (without extension)
 TRG = main
 FORMAT = srec
 MYLIB = .

#put your C sourcefiles here 
 SRC = $(TRG).c \
#   led.c
#   $(MYLIB)/uart0.c \
#   $(MYLIB)/uart1.c \
#   $(MYLIB)/utils.c
##########################################################################
##########################################################################


#put additional assembler source file here
 ASRC =

#additional libraries and object files to link
 LIB = 

#additional includes to compile
 INC = 

#INCDIR means .h file search path
 INCDIR = . -I$(MYLIB)

#compiler flags
 CPFLAGS = -g -O3 -Wall -Wstrict-prototypes -Wa,-ahlms=$(<:.c=.lst)

#linker flags
 LDFLAGS = -Wl,-Map=$(TRG).map,--cref

########### you should not need to change the following line #############
# GCC-AVR standard Makefile part 2
# Volker Oth 1/2000

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

#this defines the aims of the make process
#all: $(TRG).obj $(TRG).elf $(TRG).rom $(TRG).eep
all: $(TRG).elf $(TRG).lis $(TRG).rom $(TRG).eep $(TRG).siz

#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) $< $@

%eep: %elf
 $(BIN) -j .eeprom --change-section-lma .eeprom=0 -O $(FORMAT) $< $@
# $(BIN) -j .eeprom --set-section-flags=.eeprom="alloc,load" -O $(FORMAT) $< $@

%lis: %elf
 $(OBJDUMP) -h -S $< > $@

%lis: %o
 $(OBJDUMP) -h -S $< > $@

#make size information file
size: $(TRG).siz
 @cat $(TRG).siz

$(TRG).siz : $(TRG).elf
 avr-size --format=sysv $(TRG).elf > $(TRG).siz
 @cat $(TRG).siz

#make instruction to delete created files
clean:
 $(RM) $(OBJ)
 $(RM) $(SRC:.c=.s)
 $(RM) $(SRC:.c=.lst)
 $(RM) $(SRC:.c=.lis)
 $(RM) $(TRG).elf $(TRG).obj $(TRG).map
 $(RM) $(TRG).rom $(TRG).eep
 $(RM) *.bak *.log $(TRG).siz
 $(RM) _reset.e2s _prog.e2s

_reset.e2s:
 echo -e "SELECTDEVICE $(MCU)"  >  _reset.e2s
 echo -e "RESET"    >> _reset.e2s

_prog.e2s: $(TRG).rom
 echo -e "SELECTDEVICE $(MCU)"  >  _prog.e2s
 echo -e "LOAD-PROG $(TRG).rom"  >> _prog.e2s
 echo -e "ERASE-ALL"   >>  _prog.e2s
 echo -e "WRITE-PROG"    >> _prog.e2s

program: _prog.e2s
 $(PONYPROG) _prog.e2s

reset: _reset.e2s
 $(PONYPROG) _reset.e2s
