# ----------------------------------------------------------------------------
#         ATMEL Microcontroller Software Support  -  SDC  -
# ----------------------------------------------------------------------------
# Copyright (c) 2006, 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 disclaiimer below.
# 
# - Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the disclaimer below in the documentation and/or
# other materials provided with the distribution. 
# 
# 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-project

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

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

# Trace level
trace_LEVEL = 1

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

# AT91 library directory
AT91LIB = ../at91lib

# Output file basename
OUTPUT = basic-can-project-$(CHIP)

# Output directories
BIN = bin
OBJ = obj

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

CFLAGS = -Wall -mlong-calls -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
VPATH += $(PERIPH)/can $(PERIPH)/rstc
VPATH += $(BOARDS)/$(BOARD) $(BOARDS)/$(BOARD)/$(CHIP)
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 pio_it.o aic.o
C_OBJECTS += can.o
C_OBJECTS += main.o 

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

# Append OBJ and BIN directories to filenames
C_OBJECTS := $(addprefix $(OBJ)/, $(C_OBJECTS))
ASM_OBJECTS := $(addprefix $(OBJ)/, $(ASM_OBJECTS))
OUTPUT := $(BIN)/$(OUTPUT)

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

# Get the list of available targets from the board.mak file
include $(AT91LIB)/boards/$(BOARD)/board.mak

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

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

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

$(C_OBJECTS): $(OBJ)/%.o: %.c Makefile
	$(CC) $(CFLAGS) -c -o $@ $<

$(ASM_OBJECTS): $(OBJ)/%.o: %.S Makefile
	$(CC) $(ASFLAGS) -c -o $@ $<

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

cleanobj:
	-rm -f $(OBJ)/*.o

