# 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

#OBJS   = fehler.o
#BIN    = fehler

#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

# 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 upload extcoff
#extcoff

#create ROM-Image
image: $(BIN)
	avr-size $(BIN).elf

#Upload
upload:
#	avrdude -pm8 -cstk200 -e -i main.hex

#asm
asm: $(ASMS)

#clean source directory
clean:
	rm *.o
	rm *.s
	rm *.bak

# Build rules:
%.o: %.c
	$(CC) $(CCFLAGS) $(ASTFLAG) -c $<
	
%.s: %.c
	$(CC) $(CCFLAGS) -S $(ASTFLAG) -c $<

$(BIN): $(OBJS)
	$(LD) $(LDFLAGS) -o$(BIN).elf $^
	avr-objcopy -O ihex $(BIN).elf $(BIN).hex
	
coff: $(BIN).elf
	$(COFFCONVERT) -O coff-avr $< $(BIN).cof

extcoff: $(BIN).elf
	$(COFFCONVERT) -O coff-ext-avr $< $(BIN).cof
