

# Path to the STM32L1xx_StdPeriph_Lib_V1.0.3/ directory
STM_COMMON=../..

# Put your stlink folder here so make burn will work.
STLINK=~/stlink

# Put your source files here (or *.c, etc)
SRCS= main.c system_stm32l1xx.c stm32l1xx_it.c 

SRCS+= $(wildcard $(STM_COMMON)/Libraries/STM32L1xx_StdPeriph_Driver/src/*.c)
SRCS+= $(STM_COMMON)/Utilities/STM32L-DISCOVERY/stm32l_discovery_lcd.c

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


# Normally you shouldn't need to change anything below this line!
#######################################################################################

CC=arm-none-eabi-gcc
OBJCOPY=arm-none-eabi-objcopy

CFLAGS  = -g -O2 -Wall -Tstm32_flash.ld
CFLAGS += -mlittle-endian -mthumb -mcpu=cortex-m3 -mthumb-interwork 
CFLAGS += -mfloat-abi=soft -lc
CFLAGS += -DUSE_STDPERIPH_DRIVER -DSTM32L1XX -DHSE_VALUE=8000000 -nostartfiles
CFLAGS += -I.

# Include files from STM libraries
CFLAGS += -I$(STM_COMMON)/Utilities/STM32L-DISCOVERY
CFLAGS += -I$(STM_COMMON)/Libraries/CMSIS/Include -I$(STM_COMMON)/Libraries/CMSIS/Device/ST/STM32L1xx/Include
CFLAGS += -I$(STM_COMMON)/Libraries/STM32L1xx_StdPeriph_Driver/inc

# add startup file to build
SRCS += $(STM_COMMON)/Libraries/CMSIS/Device/ST/STM32L1xx/Source/Templates/gcc_ride7/startup_stm32l1xx_md.s
OBJS = $(SRCS:.c=.o)


.PHONY: proj

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

clean:
	rm -f *.o $(PROJ_NAME).elf $(PROJ_NAME).hex $(PROJ_NAME).bin

# Flash the STM32
burn: proj
	$(STLINK)/st-flash write $(PROJ_NAME).bin 0x8000000
