summaryrefslogtreecommitdiff
path: root/digital/avr/make/Makefile.avr
blob: 9547fd2120fc71d612ec5df7ba1c1721084719fd (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
# Makefile.avr - AVR part Makefile.
#
# Flags.

AVR_CFLAGS := $(CFLAGS) -mmcu=$(AVR_MCU)
AVR_ASFLAGS := $(ASFLAGS) -mmcu=$(AVR_MCU)
AVR_CPPFLAGS := $(CPPFLAGS) $(AVR_DEFS) -DTARGET_avr=1
AVR_LDFLAGS := $(LDFLAGS)
AVR_LDLIBS := $(LDLIBS) $(AVR_LIBS)

AVR_CC := avr-gcc
AVR_OBJCOPY := avr-objcopy
AVR_OBJDUMP := avr-objdump
AVR_SIZE := avr-size --mcu=$(AVR_MCU) -C
AVR_COMPILE.c = $(AVR_CC) $(AVR_CFLAGS) $(AVR_CPPFLAGS) -c
ifdef L
AVR_COMPILE.c += -Wa,-adhlns=$(@:%.avr.o=%.c.avr.lst)
endif
AVR_COMPILE.S := $(AVR_CC) $(AVR_ASFLAGS) $(AVR_CPPFLAGS) -c
AVR_LINK.o := $(AVR_CC) $(AVR_CFLAGS) $(AVR_LDFLAGS)

AVRDUDE := avrdude
AVRDUDE_PROGRAMMER ?= stk500v2
AVRDUDE_PORT ?= /dev/ttyUSB1
AVR_PROGRAM ?= $(AVRDUDE) -c $(AVRDUDE_PROGRAMMER) -P $(AVRDUDE_PORT) -p $(AVR_MCU) -U flash:w:$<

# Main rules.

avr: elf lst hex size

simu: simuelf

.PHONY: avr simu clean.avr clean.simu elf simuelf lst \
	text hex srec bin eeprom ehex esrec ebin program

# General rules.

AVR_PROGS += $(PROGS)
AVR_ELFS := $(AVR_PROGS:%=%.avr.elf)
AVR_SIMU_ELFS := $(SIMU_PROGS:%=%.avr.simu.elf)
AVR_SOURCES := $(filter-out %.host.c,$(ALL_SOURCES))
AVR_C_SOURCES := $(filter %.c,$(AVR_SOURCES))
AVR_S_SOURCES := $(filter %.S,$(AVR_SOURCES))
AVR_OBJECTS := $(AVR_C_SOURCES:%.c=$(OBJDIR)/%.avr.o) \
	       $(AVR_S_SOURCES:%.S=$(OBJDIR)/%.avr.o)
AVR_SIMU_OBJECTS := $(AVR_OBJECTS:%.avr.o=%.avr.simu.o)

elf: $(AVR_ELFS)
simuelf: $(AVR_SIMU_ELFS)
lst: $(AVR_PROGS:%=%.avr.lst)

define AVR_PROG_template
$(1).avr.elf: $$(patsubst %.S,$(OBJDIR)/%.avr.o,\
	$$(patsubst %.c,$(OBJDIR)/%.avr.o,\
	$$(filter-out %.host.c,$$($(1)_SOURCES))))
endef

$(foreach prog,$(AVR_PROGS),$(eval $(call AVR_PROG_template,$(prog))))

define SIMU_PROG_template
$(1).avr.simu.elf: $$(patsubst %.S,$(OBJDIR)/%.avr.simu.o,\
	$$(patsubst %.c,$(OBJDIR)/%.avr.simu.o,\
	$$(filter-out %.host.c,$$($(1)_SOURCES))))
endef

$(foreach prog,$(SIMU_PROGS),$(eval $(call SIMU_PROG_template,$(prog))))

$(AVR_ELFS) $(AVR_SIMU_ELFS):
	$(AVR_LINK.o) $^ $(AVR_LDLIBS) -o $@

%.avr.lst: %.avr.elf
	$(AVR_OBJDUMP) -h -S $< > $@

$(OBJDIR)/%.avr.o: %.c | $(OBJDIR)
	$(AVR_COMPILE.c) -o $@ $<

$(OBJDIR)/%.avr.simu.o: %.c | $(OBJDIR)
	$(AVR_COMPILE.c) -DSIMU=1 -o $@ $<

$(OBJDIR)/%.avr.o: %.S | $(OBJDIR)
	$(AVR_COMPILE.S) -o $@ $<

$(OBJDIR)/%.avr.simu.o: %.S | $(OBJDIR)
	$(AVR_COMPILE.S) -DSIMU=1 -o $@ $<

# Dependency checking.
-include $(AVR_OBJECTS:%.avr.o=%.avr.d)
-include $(AVR_SIMU_OBJECTS:%.avr.simu.o=%.avr.d)

# Rules for building the .text rom images.

TEXTS := $(AVR_PROGS:%=%.hex) $(AVR_PROGS:%=%.bin) $(AVR_PROGS:%=%.srec)

text: hex

hex: $(AVR_PROGS:%=%.hex)
bin: $(AVR_PROGS:%=%.bin)
srec: $(AVR_PROGS:%=%.srec)
size: $(AVR_PROGS:%=%.size)

%.hex: %.avr.elf
	$(AVR_OBJCOPY) -j .text -j .data -O ihex $< $@

%.srec: %.avr.elf
	$(AVR_OBJCOPY) -j .text -j .data -O srec $< $@

%.bin: %.avr.elf
	$(AVR_OBJCOPY) -j .text -j .data -O binary $< $@

%.size: %.avr.elf
	$(AVR_SIZE) $< | grep Full

# Rules for building the .eeprom rom images.

EEPROMS := $(AVR_PROGS:%=%_eeprom.hex) $(AVR_PROGS:%=%_eeprom.bin) \
	   $(AVR_PROGS:%=%_eeprom.srec)

eeprom: ehex

ehex: $(AVR_PROGS:%=%_eeprom.hex)
ebin: $(AVR_PROGS:%=%_eeprom.bin)
esrec: $(AVR_PROGS:%=%_eeprom.srec)

%_eeprom.hex: %.avr.elf
	$(AVR_OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@

%_eeprom.srec: %.avr.elf
	$(AVR_OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O srec $< $@

%_eeprom.bin: %.avr.elf
	$(AVR_OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O binary $< $@

# Rules for programming AVR.

ifeq ($(words $(AVR_PROGS)),1)
program: $(AVR_PROGS:%=%.program)
else
program:
	@echo Several programs, choose which one to program with "make name.program".
endif

%.program: %.hex
	$(AVR_PROGRAM)

# Cleaning.

clean.avr:
	rm -f $(OBJDIR)/*.avr.o $(OBJDIR)/*.avr.d $(AVR_ELFS) \
		*.avr.lst *.avr.map $(TEXTS) $(EEPROMS)

clean.simu:
	rm -f $(OBJDIR)/*.avr.simu.o $(OBJDIR)/*.avr.simu.d $(AVR_SIMU_ELFS)