-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBuildAvr.mk
executable file
·148 lines (116 loc) · 3.84 KB
/
BuildAvr.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
COLOR_FLASH := magenta
COLOR_SIZE := cyan
CC := avr-gcc
AR := avr-ar
OBJ-COPY := avr-objcopy
OBJ-DUMP := avr-objdump
OBJ-SIZE := avr-size
ifndef AVR_MCU
$(error AVR_MCU must be defined)
endif
ifndef AVR_FREQ
$(error AVR_FREQ must be defined)
endif
MCU := $(AVR_MCU)
MCUFLAG := -mmcu=$(MCU)
BASE_FLAGS := $(MCUFLAG) \
-std=gnu99 \
-DAVR \
-DF_CPU=$(AVR_FREQ) \
-fshort-enums \
-Wall \
-Wno-missing-braces
ifndef __BUILDDIR_MODIFIED__
# Frequency and MCU also impact the binary outputs.
BUILDDIR := $(BUILDDIR)-$(AVR_FREQ)-$(MCU)
BUILD_DIRNAME := $(BUILD_DIRNAME)-$(AVR_FREQ)-$(MCU)
__BUILDDIR_MODIFIED__ := true
endif
# Compile & assemble, do not link yet
CFLAGS := $(BASE_FLAGS) -c
ifneq ($(NOOPT), true)
ifeq ($(SPEED), true)
CFLAGS += -O3
else
CFLAGS += -Os
endif
endif
ifeq ($(DEBUG), true)
CFLAGS += -g3
ifeq ($(NOOPT), true)
# The -Wno-cpp is to suppress warnings from <util/delay.h>, that optimizations are disabled and delay() won't work correctly.
# Alternative (which destroys some debug-information): CFLAGS += -O1
CFLAGS += -Wno-cpp
endif
endif
CFLAGS += -ffunction-sections -fdata-sections # Enable linker to garbage collect functions
LIB_SUFFIX := a
TARGET_SUFFIX := elf
# the start-group/end-group flags cause the linker to handle circular dependencies.
# The objects/libraries are scanned multiple times, until all dependencies are resolved. Link-time is increased, but this is the only way.
LDFLAGS_START := $(MCUFLAG) -Wl,--start-group
# This part of the linker flags is split off to include the objects of the current project into the start-group/end-group closure
LDFLAGS_END := -Wl,--end-group -lm -Wl,--gc-sections
# TODO -- check these linker flags!
# --rodata-writable -mrelax -Wl,--defsym=__stack=0x4000
DEPENDENCY_FLAGS := $(BASE_FLAGS) -MM
ARFLAGS := rcs
# Time: 130ms
%.hex: %.elf
$(OBJ-COPY) -O ihex -R .eeprom -R .fuse -R .lock -R .signature $< $@
# Time: 70ms
%.eep: %.elf
$(OBJ-COPY) -j .eeprom --set-section-flags=.eeprom=alloc,load --change-section-lma .eeprom=0 --no-change-warnings -O ihex $< $@
# Time: 670ms (!)
%.lss: %.elf
$(OBJ-DUMP) -h -S $< > $@
hex_$(project): $(foreach o, $(outputs), $(BUILDDIR)/$o.hex)
eep_$(project): $(foreach o, $(outputs), $(BUILDDIR)/$o.eep)
lss_$(project): $(foreach o, $(outputs), $(BUILDDIR)/$o.lss)
ifneq ($(LIBRARY), true)
# Aways build the hex-files automatically when linking.
$(project): hex_$(project)
ifeq ($(LSS), true)
$(project): lss_$(project)
endif
endif
define OPTIONAL_SIZE_COMMAND
-$(COLOR) $(COLOR_SIZE); $(OBJ-SIZE) $$@ -C --mcu=$(MCU) | grep bytes; $(COLOR) off
endef
size_$(project)_%: $(BUILDDIR)/%.$(TARGET_SUFFIX)
-$(COLOR) $(COLOR_SIZE)
$(OBJ-SIZE) $< -C --mcu=$(MCU) | grep bytes
-$(COLOR) off
# =====
# == AVRDUDE commands
# =====
# Define these things just once.
ifndef AVRDUDE_COMMAND
# -v : for verbose, -v -v : extra verbose.
# -n : do not write anything to device
# -e : perform chip-erase
# -V : do not verify written data
AVRDUDE_COMMAND := avrdude -P usb -c usbasp -p $(MCU)
# This command checks connection to the AVR and prints verbose information.
con:
@echo "Connecting to $(MCU)..."
$(AVRDUDE_COMMAND) -n
endif
define do_flash
@echo "$$($(COLOR) $(COLOR_FLASH))Flashing $1$$($(COLOR) off)"
$(AVRDUDE_COMMAND) $2 -e # Chip erase
$(AVRDUDE_COMMAND) $2 -U flash:w:$1
endef
flash_$(project)_%: $(BUILDDIR)/%.hex
$(call do_flash,$<, )
flashv_$(project)_%: $(BUILDDIR)/%.hex
$(call do_flash,$<, -v -v)
flash_$(project): $(BUILDDIR)/$(primary_output).hex
$(call do_flash,$<, )
flashv_$(project): $(BUILDDIR)/$(primary_output).hex
$(call do_flash,$<, -v -v)
EEPROM_OUT := out.eepr
eeprom_$(project):
@echo "$$($(COLOR) $(COLOR_FLASH))Reading raw EEPROM to $(EEPROM_OUT)$$($(COLOR) off)"
$(AVRDUDE_COMMAND) -U eeprom:r:$(EEPROM_OUT):r
.PHONY: flash_$(project) flashv_$(project) con