#########################################################################
# Makefile for NativeSample_TestSD
# To build
# - Change TARGET_ROOT
# - Change NETMF_PK_ROOT
# - Change Toolchain path
# To change memory location
# - Change LDSCRIPT
TARGET = NativeSample_TestSD
TARGET_ROOT = E:/MicroFrameworkPK_v4_2/NativeSamples/TestSD
NETMF_PK_ROOT = E:/MicroFrameworkPK_v4_2
SOLUTION_NAME = WXMP3PLCD_FM3
PLATFORM_NAME = PLATFORM_ARM_WXMP3PLCD_FM3
PROCESSOR_NAME = PLATFORM_ARM_FM3
TARGET_LOCATION = TARGETLOCATION_FLASH
CPUFLAG = cortex-m3 -mthumb
ENDIAN = LITTLE_ENDIAN
VERSION_MAJOR = 4
VERSION_MINOR = 2
VERSION_BUILD = 0
VERSION_REVISION = 0
#EnableLWIP = 1
EnableSD = 1
#EnableLCD_DST2401PH = 1

#########################################################################
# toolchain path
CC = C:/CodeSourcery/arm-2010q1-188/bin/arm-none-eabi-gcc
LD = C:/CodeSourcery/arm-2010q1-188/bin/arm-none-eabi-ld
AS = C:/CodeSourcery/arm-2010q1-188/bin/arm-none-eabi-as
OBJCOPY = C:/CodeSourcery/arm-2010q1-188/bin/arm-none-eabi-objcopy
OBJDUMP = C:/CodeSourcery/arm-2010q1-188/bin/arm-none-eabi-objdump
SIZE = C:/CodeSourcery/arm-2010q1-188/bin/arm-none-eabi-size
NM = C:/CodeSourcery/arm-2010q1-188/bin/arm-none-eabi-nm
REMOVE = C:/CodeSourcery/arm-2010q1-188/bin/cs-rm -f
GCCLIB1 = C:/CodeSourcery/arm-2010q1-188/arm-none-eabi/lib/thumb2
GCCLIB2 = C:/CodeSourcery/arm-2010q1-188/lib/gcc/arm-none-eabi/4.4.1/thumb2

#########################################################################
# C flags
CFLAGS	= -ffunction-sections -fomit-frame-pointer -fdollars-in-identifiers -fshort-wchar -fno-exceptions  -funsigned-char
CFLAGS += -mcpu=$(CPUFLAG)
CFLAGS += -D$(PLATFORM_NAME) -D$(PROCESSOR_NAME) -D$(ENDIAN) -D$(TARGET_LOCATION)
CFLAGS += -DVERSION_MAJOR=$(VERSION_MAJOR) -DVERSION_MINOR=$(VERSION_MINOR) -DVERSION_BUILD=$(VERSION_BUILD) -DVERSION_REVISION=$(VERSION_REVISION)

#########################################################################
# CPP flags
CPPFLAGS = -Wno-invalid-offsetof -Wno-attributes 
CPPFLAGS += -ffunction-sections -fomit-frame-pointer -fdollars-in-identifiers -fshort-wchar -fno-exceptions  -funsigned-char
CPPFLAGS += -mcpu=$(CPUFLAG)
CPPFLAGS += -D$(PLATFORM_NAME) -D$(PROCESSOR_NAME) -D$(ENDIAN) -D$(TARGET_LOCATION)
CPPFLAGS += -DVERSION_MAJOR=$(VERSION_MAJOR) -DVERSION_MINOR=$(VERSION_MINOR) -DVERSION_BUILD=$(VERSION_BUILD) -DVERSION_REVISION=$(VERSION_REVISION)

#########################################################################
# library configuration
LIBS	= -lc
LIBS	+= -lgcc
LDSCRIPT = NativeSample_scatterfile.ldf
LDFLAG = --no-wchar-size-warning

#########################################################################
OUTDIR	= ./out

#########################################################################
# directories for source files 
SRCDIRS	= .
SRCDRIS += $(TARGET_ROOT)
SRCDIRS	+= $(NETMF_PK_ROOT)/DeviceCode/Drivers/BlockStorage/SD
SRCDIRS	+= $(NETMF_PK_ROOT)/DeviceCode/Drivers/Display/stubs
SRCDIRS	+= $(NETMF_PK_ROOT)/DeviceCode/Drivers/Display/autoLCD/
SRCDIRS	+= $(NETMF_PK_ROOT)/DeviceCode/Drivers/Display/TextFonts/Font8x15
SRCDIRS	+= $(NETMF_PK_ROOT)/DeviceCode/Drivers/FS/FAT
SRCDIRS	+= $(NETMF_PK_ROOT)/DeviceCode/Drivers/Stubs/Processor/stubs_cache
SRCDIRS	+= $(NETMF_PK_ROOT)/DeviceCode/Drivers/Stubs/Processor/stubs_EBIU
SRCDIRS	+= $(NETMF_PK_ROOT)/DeviceCode/Drivers/Stubs/Processor/stubs_WATCHDOG
SRCDIRS	+= $(NETMF_PK_ROOT)/DeviceCode/Drivers/Stubs/Processor/stubs_USB
SRCDIRS += $(NETMF_PK_ROOT)/DeviceCode/Targets/Native/FM3/DeviceCode/CortexM3/GlobalLock/GNU_S
SRCDIRS	+= $(NETMF_PK_ROOT)/DeviceCode/Targets/Native/FM3/DeviceCode/FM3_Bootstrap
SRCDIRS	+= $(NETMF_PK_ROOT)/DeviceCode/Targets/Native/FM3/DeviceCode/FM3_FLASH
SRCDIRS	+= $(NETMF_PK_ROOT)/DeviceCode/Targets/Native/FM3/DeviceCode/FM3_GPIO
SRCDIRS	+= $(NETMF_PK_ROOT)/DeviceCode/Targets/Native/FM3/DeviceCode/FM3_INTC
SRCDIRS	+= $(NETMF_PK_ROOT)/DeviceCode/Targets/Native/FM3/DeviceCode/FM3_Power
SRCDIRS	+= $(NETMF_PK_ROOT)/DeviceCode/Targets/Native/FM3/DeviceCode/FM3_SPI
SRCDIRS	+= $(NETMF_PK_ROOT)/DeviceCode/Targets/Native/FM3/DeviceCode/FM3_Time
SRCDIRS	+= $(NETMF_PK_ROOT)/DeviceCode/Targets/Native/FM3/DeviceCode/FM3_USART
SRCDIRS += $(NETMF_PK_ROOT)/DeviceCode/Initialization
SRCDIRS += $(NETMF_PK_ROOT)/DeviceCode/pal/AsyncProcCall
SRCDIRS	+= $(NETMF_PK_ROOT)/DeviceCode/pal/BlockStorage
SRCDIRS	+= $(NETMF_PK_ROOT)/DeviceCode/pal/COM
SRCDIRS	+= $(NETMF_PK_ROOT)/DeviceCode/pal/COM/usart
SRCDIRS	+= $(NETMF_PK_ROOT)/DeviceCode/pal/COM/sockets/Ssl/stubs
SRCDIRS	+= $(NETMF_PK_ROOT)/DeviceCode/pal/COM/sockets/stubs
SRCDIRS	+= $(NETMF_PK_ROOT)/DeviceCode/pal/COM/usb/stubs
SRCDIRS	+= $(NETMF_PK_ROOT)/DeviceCode/pal/configuration
SRCDIRS	+= $(NETMF_PK_ROOT)/DeviceCode/pal/events
SRCDIRS	+= $(NETMF_PK_ROOT)/DeviceCode/pal/fs
SRCDIRS	+= $(NETMF_PK_ROOT)/DeviceCode/pal/heap
SRCDIRS	+= $(NETMF_PK_ROOT)/DeviceCode/pal/SimpleHeap
SRCDIRS	+= $(NETMF_PK_ROOT)/DeviceCode/pal/SimpleHeap_config
SRCDIRS	+= $(NETMF_PK_ROOT)/DeviceCode/pal/time
SRCDIRS	+= $(NETMF_PK_ROOT)/DeviceCode/pal/tinycrt
SRCDIRS	+= $(NETMF_PK_ROOT)/DeviceCode/pal/watchdog/stubs
SRCDIRS	+= $(NETMF_PK_ROOT)/Solutions/$(SOLUTION_NAME)/NativeSample
SRCDIRS	+= $(NETMF_PK_ROOT)/Solutions/$(SOLUTION_NAME)/DeviceCode/Blockstorage/addDevices
SRCDIRS	+= $(NETMF_PK_ROOT)/Solutions/$(SOLUTION_NAME)/DeviceCode/Blockstorage/InternalFlash
SRCDIRS	+= $(NETMF_PK_ROOT)/Solutions/$(SOLUTION_NAME)/DeviceCode/Blockstorage/SD
SRCDIRS	+= $(NETMF_PK_ROOT)/Solutions/$(SOLUTION_NAME)/DeviceCode/DebuggerPort
SRCDIRS	+= $(NETMF_PK_ROOT)/Solutions/$(SOLUTION_NAME)/DeviceCode/Display
SRCDIRS	+= $(NETMF_PK_ROOT)/Solutions/$(SOLUTION_NAME)/DeviceCode/FS
SRCDIRS	+= $(NETMF_PK_ROOT)/Solutions/$(SOLUTION_NAME)/DeviceCode/Init
SRCDIRS	+= $(NETMF_PK_ROOT)/Solutions/$(SOLUTION_NAME)/DeviceCode/USB
SRCDIRS += $(NETMF_PK_ROOT)/Support/crc
ifdef EnableLWIP
SRCDIRS	+= $(NETMF_PK_ROOT)/DeviceCode/pal/COM/sockets_lwip/
SRCDIRS += $(NETMF_PK_ROOT)/DeviceCode/pal/lwip/lwip_1_3_2/contrib/ports/arm/proj/lwIPv4lib
SRCDIRS += $(NETMF_PK_ROOT)/DeviceCode/pal/lwip/lwip_1_3_2/src/api
SRCDIRS += $(NETMF_PK_ROOT)/DeviceCode/pal/lwip/lwip_1_3_2/src/core
SRCDIRS += $(NETMF_PK_ROOT)/DeviceCode/pal/lwip/lwip_1_3_2/src/core/ipv4
SRCDIRS += $(NETMF_PK_ROOT)/DeviceCode/pal/lwip/lwip_1_3_2/src/core/ipv6
SRCDIRS += $(NETMF_PK_ROOT)/DeviceCode/pal/lwip/lwip_1_3_2/src/core/snmp
SRCDIRS += $(NETMF_PK_ROOT)/DeviceCode/pal/lwip/lwip_1_3_2/src/netif
SRCDIRS += $(NETMF_PK_ROOT)/DeviceCode/pal/lwip/lwip_1_3_2/src/netif/ppp
SRCDIRS += $(NETMF_PK_ROOT)/DeviceCode/pal/lwip/SocketsDriver
SRCDIRS += $(NETMF_PK_ROOT)/DeviceCode/pal/lwip/SocketsDriver/stubs
SRCDIRS += $(NETMF_PK_ROOT)/DeviceCode/pal/lwip/tinyclr
SRCDIRS += $(NETMF_PK_ROOT)/DeviceCode/Targets/Native/FM3/DeviceCode/FM3_EMAC_lwip
SRCDIRS	+= $(NETMF_PK_ROOT)/Solutions/$(SOLUTION_NAME)/DeviceCode/Network
SRCDIRS += $(NETMF_PK_ROOT)/DeviceCode/Drivers/Ethernet/loopback_lwip
endif
ASMDIRS	= $(NETMF_PK_ROOT)/DeviceCode/Targets/Native/FM3/DeviceCode/CortexM3/TinyHal/GNU_S/

#########################################################################
# directories for include files 
INCS	= -I$(TARGET_ROOT)
INCS	+= -I$(NETMF_PK_ROOT)/CLR/Include
INCS	+= -I$(NETMF_PK_ROOT)/DeviceCode
INCS	+= -I$(NETMF_PK_ROOT)/DeviceCode/Drivers/BlockStorage/SD
INCS	+= -I$(NETMF_PK_ROOT)/DeviceCode/Drivers/Display/autoLCD/
INCS	+= -I$(NETMF_PK_ROOT)/DeviceCode/Include
INCS    += -I$(NETMF_PK_ROOT)/DeviceCode/Initialization
INCS	+= -I$(NETMF_PK_ROOT)/DeviceCode/Targets/Native/FM3
INCS	+= -I$(NETMF_PK_ROOT)/DeviceCode/Targets/Native/FM3/DeviceCode/
INCS	+= -I$(NETMF_PK_ROOT)/DeviceCode/Targets/Native/FM3/DeviceCode/FM3_FLASH
INCS	+= -I$(NETMF_PK_ROOT)/Solutions/$(SOLUTION_NAME)
INCS    += -I$(NETMF_PK_ROOT)/DeviceCode/pal/lwip/lwip_1_3_2/contrib/ports/arm/include
INCS    += -I$(NETMF_PK_ROOT)/DeviceCode/pal/lwip/lwip_1_3_2/contrib/ports/arm/proj/lwIPv4lib
INCS    += -I$(NETMF_PK_ROOT)/DeviceCode/pal/lwip/lwip_1_3_2/src/include
INCS    += -I$(NETMF_PK_ROOT)/DeviceCode/pal/lwip/lwip_1_3_2/src/include/arch
INCS    += -I$(NETMF_PK_ROOT)/DeviceCode/pal/lwip/lwip_1_3_2/src/include/ipv4
INCS    += -I$(NETMF_PK_ROOT)/DeviceCode/pal/lwip/lwip_1_3_2/src/include/ipv6
INCS    += -I$(NETMF_PK_ROOT)/DeviceCode/pal/lwip/lwip_1_3_2/src/include/lwip
INCS    += -I$(NETMF_PK_ROOT)/DeviceCode/pal/lwip/lwip_1_3_2/src/netif/ppp
INCS    += -I$(NETMF_PK_ROOT)/DeviceCode/pal/lwip/SocketsDriver
INCS    += -I$(NETMF_PK_ROOT)/DeviceCode/pal/net
INCS    += -I$(NETMF_PK_ROOT)/DeviceCode/Targets/Native/FM3/DeviceCode/FM3_EMAC_lwip
INCS    += -I$(NETMF_PK_ROOT)/DeviceCode/Drivers/Ethernet/loopback_lwip
#INCS	+= -I$(NETMF_PK_ROOT)/Solutions/$(SOLUTION_NAME)/NativeSample
INCS	+= -I$(NETMF_PK_ROOT)/Support/Include
VPATH	= $(SRCDIRS) $(ASMDIRS) $(OUTDIR)

#########################################################################
# source file names
ifdef EnableLWIP
ifdef EnablelwIPv4lip
CSRCS   = default_user_config.c
CSRCS   += initlib.c
endif
CSRCS   += api_lib.c
CSRCS   += api_msg.c
CSRCS   += err.c
CSRCS   += netbuf.c
CSRCS   += netdb.c
CSRCS   += netifapi.c
CSRCS   += sockets.c
CSRCS   += tcpip.c
CSRCS   += dhcp.c
CSRCS   += dns.c
CSRCS   += init.c
CSRCS   += mem.c
CSRCS   += memp.c
CSRCS   += netif.c
CSRCS   += pbuf.c
CSRCS   += raw.c
CSRCS   += stats.c
CSRCS   += sys.c
CSRCS   += tcp.c
CSRCS   += tcp_in.c
CSRCS   += tcp_out.c
CSRCS   += udp.c
CSRCS   += autoip.c
CSRCS   += icmp.c
CSRCS   += igmp.c
CSRCS   += inet.c
CSRCS   += inet_chksum.c
CSRCS   += ip.c
CSRCS   += ip_addr.c
CSRCS   += ip_frag.c
ifdef EnableIPV6
CSRCS   += icmp6.c
CSRCS   += inet6.c
CSRCS   += ip6.c
CSRCS   += ip6_addr.c
endif
CSRCS   += asn1_dec.c
CSRCS   += asn1_enc.c
CSRCS   += mib2.c
CSRCS   += mib_structs.c
CSRCS   += msg_in.c
CSRCS   += msg_out.c
CSRCS   += etharp.c
CSRCS   += ethernetif.c
CSRCS   += loopif.c
CSRCS   += slipif.c
ifdef EnablePPP
CSRCS   += auth.c
CSRCS   += chap.c
CSRCS   += chpms.c
CSRCS   += fsm.c
CSRCS   += ipcp.c
CSRCS   += lcp.c
CSRCS   += magic.c
CSRCS   += md5.c
CSRCS   += pap.c
CSRCS   += ppp.c
CSRCS   += ppp_oe.c
CSRCS   += randm.c
CSRCS   += vj.c
endif
endif
CPPSRCS	= NativeSample_TestSD.cpp
CPPSRCS += tinyhal_modified.cpp
ifdef EnableLWIP
CPPSRCS += loopback_lwip.cpp
CPPSRCS += loopback_lwip_driver.cpp
CPPSRCS += loopback_lwip_driver_config.cpp
CPPSRCS += sockets_lwip.cpp
CPPSRCS += ConfigHelper.cpp
CPPSRCS += lwIP__Sockets.cpp
CPPSRCS += lwIP_Sockets_functions.cpp
CPPSRCS += sys__tinyclr.cpp
CPPSRCS += sys_arch__tinyclr.cpp
CPPSRCS += FM3_EMAC_lwip.cpp
CPPSRCS += FM3_EMAC_lwip_driver.cpp
CPPSRCS += Network_config.cpp
endif
ifdef EnableSD
CPPSRCS += SD_BL_driver.cpp
endif
ifdef EnableLCD_DST2401PH
CPPSRCS += autoLCD_driver.cpp
CPPSRCS += autoLCD_functions.cpp
CPPSRCS += font8x15.cpp
else
CPPSRCS += stubs_config.cpp
CPPSRCS += stubs_functions.cpp
endif
CPPSRCS	+= FAT_File.cpp
CPPSRCS	+= FAT_FileHandle.cpp
CPPSRCS	+= FAT_FS.cpp
CPPSRCS	+= FAT_FS_Utility.cpp
CPPSRCS	+= FAT_LogicDisk.cpp
CPPSRCS	+= FAT_MemoryManager.cpp
CPPSRCS	+= FAT_SectorCache.cpp
CPPSRCS	+= fs.cpp
CPPSRCS	+= stubs_functions_cache.cpp
CPPSRCS	+= stubs_functions_EBIU.cpp
CPPSRCS += stubs_functions_WATCHDOG.cpp
CPPSRCS	+= stubs_functions_USB.cpp
CPPSRCS += SmartPtr_cortex.cpp
CPPSRCS += Aborts.cpp
CPPSRCS += FM3_bootstrap.cpp
CPPSRCS += FM3_Flash_driver.cpp
CPPSRCS += FM3_GPIO_functions.cpp
CPPSRCS += FM3_intc_functions.cpp
CPPSRCS += FM3_Power_functions.cpp
CPPSRCS += FM3_SPI_functions.cpp
CPPSRCS += FM3_time_functions.cpp
CPPSRCS += FM3_usart_functions.cpp
CPPSRCS += OEM_Model_SKU_NetworkID.cpp
CPPSRCS	+= Completions.cpp
CPPSRCS	+= Continuations.cpp
CPPSRCS += blockstoragelist.cpp
CPPSRCS += ssl_pal_stubs.cpp
CPPSRCS	+= ComDirector.cpp
CPPSRCS	+= usart.cpp
ifndef EnableLWIP
CPPSRCS += sockets_stubs.cpp
endif
CPPSRCS	+= usb_stubs.cpp
CPPSRCS	+= events.cpp
CPPSRCS	+= fs_pal.cpp
CPPSRCS += heap.cpp
CPPSRCS += SimpleHeap.cpp
CPPSRCS += SimpleHeap_config.cpp
CPPSRCS	+= time_driver.cpp
CPPSRCS	+= time_functions.cpp
CPPSRCS	+= tinycrt.cpp
CPPSRCS	+= Watchdog_stubs.cpp
CPPSRCS += Bl_addDevices.cpp
CPPSRCS += InternalFlash_Bl_Config.cpp
ifdef EnableSD
CPPSRCS += SD_Bl_Config.cpp
endif
ifdef EnableLCD_DST2401PH
CPPSRCS += Display_DST2401PH_config.cpp
CPPSRCS += Display_DST2401PH_functions.cpp
endif
CPPSRCS += DebuggerPort_SSL.cpp
CPPSRCS += FS_config.cpp
CPPSRCS += IO_Init.cpp
CPPSRCS += usb_config.cpp
CPPSRCS += CRC.cpp
ASMSRCS = FirstEntry.s
ASMSRCS	+= IDelayLoop.s
ASMSRCS	+= VectorsHandlers.s

#########################################################################
# object files
OBJS	= $(CSRCS:%.c=%.o) $(CPPSRCS:%.cpp=%.o)
ASMOBJS	= $(ASMSRCS:%.s=%.o) 
OBJSDIR	= $(addprefix $(OUTDIR)/, $(OBJS))
ASMOBJSDIR	= $(addprefix $(OUTDIR)/, $(ASMOBJS))

#########################################################################
# make rules

all: $(ASMOBJS) $(OBJS) $(LDSCRIPT)
	$(LD) $(LDFLAG) -Map $(OUTDIR)/$(TARGET).map -T $(LDSCRIPT) $(ASMOBJSDIR) $(OBJSDIR) -L$(GCCLIB1) -L$(GCCLIB2) $(LIBS) -o $(OUTDIR)/$(TARGET).elf
	$(OBJCOPY) -S -j ER_FLASH -j ER_RAM_RO -j ER_RAM_RW -O srec --gap-fill 0 --srec-forceS3 $(OUTDIR)/$(TARGET).elf $(OUTDIR)/$(TARGET).srec
	$(OBJCOPY) -S -j ER_FLASH -j ER_RAM_RO -j ER_RAM_RW -O ihex --gap-fill 0 $(OUTDIR)/$(TARGET).elf $(OUTDIR)/$(TARGET).ihex
#	$(OBJCOPY) -O binary $(OUTDIR)/$(TARGET).elf $(OUTDIR)/$(TARGET).bin
#	$(OBJDUMP) -D $(OUTDIR)/$(TARGET).elf > $(OUTDIR)/$(TARGET).txt	

.c.o:
	$(CC) $(CFLAGS) -g -c $(INCS) $< -o $(OUTDIR)/$@

.cpp.o:
	$(CC) $(CPPFLAGS) -g -c $(INCS) $< -o $(OUTDIR)/$@

.s.o: 
	$(AS) $< -g -o $(OUTDIR)/$@

clean:
	rm $(OUTDIR)/*.o $(OUTDIR)/$(TARGET).elf $(OUTDIR)/$(TARGET).map $(OUTDIR)/$(TARGET).srec $(OUTDIR)/$(TARGET).ihex
#	rm $(OUTDIR)/*.o $(OUTDIR)/$(TARGET).elf $(OUTDIR)/$(TARGET).map $(OUTDIR)/$(TARGET).bin $(OUTDIR)/$(TARGET).txt
