##########################################################################################################################
# 文件自动生成于 STM32CubeIDE
#
# 项目: demo
# MCU: STM32F429IGTx
# 编译器: GCC ARM Embedded
##########################################################################################################################

#######################################
# 路径定义
#######################################
# 构建输出目录
BUILD_DIR = build

#######################################
# 工具链设置
#######################################
# 工具链路径（如果工具链不在PATH中，请修改此处）
PREFIX = arm-none-eabi-
# 编译器
CC = $(PREFIX)gcc
# 汇编器
AS = $(PREFIX)gcc -x assembler-with-cpp
# 链接器
CP = $(PREFIX)objcopy
SZ = $(PREFIX)size
HEX = $(CP) -O ihex
BIN = $(CP) -O binary -S

#######################################
# 目标MCU
#######################################
MCU = -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard

#######################################
# 编译选项
#######################################
# 优化选项
OPT = -O2

# 调试选项
DEBUG = -g -gdwarf-2

# 生成依赖信息
DEPFLAGS = -MMD -MP -MF"$(@:%.o=%.d)"

# C编译选项
CFLAGS = $(MCU) $(OPT) $(DEBUG) -Wall -fdata-sections -ffunction-sections

# C++编译选项
CXXFLAGS = $(CFLAGS)

# 汇编选项
ASFLAGS = $(MCU) $(DEBUG) -Wa,--warn

# 链接选项
LDFLAGS = $(MCU) -specs=nano.specs -T$(LDSCRIPT) $(DEBUG) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections

# 库
LIBS = -lc -lm -lnosys
LIBDIR =
LDFLAGS += $(LIBDIR) $(LIBS)

#######################################
# 宏定义
#######################################
# C定义
C_DEFS = \
-DUSE_HAL_DRIVER \
-DSTM32F429xx \
-DARM_MATH_CM4 \
-D__FPU_PRESENT=1 \
-D__FPU_USED=1

#######################################
# 包含路径
#######################################
# C包含路径
C_INCLUDES = \
-ICore/Inc \
-IDrivers/STM32F4xx_HAL_Driver/Inc \
-IDrivers/STM32F4xx_HAL_Driver/Inc/Legacy \
-IDrivers/CMSIS/Device/ST/STM32F4xx/Include \
-IDrivers/CMSIS/Include \
-IMiddlewares/Third_Party/FreeRTOS/Source/include \
-IMiddlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS_V2 \
-IMiddlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM4F

#######################################
# 源文件
#######################################
# Core源文件
C_SOURCES = \
Core/Src/main.c \
Core/Src/freertos.c \
Core/Src/stm32f4xx_hal_msp.c \
Core/Src/stm32f4xx_it.c \
Core/Src/system_stm32f4xx.c \
Core/Src/syscalls.c \
Core/Src/sysmem.c

# HAL驱动源文件
C_SOURCES += \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma_ex.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_exti.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ramfunc.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c

# FreeRTOS源文件
C_SOURCES += \
Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS_V2/cmsis_os2.c \
Middlewares/Third_Party/FreeRTOS/Source/croutine.c \
Middlewares/Third_Party/FreeRTOS/Source/event_groups.c \
Middlewares/Third_Party/FreeRTOS/Source/list.c \
Middlewares/Third_Party/FreeRTOS/Source/queue.c \
Middlewares/Third_Party/FreeRTOS/Source/stream_buffer.c \
Middlewares/Third_Party/FreeRTOS/Source/tasks.c \
Middlewares/Third_Party/FreeRTOS/Source/timers.c \
Middlewares/Third_Party/FreeRTOS/Source/portable/MemMang/heap_4.c \
Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM4F/port.c

# 汇编源文件
ASM_SOURCES = \
Core/Startup/startup_stm32f429igtx.s

#######################################
# 目标文件
#######################################
# 目标名称
TARGET = demo

# 链接脚本
LDSCRIPT = STM32F429IGTX_FLASH.ld

#######################################
# 对象文件列表
#######################################
# 生成对象文件列表（所有对象文件放在build根目录，避免路径问题）
OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o)))

# 设置vpath以查找源文件
vpath %.c $(sort $(dir $(C_SOURCES)))
vpath %.s $(sort $(dir $(ASM_SOURCES)))

#######################################
# 默认目标
#######################################
all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin

#######################################
# 构建步骤
#######################################

# 创建构建目录
$(BUILD_DIR):
	@mkdir -p $@

# C源文件编译（匹配完整路径）
$(BUILD_DIR)/%.o: %.c Makefile
	@echo "编译: $<"
	@mkdir -p $(dir $@)
	@$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(<:.c=.lst) $(C_INCLUDES) $(C_DEFS) $(DEPFLAGS) -o $@ $<

# 汇编源文件编译（匹配完整路径）
$(BUILD_DIR)/%.o: %.s Makefile
	@echo "汇编: $<"
	@mkdir -p $(dir $@)
	@$(AS) -c $(ASFLAGS) $(C_INCLUDES) $(C_DEFS) -o $@ $<

# 链接
$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
	@echo "链接: $@"
	@$(CC) $(OBJECTS) $(LDFLAGS) -o $@
	@echo "输出大小:"
	@$(SZ) $@

# 生成HEX文件
$(BUILD_DIR)/$(TARGET).hex: $(BUILD_DIR)/$(TARGET).elf | $(BUILD_DIR)
	@$(HEX) $< $@

# 生成BIN文件
$(BUILD_DIR)/$(TARGET).bin: $(BUILD_DIR)/$(TARGET).elf | $(BUILD_DIR)
	@$(BIN) $< $@

#######################################
# 清理
#######################################
clean:
	-rm -fR $(BUILD_DIR)

#######################################
# 依赖
#######################################
-include $(wildcard $(BUILD_DIR)/*.d)

#######################################
# 烧录目标（使用OpenOCD，需要根据实际情况修改）
#######################################
flash: $(BUILD_DIR)/$(TARGET).elf
	openocd -f interface/stlink-v2.cfg -f target/stm32f4x.cfg -c "program $(BUILD_DIR)/$(TARGET).elf verify reset exit"

#######################################
# 调试目标（使用GDB，需要根据实际情况修改）
#######################################
debug: $(BUILD_DIR)/$(TARGET).elf
	arm-none-eabi-gdb $(BUILD_DIR)/$(TARGET).elf

#######################################
# 显示帮助信息
#######################################
help:
	@echo "可用的make目标:"
	@echo "  all      - 编译项目并生成elf、hex和bin文件"
	@echo "  clean    - 清理构建目录"
	@echo "  flash    - 烧录程序到MCU（需要OpenOCD）"
	@echo "  debug    - 启动GDB调试器"
	@echo "  help     - 显示此帮助信息"

.PHONY: all clean flash debug help
