# Project configuration:

OBJS  = main.o rs232.o adc.o i2c.o printer.o gfx.o eeprom.o
ASMS  = main.s rs232.s adc.s i2c.s printer.s gfx.s eeprom.s
BIN   = main

#Hardware
MCU     = atmega8515
ROM_START = 0x000

#Compiler flags
CCFLAGS = -g  -Os -Wall -Wstrict-prototypes -mmcu=$(MCU) -mcall-prologues -fomit-frame-pointer 
#-S für ASM-Output

#Linker flags
LDFLAGS = -Wl,-Map=$(BIN).map,--cref,--section-start=.text=$(ROM_START) -mmcu=$(MCU) 

# Assembler config
#ASTFLAG = -gstabs,-ahlms=$(<:.c=.lst)

# Compiler configuration:
CC      = avr-gcc
LD      = avr-gcc
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
UPLOAD  = avrdude

# Convert ELF to COFF for use in debugging / simulating in
# AVR Studio or VMLAB.
COFFCONVERT=$(OBJCOPY) --debugging \
	--change-section-address .data-0x800000 \
	--change-section-address .bss-0x800000 \
	--change-section-address .noinit-0x800000 \
	--change-section-address .eeprom-0x810000 

#Do all
all: image extcoff
#extcoff

#Upload
upload: $(BIN).hex
	$(UPLOAD) -p$(MCU) -cstk200 -e -U flash:w:$(BIN).hex -U eeprom:w:$(BIN)_eeprom.hex

#create ROM-Image
image: $(BIN)
	avr-size $(BIN).elf

#asm
asm: $(ASMS)

#clean source directory
clean:
	rm *.o *.bak *.map *.hex *.cof *.elf *.lst
	rm *.s	

#create docs
doc:
	doxygen doxy.dxy

# Build rules:
%.o: %.c
	$(CC) $(CCFLAGS) $(ASTFLAG) -c $<
	
%.s: %.c
	$(CC) $(CCFLAGS) -S $(ASTFLAG) -c $<
		
$(BIN): $(OBJS)
	$(LD) $(LDFLAGS) -o$(BIN).elf $^
	$(OBJCOPY) -O ihex -R .eeprom $(BIN).elf $(BIN).hex
	$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 -O ihex $(BIN).elf $(BIN)_eeprom.hex
	$(OBJDUMP) -t -h -S $(BIN).elf >$(BIN).lst
	
coff: $(BIN).elf
	$(COFFCONVERT) -O coff-avr $< $(BIN).cof

extcoff: $(BIN).elf
	$(COFFCONVERT) -O coff-ext-avr $< $(BIN).cof
	