-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
164 lines (124 loc) · 3.78 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
# Makefile
# (C) 2011-13 Akafugu Corporation
#
# 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 2 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.
# Define your programmer in this file: ~/user.mk
-include ~/user.mk
SILENT ?= @
CROSS ?= avr-
include Makefile.config
# These are set in Makefile.conf
# MCU
# F_CPU
# TARGET
# SRCS
# VALUE_DEFS
# YESNO_DEFS
OBJS = $(SRCS:.c=.o)
ifneq ($(CROSS), )
CC = $(CROSS)gcc
CXX = $(CROSS)g++
OBJCOPY = $(CROSS)objcopy
OBJDUMP = $(CROSS)objdump
SIZE = $(CROSS)size
endif
ifneq ($(F_CPU),)
CFLAGS += -DF_CPU=$(F_CPU)
endif
## Special defines
define CHECK_YESNO_ANSWER
ifeq ($$($(1)), YES)
CFLAGS += -D$(1)
endif
endef
$(foreach i,$(YESNO_DEFS),$(eval $(call CHECK_YESNO_ANSWER,$(i))))
define CHECK_VALUE_ANSWER
ifneq ($$($(1)), )
CFLAGS += -D$(1)=$$($(1))
endif
endef
$(foreach i,$(VALUE_DEFS),$(eval $(call CHECK_VALUE_ANSWER,$(i))))
##
OPT=s
CFLAGS += -g -O$(OPT) \
-ffreestanding -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums \
-Wall -Wstrict-prototypes \
-Wa,-adhlns=$(<:.c=.lst) -std=gnu99 -mmcu=$(MCU)
LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
all: $(TARGET).elf size
size: $(TARGET).elf
$(SILENT) $(SIZE) -C --mcu=$(MCU) $(TARGET).elf
ifneq ($(wildcard $(OBJS) $(TARGET).elf $(TARGET).hex $(TARGET).eep $(TARGET).map $(OBJS:%.o=%.d) $(OBJS:%.o=%.lst)), )
clean:
-rm $(wildcard $(OBJS) $(TARGET).elf $(TARGET).hex $(TARGET).eep $(TARGET).map $(OBJS:%.o=%.d) $(OBJS:%.o=%.lst))
else
clean:
@echo "Nothing to clean."
endif
.SECONDARY:
%.elf: $(OBJS)
@echo "Linking:" $@...
$(SILENT) $(CC) $(CFLAGS) $(OBJS) --output $@ $(LDFLAGS)
%.o : %.cpp
@echo "[$(TARGET)] Compiling:" $@...
$(SILENT) $(CXX) $(CXXFLAGS) -MMD -MF $(@:%.o=%.d) -c $< -o $@
%.o : %.c
@echo "[$(TARGET)] Compiling:" $@...
$(SILENT) $(CC) $(CFLAGS) -MMD -MF $(@:%.o=%.d) -c $< -o $@
%.d : %.cpp
@echo "[$(TARGET)] Generating dependency:" $@...
$(SILENT) $(CXX) $(CXXFLAGS) -MM -MT $(addsuffix .o, $(basename $@)) -MF $@ $<
%.d : %.c
@echo "[$(TARGET)] Generating dependency:" $@...
$(SILENT) $(CC) $(CFLAGS) -MM -MT $(addsuffix .o, $(basename $@)) -MF $@ $<
###############
## Programming
AVRDUDE := avrdude
AVRDUDE_FLAGS += -p $(MCU)
ifneq ($(AVRDUDE_PORT), )
AVRDUDE_FLAGS += -P $(AVRDUDE_PORT)
endif
ifneq ($(AVRDUDE_PROGRAMMER), )
AVRDUDE_FLAGS += -c $(AVRDUDE_PROGRAMMER)
endif
ifneq ($(AVRDUDE_SPEED), )
AVRDUDE_FLAGS += -b $(AVRDUDE_SPEED)
endif
#Add more verbose output if we dont have SILENT set
ifeq ($(SILENT), )
AVRDUDE_FLAGS += -v -v
endif
AVRDUDE_FLAGS += -B 250
# Fuses for 32KHz oscillator
AVRDUDE_WRITE_FUSE ?= -U lfuse:w:0xd6:m -U hfuse:w:0xde:m
ifneq ($(AVRDUDE_PROGRAMMER), )
flash: $(TARGET).hex $(TARGET).eep
$(AVRDUDE) $(AVRDUDE_FLAGS) -U flash:w:$(TARGET).hex -U eeprom:w:$(TARGET).eep
fuse:
$(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FUSE)
install: fuse flash
%.hex: %.elf
@echo "Creating flash file:" $@...
$(SILENT) $(OBJCOPY) -O ihex -R .eeprom $< $@
%.eep: %.elf
@echo "Creating eeprom file:" $@...
$(SILENT) $(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
--change-section-lma .eeprom=0 -O ihex $< $@
else
FLASH_MSG="You need to set AVRDUDE_PROGRAMMER/AVRDUDE_PORT/AVRDUDE_SPEED in ~/user.mk"
flash:
@echo $(FLASH_MSG)
fuse:
@echo $(FLASH_MSG)
endif
###############
# Check which .o files we already have and include their dependency files.
PRIOR_OBJS := $(wildcard $(OBJS))
include $(PRIOR_OBJS:%.o=%.d)