-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
138 lines (111 loc) · 4.01 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
VERSION = v1.0.0
PROJECT = arisr_firmware.$(VERSION)
## Cross-compilation commands
CC = arm-none-eabi-gcc
CXX = arm-none-eabi-g++
LD = arm-none-eabi-ld
AR = arm-none-eabi-ar
AS = arm-none-eabi-as
OBJCOPY = arm-none-eabi-objcopy
OBJDUMP = arm-none-eabi-objdump
SIZE = arm-none-eabi-size
# Project target
CPU = cortex-m3
MCU = stm32f103xb
DMCU = $(shell echo $(MCU) | sed 's/x/PLACEHOLDER/g' | tr '[:lower:]' '[:upper:]' | sed 's/PLACEHOLDER/x/g')
FLASH = uart
USB = /dev/ttyUSB0
# Lower to prevent errors
RATE = 9600
# Project structure
SRCDIR = source
INCDIR = include
BINDIR = bin
OBJDIR = obj
CMSIS_DIR = cmsis
HALDIR = hal
CMSIS_DEV_SRC = $(CMSIS_DIR)/device/source
CMSIS_DEV_INC = $(CMSIS_DIR)/device/include
CMSIS_INC = $(CMSIS_DIR)/include
HAL_DIR = hal
HAL_SRC = $(HAL_DIR)/source
HAL_INC = $(HAL_DIR)/include
# Linker script
LDDIR = $(CMSIS_DIR)/linker
LDSCRIPT = $(LDDIR)/$(MCU)_flash.ld
# Core sources
HAL_OBJ_SRC = $(wildcard $(HAL_SRC)/*.c)
HAL_LIB_OBJS = $(HAL_OBJ_SRC:.c=.o)
HAL_LOCAL_LIB_OBJS = $(notdir $(HAL_LIB_OBJS))
# Program sources
OBJ_SRC = $(wildcard $(SRCDIR)/*.c)
OBJ_ASM = $(wildcard $(SRCDIR)/*.s)
LOCAL_LIB_OBJS = $(notdir $(OBJ_SRC:.c=.o))
LOCAL_LIB_ASM = $(notdir $(OBJ_ASM:.s=.o))
LIB_OBJS = $(addprefix $(OBJDIR)/,$(LOCAL_LIB_OBJS))
LIB_OBJS += $(addprefix $(OBJDIR)/,$(LOCAL_LIB_ASM))
# Include paths
INC = -I$(CMSIS_INC) -I$(HAL_INC) -I$(CMSIS_DEV_INC) -I$(INCDIR)
# Build Arguments
CFLAGS = -std=c99 -Wall -fno-common -mthumb -mcpu=$(CPU) -D$(DMCU) --specs=nosys.specs -g -Wa,-ahlms=$(addprefix $(OBJDIR)/,$(notdir $(<:.c=.lst)))
CFLAGS += $(INC)
ASFLAGS = -mcpu=$(CPU)
LFLAGS = -T$(LDSCRIPT) -mthumb -mcpu=$(CPU) --specs=nano.specs --specs=nosys.specs -Wl,--gc-sections
RM = rm -rf
# Targets
all:: $(SRCDIR)/startup_$(MCU).s $(BINDIR)/$(PROJECT).bin $(BINDIR)/$(PROJECT).hex
$(BINDIR)/$(PROJECT).bin: $(BINDIR)/$(PROJECT).elf
$(OBJCOPY) -R .stack --strip-unneeded -O binary $< $@
$(BINDIR)/$(PROJECT).hex: $(BINDIR)/$(PROJECT).elf
$(OBJCOPY) -O ihex $< $@
$(BINDIR)/$(PROJECT).elf: $(LIB_OBJS) $(OBJDIR)/hal.a
@echo "Creating $(PROJECT).elf"
@mkdir -p $(dir $@)
$(CC) $^ $(LFLAGS) -o $(BINDIR)/$(PROJECT).elf
$(OBJDUMP) -D $(BINDIR)/$(PROJECT).elf > $(BINDIR)/$(PROJECT).lst
$(SIZE) $(BINDIR)/$(PROJECT).elf
$(OBJDIR)/hal.a: $(HAL_OBJ_SRC)
@echo "Creating core lib (hal.a)"
@mkdir -p $(dir $@)
$(CC) $(HAL_OBJ_SRC) $(CFLAGS) -c
$(AR) rcs $@ $(HAL_LOCAL_LIB_OBJS)
@rm -f $(HAL_LOCAL_LIB_OBJS)
$(OBJDIR)/%.o: $(SRCDIR)/%.c
@echo "Compiling c source files in src"
@mkdir -p $(dir $@)
$(CC) -c -o $@ $< $(CFLAGS)
$(OBJDIR)/%.o: $(SRCDIR)/%.s
@echo "Compiling asm files in src"
@mkdir -p $(dir $@)
$(AS) $(ASFLAGS) -o $@ $<
# Move target MCU startup to project directory
$(SRCDIR)/startup_$(MCU).s:
@echo "Getting asm source for MCU $(MCU)"
@cp $(CMSIS_DEV_SRC)/startup_$(MCU).s $@
clean:
@rm -rf $(OBJDIR)/*.o
@rm -f $(HAL_LOCAL_LIB_OBJS)
cleanall:
@rm -rf $(OBJDIR)/*
@rm -f $(BINDIR)/*
@rm -rf $(OBJDIR) $(BINDIR)
@rm -f $(HAL_LOCAL_LIB_OBJS)
@rm -f $(SRCDIR)/startup_$(MCU).s
# If $(FLASH) is set to uart, use st32flash to flash the target
flash: $(BINDIR)/$(PROJECT).bin
ifeq ($(FLASH), uart)
@echo "UART Bootloader protocol selected. Using stm32flash."
@stm32flash -b $(RATE) -w $(BINDIR)/$(PROJECT).bin -v -g 0x08000000 $(USB)
# else if $(FLASH) is set to swd, use st-flash to flash the target
else ifeq ($(FLASH), swd)
@echo "SWD Bootloader protocol selected. Using st-flash."
@st-flash --reset write $(BINDIR)/$(PROJECT).bin 0x08000000
else ifeq ($(FLASH), usb)
@echo "USB Bootloader protocol selected. Using dfu-util."
@dfu-util -d 0483:df11 -a 0 -s 0x08000000:leave -D $(BINDIR)/$(PROJECT).bin
# else use STM32CubeProgrammer to flash the target
else
@echo "Using generic flasher STM32CubeProgrammer"
@echo "Please make sure STM32CubeProgrammer is installed and in PATH"
@STM32_Programmer_CLI -c port=$(USB) --serial_baudrate=$(RATE) --serial_parity=even -w $(BINDIR)/$(PROJECT).bin 0x08000000 --verify -g 0x08000000
endif