summaryrefslogtreecommitdiff
path: root/Makefile
blob: 90edd99ca03795cdbc78114f92698763553412d7 (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
154
155
156
157
158
159
160
161
162
163
164
.PHONY: \
	vars \
	help \
	clean \
	compile \
	link \
	upload \
	fuses \
	check-fuses

# Hack to get the directory this makefile is in:
MKFILE_PATH   := $(lastword $(MAKEFILE_LIST))
MKFILE_DIR    := $(notdir $(patsubst %/,%,$(dir $(MKFILE_PATH))))
MKFILE_ABSDIR := $(abspath $(MKFILE_DIR))

# Hack to get all *.h files into compile dependencies:
HEADERS        = $(shell find $(MKFILE_DIR) -name "*.h")


BUILDTMP         ?= $(MKFILE_DIR)/build-tmp
OPTIMIZATION     ?= -Os
AVRCC            ?= avr-gcc
AVRDUDE          ?= avrdude
#---------------------------------------------------------
# AVRDUDE_FLASHARG:
# This preserves the chip memory when updating the fuses.
# To erase the chip when setting fuses, do:
#
#     make AVRDUDE_FLASHARG=-e fuses
#
AVRDUDE_FLASHARG ?= -D
#---------------------------------------------------------
AVR_SIZE         ?= avr-size
AVR_OBJCOPY      ?= avr-objcopy
DEVICE           ?= attiny85
CLOCK            ?= 8000000L
PROGRAMMER       ?= avrisp
PORT             ?= /dev/ttyACM0
BAUD             ?= 19200
UNIT             ?= main.c
FUSE_EXT         ?= 0xff
FUSE_HIGH        ?= 0xdf
FUSE_LOW         ?= 0xe2

# Misc target info:
help_spacing  := 12

.DEFAULT_GOAL := compile

#---------------------------------------------------------
# Ensure temp directories.
#
# In order to ensure temp dirs exit, we include a file
# that doesn't exist, with a target declared as PHONY
# (above), and then have the target create our tmp dirs.
#---------------------------------------
-include ensure-tmp
ensure-tmp:
	@mkdir -p $(BUILDTMP)

vars: ## Print relevant environment vars
	@printf  "%-20.20s%s\n"  "MKFILE_PATH:"    "$(MKFILE_PATH)"
	@printf  "%-20.20s%s\n"  "MKFILE_DIR:"     "$(MKFILE_DIR)"
	@printf  "%-20.20s%s\n"  "MKFILE_ABSDIR:"  "$(MKFILE_ABSDIR)"
	@printf  "%-20.20s%s\n"  "BUILDTMP:"       "$(BUILDTMP)"
	@printf  "%-20.20s%s\n"  "OPTIMIZATION:"   "$(OPTIMIZATION)"
	@printf  "%-20.20s%s\n"  "AVRCC:"          "$(AVRCC)"
	@printf  "%-20.20s%s\n"  "AVRDUDE:"        "$(AVRDUDE)"
	@printf  "%-20.20s%s\n"  "AVRDUDE_OPTS:"   "$(AVRDUDE_OPTS)"
	@printf  "%-20.20s%s\n"  "AVR_SIZE:"       "$(AVR_SIZE)"
	@printf  "%-20.20s%s\n"  "AVR_OBJCOPY:"    "$(AVR_OBJCOPY)"
	@printf  "%-20.20s%s\n"  "DEVICE:"         "$(DEVICE)"
	@printf  "%-20.20s%s\n"  "CLOCK:"          "$(CLOCK)"
	@printf  "%-20.20s%s\n"  "PROGRAMMER:"     "$(PROGRAMMER)"
	@printf  "%-20.20s%s\n"  "BAUD:"           "$(BAUD)"
	@printf  "%-20.20s%s\n"  "UNIT:"           "$(UNIT)"

help: ## Print this makefile help menu
	@echo "TARGETS:"
	@grep '^[a-z_\-]\{1,\}:.*##' $(MAKEFILE_LIST) \
		| sed 's/^\([a-z_\-]\{1,\}\): *\(.*[^ ]\) *## *\(.*\)/\1:\t\3 (\2)/g' \
		| sed 's/^\([a-z_\-]\{1,\}\): *## *\(.*\)/\1:\t\2/g' \
		| awk '{$$1 = sprintf("%-$(help_spacing)s", $$1)} 1' \
		| sed 's/^/  /'
	@printf "\nUsage:\n    make \\ \n    %s \\ \n    %s \\ \n    %s \\ \n    %s\n" \
		"USBDEVICE=/dev/cu.usbserial-1234" \
		"UNIT=my_source.c" \
		"DEVICE=<mcu>" \
		"<make target>"

clean: ## Clean build artifacts
	rm -rf $(BUILDTMP)/*
	rm -vf *.s

compile: $(UNIT) $(HEADERS) ## Compile project
	$(AVRCC) \
	     -c \
	     -Wall \
	     $(OPTIMIZATION) \
	     -DF_CPU=$(CLOCK) \
	     -mmcu=$(DEVICE) \
	     -I$(MKFILE_DIR) \
 	     $(UNIT) \
	     -o $(BUILDTMP)/$(UNIT).o

link: compile ## Link compilation artifacts and package for upload
	$(AVRCC) \
	    -w \
	    $(OPTIMIZATION) \
	    -flto \
	    -fuse-linker-plugin \
	    -Wl,--gc-sections \
	    -mmcu=$(DEVICE) \
	    -o $(BUILDTMP)/$(UNIT).elf \
	    $(BUILDTMP)/$(UNIT).o \
	    -L$(BUILDTMP) \
	    $(LDFLAGS)
	$(AVR_OBJCOPY) \
	    -O ihex \
	    -j .eeprom \
	    --set-section-flags=.eeprom=alloc,load \
	    --no-change-warnings \
	    --change-section-lma .eeprom=0 \
	    $(BUILDTMP)/$(UNIT).elf \
	    $(BUILDTMP)/$(UNIT).eep
	$(AVR_OBJCOPY) \
	    -O ihex \
	    -R .eeprom \
	    $(BUILDTMP)/$(UNIT).elf \
	    $(BUILDTMP)/$(UNIT).hex
	$(AVR_SIZE) \
	    -A $(BUILDTMP)/$(UNIT).elf

upload: link ## Upload
	$(AVRDUDE) \
	    $(AVRDUDE_OPTS) \
	    -v \
	    -p$(DEVICE) \
	    -c$(PROGRAMMER) \
			-P${PORT} \
	    -b$(BAUD) \
	    -Uflash:w:$(BUILDTMP)/$(UNIT).hex:i


fuses: ## Flash the fuses
	$(AVRDUDE) \
	    $(AVRDUDE_OPTS) \
	    -v \
	    -D \
	    -p$(DEVICE) \
	    -c$(PROGRAMMER) \
			-P${PORT} \
	    -b$(BAUD) \
	    -Uefuse:w:$(FUSE_EXT):m \
	    -Uhfuse:w:$(FUSE_HIGH):m \
	    -Ulfuse:w:$(FUSE_LOW):m

check-fuses: ## Verify device signature and check fuse values
	$(AVRDUDE) \
	    $(AVRDUDE_OPTS) \
	    -p$(DEVICE) \
	    -c$(PROGRAMMER) \
			-P${PORT} \
	    -b$(BAUD)