-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
168 lines (150 loc) · 4.79 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
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
.PHONY: \
ensure-tmp \
vars \
help \
code-fmt \
clean \
compile \
ping \
link \
upload
# Hack to get the directory this makefile is in:
MKFILE_PATH := $(lastword $(MAKEFILE_LIST))
MKFILE_DIR := $(notdir $(patsubst %/,%,$(dir $(MKFILE_PATH))))
MKFILE_ABSDIR := $(abspath $(MKFILE_DIR))
BUILDTMP ?= $(MKFILE_DIR)/build-tmp
OPTIMIZATION ?= -Os
AVRCC ?= $(shell type -p "avr-gcc")
AVRCPP ?= $(shell type -p "avr-cpp")
PYMCUPROG ?= $(shell type -p "pymcuprog")
AVR_SIZE ?= $(shell type -p "avr-size")
AVR_OBJCOPY ?= $(shell type -p "avr-objcopy")
DEVICE ?= avr128da28
UDPI_TARGET ?= $(DEVICE)
CLOCK ?= 4000000L
BAUD ?= 115200
PROGTYPE ?= uart
HEADERS = $(shell find $(MKFILE_DIR)/lib -name "*.h")
UNIT ?= blink
# Misc target info:
help_spacing := 18
.DEFAULT_GOAL := compile
#---------------------------------------------------------
# Ensure temp directories.
#
# In order to ensure temp dirs exit, we include a file
# that doesn't exist, with a target declared as PHONY
# (above), and then have the target create our tmp dirs.
#---------------------------------------
-include ensure-tmp
ensure-tmp:
@mkdir -p $(BUILDTMP)
vars: ## Print relevant environment vars
@printf "\nPaths:\n------------\n"
@printf "%-20.20s%s\n" "MKFILE_PATH:" "$(MKFILE_PATH)"
@printf "%-20.20s%s\n" "MKFILE_DIR:" "$(MKFILE_DIR)"
@printf "%-20.20s%s\n" "MKFILE_ABSDIR:" "$(MKFILE_ABSDIR)"
@printf "%-20.20s%s\n" "BUILDTMP:" "$(BUILDTMP)"
@printf "%-20.20s%s\n" "ATPACK:" "$(ATPACK)"
@printf "\nPrograms:\n------------\n"
@printf "%-20.20s%s\n" "AVRCC:" "$(AVRCC)"
@printf "%-20.20s%s\n" "PYMCUPROG:" "$(PYMCUPROG)"
@printf "%-20.20s%s\n" "AVR_SIZE:" "$(AVR_SIZE)"
@printf "%-20.20s%s\n" "AVR_OBJCOPY:" "$(AVR_OBJCOPY)"
@printf "\nOptions:\n------------\n"
@printf "%-20.20s%s\n" "OPTIMIZATION:" "$(OPTIMIZATION)"
@printf "%-20.20s%s\n" "DEVICE:" "$(DEVICE)"
@printf "%-20.20s%s\n" "UDPI_TARGET:" "$(UDPI_TARGET) (defaults to DEVICE)"
@printf "%-20.20s%s\n" "CLOCK:" "$(CLOCK)"
@printf "%-20.20s%s\n" "PROGTYPE:" "$(PROGTYPE)"
@printf "%-20.20s%s\n" "BAUD:" "$(BAUD)"
@printf "%-20.20s%s\n" "USBDEVICE:" "$(USBDEVICE)"
@printf "%-20.20s%s\n" "UNIT:" "$(UNIT) (main source, sans extension)"
help: ## Print this makefile help menu
@echo "TARGETS:"
@grep '^[a-z_\-]\{1,\}:.*##' $(MAKEFILE_LIST) \
| sed 's/^\([a-z_\-]\{1,\}\): *\(.*[^ ]\) *## *\(.*\)/\1:\t\3 (\2)/g' \
| sed 's/^\([a-z_\-]\{1,\}\): *## *\(.*\)/\1:\t\2/g' \
| awk '{$$1 = sprintf("%-$(help_spacing)s", $$1)} 1' \
| sed 's/^/ /'
code-fmt: ## Use uncrustify to format source code
find "$(MKFILE_DIR)" \
\( -iname "*.c" -or -iname "*.h" -or -iname "*.ino" \) \
-exec uncrustify -c $(MKFILE_DIR)/uncrustify.cfg \
--no-backup '{}' \+
clean: ## Clean build artifacts
rm -rf $(BUILDTMP)/*
rm -vf *.s
compile: $(UNIT).c ## Compile project
ifndef ATPACK
$(error 'ATPACK not defined! Please set ATPACK env var!')
endif # ATPACK
$(AVRCC) \
-c \
-Wall \
$(OPTIMIZATION) \
-DF_CPU=$(CLOCK) \
-mmcu=$(DEVICE) \
-B$(ATPACK)/gcc/dev/$(DEVICE) \
-isystem $(ATPACK)/include \
-I$(MKFILE_DIR)/lib \
$(UNIT).c \
-o $(BUILDTMP)/$(UNIT).c.o
ping: ## Ping the device using $(PYMCUPROG)
ifndef USBDEVICE
$(error 'USBDEVICE not defined! Please set USBDEVICE env var!')
endif # USBDEVICE
$(PYMCUPROG) ping \
-c $(BAUD) \
-v info \
-d $(UDPI_TARGET) \
-t uart \
--uart-timeout 2 \
-u $(USBDEVICE)
link: compile ## Link compilation artifacts and package for upload
$(AVRCC) \
-w \
$(OPTIMIZATION) \
-flto \
-fuse-linker-plugin \
-Wl,--gc-sections \
-mmcu=$(DEVICE) \
-B$(ATPACK)/gcc/dev/$(DEVICE) \
-isystem $(ATPACK)/include \
-o $(BUILDTMP)/$(UNIT).c.elf \
$(BUILDTMP)/$(UNIT).c.o \
-L$(BUILDTMP)
$(AVR_OBJCOPY) \
-O ihex \
-j .eeprom \
--set-section-flags=.eeprom=alloc,load \
--no-change-warnings \
--change-section-lma .eeprom=0 \
$(BUILDTMP)/$(UNIT).c.elf \
$(BUILDTMP)/$(UNIT).c.eep
$(AVR_OBJCOPY) \
-O ihex \
-R .eeprom \
$(BUILDTMP)/$(UNIT).c.elf \
$(BUILDTMP)/$(UNIT).c.hex
$(AVR_SIZE) \
-A $(BUILDTMP)/$(UNIT).c.elf
upload: link ## Upload (NOTE: USBDEVICE must be set)
ifndef USBDEVICE
$(error 'USBDEVICE not defined! Please set USBDEVICE env var!')
endif # USBDEVICE
$(PYMCUPROG) write \
-c $(BAUD) \
-v info \
-d $(UDPI_TARGET) \
-t uart \
--uart-timeout 2 \
-u $(USBDEVICE) \
-f $(BUILDTMP)/$(UNIT).c.hex \
--erase \
--verify
fuses: ## Flash the fuses
ifndef USBDEVICE
$(error 'USBDEVICE not defined! Please set USBDEVICE env var!')
endif # USBDEVICE
@echo "TODO"