# ----------------------------------------------------------------------------
#         ATMEL Microcontroller Software Support 
# ----------------------------------------------------------------------------
# Copyright (c) 2008, Atmel Corporation
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# - Redistributions of source code must retain the above copyright notice,
# this list of conditions and the disclaimer below.
#
# Atmel's name may not be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
# DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ----------------------------------------------------------------------------

# 	Makefile for compiling basic-emac-uip-project

#-------------------------------------------------------------------------------
#		User-modifiable options
#-------------------------------------------------------------------------------

# Chip & board used for compilation
# (can be overriden by adding CHIP=chip and BOARD=board to the command-line)
CHIP  = at91sam9g20
BOARD = at91sam9g20-ek

# Trace level
trace_LEVEL = 2

# Optimization level, put in comment for debugging
OPTIMIZATION = -Os

# AT91 library directory
AT91LIB = ../at91lib

# Compile for all memories available on the board (this sets $(MEMORIES))
include $(AT91LIB)/boards/$(BOARD)/board.mak

# Output file basename
OUTPUT = basic-emac-uip-$(UIP_APP)-$(BOARD)-$(CHIP)

# Output directories
BIN = bin
OBJ = obj

#-------------------------------------------------------------------------------
#		uIP configuration
#-------------------------------------------------------------------------------

# DHCP option
UIP_DHCP = off

# uIP sample application
UIP_APP = hello-world
#UIP_APP = webserver
#UIP_APP = smtp
#UIP_APP = telnetd
#UIP_APP = webserver
#UIP_APP = dhcpc
#UIP_APP = resolv
#UIP_APP = webclient

# uIP library directory
UIPLIB = ../external_libs/ethernet/uip

#-------------------------------------------------------------------------------
#		Tools
#-------------------------------------------------------------------------------

# Tool suffix when cross-compiling
CROSS = arm-elf-

# Compilation tools
CC = $(CROSS)gcc
SIZE = $(CROSS)size
STRIP = $(CROSS)strip
OBJCOPY = $(CROSS)objcopy

# Flags
INCLUDES = -I$(AT91LIB)/boards/$(BOARD) -I$(AT91LIB)/peripherals 
INCLUDES += -I$(AT91LIB)/components -I$(AT91LIB) -I.

CFLAGS = -Wall -mlong-calls -mstructure-size-boundary=8 -ffunction-sections
CFLAGS += -g $(OPTIMIZATION) $(INCLUDES) -D$(CHIP) -Dtrace_LEVEL=$(trace_LEVEL)
ASFLAGS = -g $(OPTIMIZATION) $(INCLUDES) -D$(CHIP) -D__ASSEMBLY__
LDFLAGS = -g $(OPTIMIZATION) -nostartfiles -Wl,--gc-sections

#-------------------------------------------------------------------------------
#		Files
#-------------------------------------------------------------------------------

# Directories where source files can be found
COMP = $(AT91LIB)/components
PERIPH = $(AT91LIB)/peripherals
BOARDS = $(AT91LIB)/boards
UTILITY = $(AT91LIB)/utility

VPATH += $(UTILITY)
VPATH += $(PERIPH)/dbgu $(PERIPH)/usart $(PERIPH)/pio $(PERIPH)/aic $(PERIPH)/tc
VPATH += $(PERIPH)/emac $(PERIPH)/rstc
VPATH += $(COMP)/ethernet/dm9161
VPATH += $(BOARDS)/$(BOARD) $(BOARDS)/$(BOARD)/$(CHIP)
VPATH += $(UIPLIB)/apps/$(UIP_APP)
INCLUDES += -I$(UIPLIB)/apps/$(UIP_APP) 
INCLUDES += -I$(UTILITY)

# Objects built from C source files
C_OBJECTS = stdio.o
C_OBJECTS += string.o
C_OBJECTS += board_memories.o board_lowlevel.o
C_OBJECTS += dbgu.o usart.o pio.o aic.o tc.o
C_OBJECTS += emac.o dm9161.o rstc.o
C_OBJECTS += main.o

# Objects built from Assembly source files
ASM_OBJECTS = board_cstartup.o

#-------------------------------------------------------------------------------
#		uIP support
#-------------------------------------------------------------------------------
VPATH += $(UIPLIB)/uip
INCLUDES += -I$(UIPLIB)/uip
C_OBJECTS += uip.o psock.o uip_arp.o
C_OBJECTS += timer.o
C_OBJECTS += tapdev.o
C_OBJECTS += clock-arch.o
#dhcpc path
ifeq ($(UIP_DHCP), on)
CFLAGS += -DUIP_DHCP_on
VPATH += $(UIPLIB)/apps/dhcpc
INCLUDES += -I$(UIPLIB)/apps/dhcpc
C_OBJECTS += dhcpc.o
endif
ifeq ($(UIP_APP), hello-world)
#problem with the minus caracter
CFLAGS += -DUIP_APP4SAM_helloworld
C_OBJECTS += hello-world.o
else
CFLAGS += -DUIP_APP4SAM_$(UIP_APP)
endif
ifeq ($(UIP_APP), webserver)
C_OBJECTS += httpd.o http-strings.o httpd-fs.o httpd-cgi.o
endif
ifeq ($(UIP_APP), telnetd)
VPATH += $(UIPLIB)/lib
INCLUDES += -I$(UIPLIB)/lib
C_OBJECTS += memb.o
C_OBJECTS += telnetd.o shell.o
endif
#-------------------------------------------------------------------------------
#		End uIP support
#-------------------------------------------------------------------------------

# Append OBJ and BIN directories to output filename
OUTPUT := $(BIN)/$(OUTPUT)

#-------------------------------------------------------------------------------
#		Rules
#-------------------------------------------------------------------------------

all: $(BIN) $(OBJ) $(MEMORIES)

$(BIN) $(OBJ):
	mkdir $@

define RULES
C_OBJECTS_$(1) = $(addprefix $(OBJ)/$(1)_, $(C_OBJECTS))
ASM_OBJECTS_$(1) = $(addprefix $(OBJ)/$(1)_, $(ASM_OBJECTS))

$(1): $$(ASM_OBJECTS_$(1)) $$(C_OBJECTS_$(1))
	$(CC) $(LDFLAGS) -T"$(AT91LIB)/boards/$(BOARD)/$(CHIP)/$$@.lds" -o $(OUTPUT)-$$@.elf $$^
	$(OBJCOPY) -O binary $(OUTPUT)-$$@.elf $(OUTPUT)-$$@.bin
	$(SIZE) $$^ $(OUTPUT)-$$@.elf

$$(C_OBJECTS_$(1)): $(OBJ)/$(1)_%.o: %.c Makefile
	$(CC) $(CFLAGS) -D$(1) -c -o $$@ $$<

$$(ASM_OBJECTS_$(1)): $(OBJ)/$(1)_%.o: %.S Makefile
	$(CC) $(ASFLAGS) -D$(1) -c -o $$@ $$<
endef

$(foreach MEMORY, $(MEMORIES), $(eval $(call RULES,$(MEMORY))))

clean:
	-rm -f $(OBJ)/*.o $(BIN)/*.bin $(BIN)/*.elf

