MCU		= cortex-m3 ;
CHIP		= STM32F103ZE ;
BOARD		= maple_native ;

#CHIP		= at91sam3u4 ;
#BOARD		= sam3u-ek ;

if ! $(TOOLSET)
{
	TOOLSET = devkit ;
	Echo "Assuming TOOLSET=devkit" ;
}

if $(TOOLSET) = yagarto
{
	TOOLS_PATH	= ~/Source/yagarto-4.6.2/bin ;
	TOOLS_ARCH	= arm-none-eabi- ;
}
if $(TOOLSET) = yagarto-install
{
	TOOLS_PATH	= ~/Source/yagarto/install/bin ;
	TOOLS_ARCH	= arm-none-eabi- ;
}
else if $(TOOLSET) = devkit
{
	TOOLS_PATH	= /opt/devkitARM/bin ;
	TOOLS_ARCH	= arm-eabi- ;
}
else if $(TOOLSET) = maple
{
	TOOLS_PATH	= /opt/Maple/Resources/Java/hardware/tools/arm/bin ; 
	TOOLS_ARCH	= arm-none-eabi- ;
}
else if $(TOOLSET) = ports
{
	TOOLS_PATH	= /opt/local/bin ;
	TOOLS_ARCH	= arm-none-eabi- ;
}

CC	  	= $(TOOLS_PATH)/$(TOOLS_ARCH)gcc ;
C++	 	= $(TOOLS_PATH)/$(TOOLS_ARCH)g++ ;
AS	  	= $(TOOLS_PATH)/$(TOOLS_ARCH)gcc -c ;
LINK	  	= $(TOOLS_PATH)/$(TOOLS_ARCH)g++ ;
OBJCOPY 	= $(TOOLS_PATH)/$(TOOLS_ARCH)objcopy ;
DFU		= dfu-util ;

DEFINES  	+= VECT_TAB_FLASH BOARD_$(BOARD) MCU_$(CHIP) ERROR_LED_PORT=GPIOC ERROR_LED_PIN=15 STM32_HIGH_DENSITY MAPLE_IDE ;
OPTIM		= -Os ;
MFLAGS		= cpu=$(MCU) thumb arch=armv7-m ;
CCFLAGS  	= -Wall -m$(MFLAGS) -g -nostdlib -ffunction-sections -fdata-sections -Wl,--gc-sections ;
C++FLAGS  	= $(CCFLAGS) -fno-rtti -fno-exceptions ; 
LINKFLAGS  	+= -m$(MFLAGS) -Xlinker --gc-sections ;
DFUFLAGS	= -a1 -d 0x1eaf:0x0003 -R ;

MAPLE_DIR	= $(HOME)/Source/SAM3U/libmaple ;
MAPLE_LIBS	= Servo LiquidCrystal Wire FreeRTOS ;
MAPLE_SUBDIRS	= wirish wirish/comm wirish/boards libmaple libmaple/usb libmaple/usb/usb_lib ; 

SKETCH_DIR	= $(HOME)/Source/Arduino ;
SKETCH_LIBS	= RF24 ;

MODULE_DIRS	= . $(MAPLE_DIR)/$(MAPLE_SUBDIRS) $(MAPLE_DIR)/libraries/$(MAPLE_LIBS) $(SKETCH_DIR)/libraries/$(SKETCH_LIBS) ;
HDRS		= $(MODULE_DIRS) ;
LOCATE_TARGET	= out/$(TOOLSET) ;
LOCATE_SOURCE	= $(LOCATE_TARGET) ; 

rule Pde
{
	Depends $(<) : $(>) ;
	MakeLocate $(<) : $(LOCATE_SOURCE) ;
	Clean clean : $(<) ;
}

if ( $(ARDUINO_VERSION) < 100 )
{
	ARDUINO_H = WProgram.h ;
}
else
{
	ARDUINO_H = Arduino.h ;
}

actions Pde
{
	echo "#include <$(ARDUINO_H)>" > $(<) 
	echo "#line 1 \"$(>)\"" >> $(<)
	cat $(>) >> $(<) 
}

rule C++Pde
{
	local _CPP = $(>:B).cpp ;
	Pde $(_CPP) : $(>) ;
	C++ $(<) : $(_CPP) ;
}

rule Hex
{
	Depends $(<) : $(>) ;
	MakeLocate $(<) : $(LOCATE_TARGET) ;
	Depends hex : $(<) ;
	Clean clean : $(<) ;
}

actions Hex
{
	$(OBJCOPY) -O ihex $(>) $(<)
}

rule Binary
{
	Depends $(<) : $(>) ;
	MakeLocate $(<) : $(LOCATE_TARGET) ;
	Depends binary : $(<) ;
	Clean clean : $(<) ;
}

actions Binary
{
	$(OBJCOPY) -O binary $(>) $(<)
}

rule UserObject
{
	switch $(>:S)
	{
		case .S : As $(<) : $(>) ;
		case .ino : C++Pde $(<) : $(>) ;
		case .pde : C++Pde $(<) : $(>) ;
	}
}

rule Upload
{
	Depends up : $(<) ;
	NotFile up ;
	Always $(<) ;
	Always up ;
}

actions Upload
{
	$(DFU) $(DFUFLAGS)  -D $(<)
}

# Override base objects rule, so all output can go in the output dir
rule Objects
{
        local _i ;

        for _i in [ FGristFiles $(<) ]
        {
		local _b = $(_i:B)$(SUFOBJ) ;
		local _o = $(_b:G=$(SOURCE_GRIST:E)) ;
                Object $(_o) : $(_i) ;
                Depends obj : $(_o) ;
        }
}

# Override base main rule, so all output can go in the output dir
rule Main
{
        MainFromObjects $(<) : $(>:B)$(SUFOBJ) ;
        Objects $(>) ;
}

# Modules
MODULES	= [ GLOB $(MODULE_DIRS) : *.pde *.c *.cpp *.S ] ; 

# Main output executable
MAIN		= $(PWD:B).elf ;

# Linker script
LINK_DIR	= $(MAPLE_DIR)/support/ld ;
LINKSCRIPT	= $(LINK_DIR)/$(BOARD)/flash.ld ; 

# Bring in the map and link script
LINKFLAGS	+= -Wl,-Map=$(LOCATE_TARGET)/$(MAIN:B).map -T$(LINKSCRIPT) -L$(LINK_DIR) ;

Main $(MAIN) : $(MODULES) ;
Binary $(MAIN:B).bin : $(MAIN) ;
Upload $(MAIN:B).bin ;
