# Tools
TOOLTARGET      = arm-elf
AR              = $(TOOLTARGET)-ar
AS              = $(TOOLTARGET)-gcc
AWK             = gawk
CC              = $(TOOLTARGET)-gcc
LD              = $(TOOLTARGET)-gcc
OBJCOPY         = $(TOOLTARGET)-objcopy
RANLIB          = $(TOOLTARGET)-ranlib
RM              = rm -f
SED             = sed
SHELL           = bash --posix
SIZE            = $(TOOLTARGET)-size
STRIP           = $(TOOLTARGET)-strip
DOWNLOAD        = lpc21isp.exe

# Name of target (executable program or library) 
NAME    = demo
TARGET  = $(NAME).hex

CPU       = arm7tdmi
CPU_VARIANT = LPC2104

# List C source files here.
CSRCS   = main.c           \
          hw.c

# List assembler source files here
ASRCS   = 

# List object files
OBJS ?= $(CSRCS:.c=.o) $(ASRCS:.S=.o)

# List subdirectories to recursively invoke make in 
SUBDIRS = 

# List additional libraries to link with
LIBS    =  
#          ../gcc_files/libm.a 

# Add include search path for startup files, and other include directories
INC     = 

# Output format on hex file (if making a program); can be [srec | ihex]
HEX_FORMAT  = ihex

# Configurations for download program
# Which com-pot that is used, which download speed and what crystal frequency on the board.
DL_COMPORT  = com3
DL_BAUDRATE = 115200
DL_CRYSTAL  = 14746

# Compiler waring settings
WFLAGS    = -Wall -Wcast-align -Wcast-qual -Wimplicit \
            -Wnested-externs -Wpointer-arith -Wswitch \
            -Wreturn-type 

# Optimization setting
# (-Os for small code size, -O2 for speed)
OFLAGS  = -O2

DFLAGS  =
#DFLAGS  = -gdwarf-2

#TFLAGS  =
TFLAGS  = -mthumb -mthumb-interwork -DTHUMB_CSTART -DTHUMB_INTERWORK

CFLAGS  = -mcpu=$(CPU) -D$(CPU_VARIANT) -DHWVWR=$(HWVER) $(WFLAGS) $(OFLAGS) $(DFLAGS) $(TFLAGS) \
          $(INC) -Wa,-ahlms=$(<:.c=.lst)
AFLAGS  = $(CFLAGS) -x assembler-with-cpp -gstabs


LD_SCRIPT      = link_16k_128k_rom.ld
LD_SCRIPT_PATH = ../build_files

LFLAGS   = -mcpu=$(CPU) $(TFLAGS) \
	   -o $(NAME).elf -Wl,-Map=$(NAME).map,--cref
# -T$(LD_SCRIPT_PATH)/$(LD_SCRIPT)

all: pre_all depend $(TARGET)

pre_all:
ifdef SUBDIRS
	@for dir in $(SUBDIRS) ; do \
	  (cd $$dir && $(MAKE) $(MAKEFLAGS) all) || exit 1; \
	done
endif

depend:
	$(RM) .depend 
	@for f in $(CSRCS) ; do \
		$(CC) -MM $(CC_OPTS) $$f >> .depend ; \
	done
	@for f in $(ASRCS) ; do \
		$(AS) -MM $(CC_OPTS) $$f >> .depend ; \
	done

ifneq (clean, $(MAKECMDGOALS))
-include .depend
endif

%.o: %.c
	$(CC) -c $(CFLAGS) -o $@ $<
%.o: %.S
	$(AS) -c $(AFLAGS) -o $@ $<

# Always recompile all if makefile changes
$(OBJS): ./makefile

clean:
ifdef SUBDIRS
	@for dir in $(SUBDIRS) ; do \
		(cd $$dir && $(MAKE) $(MAKEFLAGS) clean) || exit 1; \
	done
endif
	$(RM) .depend $(TARGET) $(OBJS) $(OBJS:.o=.lst)
ifeq (0, $(MAKELEVEL))
	$(RM) $(NAME).hex $(NAME).elf $(NAME).map $(NAME).obj
endif

$(TARGET): $(OBJS) $(LIBS)
	$(LD) $(LFLAGS) $(INC) $(OBJS) $(LIBS)
	$(OBJCOPY) -O $(HEX_FORMAT) $(NAME).elf $(TARGET)

deploy: all
	$(DOWNLOAD) -hex -term -control $(TARGET) $(DL_COMPORT) $(DL_BAUDRATE) $(DL_CRYSTAL)

terminal:
	$(DOWNLOAD) -hex -termonly $(TARGET) $(DL_COMPORT) $(DL_BAUDRATE) $(DL_CRYSTAL)
