PROJECT=F4D_MP3

#ProgramPath
TOP=$(shell readlink -f "$(dir $(lastword $(MAKEFILE_LIST)))")/..
PP=$(TOP)/src/

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

CC=arm-none-eabi-gcc
CPP=arm-none-eabi-g++
OBJCOPY=arm-none-eabi-objcopy
AR=arm-none-eabi-ar

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

# to run from FLASH
CFLAGS+=-Wl,-T,$(TOP)/src/ChibiOS_2.4.0/os/ports/GCC/ARMCMx/STM32F4xx/ld/STM32F407xG.ld

CFLAGS+=-I$(TOP)/
CFLAGS+=-I$(PP)

CFLAGS+=-DVECT_TAB_FLASH
#CFLAGS+=-DUSE_STDPERIPH_DRIVER

CFLAGS+=-I$(TOP)

#ChibiOS_2.4.0
CFLAGS+=-I$(TOP)/src/ChibiOS_2.4.0/os/kernel/include
CFLAGS+=-I$(TOP)/src/ChibiOS_2.4.0/os/ports/GCC/ARMCMx
CFLAGS+=-I$(TOP)/src/ChibiOS_2.4.0/os/ports/GCC/ARMCMx/STM32F4xx
CFLAGS+=-I$(TOP)/src/ChibiOS_2.4.0/os/ports/common/ARMCMx
CFLAGS+=-I$(TOP)/src/ChibiOS_2.4.0/os/hal/include
CFLAGS+=-I$(TOP)/src/ChibiOS_2.4.0/os/various
CFLAGS+=-I$(TOP)/src/ChibiOS_2.4.0/boards/ST_STM32F4_DISCOVERY
CFLAGS+=-I$(TOP)/src/ChibiOS_2.4.0/os/hal/platforms/STM32F4xx
CFLAGS+=-I$(TOP)/src/ChibiOS_2.4.0/os/hal/platforms/STM32
CFLAGS+=-I$(TOP)/src/ChibiOS_2.4.0/os/hal/platforms/STM32/GPIOv2
CFLAGS+=-I$(TOP)/src/ChibiOS_2.4.0/os/ports/common/ARMCMx/CMSIS/include
CFLAGS+=-I$(TOP)/src/ChibiOS_2.4.0/ext/fatfs/src


#mp3
CFLAGS+=-I$(TOP)/src/mp3dec/pub

#codec
CFLAGS+=-I$(TOP)/src/codec

all: clean libcodec.a libChibiOS_2.4.0.a libmp3dec.a $(BIN_IMAGE)

$(BIN_IMAGE): $(EXECUTABLE)
	@echo $(TOP)
	$(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
	$(CC) $(CFLAGS) $^ -o $@ -Wa,-amhls=$(MAP_FILE) -L$(TOP)/Debug -lcodec -lChibiOS_2.4.0 -lmp3dec -lstdc++ -lc

libcodec.a: 
	$(CC) $(CFLAGS) -c \
			-D"assert_param(expr)=((void)0)" \
			-I../inc \
			$(TOP)/src/codec/*.c
	@$(AR) cr $@ $(TOP)/Debug/*.o 
	rm -rf $(TOP)/Debug/*.o

libChibiOS_2.4.0.a: 
	$(CC) $(CFLAGS) -c \
			-D"assert_param(expr)=((void)0)" \
			-I../inc \
			$(TOP)/src/ChibiOS_2.4.0/boards/ST_STM32F4_DISCOVERY/board.c \
			$(TOP)/src/ChibiOS_2.4.0/ext/fatfs/src/option/syscall.c \
			$(TOP)/src/ChibiOS_2.4.0/ext/fatfs/src/*.c \
			$(TOP)/src/ChibiOS_2.4.0/os/hal/platforms/STM32/serial_lld.c \
			$(TOP)/src/ChibiOS_2.4.0/os/various/chprintf.c \
			$(TOP)/src/ChibiOS_2.4.0/os/kernel/src/*.c \
			$(TOP)/src/ChibiOS_2.4.0/os/hal/platforms/STM32/*.c \
			$(TOP)/src/ChibiOS_2.4.0/os/hal/platforms/STM32/GPIOv2/*.c \
			$(TOP)/src/ChibiOS_2.4.0/os/hal/src/*.c \
			$(TOP)/src/ChibiOS_2.4.0/os/hal/platforms/STM32F4xx/*.c \
			$(TOP)/src/ChibiOS_2.4.0/os/ports/GCC/ARMCMx/*.c \
			$(TOP)/src/ChibiOS_2.4.0/os/ports/GCC/ARMCMx/STM32F4xx/vectors.c \
			$(TOP)/src/ChibiOS_2.4.0/os/ports/common/ARMCMx/nvic.c
	@$(AR) cr $@ $(TOP)/Debug/*.o 
	rm -rf $(TOP)/Debug/*.o

libmp3dec.a: 
	$(CC) $(CFLAGS) -c \
			-D"assert_param(expr)=((void)0)" \
			-I../inc \
			$(TOP)/src/mp3dec/real/*.c \
			$(TOP)/src/mp3dec/mpadecobj.cpp \
			$(TOP)/src/mp3dec/*.c
	@$(AR) cr $@ $(TOP)/Debug/*.o 
	rm -rf $(TOP)/Debug/*.o


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

.PHONY: all libcodec.a libChibiOS_2.4.0.a libmp3dec.a clean
