##
# Open-BLDC - Open BruschLess DC Motor Controller
# Copyright (c) 2009 Piotr Esden-Tempski <piotr@esden.net>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
#    derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
##

NAME   ?= main

PREFIX ?= arm-elf
STM32LIB_PATH ?= ../../stm32/
STM32INCLUDE_PATH ?=$(STM32LIB_PATH)
LINKER_SCRIPT_PATH ?=$(STM32LIB_PATH)

OOCD_INTERFACE ?= openocd-usb
OOCD_TARGET    ?= olimex_stm32_h103

CC      = $(PREFIX)-gcc
LD      = $(PREFIX)-ld
AR      = $(PREFIX)-ar
AS      = $(PREFIX)-as
CP      = $(PREFIX)-objcopy
OD	= $(PREFIX)-objdump
SIZE    = $(PREFIX)-size
OOCD	= openocd

TOOLCHAIN_LIB_NEWLIB = `dirname \`which $(CC)\``/../$(PREFIX)/lib
#TOOLCHAIN_LIB_DIR2 = `dirname \`which $(CC)\``/..//lib/gcc/$(PREFIX)/4.4.0/thumb
TOOLCHAIN_LIB_GCC = `dirname \`which $(CC)\``/../lib/gcc/$(PREFIX)/4.4.0


CFLAGS   =  -I $(STM32INCLUDE_PATH) -Wall -std=c99 -c -fno-common -Os -g -mcpu=cortex-m3 -mtune=cortex-m3 -mthumb -ffunction-sections -fdata-sections -Wswitch-enum -Wc++-compat -mfloat-abi=soft -mfpu=fpa
#-ffunction-sections -fdata-sections put functions and data in separated sections. Is problematic with -g
#-fno-common puts global uninitialize variables in .data section and not in common sections
ASFLAGS  = -ahls -mapcs-32
LDFLAGS  = -v -T$(LINKER_SCRIPT_PATH)/stm32.ld -nostartfiles -L$(STM32LIB_PATH) -L$(TOOLCHAIN_LIB_GCC) -L$(TOOLCHAIN_LIB_NEWLIB) -O0 
#LDFLAGS  = -v -Tblink.ld -nostartfiles -L$(STM32LIB_PATH) -L$(TOOLCHAIN_LIB_DIR2) -L$(TOOLCHAIN_LIB_DIR1) -O0 --no-gc-sections
#--gc-section: garbage collection of functions that are not used by anybody
#-nostartfiles: related to not use stdlib 
LDLIBS   = -lopenstm32 -lm -lc -lgcc
CPFLAGS  = -j .isr_vector -j .text -j .data
#CPFLAGS =
#-j copy only the referenced section to the output file
ODFLAGS	 = -S
#-S intermixes source code with disassembly
SIZEFLAGS = -A -x

OBJECTS = main.o 

all: gccversion images size


.SUFFIXES: .elf .bin .hex .srec .lst

images: $(NAME).bin $(NAME).hex $(NAME).srec $(NAME).lst


$(NAME).elf: $(OBJECTS)
	@echo -e "  LD\t$@"
	@$(LD) $(LDFLAGS) -o $(NAME).elf  $(OBJECTS) $(LDLIBS) 

clean:
	@for i in $(OBJECTS) $(NAME).elf $(NAME).hex $(NAME).bin $(NAME).lst $(NAME).srec; \
	do \
		echo -e "  CLEAN\t$$i"; \
		rm -f $$i; \
	done
	@rm -f *~

flash: $(NAME).hex
	@echo -e "  OOCD\t$<"
	@$(OOCD) -f interface/$(OOCD_INTERFACE).cfg \
		 -f board/$(OOCD_TARGET).cfg \
		 -c init \
		 -c "reset run" \
		 -c "halt" \
		 -c "flash write_image erase $(NAME).hex" \
		 -c reset \
		 -c shutdown

gccversion:
	@$(CC) --version

size: $(NAME).elf
	@echo
	@$(SIZE) $(SIZEFLAGS) $<

# Suffix rules

%.bin: %.elf
	@echo -e "  CP\t$@"
	@$(CP) $(CPFLAGS) -Obinary $< $@

%.hex: %.elf
	@echo -e "  CP\t$@"
	@$(CP) $(CPFLAGS) -Oihex -v $< $@

%.srec: %.elf
	@echo -e "  CP\t$@"
	@$(CP) $(CPFLAGS) -Osrec $< $@

%.lst: %.elf
	@echo -e "  OD\t$@"
	@$(OD) $(ODFLAGS) $(NAME).elf > $(NAME).lst

%.o: %.c %.h
	@echo -e "  CC\t$@"
	@$(CC) $(CFLAGS) $<
