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+=-DUSE_STM324xG_EVAL
#CFLAGS+=-DUSE_OTG_MODE
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
CFLAGS+=-I$(TOP)Utilities/STM32_Audio/Common
CFLAGS+=-I$(TOP)Utilities/STM32_Audio/STM3240_41_G_EVAL
CFLAGS+=-I$(TOP)Utilities/STM32_Audio/Addons/SpiritDSP_Equalizer
CFLAGS+=-I$(TOP)Utilities/STM32_Audio/Addons/SpiritDSP_LoudnessControl

#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_Device_Library/Core/inc
CFLAGS+=-I$(TOP)Libraries/STM32_USB_Device_Library/Class/msc/inc
CFLAGS+=-I$(TOP)Libraries/STM32_USB_Device_Library/Class/hid/inc
CFLAGS+=-I$(TOP)Libraries/STM32_USB_Device_Library/Class/Audio/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.a \
			libCore_Graphics.a \
			libModules.a \
			libCore_Devices.a \
			libCore_Lib.a \
			libLibraries.a \
			libCore_Mod_manager.a $(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 $@  -L$(TOP)\
		-l STM32F4xx_StdPeriph_Driver \
		-l Core_Graphics \
		-l Modules \
		-l Core_Devices \
		-l Core_Mod_manager \
		-l Core_Lib \
		-l Libraries
		

libSTM32F4xx_StdPeriph_Driver.a: 
	$(CC) $(CFLAGS) -c \
			-I../inc \
			$(TOP)Libraries/STM32F4xx_StdPeriph_Driver/src/*.c
	@$(AR) cr $@ $(TOP)*.o 
	rm -rf $(TOP)*.o
	
libLibraries.a: 
	$(CC) $(CFLAGS) -c \
			-I../inc \
			$(TOP)Libraries/Embedded_GUI_HAL/src/*.c \
			$(TOP)Libraries/Embedded_GUI_Library/src/*.c \
			$(TOP)Libraries/STM32_USB_HOST_Library/Core/src/*.c \
			$(TOP)Libraries/STM32_USB_HOST_Library/Class/msc/src/*.c \
			$(TOP)Libraries/STM32_USB_Device_Library/Core/src/*.c \
			$(TOP)Libraries/STM32_USB_Device_Library/Class/msc/src/*.c \
			$(TOP)Libraries/STM32_USB_Device_Library/Class/hid/src/*.c \
			$(TOP)Libraries/STM32_USB_Device_Library/Class/Audio/src/*.c \
			$(TOP)Libraries/STM32F4x7_ETH_Driver/src/*.c \
			$(TOP)Utilities/STM32_EVAL/Common/*.c \
			$(TOP)Utilities/STM32_EVAL/STM3240_41_G_EVAL/*.c
			#$(TOP)Libraries/STM32_USB_OTG_Driver/src/*.c 
	@$(AR) cr $@ $(TOP)*.o 
	rm -rf $(TOP)*.o	
	
libCore_Graphics.a:	
	$(CC) $(CFLAGS) -c \
			-I../inc \
			$(TOP)Project/Core/Graphics/*.c
	@$(AR) cr $@ $(TOP)*.o 
	rm -rf $(TOP)*.o

libModules.a:  
	$(CC) $(CFLAGS) -c \
      	$(TOP)Project/Modules/Audio_Player/*.c \
      	$(TOP)Project/Modules/Calendar/*.c \
      	$(TOP)Project/Modules/Console/*.c \
      	$(TOP)Project/Modules/Ethernet/*.c \
      	$(TOP)Project/Modules/Serial/*.c \
      	$(TOP)Project/Modules/Image_Browser/*.c \
      	$(TOP)Project/Modules/USBD/*.c 
	@$(AR) cr $@ $(TOP)*.o 
	rm -rf $(TOP)*.o
	
libCore_Devices.a:  
	$(CC) $(CFLAGS) -c \
      	$(TOP)Project/Core/Devices/STM32F4xx/*.c 
	@$(AR) cr $@ $(TOP)*.o 
	rm -rf $(TOP)*.o  	

libCore_Lib.a:  
	$(CC) $(CFLAGS) -c \
      	$(TOP)Project/Core/Lib/*.c 
	@$(AR) cr $@ $(TOP)*.o 
	rm -rf $(TOP)*.o  	  	

libCore_Mod_manager.a:  
	$(CC) $(CFLAGS) -c \
      	$(TOP)Project/Core/Mod_manager/*.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
