-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
186 lines (124 loc) · 4.36 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#==== Main Options =============================================================
MCU = attiny45
F_CPU = 16000000
LFUSE = 0xf1
HFUSE = 0xdf
TARGET = main
SRC = $(TARGET).c
OBJDIR = obj
BACKUPDIR = backup
#==== Compile Options ==========================================================
OPT = s
CFLAGS = -mmcu=$(MCU) -I.
CFLAGS += -DF_CPU=$(F_CPU)UL
CFLAGS += -O$(OPT)
#CFLAGS += -mint8
#CFLAGS += -mshort-calls
CFLAGS += -funsigned-char
CFLAGS += -funsigned-bitfields
CFLAGS += -fpack-struct
CFLAGS += -fshort-enums
#CFLAGS += -fno-unit-at-a-time
CFLAGS += -Wall
CFLAGS += -Wstrict-prototypes
hCFLAGS += -Wundef
#CFLAGS += -Wunreachable-code
#CFLAGS += -Wsign-compare
CFLAGS += -std=gnu99
#CFLAGS += -Wa,-adhlns=$(<:%.c=$(OBJDIR)/%.lst)
#CFLAGS += -flto
#LDFLAGS =
#==== Programming Options (avrdude) ============================================
AVRDUDE_PROGRAMMER = stk500v1
AVRDUDE_PORT = /dev/ttyACM3
AVRDUDE_BAUD = 19200
#AVRDUDE_NO_VERIFY = -V
AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -b $(AVRDUDE_BAUD) -c $(AVRDUDE_PROGRAMMER) $(AVRDUDE_NO_VERIFY)
#==== Targets ==================================================================
CC = avr-gcc
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
AVRDUDE = avrdude
AVRSIZE = avr-size
REMOVE = rm -f
REMOVEDIR = rm -rf
TAIL = tail
AWK = awk
OBJ = $(SRC:%.c=$(OBJDIR)/%.o)
AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
MEMORYTYPES = flash eeprom fuse lfuse hfuse efuse boot calibration lock signature application apptable prodsig usersig
all: build
help:
@echo 'Basic targets:'
@echo ' build Create all files.'
@echo ' clean Remove files created by make.'
@echo ' size Show the size of each section in the .elf file.'
@echo
@echo 'Create files:'
@echo ' elf Create binary .elf file.'
@echo ' hex Create .hex file containing .text and .data sections.'
@echo ' eep Create .eep file with the EEPROM content.'
@echo ' lss Create .lss file with a listing of the program.'
@echo
@echo 'Flashing:'
@echo ' program Write flash and EEPROM.'
@echo ' flash Write only flash.'
@echo ' eeprom Write only EEPROM.'
@echo ' backup Backup MCU content to "$(BACKUPDIR)". Available memory types:'
@echo ' $(MEMORYTYPES)'
@echo
@echo 'Fuses:'
@echo ' readfuses Read fuses from MCU.'
@echo ' writefuses Write fuses to MCU using .fuse section.'
@echo ' printfuses Print fuses from .fuse section.'
build: elf hex eep lss size
elf: $(TARGET).elf
hex: $(TARGET).hex
eep: $(TARGET).eep
lss: $(TARGET).lss
program: flash eeprom
flash: $(TARGET).hex
$(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH)
eeprom: $(TARGET).eep
$(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_EEPROM)
readfuses:
$(AVRDUDE) $(AVRDUDE_FLAGS) -U lfuse:r:-:i -U hfuse:r:-:i
writefuses:
$(AVRDUDE) $(AVRDUDE_FLAGS) -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m
#writefuses: FUSES = $(shell $(OBJDUMP) -s --section=.fuse $(TARGET).elf | tail -1 | awk '{print substr($$2,1,2),substr($$2,3,2),substr($$2,5,2)}')
#writefuses: $(TARGET).elf
# $(AVRDUDE) $(AVRDUDE_FLAGS) \
# $(if $(word 1,$(FUSES)),-U lfuse:w:0x$(word 1,$(FUSES)):m) \
# $(if $(word 2,$(FUSES)),-U hfuse:w:0x$(word 2,$(FUSES)):m) \
# $(if $(word 3,$(FUSES)),-U efuse:w:0x$(word 3,$(FUSES)):m)
printfuses: FUSES = $(shell $(OBJDUMP) -s --section=.fuse $(TARGET).elf | tail -1 | awk '{print substr($$2,1,2),substr($$2,3,2),substr($$2,5,2)}')
printfuses: $(TARGET).elf
@echo 'FUSES = $(FUSES)'
%.hex: %.elf
$(OBJCOPY) -O ihex -j .text -j .data $< $@
%.eep: %.elf
-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 -O ihex $< $@
%.lss: %.elf
$(OBJDUMP) -h -S $< > $@
.SECONDARY: $(TARGET).elf
.PRECIOUS: $(OBJ)
%.elf: $(OBJ)
$(CC) $(CFLAGS) $^ --output $@ $(LDFLAGS)
$(OBJDIR)/%.o: %.c
$(shell mkdir -p $(OBJDIR) 2>/dev/null)
$(CC) -c $(CFLAGS) $< -o $@
size: $(TARGET).elf
$(AVRSIZE) -A $(TARGET).elf
clean:
$(REMOVE) "$(TARGET).hex"
$(REMOVE) "$(TARGET).eep"
$(REMOVE) "$(TARGET).elf"
$(REMOVE) "$(TARGET).lss"
$(REMOVEDIR) "$(OBJDIR)"
backup:
$(shell mkdir -p $(BACKUPDIR) 2>/dev/null)
@for memory in $(MEMORYTYPES); do \
$(AVRDUDE) $(AVRDUDE_FLAGS) -U $$memory:r:$(BACKUPDIR)/$(MCU).$$memory.hex:i; \
done
.PHONY: all size build elf hex eep lss clean program flash eeprom readfuses writefuses printfuses backup help