# WinARM template makefile 
# by Martin Thomas, Kaiserslautern, Germany 
# <eversmith@heizung-thomas.de>
#
# based on the WinAVR makefile written by Eric B. Weddington, Jrg Wunsch, et al.
# Released to the Public Domain
# Please read the make user manual!
#
#
# On command line:
#
# make all = Make software.
#
# make clean = Clean out built project files.
#
# To rebuild project do "make clean" then "make all".
#
# -     Jan. 2010  - CPV, adapted for Sceptre
#


# Define here if the generated code will be ARM or Thumb Interwork (iw).
ARM_MODE = arm
#ARM_MODE =

# Compiling everything in ARM mode makes code larger.
ifeq ($(ARM_MODE),arm)
THUMB    = 
THUMB_IW = 
# Target file name (without extension).
TARGET = sceptre
else 
THUMB    = -mthumb
THUMB_IW = -mthumb-interwork
# Target file name (without extension).
TARGET = sceptre-iw
endif

# MCU name and submodel
MCU      = arm7tdmi-s
SUBMDL   = LPC2138

# This is used by the startup code and the swi handler.
## Create ROM-Image (final)
RUN_MODE=ROM_RUN
## Create RAM-Image (debugging) - not used in this example
#RUN_MODE=RAM_RUN

# This is used by the startup code and the swi handler.
# not supported in this example:
## Exception-Vector placement only supported for "ROM_RUN"
## (placement settings ignored when using "RAM_RUN")
## - Exception vectors in ROM:
##VECTOR_LOCATION=VECTORS_IN_ROM
## - Exception vectors in RAM:
##VECTOR_LOCATION=VECTORS_IN_RAM


# List C source files here. (C dependencies are automatically generated.)
# use file-extension c for "c-only"-files
SRC =

# List C source files here which must be compiled in ARM-Mode.
# use file-extension c for "c-only"-files
#SRC += filesystem/fflushdir.c
#SRC += filesystem/drive.c 
#SRC += filesystem/ssp.c 
#SRC += filesystem/sdcard_driver.c 
#SRC += filesystem/sdcard_device.c 
SRC += filesystem/sd_spi_lpc2000.c 
#SRC += filesystem/sdcard_device_ff.c 
SRC += filesystem/fatfs_support.c 
SRC += filesystem/fatfs/ff.c 
SRC += newlib/device.c 
SRC += newlib/close_r.c 
SRC += newlib/fstat_r.c 
SRC += newlib/gettimeofday_r.c 
SRC += newlib/isatty.c
SRC += newlib/link_r.c
SRC += newlib/lseek_r.c 
SRC += newlib/open_r.c 
SRC += newlib/read_r.c 
SRC += newlib/sbrk_r.c 
SRC += newlib/time_r.c
SRC += newlib/unlink_r.c 
SRC += newlib/write_r.c 
SRC += system/target.c 
SRC += system/irq.c 
SRC += time/rtc.c 
SRC += time/timer.c 
SRC += uart/uart.c 
SRC += thermometer/ds18b20.c
SRC += thermometer/thermo_device.c
SRC += thermometer/thermometer.c
SRC += accelerometer/accelero.c
SRC += accelerometer/accelero_device.c
SRC += accelerometer/accelerometer.c
SRC += bluetooth/btm222.c
SRC += bluetooth/bluetooth.c
SRC += usb/usb_mass_storage.c
SRC += usb/msc_bot.c
SRC += usb/msc_scsi.c
SRC += usb/blockdev_sd.c
SRC += usb/lpcusb/target/usbhw_lpc.c
SRC += usb/lpcusb/target/usbcontrol.c
SRC += usb/lpcusb/target/usbstdreq.c
SRC += usb/lpcusb/target/usbinit.c

# List C++ source files here.
# use file-extension cpp for C++-files (use extension .cpp)
CPPSRC = 

# List C++ source files here which must be compiled in ARM-Mode.
# use file-extension cpp for C++-files (use extension .cpp)
CPPSRCARM = 

# List Assembler source files here.
# Make them always end in a capital .S.  Files ending in a lowercase .s
# will not be considered source files but generated files (assembler
# output from the compiler), and will be deleted upon "make clean"!
# Even though the DOS/Win* filesystem matches both .s and .S the same,
# it will preserve the spelling of the filenames, and gcc itself does
# care about how the name is spelled on its command-line.
ASRC = 

# List Assembler source files here which must be assembled in ARM-Mode..
#ASRCARM = system/Startup.S
#ASRCARM += system/swi_handler.S

# Optimization level, can be [0, 1, 2, 3, s]. 
# 0 = turn off optimization. s = optimize for size.
# (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
OPT = s
#OPT = 0

# Debugging format.
# Native formats for AVR-GCC's -g are stabs [default], or dwarf-2.
# AVR (extended) COFF requires stabs, plus an avr-objcopy run.
#DEBUG = stabs
#DEBUG = dwarf-2

# List any extra directories to look for include files here.
#     Each directory must be seperated by a space.
EXTRAINCDIRS = accelerometer
EXTRAINCDIRS += bluetooth
EXTRAINCDIRS += filesystem
EXTRAINCDIRS += filesystem/fatfs
EXTRAINCDIRS += newlib
EXTRAINCDIRS += system
EXTRAINCDIRS += thermometer
EXTRAINCDIRS += time
EXTRAINCDIRS += uart
EXTRAINCDIRS += usb
EXTRAINCDIRS += usb/lpcusb/target

# List any extra directories to look for library files here.
#     Each directory must be seperated by a space.
EXTRA_LIBDIRS = 


# Compiler flag to set the C Standard level.
# c89   - "ANSI" C
# gnu89 - c89 plus GCC extensions
# c99   - ISO C99 standard (not yet fully implemented)
# gnu99 - c99 plus GCC extensions
CSTANDARD = -std=gnu99

# Place -D or -U options for C here
CDEFS = -D$(RUN_MODE)

# This one is needed for lpcusb.
CDEFS += -DLPC214x
#CDEFS += -DDEBUG

# Place -I options here
CINCS =

# Place -D or -U options for ASM here
ADEFS = -D$(RUN_MODE)

ifdef VECTOR_LOCATION
CDEFS += -D$(VECTOR_LOCATION)
ADEFS += -D$(VECTOR_LOCATION)
endif


# Compiler flags.
#  -g*:          generate debugging information
#  -O*:          optimization level
#  -f...:        tuning, see GCC manual and avr-libc documentation
#  -Wall...:     warning level
#  -Wa,...:      tell GCC to pass this to the assembler.
#  -adhlns...: create assembler listing
#
# Flags for C and C++ (arm-elf-gcc/arm-elf-g++)
CFLAGS = -g$(DEBUG)
CFLAGS += $(CDEFS) $(CINCS)
CFLAGS += -O$(OPT)
CFLAGS += -Wall -Wcast-align -Wimplicit 
CFLAGS += -Wpointer-arith -Wswitch
CFLAGS += -Wredundant-decls -Wreturn-type -Wshadow -Wunused
CFLAGS += -Wa,-adhlns=$(subst $(suffix $<),.lst,$<) 
CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))

# flags only for C
CONLYFLAGS += -Wnested-externs 
CONLYFLAGS += $(CSTANDARD)

ifneq ($(AT91LIBNOWARN),yes)
#AT91-lib warnings with:
#CFLAGS += -Wcast-qual
CONLYFLAGS += -Wmissing-prototypes 
CONLYFLAGS += -Wstrict-prototypes
CONLYFLAGS += -Wmissing-declarations
endif

# flags only for C++ (arm-elf-g++)
# CPPFLAGS = -fno-rtti -fno-exceptions
CPPFLAGS = 

# Assembler flags.
#  -Wa,...:    tell GCC to pass this to the assembler.
#  -ahlns:     create listing
#  -g$(DEBUG): have the assembler create line number information
ASFLAGS = $(ADEFS) -Wa,-adhlns=$(<:.S=.lst),-g$(DEBUG)


#Additional libraries.

# Extra libraries
#    Each library-name must be seperated by a space.
#    To add libxyz.a, libabc.a and libefsl.a: 
#    EXTRA_LIBS = xyz abc efsl
EXTRA_LIBS =

#Support for newlibc-lpc (file: libnewlibc-lpc.a)
#NEWLIBLPC = -lnewlib-lpc

MATH_LIB = -lm

# CPLUSPLUS_LIB = -lstdc++


# Define programs and commands.
SHELL = sh
CC = arm-elf-gcc
CPP = arm-elf-g++
AR = arm-elf-ar
OBJCOPY = arm-elf-objcopy
OBJDUMP = arm-elf-objdump
SIZE = arm-elf-size
NM = arm-elf-nm
REMOVE = rm -f
COPY = cp


# Define all object files.
COBJ      = $(SRC:.c=.o) 
AOBJ      = $(ASRC:.S=.o)
COBJARM   = $(SRCARM:.c=.o)
AOBJARM   = $(ASRCARM:.S=.o)
CPPOBJ    = $(CPPSRC:.cpp=.o) 
CPPOBJARM = $(CPPSRCARM:.cpp=.o)
LIBOBJS = $(COBJ) $(AOBJ) $(COBJARM) $(AOBJARM) $(CPPOBJ) $(CPPOBJARM)

# Define all listing files.
LST = $(ASRC:.S=.lst) $(ASRCARM:.S=.lst) $(SRC:.c=.lst) $(SRCARM:.c=.lst)
LST += $(CPPSRC:.cpp=.lst) $(CPPSRCARM:.cpp=.lst)

# Compiler flags to generate dependency files.
### GENDEPFLAGS = -Wp,-M,-MP,-MT,$(*F).o,-MF,.dep/$(@F).d
GENDEPFLAGS = -MD -MP -MF .dep/$(@F).d

# Combine all necessary flags and optional flags.
# Add target processor to flags.
ALL_CFLAGS  = -mcpu=$(MCU) $(THUMB_IW) -I. $(CFLAGS) $(GENDEPFLAGS)
ALL_ASFLAGS = -mcpu=$(MCU) $(THUMB_IW) -I. -x assembler-with-cpp $(ASFLAGS)

# AR flags
ARFLAGS = -rcs


# Default target.
#all: begin lib toc
all: begin lib


# Let the user know what mode we are in.
begin:
	@echo
ifeq ($(ARM_MODE),arm)
	@echo Compiling in ARM mode.
else 
	@echo Compiling in THUMB mode.
endif
	@echo

# Display compiler version information.
gccversion : 
	@$(CC) --version

# build lib
lib: $(TARGET).a

$(TARGET).a: $(LIBOBJS)
	@echo
	@echo Building library: $(TARGET).a
	$(AR) $(ARFLAGS) $@ $^

# Use this rule to display the contents of the library
toc: $(TARGET).a
	$(AR) t $^

# Compile: create object files from C source files. ARM/Thumb
$(COBJ) : %.o : %.c
	@echo
	@echo "Compliling C file:" $<
# $(CC) -c -save-temps $(THUMB) $(ALL_CFLAGS) $(CONLYFLAGS) $< -o $@ 
	$(CC) -c $(THUMB) $(ALL_CFLAGS) $(CONLYFLAGS) $< -o $@ 

# Compile: create object files from C source files. ARM-only
$(COBJARM) : %.o : %.c
	@echo
	@echo "Compliling C-file (ARM only):" $<
	$(CC) -c $(ALL_CFLAGS) $(CONLYFLAGS) $< -o $@ 

# Compile: create object files from C++ source files. ARM/Thumb
$(CPPOBJ) : %.o : %.cpp
	@echo
	@echo "Compliling C++ file:" $<
	$(CPP) -c $(THUMB) $(ALL_CFLAGS) $(CPPFLAGS) $< -o $@ 

# Compile: create object files from C++ source files. ARM-only
$(CPPOBJARM) : %.o : %.cpp
	@echo
	@echo "Compliling C++ file (ARM only):" $<
	$(CPP) -c $(ALL_CFLAGS) $(CPPFLAGS) $< -o $@ 


# Compile: create assembler files from C source files. ARM/Thumb
## does not work - TODO - hints welcome
##$(COBJ) : %.s : %.c
##	$(CC) $(THUMB) -S $(ALL_CFLAGS) $< -o $@


# Assemble: create object files from assembler source files. ARM/Thumb
$(AOBJ) : %.o : %.S
	@echo
	@echo "Assembling S file:" $<
	$(CC) -c $(THUMB) $(ALL_ASFLAGS) $< -o $@


# Assemble: create object files from assembler source files. ARM-only
$(AOBJARM) : %.o : %.S
	@echo
	@echo "Assembling S file (ARM only):" $<
	$(CC) -c $(ALL_ASFLAGS) $< -o $@


# Target: clean project.
clean: clean_list

clean_list :
	$(REMOVE) $(TARGET).bak
	$(REMOVE) $(TARGET).hex
	$(REMOVE) $(TARGET).bin
	$(REMOVE) $(TARGET).obj
	$(REMOVE) $(TARGET).elf
	$(REMOVE) $(TARGET).map
	$(REMOVE) $(TARGET).obj
	$(REMOVE) $(TARGET).a90
	$(REMOVE) $(TARGET).sym
	$(REMOVE) $(TARGET).lnk
	$(REMOVE) $(TARGET).lss
	$(REMOVE) $(COBJ)
	$(REMOVE) $(CPPOBJ)
	$(REMOVE) $(AOBJ)
	$(REMOVE) $(COBJARM)
	$(REMOVE) $(CPPOBJARM)
	$(REMOVE) $(AOBJARM)
	$(REMOVE) $(LST)
	$(REMOVE) $(SRC:.c=.s)
	$(REMOVE) $(SRC:.c=.d)
	$(REMOVE) $(SRCARM:.c=.s)
	$(REMOVE) $(SRCARM:.c=.d)
	$(REMOVE) $(CPPSRC:.cpp=.s) 
	$(REMOVE) $(CPPSRC:.cpp=.d)
	$(REMOVE) $(CPPSRCARM:.cpp=.s) 
	$(REMOVE) $(CPPSRCARM:.cpp=.d)
	$(REMOVE) .dep/*


# Include the dependency files.
-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)


# Listing of phony targets.
.PHONY : all begin gccversion lib toc clean clean_list

