######################################
# target
######################################
TARGET = OpenHarmonyDemo

#Chip type
CHIP_TYPE = N32H785xIX7:CM7

######################################
# building variables
######################################
# debug build?
DEBUG = 1
# optimization
OPT = -Os

#######################################
# paths
#######################################
# Build path
BUILD_DIR = build

PROJSRC := ../../../../../../
PROJSRC := $(realpath $(PROJSRC))

######################################
# source
######################################
# C sources
C_SOURCES =  \
../Src/main.c \
../Src/n32h76x_78x_it.c

# BSP
C_SOURCES     += $(wildcard $(PROJSRC)/projects/n32h78x_EVAL/bsp/src/*.c)

# Driver
C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/misc.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_adc.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_comp.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_cordic.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_crc.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_dac.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_dbg.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_dcmu.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_dma.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_dmamux.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_dsmu.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_dvp.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_eccmon.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_eth.c
C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_exti.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_fdcan.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_femc.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_fmac.c
C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_gpio.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_i2c.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_i2s.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_iwdg.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_lcdc.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_lptim.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_lpuart.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_mdma.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_mmu.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_otpc.c
C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_pwr.c
C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_rcc.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_rtc.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_sdmmc.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_sdram.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_sema4.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_shrtim.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_smu.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_spi.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_tim.c
C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_usart.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_vrefbuf.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_wwdg.c
#C_SOURCES     += $(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/src/n32h76x_78x_xspi.c

#device
C_SOURCES     += $(PROJSRC)/firmware/CMSIS/device/system_n32h76x_78x.c
# ASM sources
ASM_SOURCES    = startup_n32h78x_gcc.s

#######################################
# Compiler
#######################################
CROSS_COMPILE = arm-none-eabi-
# The gcc compiler bin path can be either defined in make command via GCC_PATH variable (> make GCC_PATH=xxx)
# either it can be added to the PATH environment variable.
ifdef GCC_PATH
CC = $(GCC_PATH)/$(CROSS_COMPILE)gcc
AS = $(GCC_PATH)/$(CROSS_COMPILE)gcc -x assembler-with-cpp
CP = $(GCC_PATH)/$(CROSS_COMPILE)objcopy
SZ = $(GCC_PATH)/$(CROSS_COMPILE)size
OB = $(GCC_PATH)/$(CROSS_COMPILE)objdump
else
CC = $(CROSS_COMPILE)gcc
AS = $(CROSS_COMPILE)gcc -x assembler-with-cpp
CP = $(CROSS_COMPILE)objcopy
SZ = $(CROSS_COMPILE)size
OB = $(CROSS_COMPILE)objdump
endif
HEX = $(CP) -O ihex
BIN = $(CP) -O binary -S
 
#######################################
# CFLAGS
#######################################
# cpu
CPU = -mcpu=cortex-m7

# fpu
FPU = -mfpu=fpv5-sp-d16
#FPU = -mfpu=fpv5-d16

# float-abi
FLOAT-ABI = -mfloat-abi=hard

# mcu
MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI)

# macros for gcc
# AS defines
AS_DEFS = 	\
-DUSE_STDPERIPH_DRIVER \
-DCORE_CM7

# C defines
C_DEFS =  \
-DUSE_STDPERIPH_DRIVER \
-DCORE_CM7

# AS includes
AS_INCLUDES = \
-I../inc \
-I$(PROJSRC)/firmware/CMSIS/core	\
-I$(PROJSRC)/firmware/CMSIS/device

# C includes
C_INCLUDES =  \
-I../inc \
-I$(PROJSRC)/projects/n32h78x_EVAL/bsp/inc  \
-I$(PROJSRC)/firmware/n32h76x_78x_std_periph_driver/inc \
-I$(PROJSRC)/firmware/CMSIS/core	\
-I$(PROJSRC)/firmware/CMSIS/device

#LiteOS-M
include liteos_m.mk

# compile gcc flags
ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -fdata-sections -ffunction-sections
ASFLAGS += -imacros $(PROJSRC)/firmware/CMSIS/device/n32h76x_78x.h
ASFLAGS += -imacros $(PROJSRC)/firmware/CMSIS/core/core_cm7.h

CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -fdata-sections -ffunction-sections

ifeq ($(DEBUG), 1)
CFLAGS += -g -gdwarf-2
endif

# Generate dependency information
CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"

#######################################
# LDFLAGS
#######################################
# link script
LDSCRIPT = $(PROJSRC)/firmware/CMSIS/device/n32h78x_flash.ld

# libraries
LIBS = -lc -lm -lnosys 
LIBDIR = 
LDFLAGS = $(MCU) -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections

# default action: build all
all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin

#######################################
# build the application
#######################################
# list of objects
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(C_SOURCES)))

# list of ASM program objects
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o)))
vpath %.s $(sort $(dir $(ASM_SOURCES)))

OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASMS_SOURCES:.S=.o)))
vpath %.S $(sort $(dir $(ASMS_SOURCES)))

$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR) 
	$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@

$(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
	$(AS) -c $(ASFLAGS) $< -o $@

$(BUILD_DIR)/%.o: %.S Makefile | $(BUILD_DIR)
	$(AS) -c $(ASFLAGS) $< -o $@

$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
	$(CC) $(OBJECTS) $(LDFLAGS) -o $@
	$(SZ) $@

$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
	$(HEX) $< $@
	
$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
	$(BIN) $< $@
	
$(BUILD_DIR):
	mkdir $@

#######################################
# clean up
#######################################
clean:
	rd $(BUILD_DIR) /s /q
#	rm -r $(BUILD_DIR)
	
text:
	$(OB) -S $(BUILD_DIR)/$(TARGET).elf > ./$(TARGET).txt
#######################################
# dependencies
#######################################
-include $(wildcard $(BUILD_DIR)/*.d)

#######################################
# download .hex/.bin by jlink
#######################################
#Your JLink installation directory
PATH_WINPC = 'C:/Program Files (x86)/SEGGER/JLink/'
#PATH_LINUX = /opt/SEGGER/JLink_V640b/JLinkExe
JK_DPATH = $(PATH_WINPC)
#Jlink script store directory
JKS_DIR = ./

download:
	@$(JK_DPATH)JLink.exe -device $(CHIP_TYPE) -if SWD -speed 4000 -autoconnect 1 -CommanderScript $(JKS_DIR)/flash.jlink
	@echo "Download Completed!"

debug:
	@$(JK_DPATH)JLinkGDBServer.exe -select USB -device $(CHIP_TYPE) -if SWD -speed auto -noir -LocalhostOnly

# *** EOF ***
