1
########################################################################
3
# Arduino command line tools Makefile
4
# System part (i.e. project independent)
6
# Copyright (C) 2010 Martin Oldfield <m@mjo.tc>, based on work that is
7
# Copyright Nicholas Zambetti, David A. Mellis & Hernando Barragan
9
# This file is free software; you can redistribute it and/or modify it
10
# under the terms of the GNU Lesser General Public License as
11
# published by the Free Software Foundation; either version 2.1 of the
12
# License, or (at your option) any later version.
14
# Adapted from Arduino 0011 Makefile by M J Oldfield
16
# Original Arduino adaptation by mellis, eighthave, oli.keller
18
# Version 0.1 17.ii.2009 M J Oldfield
20
# 0.2 22.ii.2009 M J Oldfield
21
# - fixes so that the Makefile actually works!
22
# - support for uploading via ISP
23
# - orthogonal choices of using the Arduino for
24
# tools, libraries and uploading
26
# 0.3 21.v.2010 M J Oldfield
27
# - added proper license statement
28
# - added code from Philip Hands to reset
29
# Arduino prior to upload
31
# 0.4 25.v.2010 M J Oldfield
32
# - tweaked reset target on Philip Hands' advice
34
########################################################################
36
# STANDARD ARDUINO WORKFLOW
38
# Given a normal sketch directory, all you need to do is to create
39
# a small Makefile which defines a few things, and then includes this one.
43
# ARDUINO_DIR = /usr/share/arduino
46
# ARDUINO_LIBS = LiquidCrystal
50
# ARDUINO_PORT = /dev/cu.usb*
52
# include /usr/local/share/Arduino.mk
54
# Hopefully these will be self-explanatory but in case they're not:
56
# ARDUINO_DIR - Where the Arduino software has been unpacked
57
# TARGET - The basename used for the final files. Canonically
58
# this would match the .pde file, but it's not needed
59
# here: you could always set it to xx if you wanted!
60
# ARDUINO_LIBS - A list of any libraries used by the sketch (we assume
61
# these are in $(ARDUINO_DIR)/hardware/libraries
62
# MCU,F_CPU - The target processor description
63
# ARDUINO_PORT - The port where the Arduino can be found (only needed
66
# Once this file has been created the typical workflow is just
70
# All of the object files are created in the build-cli subdirectory
71
# All sources should be in the current directory and can include:
72
# - at most one .pde file which will be treated as C++ after the standard
73
# Arduino header and footer have been affixed.
74
# - any number of .c, .cpp, .s and .h files
77
# Besides make upload you can also
79
# make clean - remove all our dependencies
80
# make depends - update dependencies
81
# make reset - reset the Arduino by tickling DTR on the serial port
82
# make raw_upload - upload without first resetting
84
########################################################################
86
# ARDUINO WITH OTHER TOOLS
88
# If the tools aren't in the Arduino distribution, then you need to
89
# specify their location:
91
# AVR_TOOLS_PATH = /usr/bin
92
# AVRDUDE_CONF = /etc/avrdude/avrdude.conf
94
########################################################################
98
# You need to specify some details of your ISP programmer and might
99
# also need to specify the fuse values:
101
# ISP_PROG = -c stk500v2
102
# ISP_PORT = /dev/ttyACM0
104
# ISP_LOCK_FUSE_PRE = 0x3f
105
# ISP_LOCK_FUSE_POST = 0xcf
106
# ISP_HIGH_FUSE = 0xdf
107
# ISP_LOW_FUSE = 0xff
108
# ISP_EXT_FUSE = 0x01
110
# I think the fuses here are fine for uploading to the ATmega168
111
# without bootloader.
113
# To actually do this upload use the ispload target:
118
########################################################################
124
ifndef AVR_TOOLS_PATH
125
AVR_TOOLS_PATH = /usr/bin
128
ifndef ARDUINO_ETC_PATH
129
ARDUINO_ETC_PATH = /etc
133
AVRDUDE_CONF = $(ARDUINO_ETC_PATH)/avrdude.conf
136
ARDUINO_LIB_PATH = $(ARDUINO_DIR)/hardware/libraries
137
ARDUINO_CORE_PATH = $(ARDUINO_DIR)/hardware/arduino/cores/arduino
141
# Everything gets built in here
144
########################################################################
147
LOCAL_C_SRCS = $(wildcard *.c)
148
LOCAL_CPP_SRCS = $(wildcard *.cpp)
149
LOCAL_CC_SRCS = $(wildcard *.cc)
150
LOCAL_PDE_SRCS = $(wildcard *.pde)
151
LOCAL_AS_SRCS = $(wildcard *.S)
152
LOCAL_OBJ_FILES = $(LOCAL_C_SRCS:.c=.o) $(LOCAL_CPP_SRCS:.cpp=.o) \
153
$(LOCAL_CC_SRCS:.cc=.o) $(LOCAL_PDE_SRCS:.pde=.o) \
154
$(LOCAL_AS_SRCS:.S=.o)
155
LOCAL_OBJS = $(patsubst %,$(OBJDIR)/%,$(LOCAL_OBJ_FILES))
158
DEPS = $(LOCAL_OBJS:.o=.d)
161
ifeq ($(strip $(NO_CORE)),)
162
ifdef ARDUINO_CORE_PATH
163
CORE_C_SRCS = $(wildcard $(ARDUINO_CORE_PATH)/*.c)
164
CORE_CPP_SRCS = $(wildcard $(ARDUINO_CORE_PATH)/*.cpp)
165
CORE_OBJ_FILES = $(CORE_C_SRCS:.c=.o) $(CORE_CPP_SRCS:.cpp=.o)
166
CORE_OBJS = $(patsubst $(ARDUINO_CORE_PATH)/%, \
167
$(OBJDIR)/%,$(CORE_OBJ_FILES))
172
OBJS = $(LOCAL_OBJS) $(CORE_OBJS)
174
########################################################################
175
# Rules for making stuff
178
# The name of the main targets
179
TARGET_HEX = $(OBJDIR)/$(TARGET).hex
180
TARGET_ELF = $(OBJDIR)/$(TARGET).elf
181
TARGETS = $(OBJDIR)/$(TARGET).*
183
# A list of dependencies
184
DEP_FILE = $(OBJDIR)/depends.mk
186
# Names of executables
187
CC = $(AVR_TOOLS_PATH)/avr-gcc
188
CXX = $(AVR_TOOLS_PATH)/avr-g++
189
OBJCOPY = $(AVR_TOOLS_PATH)/avr-objcopy
190
OBJDUMP = $(AVR_TOOLS_PATH)/avr-objdump
191
AR = $(AVR_TOOLS_PATH)/avr-ar
192
SIZE = $(AVR_TOOLS_PATH)/avr-size
193
NM = $(AVR_TOOLS_PATH)/avr-nm
200
SYS_LIBS = $(patsubst %,$(ARDUINO_LIB_PATH)/%,$(ARDUINO_LIBS))
201
SYS_INCLUDES = $(patsubst %,-I%,$(SYS_LIBS))
202
SYS_OBJS = $(wildcard $(patsubst %,%/*.o,$(SYS_LIBS)))
204
CPPFLAGS = -mmcu=$(MCU) -DF_CPU=$(F_CPU) \
205
-I. -I$(ARDUINO_CORE_PATH) \
206
$(SYS_INCLUDES) -g -Os -w -Wall \
207
-ffunction-sections -fdata-sections
209
CXXFLAGS = -fno-exceptions
210
ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp
211
LDFLAGS = -mmcu=$(MCU) -lm -Wl,--gc-sections -Os
213
# Rules for making a CPP file from the main sketch (.cpe)
214
PDEHEADER = \\\#include \"WProgram.h\"
216
# Expand and pick the first port
217
ARD_PORT = $(firstword $(wildcard $(ARDUINO_PORT)))
219
# Implicit rules for building everything (needed to get everything in
220
# the right directory)
222
# Rather than mess around with VPATH there are quasi-duplicate rules
223
# here for building e.g. a system C++ file and a local C++
224
# file. Besides making things simpler now, this would also make it
225
# easy to change the build options in future
227
# normal local sources
228
# .o rules are for objects, .d for dependency tracking
229
# there seems to be an awful lot of duplication here!!!
231
$(CC) -c $(CPPFLAGS) $(CFLAGS) $< -o $@
234
$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $< -o $@
237
$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $< -o $@
240
$(CC) -c $(CPPFLAGS) $(ASFLAGS) $< -o $@
243
$(CC) -c $(CPPFLAGS) $(ASFLAGS) $< -o $@
246
$(CC) -MM $(CPPFLAGS) $(CFLAGS) $< -MF $@ -MT $(@:.d=.o)
249
$(CXX) -MM $(CPPFLAGS) $(CXXFLAGS) $< -MF $@ -MT $(@:.d=.o)
252
$(CXX) -MM $(CPPFLAGS) $(CXXFLAGS) $< -MF $@ -MT $(@:.d=.o)
255
$(CC) -MM $(CPPFLAGS) $(ASFLAGS) $< -MF $@ -MT $(@:.d=.o)
258
$(CC) -MM $(CPPFLAGS) $(ASFLAGS) $< -MF $@ -MT $(@:.d=.o)
260
# the pde -> cpp -> o file
261
$(OBJDIR)/%.cpp: %.pde
262
$(ECHO) $(PDEHEADER) > $@
265
$(OBJDIR)/%.o: $(OBJDIR)/%.cpp
266
$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $< -o $@
268
$(OBJDIR)/%.d: $(OBJDIR)/%.cpp
269
$(CXX) -MM $(CPPFLAGS) $(CXXFLAGS) $< -MF $@ -MT $(@:.d=.o)
272
$(OBJDIR)/%.o: $(ARDUINO_CORE_PATH)/%.c
273
$(CC) -c $(CPPFLAGS) $(CFLAGS) $< -o $@
275
$(OBJDIR)/%.o: $(ARDUINO_CORE_PATH)/%.cpp
276
$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $< -o $@
278
# various object conversions
279
$(OBJDIR)/%.hex: $(OBJDIR)/%.elf
280
$(OBJCOPY) -O ihex -R .eeprom $< $@
282
$(OBJDIR)/%.eep: $(OBJDIR)/%.elf
283
-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
284
--change-section-lma .eeprom=0 -O ihex $< $@
286
$(OBJDIR)/%.lss: $(OBJDIR)/%.elf
287
$(OBJDUMP) -h -S $< > $@
289
$(OBJDIR)/%.sym: $(OBJDIR)/%.elf
292
########################################################################
297
AVRDUDE = $(AVR_TOOLS_PATH)/avrdude
300
AVRDUDE_COM_OPTS = -q -V -p $(MCU)
302
AVRDUDE_COM_OPTS += -C $(AVRDUDE_CONF)
305
ifndef AVRDUDE_ARD_PROGRAMMER
306
AVRDUDE_ARD_PROGRAMMER = stk500v1
309
ifndef AVRDUDE_ARD_BAUDRATE
310
AVRDUDE_ARD_BAUDRATE = 19200
313
AVRDUDE_ARD_OPTS = -c $(AVRDUDE_ARD_PROGRAMMER) -b $(AVRDUDE_ARD_BAUDRATE) -P $(ARD_PORT) $(AVRDUDE_ARD_EXTRAOPTS)
315
ifndef ISP_LOCK_FUSE_PRE
316
ISP_LOCK_FUSE_PRE = 0x3f
319
ifndef ISP_LOCK_FUSE_POST
320
ISP_LOCK_FUSE_POST = 0xcf
336
ISP_PROG = -c stk500v2
339
AVRDUDE_ISP_OPTS = -P $(ISP_PORT) $(ISP_PROG)
342
########################################################################
344
# Explicit targets start here
347
all: $(OBJDIR) $(TARGET_HEX)
352
$(TARGET_ELF): $(OBJS)
353
$(CC) $(LDFLAGS) -o $@ $(OBJS) $(SYS_OBJS)
355
$(DEP_FILE): $(OBJDIR) $(DEPS)
356
cat $(DEPS) > $(DEP_FILE)
358
upload: reset raw_upload
360
raw_upload: $(TARGET_HEX)
361
$(AVRDUDE) $(AVRDUDE_COM_OPTS) $(AVRDUDE_ARD_OPTS) \
362
-U flash:w:$(TARGET_HEX):i
364
# BSD stty likes -F, but GNU stty likes -f/--file. Redirecting
365
# stdin/out appears to work but generates a spurious error on MacOS at
366
# least. Perhaps it would be better to just do it in perl ?
368
for STTYF in 'stty --file' 'stty -f' 'stty <' ; \
369
do $$STTYF /dev/tty >/dev/null 2>&1 && break ; \
371
$$STTYF $(ARD_PORT) hupcl ; \
372
(sleep 0.1 2>/dev/null || sleep 1) ; \
373
$$STTYF $(ARD_PORT) -hupcl
375
ispload: $(TARGET_HEX)
376
$(AVRDUDE) $(AVRDUDE_COM_OPTS) $(AVRDUDE_ISP_OPTS) -e \
377
-U lock:w:$(ISP_LOCK_FUSE_PRE):m \
378
-U hfuse:w:$(ISP_HIGH_FUSE):m \
379
-U lfuse:w:$(ISP_LOW_FUSE):m \
380
-U efuse:w:$(ISP_EXT_FUSE):m
381
$(AVRDUDE) $(AVRDUDE_COM_OPTS) $(AVRDUDE_ISP_OPTS) -D \
382
-U flash:w:$(TARGET_HEX):i
383
$(AVRDUDE) $(AVRDUDE_COM_OPTS) $(AVRDUDE_ISP_OPTS) \
384
-U lock:w:$(ISP_LOCK_FUSE_POST):m
387
$(REMOVE) $(OBJS) $(TARGETS) $(DEP_FILE) $(DEPS)
390
cat $(DEPS) > $(DEP_FILE)
392
.PHONY: all clean depends upload raw_upload reset