# put your *.o targets here, make should handle the rest!

SRCS = main.c system_stm32f4xx.c newlib_stubs.c stm32f4xx_it.c

# all the files will be generated with this name (main.elf, main.bin, main.hex, etc)

PROJ_NAME = dibase
OUT_DIR = bin/

###################################################

CROSS_COMPILE = /usr/bin/arm-stm32/bin/arm-none-eabi-
CC=$(CROSS_COMPILE)gcc
OBJCOPY=$(CROSS_COMPILE)objcopy

###################################################

vpath %.c src
vpath %.a lib

ROOT=$(shell pwd)

CFLAGS = -g -O2 -std=c99 -Wfatal-errors -Tstm32_flash.ld
#CFLAGS += -Wall 
CFLAGS += -mlittle-endian -mthumb -mcpu=cortex-m4 -mthumb-interwork
CFLAGS += -mfloat-abi=hard -mfpu=fpv4-sp-d16

INCLUDE_DIRS = -Iinc -Ilib -Ilib/inc -Idrivers/Ethernet/inc -Idrivers/USB_CDC/inc -Idrivers/Comm
INCLUDE_DIRS += -Ilib/inc/core -Ilib/inc/peripherals

DEFINES = -DUSE_STM32F4_DISCOVERY -DUSE_USB_OTG_FS -DUSE_STDPERIPH_DRIVER

CFLAGS += $(INCLUDE_DIRS) $(DEFINES)

SRCS += lib/src/*.c
SRCS += lib/startup_stm32f4xx.s # add startup file to build
SRCS += drivers/Ethernet/src/*.c
SRCS += drivers/Comm/*.c
SRCS += drivers/USB_CDC/src/*.c

OBJS = $(SRCS:.c=.o)

###################################################

.PHONY: lib proj

rebuild: clean all

all: lib proj

lib:
	$(MAKE) -C lib

proj: 	$(PROJ_NAME).elf

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

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