#
#       !!!! Do NOT edit this makefile with an editor which replace tabs by spaces !!!!    
#
##############################################################################################
# 
# On command line:
#
# make all = Create project
#
# make clean = Clean project files.
#
# To rebuild project do "make clean" and "make all".
#

##############################################################################################
# Start of default section
#

TRGT = arm-none-eabi-
CC   = $(TRGT)gcc
CP   = $(TRGT)objcopy
AS   = $(TRGT)gcc -x assembler-with-cpp
BIN  = $(CP) -O ihex 

MCU  = cortex-m3

# List all default C defines here, like -D_DEBUG=1
DDEFS = 

# List all default ASM defines here, like -D_DEBUG=1
DADEFS = 

# List all default directories to look for include files here
DINCDIR = 

# List the default directory to look for the libraries here
DLIBDIR =

# List all default libraries here
DLIBS = 

#
# End of default section
##############################################################################################

##############################################################################################
# Start of user section
#

# Define project name here
PROJECT = USB_Stamp

# Define using Dir "out"
USE_OUT = yes
#USE_OUT = no

# Define linker script file here
LDSCRIPT_ROM = ./prj/STM32F103RE-ROM.ld

# List all user C define here, like -D_DEBUG=1
UDEFS = 

# Define ASM defines here
UADEFS = 

# List C source files here
SRC  = src/fw_update.c

SRC += src/main.c 
SRC += src/Seriell.c
SRC += src/stm32f10x_it.c
SRC += src/stm32f10x_vector.c
SRC += src/timer_task.c
SRC += src/Lcd_Task.c
SRC += src/LED.c
SRC += src/Menu.c
#SRC += src/Uhr.c

# Quell-Dateien der STM32 Bibliothek:
SRC += lib/cm3/core_cm3.c
SRC += lib/src/misc.c
SRC += lib/system_stm32f10x.c
#SRC += lib/src/stm32f10x_adc.c
SRC += lib/src/stm32f10x_bkp.c
#SRC += lib/src/stm32f10x_can.c
#SRC += lib/src/stm32f10x_cec.c
#SRC += lib/src/stm32f10x_crc.c
#SRC += lib/src/stm32f10x_dac.c
#SRC += lib/src/stm32f10x_dbgmcu.c
#SRC += lib/src/stm32f10x_dma.c
#SRC += lib/src/stm32f10x_exti.c
SRC += lib/src/stm32f10x_flash.c
#SRC += lib/src/stm32f10x_fsmc.c
SRC += lib/src/stm32f10x_gpio.c
#SRC += lib/src/stm32f10x_i2c.c
#SRC += lib/src/stm32f10x_iwdg.c
#SRC += lib/src/stm32f10x_pwr.c
SRC += lib/src/stm32f10x_rcc.c
#SRC += lib/src/stm32f10x_rtc.c
#SRC += lib/src/stm32f10x_sdio.c
SRC += lib/src/stm32f10x_spi.c
SRC += lib/src/stm32f10x_tim.c
SRC += lib/src/stm32f10x_usart.c
#SRC += lib/src/stm32f10x_wwdg.c

# List ASM source files here
ASRC =

# List all user directories here
UINCDIR = ./inc
UINCDIR += ./lib
UINCDIR += ./lib/cm3
UINCDIR += ./lib/inc

# List the user directory to look for the libraries here
ULIBDIR =

# List all user libraries here
ULIBS = 

# Define optimisation level here
OPT = -O0

#
# End of user defines
##############################################################################################


INCDIR  = $(patsubst %,-I%,$(DINCDIR) $(UINCDIR))
LIBDIR  = $(patsubst %,-L%,$(DLIBDIR) $(ULIBDIR))
DEFS    = $(DDEFS) $(UDEFS)
ADEFS   = $(DADEFS) $(UADEFS)
OBJS    = $(ASRC:.s=.o) $(SRC:.c=.o)
LIBS    = $(DLIBS) $(ULIBS)
MCFLAGS = -mcpu=$(MCU)

ASFLAGS = $(MCFLAGS) -g -gdwarf-2 -Wa,-amhls=$(<:.s=.lst) $(ADEFS)
ifeq ($(USE_OUT), no)
CPFLAGS = $(MCFLAGS) $(OPT) -gdwarf-2 -mthumb -fomit-frame-pointer -Wall -Wstrict-prototypes -fverbose-asm -Wa,-ahlms=$(addprefix ./, $(<:.c=.lst)) $(DEFS)
else
CPFLAGS = $(MCFLAGS) $(OPT) -gdwarf-2 -mthumb -fomit-frame-pointer -Wall -Wstrict-prototypes -fverbose-asm -Wa,-ahlms=$(addprefix ./out/, $(<:.c=.lst)) $(DEFS)
endif
LDFLAGS_ROM = $(MCFLAGS) -mthumb -nostartfiles -T$(LDSCRIPT_ROM) -Wl,-Map=$(PROJECT)_rom.map,--cref,--no-warn-mismatch $(LIBDIR)

# Generate dependency information
CPFLAGS += -MD -MP -MF .dep/$(@F).d

#
# makefile rules
#

all: START sizebefore ROM sizeafter

START:
	$(CC) --version

sizebefore:
	@if [ -f $(PROJECT)_rom.elf ]; then echo; echo Size before:; $(TRGT)size -A $(PROJECT)_rom.elf; echo; fi

sizeafter:
	@if [ -f $(PROJECT)_rom.elf ]; then echo; echo Size after:; $(TRGT)size -A $(PROJECT)_rom.elf; echo; fi

ROM: $(OBJS) $(PROJECT)_rom.elf $(PROJECT)_rom.hex

%o : %c
ifeq ($(USE_OUT), no)
	$(CC) -c $(CPFLAGS) -I . $(INCDIR) $(addprefix ./, $<) -o $(addprefix ./, $@)
else
	$(CC) -c $(CPFLAGS) -I . $(INCDIR) $(addprefix ./, $<) -o $(addprefix ./out/, $@)
endif

%o : %s
ifeq ($(USE_OUT), no)
	$(AS) -c $(ASFLAGS) $(addprefix ./, $<) -o $(addprefix ./, $@)
else
	$(AS) -c $(ASFLAGS) $(addprefix ./, $<) -o $(addprefix ./out/, $@)
endif

%rom.elf: $(OBJS)
ifeq ($(USE_OUT), no)
	$(CC) $(addprefix ./, $(OBJS)) $(LDFLAGS_ROM) $(LIBS) -o $@
else
	$(CC) $(addprefix ./out/, $(OBJS)) $(LDFLAGS_ROM) $(LIBS) -o $@
endif

%hex: %elf
	$(BIN) $< $@

clean:
ifeq ($(USE_OUT), no)
	-rm -f $(addprefix ./, $(OBJS))
else
	-rm -f $(addprefix ./out/, $(OBJS))
endif
	-rm -f $(PROJECT)_rom.elf
	-rm -f $(PROJECT)_rom.map
	-rm -f $(PROJECT)_rom.hex
	-rm -f $(SRC:.c=.c.bak)
ifeq ($(USE_OUT), no)
	-rm -f $(addprefix ./, $(SRC:.c=.lst))
else
	-rm -f $(addprefix ./out/, $(SRC:.c=.lst))
endif
	-rm -f $(ASRC:.s=.s.bak)
ifeq ($(USE_OUT), no)
	-rm -f $(addprefix ./, $(ASRC:.s=.lst))
else
	-rm -f $(addprefix ./out/, $(ASRC:.s=.lst))
endif
	-rm -fR .dep

# 
# Include the dependency files, should be the last of the makefile
#
-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)

.PHONY : %o ROM

# *** EOF ***