登录后台

页面导航

本文编写于 347 天前,最后修改于 347 天前,其中某些信息可能已经过时。
######################################
# target
######################################
TARGET = $(shell basename $(shell pwd))

######################################
# user code
######################################
USER_C_DIR += \
test \
common \

USER_C_SOURCE = $(foreach d,$(USER_C_DIR),$(wildcard ${d}/*.c)) \

USER_C_INCLUDE = $(addprefix -I,$(USER_C_DIR)) \

######################################
# options
######################################
DEBUG = 1
#Available platform:STM32F103ZEx STM32F103C8x STM32F407IGTx STM32F103RCTx
PLATFORM = STM32F103ZEx
DEBUGGER = stlink-v2
# PATH  This is STM32CubeMX default path.
LOCATION = $(shell pwd)
RAW_PATH = $(dir $(LOCATION)/$(firstword $(MAKEFILE_LIST)))
BUILD_DIR = build
HAL_PATH = Drivers
CMSIS_PATH = Drivers
MAIN_SRC_PATH = Core/Src
MAIN_INC_PATH = Core/Inc
STARTUP_PATH = $(RAW_PATH)
LINK_SCRIPT_PATH = $(RAW_PATH)

######################################
# cpu param
######################################
ifeq ($(PLATFORM),STM32F103ZEx)
# cpu
CPU = -mcpu=cortex-m3
# PATH
HAL_DRIVER = STM32F1xx_HAL_Driver
CMSIS_DRIVER = STM32F1xx
# C/ASM
STARTUP = startup_stm32f103xe.s
C_DEFS += -DSTM32F103xE
# link script
LDSCRIPT = STM32F103ZETx_FLASH.ld
# DEBUG
DEBUG_TARGET = stm32f1x
else ifeq ($(PLATFORM),STM32F103C8x)
# cpu
CPU = -mcpu=cortex-m3
# PATH
HAL_DRIVER = STM32F1xx_HAL_Driver
CMSIS_DRIVER = STM32F1xx
# C/ASM
STARTUP = startup_stm32f103xb.s
C_DEFS += -DSTM32F103xB
# link script
LDSCRIPT = STM32F103C8Tx_FLASH.ld
# DEBUG
DEBUG_TARGET = stm32f1x
else ifeq ($(PLATFORM),STM32F407IGTx)
# cpu
CPU = -mcpu=cortex-m4
# PATH
HAL_DRIVER = STM32F4xx_HAL_Driver
CMSIS_DRIVER = STM32F4xx
# C/ASM
STARTUP = startup_stm32f407xx.s
C_DEFS += -DSTM32F407xx
# link script
LDSCRIPT = STM32F407IGTx_FLASH.ld
# fpu
FPU = -mfpu=fpv4-sp-d16
# float-abi
FLOAT-ABI = -mfloat-abi=hard
# DEBUG
DEBUG_TARGET = stm32f4x

else ifeq ($(PLATFORM),STM32F103RCTx)
# cpu
CPU = -mcpu=cortex-m3
# PATH
HAL_DRIVER = STM32F1xx_HAL_Driver
CMSIS_DRIVER = STM32F1xx
# C/ASM
STARTUP = startup_stm32f103xe.s
C_DEFS += -DSTM32F103xE
# link script
LDSCRIPT = STM32F103RCTx_FLASH.ld
# DEBUG
DEBUG_TARGET = stm32f1x
else
    $(error Platform not found.)
endif

######################################
# source
######################################
# C sources
TEMP += $(wildcard $(HAL_PATH)/$(HAL_DRIVER)/Src/*template.c)
C_RAW_SOURCES += $(wildcard $(MAIN_SRC_PATH)/*.c)
C_RAW_SOURCES += $(wildcard $(HAL_PATH)/$(HAL_DRIVER)/Src/*.c)
C_RAW_SOURCES += $(USER_C_SOURCE)
C_SOURCES = $(sort $(filter-out $(TEMP),$(C_RAW_SOURCES)))

# C includes
C_RAW_INCLUDES =  \
-I$(MAIN_INC_PATH) \
-I$(HAL_PATH)/$(HAL_DRIVER)/Inc \
-I$(HAL_PATH)/$(HAL_DRIVER)/Inc/Legacy \
-I$(CMSIS_PATH)/CMSIS/Device/ST/$(CMSIS_DRIVER)/Include \
-I$(CMSIS_PATH)/CMSIS/Include
C_RAW_INCLUDES += $(USER_C_INCLUDE)
C_INCLUDES = $(sort $(C_RAW_INCLUDES))

# ASM sources
ASM_SOURCES += $(STARTUP_PATH)/$(STARTUP) \

# ASM includes
AS_INCLUDES += \
#######################################
# binaries
#######################################
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

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

AS_DEFS = 

C_DEFS += -DUSE_HAL_DRIVER
# compile gcc flags
ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections

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

ifeq ($(DEBUG), 1)
OPT = -Og
CFLAGS += -g -gdwarf-2
C_DEFS += -DDEBUG
else
OPT = -O3
endif
# Generate dependency information
CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"
#######################################
# LDFLAGS
#######################################
# libraries
LIBS = -lc -lm -lnosys 
LIBDIR = 
LDFLAGS = $(MCU) -specs=nano.specs -T$(LINK_SCRIPT_PATH)/$(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
    @echo $(TARGET)编译完成
    
#######################################
# 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)))

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

$(BUILD_DIR)/%.o: %.s  $(BUILD_DIR)
    $(AS) -c $(CFLAGS) $< -o $@

$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS)
    $(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:
    -rm -fR $(BUILD_DIR)

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

#######################################
# debug
#######################################
.PHONY:disconnect connect flash debug test

disconnect:
ifeq ($(shell pgrep openocd),$(NULL))
    @echo 并未连接
else
    @echo 进程ID为$(shell pgrep openocd)
    $(shell kill $$(pgrep openocd))
    @echo 已经断开
endif

connect:
    @echo 开始连接
    nohup openocd -f interface/$(DEBUGGER).cfg -f target/$(DEBUG_TARGET).cfg &
flash:
    @echo 开始烧录
    openocd -f interface/$(DEBUGGER).cfg -f target/$(DEBUG_TARGET).cfg -c "program $(RAW_PATH)/$(BUILD_DIR)/$(TARGET).hex reset exit"
debug:disconnect flash connect
    $(PREFIX)gdb -tui  $(RAW_PATH)/$(BUILD_DIR)/$(TARGET).elf

test:
    @echo 请编写测试代码

# *** EOF ***