-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
124 lines (97 loc) · 3.57 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# 项目名称
PROJECT = LEDBlink
MCU = cortex-m3
DBG = -g3
OPT = #-Os
#####BEGIN MODIFY#####
# 链接文件
LINKER = stm32f1x_64KB_flash.ld
# 启动汇编文件
STARTUP = Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x/startup/TrueSTUDIO/startup_stm32f10x_md.s
# 宏定义
DEFINES = \
STM32F10X_MD \
USE_STDPERIPH_DRIVER
# 包含目录
INCLUDE_DIR = \
Libraries/CMSIS/CM3/CoreSupport \
Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x \
Libraries/STM32F10x_StdPeriph_Driver/inc \
$(wildcard System/*) \
User
# 使用$(wildcard *)作为通配符
SRC_FILES = \
$(wildcard Libraries/CMSIS/CM3/CoreSupport/*.c) \
$(wildcard Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x/*.c) \
$(wildcard Libraries/STM32F10x_StdPeriph_Driver/src/*.c) \
$(wildcard System/**/*.c) \
$(wildcard User/*.c)
#####END MODIFY#####
# 工具链
#####################################################################
# TOOLS #
#####################################################################
PREFIX = arm-none-eabi-
CC = $(PREFIX)gcc
CXX = $(PREFIX)g++
GDB = $(PREFIX)gdb
CP = $(PREFIX)objcopy
AS = $(PREFIX)gcc -x assembler-with-cpp
HEX = $(CP) -O ihex
BIN = $(CP) -O binary -S
# 默认目录
#####################################################################
# DEFAULT DIRECTORIES #
#####################################################################
# Object Files
OBJ_DIR = obj
BUILD_DIR = build
LIBDIR = lib
DEFS = $(patsubst %,-D%, $(DEFINES))
# Header file
INC = $(patsubst %,-I%, $(INCLUDE_DIR))
# Library files
LIB = $(patsubst %,-L%, $(LIBDIR))
#####################################################################
# User/Aplication Source Files #
#####################################################################
# Driver Source Files
OBJ_FILES = $(addprefix $(OBJ_DIR)/,$(notdir $(SRC_FILES:.c=.o)))
vpath %.c $(sort $(dir $(SRC_FILES)))
# list of ASM program objects
OBJ_FILES += $(addprefix $(OBJ_DIR)/,$(notdir $(STARTUP:.s=.o)))
vpath %.s $(sort $(dir $(STARTUP)))
#####################################################################
# Flags #
#####################################################################
COMFLAGS = -mcpu=$(MCU) -mthumb -mfloat-abi=soft
ASFLAGS = $(COMFLAGS) $(DBG)
CPFLAGS = $(COMFLAGS) $(OPT) $(DEFS) $(DBG) -Wall -fmessage-length=0 -ffunction-sections
LDFLAGS = $(COMFLAGS) -T$(LINKER) -Wl,-Map=$(BUILD_DIR)/$(PROJECT).map -Wl,--gc-sections $(LIB)
#####################################################################
# Makefile Rules #
#####################################################################
all: $(OBJ_FILES) $(BUILD_DIR)/$(PROJECT).elf $(BUILD_DIR)/$(PROJECT).hex $(BUILD_DIR)/$(PROJECT).bin
$(PREFIX)size $(BUILD_DIR)/$(PROJECT).elf
$(OBJ_DIR)/%.o: %.c Makefile | $(BUILD_DIR)
$(CC) -c $(CPFLAGS) -I . $(INC) $< -o $@
$(OBJ_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
$(CC) -c $(ASFLAGS) $< -o $@
$(BUILD_DIR)/$(PROJECT).elf: $(OBJ_FILES) Makefile | $(BUILD_DIR)
$(CC) $(OBJ_FILES) $(LDFLAGS) -o $@
$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf
$(HEX) $< $@
$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf
$(BIN) $< $@
$(BUILD_DIR):
mkdir $(BUILD_DIR)
mkdir $(OBJ_DIR)
#####CLEAN#####
clean:
rm -fR $(BUILD_DIR) $(OBJ_DIR)
# 自定义脚本
#标签: 条件
#TAB缩进 命令行
##刷机
flash: $(BUILD_DIR)/$(PROJECT).hex
openocd -f interface/stlink.cfg -f target/stm32f1x.cfg -c "program $(BUILD_DIR)/$(PROJECT).hex verify reset exit"