# (C) Copyright 2003 by Bertolt Mildner.

# Define directories.
    DIRAVR = C:/Programme/WinAVR
    DIRAVRBIN = $(DIRAVR)/bin
    DIRAVRUTILS = $(DIRAVR)/utils/bin
    DIRINC = .
    DIRLIB = $(DIRAVR)/avr/lib


# Define programs.
    SHELL = sh
    COMPILE = avr-gcc
    ASSEMBLE = avr-gcc -x assembler-with-cpp
    REMOVE = rm -f
    COPY = cp
    OBJCOPY	= avr-objcopy
    OBJDUMP = avr-objdump
    ELFCOFF = objtool
    HEXSIZE = @avr-size --target=ihex $(TARGET).hex
    ELFSIZE = @avr-size $(TARGET).elf
    FINISH = @echo Errors: none
    BEGIN = @echo -------- begin --------
    END = @echo --------  end  --------
    PROG = c:/programme/ponyprog2000/ponyprog2000.exe


# MCU name
  MCU = at90s8515

# MCU clock rate
# MCU_CLK = 4000000 # 4MHz
  MCU_CLK = 7372800 # 7.3728MHz


# Compiler flags.
    CPFLAGS = -DMCU_CLK=$(MCU_CLK) -g -Os -funsigned-char -fno-exceptions -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Wa,-ahlms=$(<:.c=.lst)

# Assembler flags.
    ASFLAGS = -Wa,-ahlms=$(<:.s=.lst), -gstabs

# Linker flags (passed via GCC).
	LDFLAGS = -Wl,-Map=$(TARGET).map,--cref

# Additional library flags (-lm = math library).
	LIBFLAGS = -lm

# Target file name (without extension).
	TARGET = OpenCOS

# List main C source files here.
	SRC_MAIN	= $(TARGET).c

# List I2C module C source files here.
	SRC_I2C	= I2C/I2C.c


# Complete C source list
  SRC = $(SRC_MAIN) $(SRC_I2C)

# List Assembler source files here.
	ASRC =


# Define all project specific object files.
	OBJ	= $(SRC:.c=.o) $(ASRC:.s=.o)

# Define all listing files.
	LST = $(ASRC:.s=.lst) $(SRC:.c=.lst)

# Add target processor to flags.
  CPFLAGS += -mmcu=$(MCU)
	ASFLAGS += -mmcu=$(MCU)
	LDFLAGS += -mmcu=$(MCU)


# Default target.
.PHONY : all
all: begin gccversion sizebefore $(TARGET).elf $(TARGET).hex $(TARGET).bin $(TARGET).eep.hex $(TARGET).lss $(TARGET).cof sizeafter finished end

.PHONY : allprog
allprog: begin all prog end

# Eye candy.
.PHONY : begin
begin:
	$(BEGIN)

.PHONY : finish
finished:
    $(FINISH)

.PHONY : end
end:
	$(END)


# Display size of file.
.PHONY : sizebefore
sizebefore:
	@echo Size before:
	-$(HEXSIZE)
#	-$(ELFSIZE)

.PHONY : sizeafter
sizeafter:
	@echo Size after:
	$(HEXSIZE)
#	$(ELFSIZE)

# Display compiler version information.
.PHONY : gccversion
gccversion :
	$(COMPILE) --version


# Create AVROBJ format file from ELF output file. (Future release)
#%.obj: %.elf
#	$(OBJCOPY) -O avrobj -R .eeprom $< $@


# Create COFF format file from ELF output file.
%.cof: %.elf
	$(ELFCOFF) loadelf $< mapfile $*.map writecof $@


# Create final output files (.hex, .eep) from ELF output file.
%.hex: %.elf
	$(OBJCOPY) -O ihex -R .eeprom $< $@

# Create final output file (.bin) from ELF output file.
%.bin: %.elf
	$(OBJCOPY) -O binary -R .eeprom $< $@

%.eep.hex: %.elf
	-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 -O ihex $< $@

# Create extended listing file from ELF output file.
%.lss: %.elf
	$(OBJDUMP) -h -S $< > $@



# Link: create ELF output file from object files.
.SECONDARY : $(TARGET).elf
.PRECIOUS : $(OBJ)
%.elf: $(OBJ)
	$(COMPILE) $(LDFLAGS) $(OBJ) $(LIBFLAGS) --output $@


# Compile: create object files from C source files.
%.o : %.c
	$(COMPILE) -c $(CPFLAGS) -I$(DIRINC) $< -o $@

# Assemble: create object files from assembler files.
%.o : %.s
	$(ASSEMBLE) -c $(ASFLAGS) -I$(DIRINC) $< -o $@

# Target: clean project.
.PHONY : clean
clean: begin clean_list finished end

.PHONY : clean_list
clean_list :
	$(REMOVE) $(TARGET).hex
	$(REMOVE) $(TARGET).eep
	$(REMOVE) $(TARGET).obj
	$(REMOVE) $(TARGET).cof
	$(REMOVE) $(TARGET).elf
	$(REMOVE) $(TARGET).map
	$(REMOVE) $(TARGET).obj
	$(REMOVE) $(TARGET).a90
	$(REMOVE) $(TARGET).sym
	$(REMOVE) $(TARGET).lnk
	$(REMOVE) $(TARGET).lss
	$(REMOVE) $(OBJ)
	$(REMOVE) $(LST)
	$(REMOVE) $(SRC:.c=.s)

# Target: programm target.
.PHONY : prog
prog: begin program end

.PHONY : program
program :
	echo -e "SELECTDEVICE $(MCU)\n CLEARBUFFER \n LOAD-PROG $(TARGET).hex\n ERASE-ALL\n WRITE&VERIFY-PROG" >isp.e2s
	$(PROG) isp.e2s
	$(RM) isp.e2s


# Automatically generate C source code dependencies. (Code taken from the GNU make user manual.)
# Note that this will work with sh (bash) and sed that is shipped with WinAVR (see the SHELL variable defined above).
# This may not work with other shells or other seds.
%.d: %.c
	set -e; $(COMPILE) -MM $(CPFLAGS) $< \
	| sed 's/\($*\)\.o[ :]*/\1.o $@ : /g' > $@; \
	[ -s $@ ] || rm -f $@

include $(SRC:.c=.d)

# List assembly only source file dependencies here:


