# Hey Emacs, this is a -*- makefile -*-
#
# WinARM template makefile 
# by Martin Thomas, Kaiserslautern, Germany 
# <eversmith@heizung-thomas.de>
#
# based on the WinAVR makefile written by Eric B. Weddington, Jörg Wunsch, et al.
# Released to the Public Domain
# Please read the make user manual!
#
#
# On command line:
#
# make all = Make software.
#
#
# make program = Download the hex file to the device, using lpc21isp
#
# (TODO: make filename.s = Just compile filename.c into the assembler code only)
#
# To rebuild project do "make clean" then "make all".
#
# Changelog:
# - 17. Feb. 2005  - added thumb-interwork support (mth)
# - 28. Apr. 2005  - added C++ support (mth)
# - 29. Arp. 2005  - changed handling for lst-Filename (mth)
# - 25. Mar. 2006  - added bootloader support (contek)

# Destination device
BOARD = sens

# MCU name and submodel
MCU = cortex-m4
SUBMDL = atsam4e16e
USE_FPU = FALSE

# Target hardware identifier
HWID = SENS
HW_NUM = 255

THUMB    = -mthumb
#THUMB_IW = -mthumb-interwork

## Create ROM-Image
#RUN_MODE=ROM_RUN
## Create RAM-Image
#RUN_MODE=RAM_RUN
## Create image for bootloader
RUN_MODE=APP_RUN

##Typ procesoru
PROC_TYPE=ARM
PROC_FAMILY=CORTEX
PROC_LIB_TYPE=__SAM4E16E__

# Output format. (can be srec, ihex, binary)
FORMAT1 = ihex
FORMAT2 = binary

# Library type
# Use compiled libraries
LIB_TYPE = COMPILED_LIB
CONTEK_LIB = ctkAppLib_sens.a

ifneq ($(LIB_TYPE),COMPILED_LIB)
# Use resource library files
LIB_TYPE = RESOURCEFILES_LIB
endif

				
# List APP C source files here. 
SRC = 	appdebug.c \
		dbgwapp.c \
		fasttick.c \
		finalize.c \
		gvariables.c \
		initialize.c \
		maintick.c \
		pccomm.c \
		realtick.c \
		slowtick.c \
		verinfo.c \
		thermocouple.c \
		lambda.c \
		photox.c \
		modbusA.c \
		modbusL2sl.c 

ifeq ($(LIB_TYPE),COMPILED_LIB)
SRC +=	version.c \
		usart.c
endif		

# List Libraries C source files here.
ifeq ($(LIB_TYPE),RESOURCEFILES_LIB)
SRC +=	timeticks.c \
		system_sam4e.c\
		startup_sam4e_APP.c\
		exceptions.c \
		iolib.c \
		ioboard.c \
		comm.c \
		uart.c \
		usart.c \
		uclp1a.c \
		uclp2a.c \
		ucrc16.c \
		ucrc32.c \
		hwmodel.c \
		hwsubmdl.c \
		hwboard.c \
		twim.c \
		nvmem.c \
		list.c \
		misc.c \
		main.c \
		ctktime.c \
		datadef.c \
		uctp.c \
		dbgmon.c \
		paramstore.c \
		sync.c
endif	

# Generated C source files
SRC +=	XmlConstantsMax.c \
		XmlConstantsMin.c \
		XmlConstantsDefault.c \
		XmlEnumTypes.c \
		XmlParamDesc.c \
		XmlIndexDesc.c \
		XmlDataDesc.c \
		XmlVariables.c
		
ifeq ($(LIB_TYPE),COMPILED_LIB)
SRC +=	XmlConditions.c \
		XmlParamIndex.c
endif

# List C source files here which must be compiled in ARM-Mode.
# use file-extension c for "c-only"-files
ifeq ($(LIB_TYPE),RESOURCEFILES_LIB)
SRCARM = sync.c
endif

# List C++ source files here.
# use file-extension cpp for C++-files (use extension .cpp)
ifeq ($(LIB_TYPE),RESOURCEFILES_LIB)
CPPSRC = 
endif

# List C++ source files here which must be compiled in ARM-Mode.
# use file-extension cpp for C++-files (use extension .cpp)
#CPPSRCARM = $(TARGET).cpp
ifeq ($(LIB_TYPE),RESOURCEFILES_LIB)
CPPSRCARM = 
endif

# 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 = appinfo.S

# List Assembler source files here which must be assembled in ARM-Mode..
ifeq ($(LIB_TYPE),RESOURCEFILES_LIB)
ASRCARM =
endif

# 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 = 3

# Directories
ROOTDIR = C:/Work/C
SRCDIR = $(ROOTDIR)/App/SensAmteo/src
LIBROOT = $(ROOTDIR)/Lib
BUILDDIR = $(ROOTDIR)/App/SensAmteo/prj/$(BOARD)/build
PARAMXML_PATH = $(SRCDIR)/ctk/xml
XMLDIR = $(PARAMXML_PATH)/params


# Subdirectories
CTKDIR = ctk
MODELDIR = $(MCU)
SUBMDLDIR = $(SUBMDL)
BOARDDIR = $(BOARD)

# Source directories

vpath %.c 	$(SRCDIR)/$(BOARDDIR) \
			$(SRCDIR)/$(CTKDIR) \
			$(PARAMXML_PATH)\
			$(LIBROOT)/$(BOARDDIR)/src \
			$(LIBROOT)/$(SUBMDLDIR)/src \
			$(LIBROOT)/$(MODELDIR)/src \
			$(LIBROOT)/$(CTKDIR)/src \
			$(LIBROOT)/fonts/src \
			$(LIBROOT)

vpath %.S	$(SRCDIR)/$(BOARDDIR) \
			$(SRCDIR)/$(CTKDIR) \
			$(PARAMXML_PATH)\
			$(LIBROOT)/$(BOARDDIR)/src \
			$(LIBROOT)/$(SUBMDLDIR)/src \
			$(LIBROOT)/$(MODELDIR)/src \
			$(LIBROOT)/$(CTKDIR)/src \
			$(LIBROOT)/fonts/src \
			$(LIBROOT)
			
vpath %.o	$(BUILDDIR)

# Include directories
EXTRAINCDIRS = 	$(SRCDIR)/$(BOARDDIR) \
				$(SRCDIR)/$(CTKDIR) \
				$(PARAMXML_PATH)\
				$(LIBROOT)/$(BOARDDIR)/inc \
				$(LIBROOT)/$(BOARDDIR)/hw \
				$(LIBROOT)/$(SUBMDLDIR)/inc \
				$(LIBROOT)/$(SUBMDLDIR)/hw \
				$(LIBROOT)/$(MODELDIR)/inc \
				$(LIBROOT)/$(MODELDIR)/hw \
				$(LIBROOT)/$(CTKDIR)/inc \
				$(LIBROOT)/$(CTKDIR)/hw \
				$(LIBROOT)/fonts/inc \
				$(LIBROOT)


# 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

# Target file name (without extension).
ifeq ($(RUN_MODE),ROM_RUN)
TARGET = romboot
endif
ifeq ($(RUN_MODE),RAM_RUN)
TARGET = ramboot
endif
ifeq ($(RUN_MODE),APP_RUN)
TARGET = appboot
endif


# Place -D or -U options for C here
CDEFS = -D$(SUBMDL) -D$(RUN_MODE) -D$(LIB_TYPE) -D$(PROC_TYPE) -D$(PROC_FAMILY) -D$(PROC_LIB_TYPE) -D$(BOARD) -D$(HWID) -DHW_ID=$(HW_NUM) -D_DEBUG


# Place -I options here
CINCS =

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

# 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 = -g
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=$(BUILDDIR)/$(patsubst %.c,%.lst,$(notdir $<)) 
CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
#CFLAGS += -mstructure-size-boundary=8
#CFLAGS += -E	# generate preprocessor output
CFLAGS += -fdata-sections -ffunction-sections 
CFLAGS += -include dbgmon.h

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

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

# Assembler flags.
#  -Wa,...:   tell GCC to pass this to the assembler.
#  -ahlms:    create listing
#  -gstabs:   have the assembler create line number information; note that
#             for use in COFF files, additional information about filenames
#             and function names needs to be present in the assembler source
#             files -- see avr-libc docs [FIXME: not yet described there]
ASFLAGS = $(ADEFS) 
ASFLAGS += -mthumb
ASFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
ASFLAGS += -Wa,-adhlns=$(BUILDDIR)/$(patsubst %.S,%.lst,$(notdir $<)) 

#Additional libraries.

MATH_LIB = -lm
# CPLUSPLUS_LIB = -lstdc++

# Linker flags.
#  -Wl,...:     tell GCC to pass this to linker.
#    -Map:      create map file
#    --cref:    add cross reference to  map file
LDFLAGS = -nostartfiles -Wl,-Map=$(BUILDDIR)/$(TARGET).map,--cref
LDFLAGS += -lc
LDFLAGS +=  $(MATH_LIB) 
LDFLAGS += -lc -lgcc 
#LDFLAGS += $(CPLUSPLUS_LIB)
LDFLAGS += -Wl,-static -Wl,--gc-sections

# Set compiled library depending on ConTeK board
ifeq ($(LIB_TYPE),COMPILED_LIB)
LDFLAGS += $(LIBROOT)/$(CONTEK_LIB)
endif

# Set Linker-Script Depending On Selected Memory
ifeq ($(RUN_MODE),RAM_RUN)
LDFLAGS += -T$(LIBROOT)/$(SUBMDLDIR)/src/$(SUBMDL)-RAM.ld
endif
ifeq ($(RUN_MODE),ROM_RUN) 
LDFLAGS += -T$(LIBROOT)/$(SUBMDLDIR)/src/$(SUBMDL)-ROM.ld
endif
ifeq ($(RUN_MODE),APP_RUN)
LDFLAGS += -T$(LIBROOT)/$(SUBMDLDIR)/src/$(SUBMDL)-APP.ld
endif

# Bootloader
BLOADER = bloader.exe
BLOADER_PORT = com2
BLOADER_SPEED = 115200
BLOADER_MEM = flash

# 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


# ---------------------------------------------------------------------------

# Define directories, if needed.
## DIRARM = c:/WinARM/
## DIRARMBIN = $(DIRAVR)/bin/
## DIRAVRUTILS = $(DIRAVR)/utils/bin/

#TCHAIN = arm-elf
#TCHAIN = arm-eabi
TCHAIN = arm-none-eabi


# Define programs and commands.
SHELL = sh
CC = $(TCHAIN)-gcc
CPP = $(TCHAIN)-g++
OBJCOPY = $(TCHAIN)-objcopy
OBJDUMP = $(TCHAIN)-objdump
SIZE = $(TCHAIN)-size
NM = $(TCHAIN)-nm

REMOVE = rm -f
COPY = cp


# Define Messages
# English
MSG_ERRORS_NONE = Errors: none
MSG_BEGIN = -------- begin --------
MSG_END = --------  end  --------
MSG_SIZE_BEFORE = Size before: 
MSG_SIZE_AFTER = Size after:
MSG_FLASH = Creating load file for Flash:
MSG_EXTENDED_LISTING = Creating Extended Listing:
MSG_SYMBOL_TABLE = Creating Symbol Table:
MSG_LINKING = Linking:
MSG_COMPILING = Compiling C:
MSG_COMPILING_ARM = "Compiling C (ARM-only):"
MSG_COMPILINGCPP = Compiling C++:
MSG_COMPILINGCPP_ARM = "Compiling C++ (ARM-only):"
MSG_ASSEMBLING = Assembling:
MSG_ASSEMBLING_ARM = "Assembling (ARM-only):"
MSG_CLEANING = Cleaning project:


# 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)

# Define object files with path
BLD_COBJ = $(patsubst %, $(BUILDDIR)/%, $(COBJ))
BLD_AOBJ = $(patsubst %, $(BUILDDIR)/%, $(AOBJ))
BLD_COBJARM = $(patsubst %, $(BUILDDIR)/%, $(COBJARM))
BLD_AOBJARM = $(patsubst %, $(BUILDDIR)/%, $(AOBJARM))
BLD_CPPOBJ = $(patsubst %, $(BUILDDIR)/%, $(CPPOBJ))
BLD_CPPOBJARM = $(patsubst %, $(BUILDDIR)/%, $(CPPOBJARM))

# Define all listing files.
LSTS = $(ASRC:.S=.lst) $(ASRCARM:.S=.lst) $(SRC:.c=.lst) $(SRCARM:.c=.lst)
LSTS += $(CPPSRC:.cpp=.lst) $(CPPSRCARM:.cpp=.lst)
LST = $(patsubst %, $(BUILDDIR)/%, $(LSTS))

# 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) -D$(BOARD) -I../../src $(CFLAGS) $(GENDEPFLAGS)
ALL_ASFLAGS = -mcpu=$(MCU) $(THUMB) -D$(BOARD) -I../../src -x assembler-with-cpp $(ASFLAGS)

# Default target.
all: begin gccversion sizebefore build sizeafter finished end

build: elf hex bin lss sym

elf: $(TARGET).elf
hex: $(TARGET).hex
bin: $(TARGET).bin
lss: $(TARGET).lss 
sym: $(TARGET).sym

# Eye candy.
begin:
	@echo
	@echo $(MSG_BEGIN)

finished:
	@echo $(MSG_ERRORS_NONE)

end:
	@echo $(MSG_END)
	@echo


# Display size of file.
HEXSIZE = $(SIZE) --target=$(FORMAT1) $(BUILDDIR)/$(TARGET).hex
ELFSIZE = $(SIZE) -A $(BUILDDIR)/$(TARGET).elf
sizebefore:
	@if [ -f $(BUILDDIR)/$(TARGET).elf ]; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); echo; fi

sizeafter:
	@if [ -f $(BUILDDIR)/$(TARGET).elf ]; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); echo; fi


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

# Generate XML
xmlgen:
	$(REMOVE) $(XMLDIR)/*.h
	$(REMOVE) $(XMLDIR)/*.c
	$(XMLDIR)/XML_CGen_cmd /n /imin /comm /gall $(XMLDIR)/params.xml
	 
# Minimalize XML
xmlmin:
	$(REMOVE) $(XMLDIR)/*.h
	$(REMOVE) $(XMLDIR)/*.c
	$(XMLDIR)/XML_CGen_cmd /n /imin /omin1 /omin2 /eunused $(XMLDIR)/params.xml
	 
# Program the device.  

program: $(TARGET).hex
	$(BLOADER) -m $(BLOADER_MEM) -w $(BUILDDIR)/$(TARGET).hex -p $(BLOADER_PORT) -b $(BLOADER_SPEED) --crc -d --noverify
	$(BLOADER) -p $(BLOADER_PORT) -b $(BLOADER_SPEED) -x
	
run:
	$(BLOADER) -p $(BLOADER_PORT) -b $(BLOADER_SPEED) -x


# Create final output files (.hex, .eep) from ELF output file.
# TODO: handling the .eeprom-section should be redundant
%.hex: %.elf
	@echo
	@echo $(MSG_FLASH) $@
	$(OBJCOPY) -O $(FORMAT1) $(BUILDDIR)/$< $(BUILDDIR)/$(notdir $@)

%.bin: %.elf
	@echo
	@echo $(MSG_FLASH) $@
	$(OBJCOPY) -O $(FORMAT2) $(BUILDDIR)/$< $(BUILDDIR)/$(notdir $@)



# Create extended listing file from ELF output file.
# testing: option -C
%.lss: %.elf
	@echo
	@echo $(MSG_EXTENDED_LISTING) $@
	$(OBJDUMP) -h -S -C $(BUILDDIR)/$< > $(BUILDDIR)/$(notdir $@)


# Create a symbol table from ELF output file.
%.sym: %.elf
	@echo
	@echo $(MSG_SYMBOL_TABLE) $@
	$(NM) -n $(BUILDDIR)/$< > $(BUILDDIR)/$(notdir $@)


# Link: create ELF output file from object files.
.SECONDARY : _$(TARGET).elf
.PRECIOUS : $(AOBJARM) $(AOBJ) $(COBJARM) $(COBJ) $(CPPOBJ) $(CPPOBJARM)
$(TARGET).elf:  $(AOBJARM) $(AOBJ) $(COBJARM) $(COBJ) $(CPPOBJ) $(CPPOBJARM)
	@echo
	@echo $(MSG_LINKING) $@
	$(CC) $(THUMB) $(ALL_CFLAGS) $(BLD_AOBJARM) $(BLD_AOBJ) $(BLD_COBJARM) $(BLD_COBJ) $(BLD_CPPOBJ) $(BLD_CPPOBJARM) --output $(BUILDDIR)/$@ $(LDFLAGS)
#	$(CPP) $(THUMB) $(ALL_CFLAGS) $(AOBJARM) $(AOBJ) $(COBJARM) $(COBJ) $(CPPOBJ) $(CPPOBJARM) --output $@ $(LDFLAGS)

# Compile: create object files from C source files. ARM/Thumb
$(COBJ) : %.o : %.c
	@echo
	@echo $(MSG_COMPILING) $<
	$(CC) -c $(THUMB) $(ALL_CFLAGS) $(CONLYFLAGS) $< -o $(BUILDDIR)/$(notdir $@)

# Compile: create object files from C source files. ARM-only
$(COBJARM) : %.o : %.c
	@echo
	@echo $(MSG_COMPILING_ARM) $<
	$(CC) -c $(ALL_CFLAGS) $(CONLYFLAGS) $< -o $(BUILDDIR)/$(notdir $@)

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

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


# 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 $(MSG_ASSEMBLING) $<
	$(CC) -c $(THUMB) $(ALL_ASFLAGS) $< -o $(BUILDDIR)/$(notdir $@)


# Assemble: create object files from assembler source files. ARM-only
$(AOBJARM) : %.o : %.S
	@echo
	@echo $(MSG_ASSEMBLING_ARM) $<
	$(CC) -c $(ALL_ASFLAGS) $< -o $(BUILDDIR)/$(notdir $@)


# Target: clean project.
clean: begin clean_list finished end


clean_list :
	@echo
	@echo $(MSG_CLEANING)
	$(REMOVE) $(BUILDDIR)/$(TARGET).bin
	$(REMOVE) $(BUILDDIR)/$(TARGET).hex
	$(REMOVE) $(BUILDDIR)/$(TARGET).obj
	$(REMOVE) $(BUILDDIR)/$(TARGET).elf
	$(REMOVE) $(BUILDDIR)/_$(TARGET).elf
	$(REMOVE) $(BUILDDIR)/$(TARGET).map
	$(REMOVE) $(BUILDDIR)/$(TARGET).obj
	$(REMOVE) $(BUILDDIR)/$(TARGET).a90
	$(REMOVE) $(BUILDDIR)/$(TARGET).sym
	$(REMOVE) $(BUILDDIR)/$(TARGET).lnk
	$(REMOVE) $(BUILDDIR)/$(TARGET).lss
	$(REMOVE) $(COBJ)
	$(REMOVE) $(CPPOBJ)
	$(REMOVE) $(AOBJ)
	$(REMOVE) $(COBJARM)
	$(REMOVE) $(CPPOBJARM)
	$(REMOVE) $(AOBJARM)
	$(REMOVE) $(BLD_COBJ)
	$(REMOVE) $(BLD_CPPOBJ)
	$(REMOVE) $(BLD_AOBJ)
	$(REMOVE) $(BLD_COBJARM)
	$(REMOVE) $(BLD_CPPOBJARM)
	$(REMOVE) $(BLD_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 finish end sizebefore sizeafter gccversion \
build elf hex lss sym clean clean_list program

