PROJECT=main

#ProgramPath
TOP=./
PP=$(TOP)/Project/Core/User/

EXECUTABLE=$(PROJECT).elf
BIN_IMAGE=$(PROJECT).bin
HEX_IMAGE=$(PROJECT).hex
LIST_FILE=$(PROJECT).lst


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

CFLAGS= -Os -mlittle-endian -mthumb
CFLAGS+=-mcpu=cortex-m4 -mfpu=fpv4-sp-d16
CFLAGS+=-ffreestanding -nostdlib

# to run from FLASH
CFLAGS+=-Wl,-T,$(TOP)Project/TrueSTUDIO/STM324xG-EVAL/STM32F407IG_FLASH.ld
CFLAGS+=-I$(TOP)
CFLAGS+=-I$(PP)

CFLAGS+=-DVECT_TAB_FLASH
CFLAGS+=-DUSE_STDPERIPH_DRIVER
CFLAGS+=-DSTM32F4XX
CFLAGS+=-std=gnu99


#STM32F4xx_StdPeriph_Driver
CFLAGS+=-I$(TOP)Libraries/STM32F4xx_StdPeriph_Driver/inc
CFLAGS+=-I$(TOP)Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F40x

#Project 
CFLAGS+=-I$(TOP)Project/Config
CFLAGS+=-I$(TOP)Project/Core/Devices/STM32F4xx
CFLAGS+=-I$(TOP)Project/Core/Graphics
CFLAGS+=-I$(TOP)Project/Core/Lib
CFLAGS+=-I$(TOP)Project/Core/Mod_manager

#Modules
CFLAGS+=-I$(TOP)Project/Modules/System
CFLAGS+=-I$(TOP)Project/Modules/Audio_Player
CFLAGS+=-I$(TOP)Project/Modules/Audio_Recorder
CFLAGS+=-I$(TOP)Project/Modules/Camera
CFLAGS+=-I$(TOP)Project/Modules/Calendar
CFLAGS+=-I$(TOP)Project/Modules/Credits
CFLAGS+=-I$(TOP)Project/Modules/Serial
CFLAGS+=-I$(TOP)Project/Modules/USBD
CFLAGS+=-I$(TOP)Project/Modules/Image_Browser
CFLAGS+=-I$(TOP)Project/Modules/Ethernet
CFLAGS+=-I$(TOP)Project/Modules/Common
CFLAGS+=-I$(TOP)Project/Modules/Console
CFLAGS+=-I$(TOP)Project/Modules/File_Manager

#FreeRTOS
CFLAGS+=-I$(TOP)Utilities/Third_Party/FreeRTOS/Source/include
CFLAGS+=-I$(TOP)Utilities/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM4F


#ff
CFLAGS+=-I$(TOP)Utilities/Third_Party/fat_fs/inc

#lwip
CFLAGS+=-I$(TOP)Utilities/Third_Party/lwip_v1.3.2/src/include
CFLAGS+=-I$(TOP)Utilities/Third_Party/lwip_v1.3.2/src/include/ipv4
CFLAGS+=-I$(TOP)Utilities/Third_Party/lwip_v1.3.2/port/STM32
CFLAGS+=-I$(TOP)Utilities/Third_Party/lwip_v1.3.2/port/STM32/FreeRTOS

#jpeg-8d
CFLAGS+=-I$(TOP)Utilities/Third_Party/jpeg-8d/include

#Utilities
CFLAGS+=-I$(TOP)Utilities/STM32_EVAL/STM3240_41_G_EVAL
CFLAGS+=-I$(TOP)Utilities/STM32_EVAL/Common

#Libs
CFLAGS+=-I$(TOP)Libraries/Embedded_GUI_HAL/inc
CFLAGS+=-I$(TOP)Libraries/STM32F4x7_ETH_Driver/inc
CFLAGS+=-I$(TOP)Libraries/Embedded_GUI_Library/inc
CFLAGS+=-I$(TOP)Libraries/STM32_USB_HOST_Library/Core/inc
CFLAGS+=-I$(TOP)Libraries/STM32_USB_OTG_Driver/inc
CFLAGS+=-I$(TOP)Libraries/STM32_USB_HOST_Library/Class/MSC/inc

#CMSIS
CFLAGS+=-I$(TOP)Libraries/CMSIS/Device/ST/STM32F4xx/Include/
CFLAGS+=-I$(TOP)Libraries/CMSIS/Include/

all: clean libSTM32F4xx_StdPeriph_Driver.lib libCore_Graphics.lib $(BIN_IMAGE)
 

$(BIN_IMAGE): $(EXECUTABLE)
	$(OBJCOPY) -O binary $^ $@
	$(OBJCOPY) -O ihex $^ $(HEX_IMAGE)
	arm-none-eabi-objdump -h -S -D $(EXECUTABLE) > $(PROJECT).lst
	arm-none-eabi-size $(EXECUTABLE)
	
$(EXECUTABLE): 	$(PP)main.c  \
	$(TOP)Libraries/CMSIS/Device/ST/STM32F4xx/Source/Templates/gcc_ride7/startup_stm32f4xx.s \
	$(TOP)Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F40x/system_stm32f4xx.c
	$(CC) $(CFLAGS) $^ -o $@  -llibSTM32F4xx_StdPeriph_Driver -llibCore_Graphics

libSTM32F4xx_StdPeriph_Driver.lib: 
	$(CC) $(CFLAGS) -c \
			-I../inc \
			$(TOP)Libraries/STM32F4xx_StdPeriph_Driver/src/*.c
	@$(AR) cr $@ $(TOP)*.o 
	rm -rf $(TOP)*.o
	
libCore_Graphics.lib:	
	$(CC) $(CFLAGS) -c \
			-I../inc \
			$(TOP)Project/Core/Graphics/*.c
	@$(AR) cr $@ $(TOP)*.o 
	rm -rf $(TOP)*.o

	
#-D"assert_param(expr)=((void)0)" \

clean:
	rm -rf $(EXECUTABLE)
	rm -rf $(BIN_IMAGE)
	rm -rf $(HEX_IMAGE)
	rm -rf *.{lst,a}
	

.PHONY: all clean
