# Path to stlink folder for uploading code to board
STLINK=~/Embedded/stlink-1.5.1

# Put your source files here (*.c)
SRCS=main.c system_stm32f10x.c

# Libraries source files, add ones that you intent to use
SRCS += stm32f10x_rcc.c
SRCS += stm32f10x_gpio.c
SRCS += stm32f10x_it.c

# Binaries will be generated with this name (.elf, .bin, .hex)
PROJ_NAME=a

# Put your STM32F1 library code directory here, change YOURUSERNAME to yours
STM_COMMON=/home/misha/stm/STM32F10x_StdPeriph_Lib_V3.6.0

# Compiler settings. Only edit CFLAGS to include other header files.
CC=arm-none-eabi-gcc
OBJCOPY=arm-none-eabi-objcopy

# Compiler flags
CFLAGS  = -g -O2 -Wall -Tstm32_flash.ld 
CFLAGS += -DUSE_STDPERIPH_DRIVER
CFLAGS += -mcpu=cortex-m3 -mthumb-interwork -mfix-cortex-m3-ldrd
CFLAGS += -mlittle-endian -mthumb
CFLAGS += -I.
CFLAGS += -u .isr_vectors

# Include files from STM libraries
CFLAGS += -I$(STM_COMMON)/Libraries/CMSIS/CM3/CoreSupport
CFLAGS += -I$(STM_COMMON)/Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x
CFLAGS += -I$(STM_COMMON)/Libraries/STM32F10x_StdPeriph_Driver/inc

# add startup file to build
SRCS += $(STM_COMMON)/Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x/startup/TrueSTUDIO/startup_stm32f10x_ld.s
OBJS = $(SRCS:.c=.o)

vpath %.c $(STM_COMMON)/Libraries/STM32F10x_StdPeriph_Driver/src \

.PHONY: proj

# Commands
all: proj

proj: $(PROJ_NAME).elf

$(PROJ_NAME).elf: $(SRCS)
	$(CC) $(CFLAGS) $^ -o $@
	$(OBJCOPY) -O ihex $(PROJ_NAME).elf $(PROJ_NAME).hex
	$(OBJCOPY) -O binary $(PROJ_NAME).elf $(PROJ_NAME).bin

asm: $(SRCS)
	$(CC) $(CFLAGS) -S $^
clean:
	rm -f *.o $(PROJ_NAME).elf $(PROJ_NAME).hex $(PROJ_NAME).bin

# Flash the STM32F4
flash: proj
	st-flash write $(PROJ_NAME).bin 0x8000000
