###############################################################################
# Makefile for the project AVRserver
###############################################################################

## General Flags
PROJECT = AVRserver
MCU = atmega128
TARGET = AVRserver.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 -std=gnu99 -DF_OSC=F_CPU -D__ARTHERNET__                                -DF_CPU=16000000UL -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wl,--defsym=__head_start=0x801100,--defsym=__heap_end=0x809100
CFLAGS += -MD -MP -MT $(*F).o -MF dep/$(@F).d 

## Assembly specific flags
ASMFLAGS = $(COMMON)
ASMFLAGS += $(CFLAGS)
ASMFLAGS += -x assembler-with-cpp -Wa,-gdwarf2

## Linker flags
LDFLAGS = $(COMMON)
LDFLAGS +=  -Wl,-Map=AVRserver.map


## 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


## Objects that must be built in order to link
OBJECTS = app.o ax88796.o cpld.o crc8.o delay.o extmem.o hw_init.o main.o nic.o rtl8019.o serial.o uip.o uip_arch.o uip_arp.o fpga_serial.o buffer.o webclient.o rawtransmission.o direct_tests.o 

## Objects explicitly added by the user
LINKONLYOBJECTS = 

## Build
all: $(TARGET) AVRserver.hex AVRserver.eep AVRserver.lss size

## Compile
app.o: ../app.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

ax88796.o: ../ax88796.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

cpld.o: ../cpld.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

crc8.o: ../crc8.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

delay.o: ../delay.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

extmem.o: ../extmem.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

hw_init.o: ../hw_init.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

main.o: ../main.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

nic.o: ../nic.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

rtl8019.o: ../rtl8019.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

serial.o: ../serial.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

uip.o: ../uip.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

uip_arch.o: ../uip_arch.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

uip_arp.o: ../uip_arp.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

fpga_serial.o: ../fpga_serial.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

buffer.o: ../buffer.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

webclient.o: ../webclient.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

rawtransmission.o: ../rawtransmission.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

direct_tests.o: ../direct_tests.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) AVRserver.elf dep/* AVRserver.hex AVRserver.eep AVRserver.lss AVRserver.map


## Other dependencies
-include $(shell mkdir dep 2>/dev/null) $(wildcard dep/*)

