-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
140 lines (111 loc) · 3.7 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# ------------------------------------------------------------------------------------------- #
# Boards
# ST NUCLEO F401RE board
#BOARD := NUCLEO_F401RE
#FPU := hard
#HSE_VALUE := 0
# STM32F4-Discovery board
#BOARD := STM32F4_DISCOVERY
#FPU := hard
#HSE_VALUE := 8000000
# Famous Blue pill STM32F103C8 board
BOARD := STM32F1_BLUEPILL
FPU := soft
HSE_VALUE := 8000000
# On-motor STM32F103C8 board
#BOARD := STM32F1_ONMOTOR
#FPU := soft
#HSE_VALUE := 0
# ------------------------------------------------------------------------------------------- #
# MCU
# ------------------------------------------------------ #
ifeq ($(BOARD),STM32F1_BLUEPILL)
CPU := STM32F1
MCPU := -mcpu=cortex-m3 -mfloat-abi=soft
LDSCRIPT := -T "stm32f103x8.ld"
OOCD_IF ?= stlink_v2
# ------------------------------------------------------ #
else ifeq ($(BOARD),STM32F1_ONMOTOR)
CPU := STM32F1
MCPU := -mcpu=cortex-m3 -mfloat-abi=soft
LDSCRIPT := -T "stm32f103x8.ld"
OOCD_IF ?= stlink_v2
# ------------------------------------------------------ #
else ifeq ($(BOARD),NUCLEO_F401RE)
CPU := STM32F4
MCPU := -mcpu=cortex-m4 -mfloat-abi=$(FPU)
ifeq ($(FPU),hard)
MCPU += -mfpu=fpv4-sp-d16
endif
LDSCRIPT += -T "stm32f401xe.ld"
STLVER ?= stlink_v2.1
# ------------------------------------------------------ #
else ifeq ($(BOARD),STM32F4_DISCOVERY)
CPU := STM32F4
MCPU := -mcpu=cortex-m4 -mfloat-abi=$(FPU)
ifeq ($(FPU),hard)
MCPU += -mfpu=fpv4-sp-d16
endif
LDSCRIPT += -T "stm32f407xg.ld"
STLVER ?= stlink_v2.1
# ------------------------------------------------------ #
endif
cpu := $(shell echo $(CPU) | tr A-Z a-z)
# ------------------------------------------------------------------------------------------- #
# Programm name
PROG_NAME := stm32_tpl
# where built files will be stored
TARGET_DIR := build
TARGET := $(PROG_NAME).elf
# Libopencm3 directory
OPENCM3_DIR := libopencm3
# Main programm sources
OBJS += $(patsubst %.c, %.o, $(shell find -L src -type f -name "*.c"))
# Optimization / debug flags
OPT := -O2 -g3
# Common C and Linker flags
FLAGS := $(MCPU) $(OPT) -mthumb -fmessage-length=0 -fsigned-char
FLAGS += -ffunction-sections -fdata-sections -ffreestanding
FLAGS += -fno-move-loop-invariants -Wall -Wextra
# C compiler flags
CFLAGS := $(FLAGS) -D$(CPU) -DHSE_VALUE=$(HSE_VALUE) -D$(BOARD)
# Linker flags
LDFLAGS := $(FLAGS)
LDFLAGS += -nostartfiles -Xlinker --gc-sections
LDFLAGS += -L "ldscripts/" $(LDSCRIPT) -Wl,-Map,"$(TARGET_DIR)/$(PROG_NAME).map"
LDFLAGS += --specs=nano.specs
# Include directories
INCLUDE := $(OPENCM3_DIR)/include src
# External libraries
LIBS := -L$(OPENCM3_DIR)/lib -lopencm3_$(cpu) -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group
# Cross-compile tools
PREFIX := arm-none-eabi-
CC := $(PREFIX)gcc
LD := $(PREFIX)gcc
all: $(TARGET)
# Make listing
$(PREFIX)objdump -d --wide $(TARGET_DIR)/$(TARGET) > $(TARGET_DIR)/$(PROG_NAME).asm
# ===========================================================================
# Memory usage
# ===========================================================================
@$(PREFIX)size -A -x $(TARGET_DIR)/$(TARGET)
# Get binary copy
bin: $(TARGET)
$(PREFIX)objcopy -O binary $(TARGET_DIR)/$(TARGET) $(TARGET_DIR)/$(PROG_NAME).bin
# Upload firmware to MCU
pgm: $(TARGET)
openocd -f oocd/$(OOCD_IF).cfg -c "source [find target/$(cpu)x.cfg]" -c "program $(TARGET_DIR)/$(TARGET) verify reset exit"
# Start OpenOCD server
debug: $(TARGET)
openocd -f oocd/$(OOCD_IF).cfg -c "source [find target/$(cpu)x.cfg]"
# ---
$(TARGET): $(OBJS)
@$(LD) $(LDFLAGS) -o $(TARGET_DIR)/$@ $^ $(LIBS)
%.o : %.c
@$(CC) $(CFLAGS) $(addprefix -I, $(INCLUDE)) -o $@ -c $<
clean:
@echo "Cleaning..."
@rm -rf $(OBJS)
@rm -rf $(TARGET_DIR)/*
.PHONY:
all clean pgm debug