# -*- mode:makefile; -*-
# Makefile-OP03 : make file for the Balancer robot control software for the Olimex LPC-P2103 target
# Copyright (c) 2005-2007 Garth Zeglin

# This file is part of ArtLPC. 

# ArtLPC is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# ArtLPC is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with ArtLPC; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

# selective forms of vpath
vpath %.c ../libstd ../libLPC2xxx
vpath %.h ../libstd ../libLPC2xxx
vpath %.S ../libstd ../libLPC2xxx

# LIBOBJDIR is used by the library makefile fragments
OBJDIR    = OP03
LIBOBJDIR = $(OBJDIR)

################################################################
# define the application sources and targets
PROGNAMES = balancer
C_SRCS    = main.c timer_interrupt.c encoder_counter.c imu.c sabertooth.c

PROGS      = $(PROGNAMES:%=$(OBJDIR)/%.hex) # the executable targets
OBJS       = $(C_SRCS:%.c=$(OBJDIR)/%.o)

INCLUDE_PATHS= -I. -I../libstd -I../libLPC2xxx

# If Subversion is installed, this can create a symbol with the current code revision.
# VERSIONFLAGS := -D'SVN_REVISION="$(shell svnversion -n .)"'
VERSIONFLAGS=-D'SVN_REVISION="public"'

# -Os 		  optimize for size, similar to -O2
# -O3 	 	  enable automatic inlining, etc.
# -mcpu=arm7tdmi  this should be the compiler default as-configured
# -Wa,-mapcs-32   selects the procedure calling convention (should be default).
CFLAGS  = -DTARGET_OP03 $(VERSIONFLAGS) -g $(INCLUDE_PATHS) -c -Os -Wall

# assembler options (given to gcc):
#   -Wa,-mapcs-32        selects the procedure calling convention (should be default).
#   -Wa,-ahls=main.lst   generates a listing file
AFLAGS  = -c -g

# linker options:
# -u   declare an undefined symbol to trigger linking of otherwise unreferenced code
LDFLAGS  =  -g  -nostartfiles -T ../libLPC2xxx/LPC2103-flash.lnk -nostdlib -u _reset_vector
LDLIBS   = -L$(OBJDIR) -lLPC2xxx -lstd -lLPC2xxx -lgcc
LIBS     = $(OBJDIR)/libstd.a $(OBJDIR)/libLPC2xxx.a

# Uncomment the following lines to strip individual functions out of
# the linked image.  This takes marginally longer to compile, but
# produces the smallest possible memory image by eliminating individual
# unneeded functions.
LDFLAGS  +=  --gc-sections
CFLAGS   +=  -ffunction-sections -fdata-sections

# configure the included makefiles:
LIBSTD_INCLUDE_UNIX_SOURCE = false
RANLIB  = arm-elf-ranlib

################################################################
default: $(OBJDIR) $(LIBS) $(PROGS)

include ../libstd/libstd-public-makefile
include ../libLPC2xxx/OP03-makefile

################################################################
# the main target gets a special entry so cpp will put the full path into the version string

$(OBJDIR)/main.o : main.c
	$(CC) -o $@ $(CFLAGS) ${PWD}/main.c

################################################################
.SUFFIXES : .o .c .S
.PHONY: clean depend
.PRECIOUS: $(OBJDIR)/%.out

CC      = arm-elf-gcc
LD      = arm-elf-ld -v
AR      = arm-elf-ar
AS      = arm-elf-as
OBJCOPY = arm-elf-objcopy
OBJDUMP = arm-elf-objdump

$(OBJDIR)/%.o : %.c
	$(CC) -o $@ $(CFLAGS) $< 

$(OBJDIR)/%.o : %.S
	$(CC) -o $@ $(AFLAGS) $<

$(OBJDIR)/%.out: $(OBJS) $(LIBS) 
	$(LD) $(LDFLAGS) -o $@ -Map $(OBJDIR)/$*.map $^ $(LDLIBS)

$(OBJDIR)/%.hex : $(OBJDIR)/%.out
	$(OBJCOPY) -O ihex $< $@

# create the object directory if needed
$(OBJDIR):
	mkdir $@

################################################################
# disassembly options for testing
$(OBJDIR)/%.disasm : $(OBJDIR)/%.o
	$(OBJDUMP) --disassemble-all $< >$@

$(OBJDIR)/%.disasm : $(OBJDIR)/%.out
	$(OBJDUMP) --disassemble-all $< >$@

# rule to generate assembly, useful for testing
$(OBJDIR)/%.S : %.c
	$(CC) -o $@ $(CFLAGS) -S $<

################################################################
clean:
	-rm $(LIBS) $(PROGS) $(OBJDIR)/*.o $(OBJDIR)/*.out \
	$(OBJDIR)/*.map $(OBJDIR)/*.disasm $(OBJDIR)/*.lst $(OBJDIR)/*.bin \
	$(OBJDIR)/.gdb_history Makefile-OP03-deps.bak Makefile-OP03-deps-tmp


################################################################
# The dependencies need a little post-processing so they are kept in a separate file.
DEPENDS = Makefile-OP03-deps

depend:
	makedepend -f$(DEPENDS) -Y $(INCLUDE_PATHS) -p$(OBJDIR)/ -- \
	$(C_SRCS) \
	$(NANOSCHEME_SRCS:%=../nanoscheme/%) \
	$(LIBSTD_SRCS:%=../libstd/%) \
	$(LPC2XXX_ASM_SRCS:%=../libLPC2xxx/%) \
	$(LPC2XXX_C_SRCS:%=../libLPC2xxx/%)
	mv $(DEPENDS) $(DEPENDS)-tmp
	sed <$(DEPENDS)-tmp -e 's|OP03/../nanoscheme/|OP03/|' | \
	sed -e 's|OP03/../libstd/|OP03/|' | \
	sed -e 's|OP03/../libLPC2xxx/|OP03/|' >$(DEPENDS)

include $(DEPENDS)

################################################################
