This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
makefile.mk
306 lines (233 loc) · 9.65 KB
/
makefile.mk
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# Copyright 2009 Emilie Gillet.
#
# Author: Emilie Gillet ([email protected])
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
AVRLIB_TOOLS_PATH ?= /usr/local/CrossPack-AVR/bin/
BUILD_ROOT = build/
BUILD_DIR = $(BUILD_ROOT)$(TARGET)/
PROGRAMMER ?= avrispmkII
PROGRAMMER_PORT ?= usb
AVRDUDE_ERASE ?= no
AVRDUDE_LOCK ?= yes
ifeq ($(FAMILY),tiny)
MCU = attiny$(MCU_NAME)
DMCU = t$(MCU_NAME)
MCU_DEFINE = ATTINY$(MCU_NAME)
else
ifeq ($(MCU_NAME),atmega2560)
MCU=atmega2560
DMCU=atmega2560
MCU_DEFINE=ATMEGA2560
else
ifeq ($(FAMILY),mega)
MCU = atmega$(MCU_NAME)
DMCU = atmega$(MCU_NAME)
MCU_DEFINE = ATMEGA$(MCU_NAME)
else
MCU = atmega$(MCU_NAME)p
DMCU = m$(MCU_NAME)p
MCU_DEFINE = ATMEGA$(MCU_NAME)P
endif
endif
endif
F_CPU ?= 20000000
VPATH = $(PACKAGES)
CC_FILES = $(notdir $(wildcard $(patsubst %,%/*.cc,$(PACKAGES))))
C_FILES = $(notdir $(wildcard $(patsubst %,%/*.c,$(PACKAGES))))
AS_FILES = $(notdir $(wildcard $(patsubst %,%/*.s,$(PACKAGES))))
OBJ_FILES = $(CC_FILES:.cc=.o) $(C_FILES:.c=.o) $(AS_FILES:.s=.o)
OBJS = $(patsubst %,$(BUILD_DIR)%,$(OBJ_FILES))
DEPS = $(OBJS:.o=.d)
TARGET_BIN = $(BUILD_DIR)$(TARGET).bin
TARGET_ELF = $(BUILD_DIR)$(TARGET).elf
TARGET_HEX = $(BUILD_DIR)$(TARGET).hex
TARGETS = $(BUILD_DIR)$(TARGET).*
DEP_FILE = $(BUILD_DIR)depends.mk
CC = $(AVRLIB_TOOLS_PATH)avr-gcc
CXX = $(AVRLIB_TOOLS_PATH)avr-g++
OBJCOPY = $(AVRLIB_TOOLS_PATH)avr-objcopy
OBJDUMP = $(AVRLIB_TOOLS_PATH)avr-objdump
AR = $(AVRLIB_TOOLS_PATH)avr-ar
SIZE = $(AVRLIB_TOOLS_PATH)avr-size
NM = $(AVRLIB_TOOLS_PATH)avr-nm
AVRDUDE = $(AVRLIB_TOOLS_PATH)avrdude
REMOVE = rm -f
CAT = cat
CPPFLAGS = -mmcu=$(MCU) -I. \
-g -Os -w -Wall \
-DF_CPU=$(F_CPU) \
-D__PROG_TYPES_COMPAT__ \
-fdata-sections \
-ffunction-sections \
-fshort-enums \
-fno-move-loop-invariants \
$(EXTRA_DEFINES) \
$(MMC_CONFIG) \
-D$(MCU_DEFINE) \
-DSERIAL_RX_0 \
-mcall-prologues
CXXFLAGS = -fno-exceptions
ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp
LDFLAGS = -mmcu=$(MCU) -lm -Os -Wl,--gc-sections$(EXTRA_LD_FLAGS)
# ------------------------------------------------------------------------------
# Source compiling
# ------------------------------------------------------------------------------
$(BUILD_DIR)%.o: %.cc
$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $< -o $@
$(BUILD_DIR)%.o: %.c
$(CC) -c $(CPPFLAGS) $(CXXFLAGS) $< -o $@
$(BUILD_DIR)%.o: %.s
$(CC) -c $(CPPFLAGS) $(ASFLAGS) $< -o $@
$(BUILD_DIR)%.d: %.cc
$(CXX) -MM $(CPPFLAGS) $(CXXFLAGS) $< -MF $@ -MT $(@:.d=.o)
$(BUILD_DIR)%.d: %.c
$(CC) -MM $(CPPFLAGS) $(CXXFLAGS) $< -MF $@ -MT $(@:.d=.o)
$(BUILD_DIR)%.d: %.s
$(CC) -MM $(CPPFLAGS) $(ASFLAGS) $< -MF $@ -MT $(@:.d=.o)
# ------------------------------------------------------------------------------
# Object file conversion
# ------------------------------------------------------------------------------
$(BUILD_DIR)%.hex: $(BUILD_DIR)%.elf
$(OBJCOPY) -O ihex -R .eeprom $< $@
$(BUILD_DIR)%.bin: $(BUILD_DIR)%.elf
$(OBJCOPY) -O binary -R .eeprom $< $@
$(BUILD_DIR)%.eep: $(BUILD_DIR)%.elf
-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
--change-section-lma .eeprom=0 -O ihex $< $@
$(BUILD_DIR)%.lss: $(BUILD_DIR)%.elf
$(OBJDUMP) -h -S $< > $@
$(BUILD_DIR)%.sym: $(BUILD_DIR)%.elf
$(NM) -n $< > $@
# ------------------------------------------------------------------------------
# AVRDude
# ------------------------------------------------------------------------------
AVRDUDE_COM_OPTS = -V -p $(DMCU)
AVRDUDE_ISP_OPTS = -c $(PROGRAMMER) -P $(PROGRAMMER_PORT)
ifeq ($(AVRDUDE_LOCK),no)
AVRDUDE_LOCK_OPTS =
else
AVRDUDE_LOCK_OPTS ?= -U lock:w:0x$(LOCK):m
endif
ifeq ($(AVRDUDE_ERASE),no)
AVRDUDE_ERASE_OPTS =
else
AVRDUDE_ERASE_OPTS ?= -D
endif
# ------------------------------------------------------------------------------
# Main targets
# ------------------------------------------------------------------------------
all: $(BUILD_DIR) $(TARGET_HEX)
$(BUILD_DIR):
mkdir -p $(BUILD_DIR)
$(TARGET_ELF): $(OBJS)
$(CC) $(LDFLAGS) -o $@ $(OBJS) $(SYS_OBJS) -lc
$(DEP_FILE): $(BUILD_DIR) $(DEPS)
cat $(DEPS) > $(DEP_FILE)
bin: $(TARGET_BIN)
upload: $(TARGET_HEX)
$(AVRDUDE) $(AVRDUDE_ERASE_OPTS) $(AVRDUDE_COM_OPTS) $(AVRDUDE_ISP_OPTS) \
-B 1 -U flash:w:$(TARGET_HEX):i $(AVRDUDE_LOCK_OPTS)
slow_upload: $(TARGET_HEX)
$(AVRDUDE) $(AVRDUDE_ERASE_OPTS) $(AVRDUDE_COM_OPTS) $(AVRDUDE_ISP_OPTS) \
-B 4 -U flash:w:$(TARGET_HEX):i $(AVRDUDE_LOCK_OPTS)
clean:
$(REMOVE) $(OBJS) $(TARGETS) $(DEP_FILE) $(DEPS)
depends: $(DEPS)
cat $(DEPS) > $(DEP_FILE)
$(TARGET).size: $(TARGET_ELF)
$(SIZE) $(TARGET_ELF) > $(TARGET).size
$(BUILD_DIR)$(TARGET).top_symbols: $(TARGET_ELF)
$(NM) $(TARGET_ELF) --size-sort -C -f bsd -r > $@
size: $(TARGET).size
cat $(TARGET).size | awk '{ print $$1+$$2 }' | tail -n1 | figlet | cowsay -n -f moose
ramsize: $(TARGET).size
cat $(TARGET).size | awk '{ print $$2+$$3 }' | tail -n1 | figlet | cowsay -n -f small
size_report: build/$(TARGET)/$(TARGET).lss build/$(TARGET)/$(TARGET).top_symbols
.PHONY: all clean depends upload
include $(DEP_FILE)
# ------------------------------------------------------------------------------
# Midi files for firmware update
# ------------------------------------------------------------------------------
HEX2SYSEX = python tools/hex2sysex/hex2sysex.py
$(BUILD_DIR)%.mid: $(BUILD_DIR)%.hex
$(HEX2SYSEX) $(SYSEX_FLAGS) -o $@ $<
$(BUILD_DIR)%.syx: $(BUILD_DIR)%.hex
$(HEX2SYSEX) $(SYSEX_FLAGS) --syx -o $@ $<
$(BUILD_DIR)%_old_bootloader.mid: $(BUILD_DIR)%.hex
$(HEX2SYSEX) $(SYSEX_FLAGS) --obsolete_manufacturer_id -o $@ $<
$(BUILD_DIR)%_old_bootloader.syx: $(BUILD_DIR)%.hex
$(HEX2SYSEX) $(SYSEX_FLAGS) --obsolete_manufacturer_id --syx -o $@ $<
midi: $(BUILD_DIR)$(TARGET).mid
syx: $(BUILD_DIR)$(TARGET).syx
old_midi: $(BUILD_DIR)$(TARGET)_old_bootloader.mid
old_syx: $(BUILD_DIR)$(TARGET)_old_bootloader.syx
# ------------------------------------------------------------------------------
# EEPROM image write
# ------------------------------------------------------------------------------
GOLDEN_EEPROM_FILE = $(TARGET)/data/$(TARGET)_eeprom_golden.hex
GOLDEN_FLASH_FILE = $(TARGET)/data/$(TARGET)_flash_golden.hex
eeprom_backup:
$(AVRDUDE) $(AVRDUDE_COM_OPTS) $(AVRDUDE_ISP_OPTS) \
-U eeprom:r:$(GOLDEN_EEPROM_FILE):i
eeprom_restore:
$(AVRDUDE) $(AVRDUDE_COM_OPTS) $(AVRDUDE_ISP_OPTS) \
-U eeprom:w:$(GOLDEN_EEPROM_FILE):i -U lock:w:0x$(LOCK):m
flash_backup:
$(AVRDUDE) $(AVRDUDE_COM_OPTS) $(AVRDUDE_ISP_OPTS) \
-U flash:r:$(GOLDEN_FLASH_FILE):i
flash_restore:
$(AVRDUDE) $(AVRDUDE_COM_OPTS) $(AVRDUDE_ISP_OPTS) \
-U flash:w:$(GOLDEN_FLASH_FILE):i -U lock:w:0x$(LOCK):m
# ------------------------------------------------------------------------------
# Resources
# ------------------------------------------------------------------------------
RESOURCE_COMPILER = avrlib/tools/resources_compiler.py
resources: $(wildcard $(RESOURCES)/*.py)
python $(RESOURCE_COMPILER) $(RESOURCES)/resources.py
# ------------------------------------------------------------------------------
# Publish a firmware version on the website
# ------------------------------------------------------------------------------
REMOTE_HOST = mutable-instruments.net
REMOTE_USER = mutable
REMOTE_PATH = public_html/static/firmware
publish: syx midi
scp $(BUILD_DIR)$(TARGET).mid $(REMOTE_USER)@$(REMOTE_HOST):$(REMOTE_PATH)/$(TARGET)_$(VERSION).mid
scp $(BUILD_DIR)$(TARGET).hex $(REMOTE_USER)@$(REMOTE_HOST):$(REMOTE_PATH)//$(TARGET)_$(VERSION).hex
scp $(BUILD_DIR)$(TARGET).syx $(REMOTE_USER)@$(REMOTE_HOST):$(REMOTE_PATH)//$(TARGET)_$(VERSION).syx
publish_old: old_midi old_syx
scp $(BUILD_DIR)$(TARGET)_old_bootloader.mid $(REMOTE_USER)@$(REMOTE_HOST):$(REMOTE_PATH)/$(TARGET)_$(VERSION).mid
scp $(BUILD_DIR)$(TARGET).hex $(REMOTE_USER)@$(REMOTE_HOST):$(REMOTE_PATH)//$(TARGET)_$(VERSION).hex
scp $(BUILD_DIR)$(TARGET)_old_bootloader.syx $(REMOTE_USER)@$(REMOTE_HOST):$(REMOTE_PATH)//$(TARGET)_$(VERSION).syx
# ------------------------------------------------------------------------------
# Set fuses
# ------------------------------------------------------------------------------
terminal:
$(AVRDUDE) $(AVRDUDE_COM_OPTS) $(AVRDUDE_ISP_OPTS) -e -tuF
fuses:
$(AVRDUDE) $(AVRDUDE_COM_OPTS) $(AVRDUDE_ISP_OPTS) -B 10 -e -u \
-U efuse:w:0x$(EFUSE):m \
-U hfuse:w:0x$(HFUSE):m \
-U lfuse:w:0x$(LFUSE):m \
-U lock:w:0x$(LOCK):m
# ------------------------------------------------------------------------------
# Program (fuses + firmware) a blank chip
# ------------------------------------------------------------------------------
bootstrap: bake
bake: $(FIRMWARE)
$(AVRDUDE) $(AVRDUDE_COM_OPTS) $(AVRDUDE_ISP_OPTS) -B 10 -e -u \
-U efuse:w:0x$(EFUSE):m \
-U hfuse:w:0x$(HFUSE):m \
-U lfuse:w:0x$(LFUSE):m \
-U lock:w:0x$(LOCK):m
$(AVRDUDE) $(AVRDUDE_COM_OPTS) $(AVRDUDE_ISP_OPTS) -B 1 \
-U flash:w:$(TARGET_HEX):i -U lock:w:0x$(LOCK):m