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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
###
# GNU ARM Embedded Toolchain
CC=arm-none-eabi-gcc
LD=arm-none-eabi-ld
AR=arm-none-eabi-ar
AS=arm-none-eabi-as
CP=arm-none-eabi-objcopy
OD=arm-none-eabi-objdump
SIZE=arm-none-eabi-size
###
# Directory Structure
BINDIR=bin
SRCDIR=.
ASOURCES=$(shell find -L $(SRCDIR) -name '*.s')
CSOURCES+=$(shell find -L $(SRCDIR) -name '*.c' -not -name 'vc.c')
HEADERS=$(shell find -L $(SRCDIR) -name '*.h' -not -name 'vc.h')
COMMON_DIR=../common
COMMON_SOURCE_LIST=$(shell cat ../common/sourcelist.txt)
CSOURCES+=$(COMMON_SOURCE_LIST:%.c=../common/%.c)
INC=$(shell find -L $(SRCDIR) -name '*.h' -exec dirname {} \; | uniq)
INC+=$(COMMON_DIR)/includes
INCLUDES=$(INC:%=-I%)
# Create object list
OBJECTS=$(CSOURCES:%.c=obj/%.o)
OBJECTS+=$(ASOURCES:%.s=obj/%.o)
OBJECTS+=obj/vc.o
# Define output files ELF & IHEX
BINELF=outp.elf
BINHEX=outp.hex
###
# MCU FLAGS
MCFLAGS=-mcpu=cortex-m4 -mthumb -mlittle-endian \
-mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb-interwork \
-include stm32f4xx.h
# COMPILE FLAGS
DEFS=-DUSE_STDPERIPH_DRIVER -DSTM32F4XX -DARM_MATH_CM4
CWARNS += -Wextra
CWARNS += -Wformat
CWARNS += -Wmissing-braces
CWARNS += -Wno-cast-align
CWARNS += -Wparentheses
CWARNS += -Wshadow
CWARNS += -Wno-sign-compare
CWARNS += -Wswitch
CWARNS += -Wuninitialized
CWARNS += -Wunknown-pragmas
CWARNS += -Wunused-function
CWARNS += -Wunused-label
CWARNS += -Wunused-parameter
CWARNS += -Wunused-value
CWARNS += -Wunused-variable
CWARNS += -Wmissing-prototypes
CFLAGS =-Wall $(CWARNS) -ggdb -std=c99 -c $(MCFLAGS) $(DEFS) $(INCLUDES)
# LINKER FLAGS
LDSCRIPT= $(SRCDIR)/bsp/stm32_flash.ld
LDFLAGS =-T $(LDSCRIPT) --specs=nosys.specs $(MCFLAGS) -Wl,-Map=$(BINDIR)/outp.map
###
# Optimizations
OPT?='O2 O3 O6'
# O1 and O4 are irrelevant
# O5 breaks FreeRTOS somehow
# I'm not trusting O7
ifneq ($(filter O1,$(OPT)),)
CXXFLAGS+=-fno-exceptions # Uncomment to disable exception handling
DEFS+=-DNO_EXCEPTIONS # The source code has to comply with this rule
endif
ifneq ($(filter O2,$(OPT)),)
CFLAGS+=-Os # Optimize for size https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
CXXFLAGS+=-Os
LDFLAGS+=-Os # Optimize for size https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
endif
ifneq ($(filter O3,$(OPT)),)
CFLAGS+=-ffunction-sections -fdata-sections # Place each function or data item into its own section in the output file
CXXFLAGS+=-ffunction-sections -fdata-sections # -||-
LDFLAGS+=-Wl,-gc-sections # Remove isolated unused sections
endif
ifneq ($(filter O4,$(OPT)),)
CFLAGS+=-fno-builtin # Disable C++ exception handling
CXXFLAGS+=-fno-builtin # Disable C++ exception handling
endif
ifneq ($(filter O5,$(OPT)),)
CFLAGS+=-flto # Enable link time optimization
CXXFLAGS+=-flto # Enable link time optimization
LDFLAGS+=-flto # Enable link time optimization
endif
ifneq ($(filter O6,$(OPT)),)
CXXFLAGS+=-fno-rtti # Disable type introspection
endif
ifneq ($(findstring O7,$(OPT)),)
LDFLAGS+=--specs=nano.specs # Use size optimized newlib
endif
###
# Build Rules
.PHONY: all release debug clean
all: release
release: $(BINDIR)/$(BINHEX)
debug: CFLAGS+=-g
debug: LDFLAGS+=-g
debug: release
$(BINDIR)/$(BINHEX): $(BINDIR)/$(BINELF)
@$(CP) -O ihex $< $@
@echo "[CP] $@"
@echo "[:)] Happiness :)"
$(BINDIR)/$(BINELF): $(OBJECTS)
@$(CC) $(LDFLAGS) $(OBJECTS) -o $@ -lm
@echo "[LINK] $@"
@$(SIZE) $(BINDIR)/$(BINELF)
dir_guard=@mkdir -p $(@D)
obj/vc.o: vc.c vc.h $(HEADERS)
$(dir_guard)
@echo "[CC] version information vc.c"
@$(CC) $(CFLAGS) vc.c -o obj/vc.o
obj/%.o: %.c $(HEADERS)
$(dir_guard)
@echo "[CC] $<"
@$(CC) $(CFLAGS) $< -o $@
obj/%.o: %.s $(HEADERS)
$(dir_guard)
@echo [AS] $<
@$(CC) $(CFLAGS) $< -o $@
.PHONY: vc.h
vc.h: ../../.git/logs/HEAD
@echo "// This file is generated by Makefile." > vc.h
@echo "// Do not edit this file!" >> vc.h
@echo "const char* vc_get_version(void);" >> vc.h
@echo >> vc.h
@git log -1 --format="format:#define GIT_VERSION \"%h\"" >> vc.h
@echo >> vc.h
@echo >> vc.h
@echo [GEN] vc.h
clean:
@rm -f $(OBJECTS) $(BINDIR)/$(BINELF) $(BINDIR)/$(BINHEX)
@echo "[RM] Cleanuped °o°"
# Connect to openocd's gdb server on port 3333
deploy: $(BINDIR)/$(BINELF)
ifeq ($(wildcard /opt/openocd/bin/openocd),)
/usr/bin/openocd -f /usr/share/openocd/scripts/board/stm32f4discovery.cfg -c "program bin/"$(BINELF)" verify reset" -c "init" -c "reset" -c "exit"
else
/opt/openocd/bin/openocd -f /opt/openocd/share/openocd/scripts/board/stm32f4discovery.cfg -c "program bin/"$(BINELF)" verify reset" -c "init" -c "reset" -c "exit"
endif
# Some STM32F4DISCOVERY have a newer ST-LINK II interface
deploy21: $(BINDIR)/$(BINELF)
ifeq ($(wildcard /opt/openocd/bin/openocd),)
/usr/bin/openocd -f stm32f4discovery-with-stlinkv2.1.cfg -c "program bin/"$(BINELF)" verify reset" -c "init" -c "reset" -c "exit"
else
/opt/openocd/bin/openocd -f stm32f4discovery-with-stlinkv2.1.cfg -c "program bin/"$(BINELF)" verify reset" -c "init" -c "reset" -c "exit"
endif
|