﻿

#!!!!!!!!!!!!!!!!!!USER CONFIG VARIABLES!!!!!!!!!!!!!!!!
#-------------------------------------------------------------------------------
# Prj and file name
TARGET  = template
#Used mcu line
DEFINES += STM32F030
MCU += -mcpu=cortex-m0
#Target file for OpenOCD
OCDTFILE += stlink-v2
#Interface file for OpenOCD
OCDCFILE += stm32f0x
#For Debug add in attach.gdb interface and target file name

#Optimization
OPT += -Os
#OPT += -ggdb3

DEFINES += DEBUG
#-------------------------------------------------------------------------------

#Toolchain
#-------------------------------------------------------------------------------
AS = arm-none-eabi-gcc
CC = arm-none-eabi-gcc
LD = arm-none-eabi-g++
CP = arm-none-eabi-objcopy
SZ = arm-none-eabi-size
RM = rm
CXX = arm-none-eabi-g++
GDB = arm-none-eabi-gdb
OCD = openocd
#-------------------------------------------------------------------------------

#OpenOCD config
#-------------------------------------------------------------------------------
OCDCFG = -f interface/$(OCDTFILE).cfg
OCDCFG += -f target/$(OCDCFILE).cfg
OCDCFG += -s scripts
OCDFL = --eval-command="target remote localhost:3333"
#-------------------------------------------------------------------------------

#startup file
#-------------------------------------------------------------------------------
STARTUP = startup_stm32.s
#-------------------------------------------------------------------------------

#Source path
#-------------------------------------------------------------------------------
SOURCEDIRS := src
#-------------------------------------------------------------------------------

#Header path
#-------------------------------------------------------------------------------
INCLUDES += inc
#-------------------------------------------------------------------------------

#GCC config
#-------------------------------------------------------------------------------
CFLAGS += -mthumb $(MCU)
CFLAGS += -mfloat-abi=soft
CFLAGS += -Wall -pedantic
CFLAGS += $(OPT)
CFLAGS += -fno-builtin
CFLAGS += -Wall -fmessage-length=0
CFLAGS += -ffunction-sections -fdata-sections
CFLAGS += -nostdlib
CFLAGS += -fno-exceptions
CFLAGS += $(addprefix -I, $(INCLUDES))
CFLAGS += $(addprefix -D, $(DEFINES))
#-------------------------------------------------------------------------------


#For C only
#-------------------------------------------------------------------------------
FLAGS  = -std=gnu99
#-------------------------------------------------------------------------------
#For C++ only
#-------------------------------------------------------------------------------
CXXFL  = -std=gnu++14 -fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables
#-------------------------------------------------------------------------------

#Linker script
#-------------------------------------------------------------------------------
LDSCRIPT   = LinkerScript.ld
#-------------------------------------------------------------------------------

#Linker config
#-------------------------------------------------------------------------------
LDFLAGS += -nostartfiles  -nostdlib -gc-sections -mthumb $(MCU)
LDFLAGS += -T $(LDSCRIPT)
#-------------------------------------------------------------------------------

#ASM config
#-------------------------------------------------------------------------------
AFLAGS += -Wnls -mapcs
#-------------------------------------------------------------------------------

#Obj file list
#-------------------------------------------------------------------------------
OBJS += $(patsubst %.c, %.o, $(wildcard  $(addsuffix /*.c, $(SOURCEDIRS))))
OBJS += $(patsubst %.cpp, %.o, $(wildcard  $(addsuffix /*.cpp, $(SOURCEDIRS))))
OBJS += $(patsubst %.s, %.o, $(STARTUP))
#-------------------------------------------------------------------------------

#List files for clean project
#-------------------------------------------------------------------------------
MRPROPER += openocd.log
MRPROPER += $(addsuffix /*.o, $(SOURCEDIRS))
MRPROPER += $(addsuffix /*.d, $(SOURCEDIRS))
MRPROPER += $(patsubst %.s, %.o, $(STARTUP))
TOREMOVE += *.elf *.hex *.bin
TOREMOVE += $(TARGET)
TOREMOVE += $(MRPROPER)
#-------------------------------------------------------------------------------


#Make all
#-------------------------------------------------------------------------------
all: size $(TARGET).hex $(TARGET).bin $(TARGET).elf
#-------------------------------------------------------------------------------

#Clean
#-------------------------------------------------------------------------------
clean:
	@$(RM) -f $(TOREMOVE)
#-------------------------------------------------------------------------------

#Show programm size
#-------------------------------------------------------------------------------
size: $(TARGET).elf
	@echo "---------------------------------------------------"
	@$(SZ) $(TARGET).elf
#-------------------------------------------------------------------------------

#Compile HEX file 
#-------------------------------------------------------------------------------
$(TARGET).hex: $(TARGET).elf
	@$(CP) -Oihex $(TARGET).elf $(TARGET).hex
#-------------------------------------------------------------------------------

#Compile BIN file
#-------------------------------------------------------------------------------
$(TARGET).bin: $(TARGET).elf
	@$(CP) -Obinary $(TARGET).elf $(TARGET).bin
#-------------------------------------------------------------------------------

#Linking
#-------------------------------------------------------------------------------
$(TARGET).elf: $(OBJS)
	@$(LD) $(LDFLAGS) $^ -o $@
#-------------------------------------------------------------------------------

#Compile Obj files from C
#-------------------------------------------------------------------------------
%.o: %.c
	@$(CC) $(CFLAGS) $(FLAGS) -MD -c $< -o $@
#-------------------------------------------------------------------------------

#Compile Obj files from C++
#-------------------------------------------------------------------------------
%.o: %.cpp
	@$(CXX) $(CFLAGS) $(CXXFL) -MD -c $< -o $@
#-------------------------------------------------------------------------------

#Compile Obj files from asm
#-------------------------------------------------------------------------------
%.o: %.s
	@$(AS) $(AFLAGS) -c $< -o $@
#-------------------------------------------------------------------------------

#Load firmware for STM with STLINK V2
#-------------------------------------------------------------------------------
load: $(TARGET).hex
	$(OCD) $(OCDCFG) -c "init" -c "reset init" -c "flash write_image erase $(TARGET).hex" -c "reset" -c "shutdown"
# reset_config srst_only srst_nogate connect_assert_srst
#	$(OCD) -f scr -c "reset" -c "flash write_image erase $(TARGET).hex" -c "reset" -c "shutdown"
#-------------------------------------------------------------------------------

#Run debug with SWD and openocd in PIPE mode
#-------------------------------------------------------------------------------
debug: $(TARGET).elf
	$(GDB) $< -x run.gdb
#-------------------------------------------------------------------------------
