-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
99 lines (81 loc) · 2.06 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
#
# Makefile
# Copyright (c) 2017 João Baptista de Paula e Silva
# este código está sob a licença MIT
#
#
# toolchain
#
CC := avr-gcc
CXX := avr-g++
AS := avr-gcc
LD := avr-g++
OBJCOPY := avr-objcopy
OBJDUMP := avr-objdump
AVRDUDE := avrdude
#
# platform configs
#
DEVICE := atmega328p
CLOCK := 16000000
PROGRAMMER := stk500v1
BAUD := 19200
LFUSE := 0xFF
HFUSE := 0xD1
EFUSE := 0xFD
#
# compiling configs
#
REGISTERS := r3 r4 r5 r6 r7
OPTRULE := -O3 -fweb -frename-registers -flto -fno-fat-lto-objects
COMFLAGS := -MMD -mmcu=$(DEVICE) -DF_CPU=$(CLOCK)UL $(addprefix --fixed-,$(REGISTERS))
CCPPFLAGS := $(COMFLAGS) $(OPTRULE) -Wall -ffunction-sections -fdata-sections -Wno-main -Wno-volatile-register-var
CFLAGS := $(CCPPFLAGS) -std=gnu11
CPPFLAGS := $(CCPPFLAGS) -std=gnu++1z -fpermissive -fno-exceptions -fno-threadsafe-statics
ASMFLAGS := $(COMFLAGS) -x assembler-with-cpp
LDFLAGS := $(OPTRULE) -fuse-linker-plugin $(addprefix --fixed-,$(REGISTERS)) -Wall -Wl,--gc-sections -mmcu=$(DEVICE)
#
# files to be compiled
#
SOURCES := $(wildcard *.c *.cpp *.S)
OBJECTS := $(SOURCES:=.o)
DEPENDS := $(OBJECTS:.o=.d)
out.hex: out.elf
#
# compilation rules (finally!)
# Makefile is included as a requisite for convenience - modifying
# the Makefile will make all sources rebuild
#
-include $(DEPENDS)
%.c.o: %.c Makefile
$(CC) $(CFLAGS) -c -o $@ $<
%.cpp.o: %.cpp Makefile
$(CXX) $(CPPFLAGS) -c -o $@ $<
%.S.o: %.S Makefile
$(AS) $(ASMFLAGS) -c -o $@ $<
#
# linking rule
#
out.elf: $(OBJECTS)
$(LD) $(LDFLAGS) -o $@ $^
@./size.py
out.hex: out.elf
$(OBJCOPY) -O ihex -R .eeprom $< $@
.PHONY: dump upload setfuses clean
#
# dump rule
#
dump: out.elf
$(OBJDUMP) -D -m avr5 out.elf > avrdisasm.txt
#
# upload and setfuses rules
#
upload: out.hex
$(AVRDUDE) -p$(DEVICE) -c$(PROGRAMMER) -b$(BAUD) -P$(PORT) -Uflash:w:out.hex:i
setfuses:
$(AVRDUDE) -p$(DEVICE) -c$(PROGRAMMER) -b$(BAUD) -P$(PORT) -Ulfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m -Uefuse:w:$(EFUSE):m -Ulock:w:0x3F:m
#
# clean rule
#
clean:
rm -rf *.o *.d out.elf out.hex