##
# 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 = libopenstm32.a

PREFIX ?= arm-elf


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_GCC = `dirname \`which $(CC)\``/../lib/gcc/$(PREFIX)/4.4.0
TOOLCHAIN_LIB_NEWLIB = `dirname \`which $(CC)\``/../$(PREFIX)/lib

CFLAGS   =  -I. -Wall -std=c99 -c -fno-common -O0 -g -mcpu=cortex-m3 -mtune=cortex-m3 -mfloat-abi=soft -mfpu=fpa -mthumb -ffunction-sections -fdata-sections -Wmissing-prototypes
#-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

OBJECTS = stm32f103.o\
	arm_cm3.o \
	gpio.o \
	rcc.o \
	utils.o \
	tim.o \
	nvic.o \
	usart.o \
	adc.o \
	flash.o 

all: gccversion $(NAME)

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

gccversion:
	$(CC) --version

$(NAME): $(OBJECTS)
	@echo -e "  AR\t$@"
	@$(AR) -rcs $(NAME) $(OBJECTS) 	

stm32f103.o: stm32.ld

# Pattern rules
%.o: %.c %.h mem_map.h types.h
	@echo -e "  CC\t$@"
	@$(CC) $(CFLAGS) $<

