aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/avrmakelib.mk
blob: 98c67c9eb1ba4267cbc54622624ad8f6a6fec7d2 (plain)
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
######################################################
# AVR make library                                   #
# Copyright (c) 2015-2020 Michael Buesch <m@bues.ch> #
# Version 1.12                                       #
######################################################

ifeq ($(NAME),)
$(error NAME not defined)
endif
ifeq ($(SRCS),)
$(error SRCS not defined)
endif
ifeq ($(F_CPU),)
$(error F_CPU not defined)
endif
ifeq ($(GCC_ARCH),)
$(error GCC_ARCH not defined)
endif
ifeq ($(AVRDUDE_ARCH),)
$(error AVRDUDE_ARCH not defined)
endif

_uppercase		= $(shell echo $(1) | tr a-z A-Z)
_lowercase		= $(shell echo $(1) | tr A-Z a-z)
_streq			= $(and $(filter 1,$(words $2)),$(filter $1,$(firstword $2)))

# The toolchain definitions
CC			:= avr-gcc
OBJCOPY			:= avr-objcopy
OBJDUMP			:= avr-objdump
SIZE			:= avr-size
MKDIR			:= mkdir
MV			:= mv
RM			:= rm
CP			:= cp
SED			:= sed
ECHO			:= echo
GREP			:= grep
TRUE			:= true
TEST			:= test
AVRDUDE			:= avrdude
MYSMARTUSB		:= mysmartusb.py
DOXYGEN			:= doxygen
PYTHON2			:= python2
PYTHON3			:= python3
SPARSE			:= sparse

# Verbose build:        	make V=1
V			:= @
# Sparsechecker build:  	make C=1
C			:= $(shell $(SPARSE) --help >/dev/null 2>&1 && $(ECHO) 1 || $(ECHO) 0)
# Debug build:          	make DEBUG=1
DEBUG			:= 0
# Optimize flag:		make O=0/1/2/3/s
O			:= s
# Link time optimization:	make LTO=1
LTO			:= 1

Q			:= $(V:1=)
QUIET_CC		= $(Q:@=@$(ECHO) '     CC       '$@;)$(CC)
QUIET_DEPEND		= $(Q:@=@$(ECHO) '     DEPEND   '$@;)$(CC)
QUIET_OBJCOPY		= $(Q:@=@$(ECHO) '     OBJCOPY  '$@;)$(OBJCOPY)
QUIET_OBJDUMP		= $(Q:@=@$(ECHO) '     OBJDUMP  '$@;)$(OBJDUMP)
QUIET_SIZE		= $(Q:@=@$(ECHO) '     SIZE     '$@;)$(SIZE)
QUIET_PYTHON2		= $(Q:@=@$(ECHO) '     PYTHON2  '$@;)$(PYTHON2)
QUIET_PYTHON3		= $(Q:@=@$(ECHO) '     PYTHON3  '$@;)$(PYTHON3)
QUIET_RM		= $(Q:@=@$(ECHO) '     RM       '$@;)$(RM)
QUIET_SED		= $(Q:@=@$(ECHO) '     SED      '$@;)$(SED)
ifeq ($(C),1)
QUIET_SPARSE		= $(Q:@=@$(ECHO) '     SPARSE   '$@;)$(SPARSE)
else
QUIET_SPARSE		= @$(TRUE)
endif

BIN			:= $(NAME).bin
HEX			:= $(NAME).hex
MAP			:= $(NAME).map
DASM			:= $(NAME).dasm
EEP			:= $(NAME).eep.hex
BOOT_BIN		:= $(NAME).bootloader.bin
BOOT_HEX		:= $(NAME).bootloader.hex
BOOT_MAP		:= $(NAME).bootloader.map
BOOT_DASM		:= $(NAME).bootloader.dasm

OBJ_DIR			:= obj
DEP_DIR			:= dep
BOOT_OBJ_DIR		:= obj-boot
BOOT_DEP_DIR		:= dep-boot

FUNC_STACK_LIMIT	?= 128

WARN_CFLAGS		:= -Wall \
			   -Wextra \
			   -Wno-unused-parameter \
			   -Wswitch-enum \
			   -Wsuggest-attribute=noreturn \
			   -Wundef \
			   -Wpointer-arith \
			   $(if $(FUNC_STACK_LIMIT),-Wstack-usage=$(FUNC_STACK_LIMIT)) \
			   -Wcast-qual \
			   -Wlogical-op \
			   -Wshadow \
			   -Wconversion

OPTIMIZE_CFLAGS		:= -O$(O) \
			   -maccumulate-args \
			   -mcall-prologues \
			   -mrelax \
			   -mstrict-X \
			   -fno-inline-small-functions \
			   -fno-move-loop-invariants \
			   -fno-split-wide-types \
			   -fshort-enums \
			   $(if $(call _streq,$(LTO),1),-flto=jobserver -fuse-linker-plugin -fno-fat-lto-objects,-fno-lto)

DEFINE_CFLAGS		:= -DF_CPU=$(F_CPU) \
			   $(if $(BOOT_OFFSET),-DBOOT_OFFSET=$(BOOT_OFFSET)) \
			   $(if $(DEBUG),-DDEBUG=$(DEBUG))

MAIN_CFLAGS		:= -mmcu=$(GCC_ARCH) \
			   -std=gnu11 \
			   -g \
			   -ffunction-sections \
			   -fdata-sections \
			   $(OPTIMIZE_CFLAGS) \
			   $(WARN_CFLAGS) \
			   $(DEFINE_CFLAGS)

MAIN_LDFLAGS		:= -Wl,-gc-sections \
			   $(if $(call _streq,$(LTO),1),,-fwhole-program)

INSTRUMENT_CFLAGS	:= -DINSTRUMENT_FUNCTIONS=1 \
			   -finstrument-functions \
			   -finstrument-functions-exclude-file-list=.h

MAIN_SPARSEFLAGS	:= -gcc-base-dir=/usr/lib/avr \
			   -I/usr/lib/avr/include \
			   -D__STDC_HOSTED__=1 \
			   -D__AVR_ARCH__=5 \
			   -D__AVR_$(subst TINY,tiny,$(subst MEGA,mega,$(call _uppercase,$(GCC_ARCH))))__=1 \
			   -Wsparse-all

CFLAGS			:= $(MAIN_CFLAGS) \
			   $(if $(INSTRUMENT_FUNC),$(INSTRUMENT_CFLAGS)) \
			   $(CFLAGS) \
			   -include sparse.h

BOOT_CFLAGS		:= $(MAIN_CFLAGS) -DBOOTLOADER \
			   $(if $(BOOT_INSTRUMENT_FUNC),$(INSTRUMENT_CFLAGS)) \
			   $(BOOT_CFLAGS) \
			   -include sparse.h

LDFLAGS			:= $(MAIN_LDFLAGS) \
			   -Wl,-Map,$(MAP) \
			   $(LDFLAGS)

BOOT_LDFLAGS		:= $(MAIN_LDFLAGS) \
			   -Wl,--section-start=.text=$(BOOT_OFFSET) \
			   -Wl,-Map,$(BOOT_MAP) \
			   $(BOOT_LDFLAGS)

SPARSEFLAGS		:= $(subst gnu11,gnu99,$(CFLAGS)) \
			   $(MAIN_SPARSEFLAGS) $(SPARSEFLAGS)
BOOT_SPARSEFLAGS	:= $(subst gnu11,gnu99,$(BOOT_CFLAGS)) \
			   $(MAIN_SPARSEFLAGS) $(BOOT_SPARSEFLAGS)

.SUFFIXES:
.DEFAULT_GOAL := all

# Programmer parameters
AVRDUDE_SPEED		?= 1
AVRDUDE_SLOW_SPEED	?= 200

AVRDUDE_PROGRAMMER	:=
ifeq ($(PROGRAMMER),mysmartusb)
AVRDUDE_PROGRAMMER	:= avr910
PROGPORT		:= /dev/ttyUSB0
endif
ifeq ($(PROGRAMMER),avrisp2)
AVRDUDE_PROGRAMMER	:= avrisp2
PROGPORT		:= usb
endif

ifeq ($(AVRDUDE_PROGRAMMER),)
$(error Invalid PROGRAMMER specified)
endif

define _programmer_cmd_pwrcycle
	$(if $(filter mysmartusb,$(PROGRAMMER)), \
		$(MYSMARTUSB) -p0 $(PROGPORT) && \
		sleep 1 && \
		$(MYSMARTUSB) -p1 $(PROGPORT) \
	)
endef

define _programmer_cmd_prog_enter
	$(if $(filter mysmartusb,$(PROGRAMMER)), \
		$(MYSMARTUSB) -mp $(PROGPORT) \
	)
endef

define _programmer_cmd_prog_leave
	$(if $(filter mysmartusb,$(PROGRAMMER)), \
		$(MYSMARTUSB) -md $(PROGPORT) \
	)
endef

DEPS = $(sort $(patsubst %.c,$(2)/%.d,$(1)))
OBJS = $(sort $(patsubst %.c,$(2)/%.o,$(1)))

# Generate dependencies
$(call DEPS,$(SRCS),$(DEP_DIR)): $(DEP_DIR)/%.d: %.c
	@$(MKDIR) -p $(dir $@)
	@$(MKDIR) -p $(OBJ_DIR)
	$(QUIET_DEPEND) -o $@.tmp -MM $(if $(GEN_SRCS),-MG) -MT "$@ $(patsubst $(DEP_DIR)/%.d,$(OBJ_DIR)/%.o,$@)" $(CFLAGS) $<
	@$(MV) -f $@.tmp $@

ifneq ($(BOOT_SRCS),)
$(call DEPS,$(BOOT_SRCS),$(BOOT_DEP_DIR)): $(BOOT_DEP_DIR)/%.d: %.c
	@$(MKDIR) -p $(dir $@)
	@$(MKDIR) -p $(BOOT_OBJ_DIR)
	$(QUIET_DEPEND) -o $@.tmp -MM $(if $(BOOT_GEN_SRCS),-MG) -MT "$@ $(patsubst $(BOOT_DEP_DIR)/%.d,$(BOOT_OBJ_DIR)/%.o,$@)" $(BOOT_CFLAGS) $<
	@$(MV) -f $@.tmp $@
endif

-include $(call DEPS,$(SRCS),$(DEP_DIR))
ifneq ($(BOOT_SRCS),)
-include $(call DEPS,$(BOOT_SRCS),$(BOOT_DEP_DIR))
endif

# Generate object files
$(call OBJS,$(SRCS),$(OBJ_DIR)): $(OBJ_DIR)/%.o: %.c
	@$(MKDIR) -p $(dir $@)
	$(QUIET_CC) -o $@ -c $(CFLAGS) $<
	$(QUIET_SPARSE) $(SPARSEFLAGS) $<

ifneq ($(BOOT_SRCS),)
$(call OBJS,$(BOOT_SRCS),$(BOOT_OBJ_DIR)): $(BOOT_OBJ_DIR)/%.o: %.c
	@$(MKDIR) -p $(dir $@)
	$(QUIET_CC) -o $@ -c $(BOOT_CFLAGS) $<
	$(QUIET_SPARSE) $(BOOT_SPARSEFLAGS) $<
endif

all: $(HEX) $(if $(BOOT_SRCS),$(BOOT_HEX))

%.s: %.c
	$(QUIET_CC) $(CFLAGS) -fno-lto -S $*.c

$(BIN): $(call OBJS,$(SRCS),$(OBJ_DIR))
	+$(QUIET_CC) $(CFLAGS) -o $(BIN) $(call OBJS,$(SRCS),$(OBJ_DIR)) $(LDFLAGS)
	+$(QUIET_OBJDUMP) -S $(BIN) > $(DASM)

$(BOOT_BIN): $(call OBJS,$(BOOT_SRCS),$(BOOT_OBJ_DIR))
	+$(QUIET_CC) $(BOOT_CFLAGS) -o $(BOOT_BIN) $(call OBJS,$(BOOT_SRCS),$(BOOT_OBJ_DIR)) $(BOOT_LDFLAGS)
	+$(QUIET_OBJDUMP) -S $(BOOT_BIN) > $(BOOT_DASM)

$(HEX): $(BIN)
	$(QUIET_OBJCOPY) -R.eeprom -O ihex $(BIN) $(HEX)
	@$(OBJDUMP) -h $(BIN) | $(GREP) -qe .eeprom && \
	 $(OBJCOPY) -j.eeprom --set-section-flags=.eeprom="alloc,load" \
	 --change-section-lma .eeprom=0 -O ihex $(BIN) $(EEP) \
	 || $(TRUE)
	$(QUIET_SIZE) --format=SysV $(BIN)

$(BOOT_HEX): $(BOOT_BIN)
	$(QUIET_OBJCOPY) -R.eeprom -O ihex $(BOOT_BIN) $(BOOT_HEX)
	$(QUIET_SIZE) --format=SysV $(BOOT_BIN)

define _avrdude_interactive
  $(AVRDUDE) -B $(AVRDUDE_SPEED) -p $(AVRDUDE_ARCH) \
    -c $(AVRDUDE_PROGRAMMER) -P $(PROGPORT) -t
endef

define _avrdude_reset
  $(AVRDUDE) -B $(AVRDUDE_SLOW_SPEED) -p $(AVRDUDE_ARCH) \
    -c $(AVRDUDE_PROGRAMMER) -P $(PROGPORT) \
    -U signature:r:/dev/null:i -q -q
endef

define _avrdude_write_flash
  $(AVRDUDE) -B $(AVRDUDE_SPEED) -p $(AVRDUDE_ARCH) \
    -c $(AVRDUDE_PROGRAMMER) -P $(PROGPORT) \
    $(if $(BOOT_SRCS),-U flash:w:$(BOOT_HEX)) \
    -U flash:w:$(HEX)
endef

define _avrdude_write_eeprom
  $(TEST) -r $(EEP) && ( \
    $(AVRDUDE) -B $(AVRDUDE_SPEED) -p $(AVRDUDE_ARCH) \
    -c $(AVRDUDE_PROGRAMMER) -P $(PROGPORT) \
    -U eeprom:w:$(EEP) \
  ) || $(TRUE)
endef

define _avrdude_write_fuse
  $(AVRDUDE) -B $(AVRDUDE_SLOW_SPEED) -p $(AVRDUDE_ARCH) \
    -c $(AVRDUDE_PROGRAMMER) -P $(PROGPORT) -q -q \
    -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m $(if $(EFUSE),-U efuse:w:$(EFUSE):m)
endef

write_flash: all
	$(call _programmer_cmd_prog_enter)
	$(call _avrdude_write_flash)
	$(call _programmer_cmd_pwrcycle)
	$(call _programmer_cmd_prog_leave)

writeflash: write_flash

write_eeprom: all
	$(call _programmer_cmd_prog_enter)
	$(call _avrdude_write_eeprom)
	$(call _programmer_cmd_pwrcycle)
	$(call _programmer_cmd_prog_leave)

writeeeprom: write_eeprom

write_mem: all
	$(call _programmer_cmd_prog_enter)
	$(call _avrdude_write_flash)
	$(call _avrdude_write_eeprom)
	$(call _programmer_cmd_pwrcycle)
	$(call _programmer_cmd_prog_leave)

writemem: write_mem
install: write_mem

write_fuses:
	$(call _programmer_cmd_prog_enter)
	$(call _avrdude_write_fuse)
	$(call _programmer_cmd_pwrcycle)
	$(call _programmer_cmd_prog_leave)

write_fuse: write_fuses
writefuse: write_fuses
writefuses: write_fuses

print_fuses:
	@$(if $(LFUSE),echo "LFUSE (low fuse)      = $(LFUSE)",$(TRUE))
	@$(if $(HFUSE),echo "HFUSE (high fuse)     = $(HFUSE)",$(TRUE))
	@$(if $(EFUSE),echo "EFUSE (extended fuse) = $(EFUSE)",$(TRUE))

printfuses: print_fuses

reset:
	$(call _programmer_cmd_prog_enter)
	$(call _avrdude_reset)
	$(call _programmer_cmd_pwrcycle)

avrdude:
	$(call _programmer_cmd_prog_enter)
	$(call _avrdude_interactive)
	$(call _programmer_cmd_pwrcycle)
	$(call _programmer_cmd_prog_leave)

doxygen:
	$(DOXYGEN) Doxyfile

clean:
	-$(QUIET_RM) -rf \
		$(OBJ_DIR) $(DEP_DIR) \
		$(BOOT_OBJ_DIR) $(BOOT_DEP_DIR) \
		$(BIN) $(BOOT_BIN) \
		$(MAP) $(BOOT_MAP) \
		$(DASM) $(BOOT_DASM) \
		*.pyc *.pyo __pycache__ \
		$(GEN_SRCS) $(BOOT_GEN_SRCS) \
		$(CLEAN_FILES)

distclean: clean
	-$(QUIET_RM) -f \
		$(HEX) $(BOOT_HEX) \
		$(EEP) \
		*.s \
		$(DISTCLEAN_FILES)
bues.ch cgit interface