ARCH ?= arm
CPU ?= stm32f4xx
ifeq ($(CPU),stm32f4xx)
C_FLAGS += -DARCH_STM32F4XX
endif
ifeq ($(BOARD), stm32f4-discovery)
C_FLAGS += -DBOARD_STM32F4_DISCOVERY
endif
CROSS_COMPILE ?= arm-none-eabi-

INCLUDES += \
	/opt/gcc-arm-none-eabi-5_4-2016q2/arm-none-eabi/include \
	/opt/gcc-arm-none-eabi-5_4-2016q2/lib/gcc/arm-none-eabi/5.4.1/include

GEN_FLAGS += \
	-mcpu=cortex-m4 \
	-mthumb \
	-mfloat-abi=hard \
	-mfpu=fpv4-sp-d16 \
	-O$(OPTIM) \
	-fmessage-length=0 \
	-fsigned-char \
	-ffunction-sections \
	-fdata-sections \
	-ffreestanding \
	-fno-move-loop-invariants \
	-Werror \
	-Wunused \
	-Wuninitialized \
	-Wall \
	-Wextra \
	-Wmissing-declarations \
	-Wconversion \
	-Wpointer-arith \
	-Wpadded \
	-Wshadow \
	-Wlogical-op \
	-Waggregate-return \
	-Wfloat-equal

C_FLAGS += \
	-DDEBUG \
	-DUSE_FULL_ASSERT \
	-DTRACE \
	-DOS_USE_TRACE_SEMIHOSTING_DEBUG \
	-DSTM32F407xx \
	-DUSE_HAL_DRIVER \
	-DHSE_VALUE=8000000 \
	$(addprefix -I, $(INCLUDES)) \
	-std=gnu11 \
	-Wmissing-prototypes \
	-Wstrict-prototypes \
	-Wbad-function-cast \
	-Wno-bad-function-cast \
	-Wno-conversion \
	-Wno-sign-conversion \
	-Wno-unused-parameter \
	-Wno-sign-compare \
	-Wno-missing-prototypes \
	-Wno-missing-declarations \
	-Wno-missing-field-initializers

L_FLAGS := \
	-T mem.ld \
	-T libs.ld \
	-T sections.ld \
	-nostartfiles \
	-Xlinker --gc-sections \
	-L"config/linker" \
	--specs=nano.specs \
	-u _printf_float

ifeq ($(DEBUG),y)
OPTIM = g
GEN_FLAGS += -g3
DBG_EXT = -dbg
else
OPTIM = s
DBG_EXT =
endif

AS_FLAGS := -mapcs-32 -g
AR_FLAGS := rcs

OOCD_IMAGE=$(BIN_FILE)
OOCD_CFG_FILE=$(EXE_DIR)/openocd.cfg

PRE_PROGRAM = echo "telnet_port 4444\ninit\nreset halt\nflash write_image erase $(OOCD_IMAGE) 0x08000000 bin\nreset run\n shutdown\n" > $(OOCD_CFG_FILE)
PROGRAM = openocd -f /usr/share/openocd/scripts/board/stm32f4discovery.cfg -f $(OOCD_CFG_FILE)