-
Notifications
You must be signed in to change notification settings - Fork 3
/
Makefile
102 lines (70 loc) · 2.36 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
include config/config.mk
include HAL/hal.mk
include OS/os.mk
# compiler
CC=$(CROSS_COMPILE)gcc
# linker
LD=$(CC)
# archiver
AR=$(CROSS_COMPILE)gcc-ar
ARFLAGS=rcs
# debugger
GDB=gdb-multiarch
GDB_FLAGS=--tui
# flash utility
FLASH_TOOL=openocd
FLASH_TOOL_FLAGS=-f $(OPENOCD_CFG_FILE) -c "setup $(INTERFACE) $(TRANSPORT) $(TARGET)"
# Libraries
LIB_PATH=$(HAL_LIB_PATH) $(OS_LIB_PATH)
LIB_NAMES=$(HAL_LIB_NAME) $(OS_LIB_NAME)
INCLUDES=$(HAL_INCLUDE_PATH) $(OS_INCLUDE_PATH)
LIB_DEFINES=$(HAL_DEFINES) $(OS_DEFINES)
# compile flags
CFLAGS+=$(CC_OPT) $(ARCH_FLAGS)
INC_FLAGS=$(patsubst %,-I%, . $(INCLUDES))
DEF_FLAGS=$(patsubst %,-D%, $(CC_DEFINES) $(LIB_DEFINES))
# link flags
LDFLAGS+=$(ARCH_FLAGS) $(LD_OPT)
LDFLAGS+=$(patsubst %,-T%, $(LINK_FILE))
LDFLAGS+=$(patsubst %,-L%, $(LIB_PATH))
LDFLAGS+=$(patsubst %,-l%, $(LIB_NAMES))
SRC= $(wildcard *.c)
HDR= $(wildcard *.h)
OBJ= $(SRC:.c=.o)
DEBUG_OBJ=$(addprefix $(DEBUG_DIR)/, $(OBJ))
RELEASE_OBJ=$(addprefix $(RELEASE_DIR)/, $(OBJ))
all: release
.DEFAULT_GOAL := all
.PHONY: all flash gdb mrproper clean clean_release clean_debug clean_dep
# not so useful targets to call from command line
$(RELEASE_DIR)/$(FIRMWARE): dep $(RELEASE_OBJ)
@$(LD) -o $@ $(RELEASE_OBJ) $(LDFLAGS)
$(DEBUG_DIR)/$(FIRMWARE): dep $(DEBUG_OBJ)
@$(LD) -o $@ $(DEBUG_OBJ) $(LDFLAGS)
$(RELEASE_DIR)/%.o: %.c $(HDR)
@mkdir -p $(@D)
@$(CC) -o $@ -c $< $(CFLAGS) $(INC_FLAGS) $(DEF_FLAGS)
$(DEBUG_DIR)/%.o: %.c $(HDR)
@mkdir -p $(@D)
@$(CC) -o $@ -c $< $(CFLAGS) $(CFLAGS_DEBUG) $(INC_FLAGS) $(DEF_FLAGS)
dep: hal os
clean_dep: clean_hal clean_os
clean_release:
@if [ -d $(RELEASE_DIR) ]; then find $(RELEASE_DIR) -type f -name "*.o" -delete; fi
clean_debug:
@if [ -d $(DEBUG_DIR) ]; then find $(DEBUG_DIR) -type f -name "*.o" -delete; fi
# useful targets to call from command line
release: $(RELEASE_DIR)/$(FIRMWARE)
debug: $(DEBUG_DIR)/$(FIRMWARE)
flash: release
@$(FLASH_TOOL) $(FLASH_TOOL_FLAGS) -c "program_release $(RELEASE_DIR)/$(FIRMWARE)"
gdb: debug
@$(FLASH_TOOL) $(FLASH_TOOL_FLAGS) &
#2 sec pause to give openocd time to start
@sleep 2
@$(GDB) $(GDB_FLAGS) --eval-command 'target remote :3333' --eval-command 'monitor reset halt' --eval-command 'load' $(DEBUG_DIR)/$(FIRMWARE)
#kill (with ctrl+C) openocd
@kill -9 `ps aux | grep openocd | awk '{print $$2}' | head -1`
clean: clean_release clean_debug
mrproper: clean_dep
@rm -rf $(BUILD_DIR)