-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMakefile
150 lines (120 loc) · 3.51 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
LIBRARY ?= avrm
VERSION ?= 0.0.5
ifeq ($(OS),Darwin)
PREFIX = /usr/local/$(LIBRARY)/$(VERSION)
else
PREFIX = /usr/avr/$(LIBRARY)/$(VERSION)
endif
DEPENDENCIES ?= $(PREFIX)
# The running speed of the AVR in Hz, mostly used for `delay_ms` time
# calculations.
F_CPU ?= 16000000UL
# AVR MCU type
#
# - atmega328p
# - TODO: ATTiny
#
# For a complete list, see https://gcc.gnu.org/onlinedocs/gcc/AVR-Options.html.
MMCU ?= atmega328p
# The system path to communicate via serial, used for both flashing and serial
# monitoring. Defaults to the first port in /dev containing "tty.usb".
PORT ?= /dev/$(shell ls /dev/ | grep -i "tty.*usb" | sed -n 1p)
# UART baud default baud rate.
BAUD ?= 9600
# Flashing baud rate.
#
# - 115200 (Arduino Uno)
# - 57600 (Arduino Mini Pro)
AVRDUDE_BAUD ?= 57600
# Programmer used to communicate with the AVR.
#
# - arduino
# - usbtiny
#
# For a complete list see `avrdude -c '?'`.
AVRDUDE_PROGRAMMER ?= arduino
# Partno of the device we're talking to. Typically related to the MMCU variable
# defined above.
#
# - m328p (Typical Arduino)
# - t85 (ATTiny)
#
# For a complete list see `avrdude -p '?'`.
AVRDUDE_PARTNO ?= m328p
# -----------------------------------------------------------------------------
CC = avr-gcc
CFLAGS = -Wall -Werror -pedantic -Os -std=c99 \
-DF_CPU=$(F_CPU) -mmcu=$(MMCU) \
-I. $(DEPENDENCIES:%=-I%/include)
LDFLAGS = -L. $(DEPENDENCIES:%=-L%/lib)
ifeq ($(LIBRARY),avrm)
LDLIBS ?= -lavrm
else
LDLIBS ?= -l$(LIBRARY) -lavrm
endif
# The `obj-copy` executable.
OBJ_COPY = avr-objcopy
OBJ_COPY_FLAGS = -O ihex -R .eeprom
# The `ar` archiver executable.
AR = avr-ar
# The `as` assembeler.
AS = avr-as
# The `avrdude` executable.
AVRDUDE = avrdude
AVRDUDE_FLAGS = -F -V -c $(AVRDUDE_PROGRAMMER) -p $(AVRDUDE_PARTNO)
# The `avr-size` executable.
AVRSIZE = avr-size
AVRSIZE_FLAGS = -C
# These rules are not file based.
.PHONY: install test clean serial
SRCS = $(shell find lib -name '*.c')
TESTS = $(shell find tests -name '*.c')
# Mark all .o files as intermediate.
.INTERMEDIATE: $(SRCS:.c=.o) $(TESTS:.c=.hex)
# Build the library.
all: lib$(LIBRARY).a($(SRCS:.c=.o))
install: all
mkdir -p $(PREFIX)/lib $(PREFIX)/include $(PREFIX)/share
install lib$(LIBRARY).a $(PREFIX)/lib
install lib/$(LIBRARY).h $(PREFIX)/include
# TODO: https://github.com/nixpulvis/avrm/issues/2
ifneq ($(wildcard lib/$(LIBRARY)/*.h),)
mkdir -p $(PREFIX)/include/$(LIBRARY)
install lib/$(LIBRARY)/*.h $(PREFIX)/include/$(LIBRARY)
endif
# Install the template AVRM Makefile.
ifeq ($(LIBRARY),avrm)
install Makefile $(PREFIX)/share
endif
test: install $(TESTS:.c=.test)
clean:
rm -f $(SRCS:.c=.o) $(TESTS:.c=.o) lib$(LIBRARY).a
# Open up a screen session for communication with the AVR
# through it's on-board UART.
serial:
screen $(PORT) $(BAUD)
%.test: %.flash
@echo "TODO: Read serial and check output #10"
# Given a binary program display information about how much memory it will
# use to hold the program.
%.size: %.bin
$(AVRSIZE) $(AVRSIZE_FLAGS) $<
# Given a hex file using `avrdude` this target flashes the AVR with the
# new program contained in the hex file.
%.flash: %.hex
echo $(PROGRAMMER)
ifeq ($(PROGRAMMER),usbtiny)
$(AVRDUDE) $(AVRDUDE_FLAGS) -U flash:w:$<
else
$(AVRDUDE) $(AVRDUDE_FLAGS) -P $(PORT) -b $(AVRDUDE_BAUD) -U flash:w:$<
endif
# *.hex <- *
%.hex: %.bin
$(OBJ_COPY) $(OBJ_COPY_FLAGS) $< $@
%.bin: %.o
$(CC) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
# .asm <- .c
%.asm: %.c
$(CC) $(CFLAGS) -S $(C_HEADERS) -DF_CPU=$(F_CPU) -mmcu=$(MMCU) -c $< -o $@
%.o: %.asm
$(AS) -mmcu=$(MMCU) -o $@ $<