#***********************************************************************************
#	Copyright 2005 Anglia Design
#	This demo code and associated components are provided as is and has no warranty,
#	implied or otherwise.  You are free to use/modify any of the provided
#	code at your own risk in your applications with the expressed limitation
#	of liability (see below)
# 
#	LIMITATION OF LIABILITY:   ANGLIA OR ANGLIA DESIGNS SHALL NOT BE LIABLE FOR ANY
#	LOSS OF PROFITS, LOSS OF USE, LOSS OF DATA, INTERRUPTION OF BUSINESS, NOR FOR
#	INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES OF ANY KIND WHETHER UNDER
#	THIS AGREEMENT OR OTHERWISE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
#
#	Author			: Spencer Oliver
#	Web     		: www.anglia-designs.com
#
#***********************************************************************************

PROJNAME = stm32demo

SRC = src usb/src
INCLUDE = inc usb/inc
OBJ = obj
DEFINES = LCD_HX8312

C_SRC = vectors.c startup.c main.c calendar.c hw_config.c lowpower.c \
		lcd.c mass_storage.c memory.c menu.c msd.c scsi_data.c \
		spi_flash.c tsensor.c usb_bot.c usb_desc.c usb_endp.c \
		usb_istr.c usb_prop.c usb_pwr.c usb_scsi.c waveplayer.c \
		usb_core.c usb_init.c usb_int.c usb_mem.c usb_regs.c
A_SRC = 

# Program names, locations

AS = as
CC = gcc
LINK = ld
OBJCOPY = objcopy
OBJDUMP = objdump
SIZE = size
RM = rm -f

INCDIR = $(patsubst %,-I%,$(INCLUDE))
SRCDIR = $(patsubst %,%,$(SRC))
VINCLUDE = $(patsubst %,%,$(INCLUDE))
DEF = $(patsubst %,-D%,$(DEFINES))

CFLAGS = -Wall $(INCDIR) ${DEF} -g -mthumb -march=armv7-m -ffunction-sections -fdata-sections -Os
LDFLAGS = -mthumb -march=armv7-m -Tstm32rom.ld -Wl,-Map=$(OBJ)/$(PROJNAME).map,-cref,--gc-sections -nostartfiles

# List of directories to be searched
VPATH = $(SRCDIR) $(OBJ) $(VINCLUDE)

# List of object files from list of source files
OBJFILES = $(patsubst %.c,%.o,$(filter %.c,$(C_SRC))) \
			$(patsubst %.s,%.o,$(filter %.s,$(A_SRC)))

# List of object files from list of source files
OBJECTS = $(addprefix $(OBJ)/,$(filter %.o,$(OBJFILES)))

# List of dependancy files from list of source files
DEPFILES = $(addprefix $(OBJ)/, \
			$(filter %.d,$(patsubst %.c,%.d,$(filter %.c,$(C_SRC)))))

# our main target(s)
all : $(PROJNAME).s19

$(PROJNAME).elf : $(OBJFILES)
	@echo Linking: $@
	$(CC) $(LDFLAGS) -o $(OBJ)/$@ $(OBJECTS) -lstm32d

$(PROJNAME).s19 : $(PROJNAME).elf
	$(OBJCOPY) -O srec $(OBJ)/$< $(OBJ)/$@
	$(SIZE) $(OBJ)/$<

# rules for compiling C files
%.o : %.c %.d
	@echo Compiling File: $<
	$(CC) -c $(CFLAGS) $< -o $(OBJ)/$@

# rules for compiling asm files
%.o : %.s
	@echo Assembling File: $<
	$(CC) -c $(CFLAGS) $< -o $(OBJ)/$@

# Generate auto depenencies for c files
$(OBJ)/%.d : %.c
	@echo Generating Dependency File: $<
	$(CC) $(INCDIR) -MM $< > $@ || $(RM) $@

# include auto depenencies
-include $(DEPFILES)

.PHONY : clean
clean :
	-$(RM) $(OBJECTS)
#	-$(RM) $(DEPFILES)
	-$(RM) $(OBJ)/$(PROJNAME).map
	-$(RM) $(OBJ)/$(PROJNAME).elf
	-$(RM) $(OBJ)/$(PROJNAME).s19
