###############################################################################
# Makefile for the project AIO
###############################################################################

## General Flags
PROJECT = AIO
MCU = atmega128
TARGET = AIO.elf
CC = avr-gcc.exe

## Options common to compile, link and assembly rules
COMMON = -mmcu=$(MCU)

## Compile options common for all C compilation units.
CFLAGS = $(COMMON)
CFLAGS += -Wall -gdwarf-2  -g -DF_CPU=14745600UL -O0 -fsigned-char -lm
CFLAGS += -MD -MP -MT $(*F).o -MF dep/$(@F).d 

## Assembly specific flags
ASMFLAGS = $(COMMON)
ASMFLAGS += $(CFLAGS)
ASMFLAGS += -x assembler-with-cpp -Wa,-gdwarf2

# external RAM, starting after internal RAM (ATmega128!),
# used for variables (.data/.bss) and heap (malloc()).
EXTMEMOPTS = -Wl,--section-start=.data=0x801100,--defsym=__heap_start=0x801100,--defsym=__heap_end=0x807fff

# 64 KB of external RAM, starting after internal RAM (ATmega128!),
# only used for heap (malloc()).
#EXTMEMOPTS=-Wl,--defsym=__malloc_heap_start=0x801100,--defsym=__malloc_heap_end=0x80ffff

#---------------- Linker Options ----------------
#  -Wl,...:     tell GCC to pass this to linker.
#    -Map:      create map file
#    --cref:    add cross reference to  map file
#LDFLAGS = $(COMMON)
#LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
LDFLAGS+=$(EXTMEMOPTS)
#LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
## Linker flags
LDFLAGS = $(COMMON)
LDFLAGS +=  -Wl,-Map=AIO.map
#LDFLAGS += -Wl,-section-start=__heap_start=0x801100
#LDFLAGS += -Wl,-section-start=__heap_end=0x802fff
#LDFLAGS += -Wl,-section-start=__uppermem=0x803000


## Intel Hex file production flags
HEX_FLASH_FLAGS = -R .eeprom

HEX_EEPROM_FLAGS = -j .eeprom
HEX_EEPROM_FLAGS += --set-section-flags=.eeprom="alloc,load"
HEX_EEPROM_FLAGS += --change-section-lma .eeprom=0 --no-change-warnings


## Include Directories
INCLUDES = -I"C:\Documents and Settings\finck\Desktop\BA\AIO\.." -I"C:\Documents and Settings\finck\Desktop\BA\AIO\..\avrx" 

## Library Directories
LIBDIRS = -L"C:\Documents and Settings\finck\Desktop\BA\AIO\..\avrx" 

## Libraries
LIBS = -lavrx 

## Objects that must be built in order to link
OBJECTS = avrx_main.o telemetry.o public_interface.o ins_accel_lis3l02as4.o ins_gyro_adis16100.o ins_main.o ins_math.o locks.o logger.o AvrXBufferedSerial.o console.o xram.o watchdog.o lanc111.o thebrain.o regler.o sd20.o mmc.o kompass.o sd_main.o absdruck.o diffdruck.o ultraschall.o twi.o destructor.o input_functions.o main.o operator_functions.o output_functions.o parser.o process.o transfer.o adwandlung.o behaviour.o clock.o lightsensor.o twi3.o echo.o camera.o AvrXFifo.o AvrXKernel.o 

## Objects explicitly added by the user
LINKONLYOBJECTS = 

## Build
all: $(TARGET) AIO.hex AIO.eep AIO.lss size

ASRC= ../xram.S

## Compile
xram.o: ../xram.S
	$(CC) $(INCLUDES) $(ASMFLAGS) -c  $<

avrx_main.o: ../avrx_main.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

telemetry.o: ../../comm/telemetry.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

public_interface.o: ../../navigation/public_interface.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

ins_accel_lis3l02as4.o: ../../navigation/ins_accel_lis3l02as4.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

ins_gyro_adis16100.o: ../../navigation/ins_gyro_adis16100.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

ins_main.o: ../../navigation/ins_main.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

ins_math.o: ../../navigation/ins_math.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

locks.o: ../../lib/locks.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

logger.o: ../../lib/logger.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

AvrXBufferedSerial.o: ../../uart/AvrXBufferedSerial.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

console.o: ../../comm/console.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

watchdog.o: ../watchdog.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

lanc111.o: ../lanc111.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

thebrain.o: ../../thebrain/thebrain.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

regler.o: ../../thebrain/regler.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

sd20.o: ../../sd20/sd20.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

mmc.o: ../../sdkarte/mmc.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

kompass.o: ../../kompass/kompass.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

sd_main.o: ../../sdkarte/sd_main.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

absdruck.o: ../../absdruck/absdruck.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

diffdruck.o: ../../diffdruck/diffdruck.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

ultraschall.o: ../../ultraschall/ultraschall.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

twi.o: ../../lib/twi.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

destructor.o: ../../Core/destructor.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

input_functions.o: ../../Core/input_functions.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

main.o: ../../Core/main.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

operator_functions.o: ../../Core/operator_functions.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

output_functions.o: ../../Core/output_functions.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

parser.o: ../../Core/parser.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

process.o: ../../Core/process.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

transfer.o: ../../Core/transfer.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

adwandlung.o: ../../ad-wandlung/adwandlung.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

behaviour.o: ../../behaviour/behaviour.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

clock.o: ../../clock/clock.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

lightsensor.o: ../../lightsensor/lightsensor.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

twi3.o: ../../lib/twi3.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

echo.o: ../../Echo/Echo/echo.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

camera.o: ../../camera/camera.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

AvrXFifo.o: ../../avrx/AvrXFifo.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

AvrXKernel.o: ../../avrx/AvrXKernel.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

##Link
$(TARGET): $(OBJECTS)
	 $(CC) $(LDFLAGS) $(OBJECTS) $(LINKONLYOBJECTS) $(LIBDIRS) $(LIBS) -o $(TARGET)

%.hex: $(TARGET)
	avr-objcopy -O ihex $(HEX_FLASH_FLAGS)  $< $@

%.eep: $(TARGET)
	-avr-objcopy $(HEX_EEPROM_FLAGS) -O ihex $< $@ || exit 0

%.lss: $(TARGET)
	avr-objdump -h -S $< > $@

size: ${TARGET}
	@echo
	@avr-size -C --mcu=${MCU} ${TARGET}

## Clean target
.PHONY: clean
clean:
	-rm -rf $(OBJECTS) AIO.elf dep/* AIO.hex AIO.eep AIO.lss AIO.map


## Other dependencies
-include $(shell mkdir dep 2>/dev/null) $(wildcard dep/*)

