-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
130 lines (104 loc) · 3.74 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
# --------------------------------------------------------
# Custom ATmega Makefile
# created by: Chao Liu([email protected])
# updated: Aug 28, 2016
# --------------------------------------------------------
# --------------------------------------------------------
# Support atmega88a, atmega168a and atmega32u4
# --------------------------------------------------------
# --------------------------------------------------------
# you shouldn't change anything below here,
# unless you really know what you're doing
# --------------------------------------------------------
# --------------------------------------------------------
# Specify the device you are using and its clock.
# --------------------------------------------------------
DEVICE = atmega32u4
CLOCK = 16000000
# --------------------------------------------------------
# if you are using JTAGICE mkII, let PROGRAMMER = jtag2isp;
# if you are using AVRISP mkII, let PROGRAMMER = avrispmkII;
# if you are using DFU, let PROGRAMMER = dfu.
# otherwise it assumes bootloader AVR109
# --------------------------------------------------------
PROGRAMMER = avr109
PORT = assignPORTtodevice
ifeq ($(DEVICE), atmega88a)
TARGET_DEVICE = m88
DEVICE_LABEL = ATmega88a
else ifeq ($(DEVICE), atmega168a)
TARGET_DEVICE = m168
DEVICE_LABEL = ATmega168a
else ifeq ($(DEVICE), atmega32u4)
TARGET_DEVICE = m32u4
DEVICE_LABEL = ATmega32U4
else
$(error DEVICE = $(DEVICE) is unknown.)
endif
COMPILE = avr-gcc -Wall -Os -DF_CPU=$(CLOCK) -mmcu=$(DEVICE) -D __DELAY_BACKWARD_COMPATIBLE__
SRCDIR = src
INCDIR = inc # directory for header files
INCLUDES += -I$(INCDIR)
_SOURCES += $(wildcard $(SRCDIR)/*.c)
SOURCES = $(notdir $(_SOURCES))
OBJDIRS = obj_$(DEVICE)
OBJECTS := $(patsubst %.c,%.o, $(SOURCES))
OBJECTS_POS = $(addprefix $(OBJDIRS)/,$(OBJECTS))
vpath %.c $(dir $(_SOURCES)) # directory for source files
vpath %.o $(OBJDIRS) # directory for object files
vpath %.elf $(OBJDIRS)
vpath %.hex .
# symbolic targets:
all: main.hex
.PHONY : all
.c.o:
@[ ! -e $@ ] && mkdir -p $(OBJDIRS)
@$(COMPILE) $(INCLUDES) -c $< -o $(OBJDIRS)/$@
@echo "[CC] $^"
.S.o:
@$(COMPILE) $(INCLUDES) -x assembler-with-cpp -c $< -o $(OBJDIRS)/$@
@echo "[>-----Generate $@ Successfully-----<]"
.c.s:
@$(COMPILE) $(INCLUDES) -S $< -o $(OBJDIRS)/$@
@echo "[>-----Generate $@ Successfully-----<]"
fuse:
ifeq ($(DEVICE), $(filter $(DEVICE), atmega88a atmega168a))
@avrdude -p $(TARGET_DEVICE) -c $(PROGRAMMER) -P usb -U lfuse:w:0xe2:m -U hfuse:w:0xdf:m -B10
@echo "[>-----Program Fuse Done-----<]"
else ifeq ($(DEVICE), atmega32u4)
@avrdude -p $(TARGET_DEVICE) -c $(PROGRAMMER) -P usb -U lfuse:w:0xe2:m -U hfuse:w:0xdf:m -B10
@echo "[>-----Program Fuse Done-----<]"
endif
install: flash
flash: all
ifeq ($(DEVICE), $(filter $(DEVICE), atmega88a atmega168a))
@avrdude -p $(TARGET_DEVICE) -c $(PROGRAMMER) -P $(PORT) -U flash:w:main.hex -B9
@echo "[>-----$(DEVICE_LABEL) Loaded-----<]"
endif
ifeq ($(DEVICE), atmega32u4)
ifneq ($(PROGRAMMER), dfu)
@avrdude -p $(TARGET_DEVICE) -c $(PROGRAMMER) -P $(PORT) -U flash:w:main.hex
@echo "[>-----$(DEVICE_LABEL) Loaded-----<]"
else
@dfu-programmer $(DEVICE) erase
@dfu-programmer $(DEVICE) flash main.hex
@echo "[>-----$(DEVICE_LABEL) Loaded-----<]"
endif
endif
clean:
rm -fr main.hex $(OBJDIRS)
# file targets:
main.elf: $(OBJECTS)
@$(COMPILE) -o $(OBJDIRS)/main.elf $(OBJECTS_POS) -lm
@echo "[ELF] $(OBJDIRS)/$@"
main.hex: main.elf
@rm -f main.hex
@avr-objcopy -j .text -j .data -O ihex $(OBJDIRS)/main.elf main.hex
@avr-size --format=avr --mcu=$(DEVICE) $(OBJDIRS)/main.elf
@echo "[>-----Generate $@ Successfully-----<]"
@echo "[>-----Build Successfully-----<]"
# Targets for code debugging and analysis:
disasm: main.elf
avr-objdump -d $(OBJDIRS)/main.elf
cpp:
$(COMPILE) -E main.c