#!/bin/sh
# ardmake: A command-line Arduino make/build environment. 2009-12-08
# For instructions, run with the argument "help"!
#
# This script is Copyright (c) 2009 Kimmo Kulovesi .
# Use at your own risk only. Released under GPL, see below for details.
# Please mark any modified copies as such, and retain the original
# copyright notice in all relevant files, usage, and documentation.
#
#
# INTRODUCTION
#
# This script runs the Makefile included with Arduino to compile and
# upload projects on the command-line. This script sets all
# board-specific variables automatically according to the board type,
# as well as detects and adds libraries automatically based on the
# #include-directives in the program (i.e. with the same convenience
# as the graphical Arduino environment). This script also supports
# burning bootloaders, setting fuses, uploading pre-compiled binaries,
# and using certain external programming devices (e.g. for stand-alone
# microcontrollers).
#
# In short, this script can completely replace the Arduino IDE for
# typical Arduino/ATMega development, and in some cases it can even
# do more than the IDE. The intended user is a relatively experienced
# command-line user, who wants to combine the power of their chosen
# editor and tools with the development speed and convenience of
# the Arduino platform. Beginners will probably be more comfortable
# starting with the simple graphical Arduino IDE.
#
# This script is not an official part of Arduino, but since it is
# in active use by its author, it will probably be rapidly updated
# to support any new Arduino releases (and some care has been
# taken to minimise the changes necessary to support each release).
#
#
# INSTALLATION
#
# Install Arduino somewhere, e.g. /opt/arduino or ~/arduino. If you
# are using additional core types (e.g. Sanguino) or custom board
# types (e.g. ADABoot), also install them. Custom libraries can be
# installed in e.g. ~/sketchbook/libraries, or anywhere you like.
#
# Install the AVR version of GCC (e.g. package name avr-gcc) and
# AVRDUDE (you can probably use the one bundled with Arduino if you
# prefer). Preferably install versions packaged for your specific
# Linux distribution (e.g. with apt-get install avr-gcc avrdude).
#
# Put this script somewhere along your PATH (e.g. /usr/local/bin,
# ~/bin, or wherever you like to install programs). This script is
# distributed by the author as "arduino_make.sh" due to historical
# reasons, but "ardmake" is the suggested name (shorter to type and
# all). Run this script with the parameter "help" and read about
# configuring and usage (usually only the board type needs to be set
# and you are good to go).
#
#
# SYSTEM REQUIREMENTS
#
# Last been tested with Arduino version 0017 on Ubuntu 9.04, with
# avr-gcc and avrdude installed from Ubuntu packages. While this script
# should run in non-Linux environments, there are some dependencies to
# GNU tools (e.g. GNU Make), so GNU/Linux should be considered the
# preferred/intended environment for running this.
#
#
# CHANGES
#
# December 2009 - Added a "serial monitor" (target "serial") with
# limited support for serial speed autodetection
# from the Arduino program.
# - Added support for using the "arduino" protocol
# in avrdude when available; this eliminates the
# need to reset the device with "stty". The old
# behaviour can be restored by using the target
# "upload_autoreset" instead of "upload".
# - Added support for downloading with external
# programmers.
# - Added basic support for reading configurations
# for external programming devices from the
# file hardware/programmers.txt, in addition to
# the built-in isp and dragon targets. This should
# enable the use of parallel programmers.
# - Added target "programmers" for listing supported
# external programming devices.
# - Don't do autoreset when uploading with external
# programmers.
# - Removed unnecessary debug outputs.
# - Additional documentation in the script file.
# November 2009 - Major bugfix for boards with CPU frequency other
# than 16MHz.
# - Possibly fixed the __cxa_pure_virtual issue.
# - Forcing user to define ARDUINO_BOARD explicitly
# since using an incorrect board type can cause
# nasty hidden errors.
# - Added reset commands to upload and download
# when not using the Makefile.
# - Added dependency on the board type, i.e. if the
# board type is changed, everything gets rebuilt.
# - Added target "boards" to list available boards.
# - Changed default library path to include the
# "~/sketchbook/libraries" directory, similarly to
# the current Arduino IDE.
# - Made building locally the default and fixed
# the problem of dependency files being built in
# the core directory.
# - Implemented reading configuration from
# ~/.ardmake.conf and ardmake.conf in the
# sketch directory.
# - Fixed build dependencies with Sanguino.
# - Rewrote most of the help texts.
# - Fixed compatibility with mawk. Thanks to Tom
# Parkin for reporting this!
# October 2009 - Support AVRISP and burning bootloaders.
# - Support building object files into the
# applet directory instead of the core and
# library directories.
# - Generate automatic dependecies for libraries
# - Support uploading specified .hex or .bin
# directly without compiling anything
# - Support downloading flash memory from
# microcontroller to .hex or .bin file
# - Replace the slightly broken build target:
# - Proper dependencies
# - Show correct file name and line numbers for errors
# - Display program size compared to controller capacity
# September 2009 - Support Arduino 017
# March 2009 - Support Arduino 014
# February 2009 - Initial version
#
#
# FANCY ARDUINO DEVICE NODES ON LINUX
#
# The default port for the Arduino is set to "/dev/arduino", which
# requires udev rules (but avoids the problem of changing ttyUSB names).
# Alternatively, it can be changed in this file. The udev rule that
# works for the Arduino clone that I have is this:
#
# KERNEL=="ttyUSB*", ATTRS{product}=="FT232R USB UART", \
# ATTRS{idProduct}=="6001", ATTRS{idVendor}=="0403", \
# SYMLINK+="arduino arduino_$attr{serial}", GROUP="avrprog", MODE="0660"
#
# You will probably want to change the group to "dialout", or create
# the "avrprog" group on your system (like I did). On Ubuntu Linux, place
# the rule in a file inside "/etc/udev/rules.d", e.g. "80-arduino.rules".
#
# If you have many devices with the same product and vendor ids,
# as may be the case with a popular chip like FT232R, you can
# add the condition "ATTRS{serial}" to your udev rules. You can
# see the serial if you first use the above rules and then look at
# the symlink "arduino_SERIAL" where SERIAL is the serial number
# of that particular device. Then create one rule for each of your
# devices' serial numbers (add ATTRS{serial}=="MySerial", right
# before SYMLINK in the above rules).
#
#
# COMPILER ERROR ABOUT __cxa_pure_virtual
#
# Some versions of Arduino and avr-gcc cause an error about a missing
# function "__cxa_pure_virtual" in programs where C++ classes are used.
# To fix this problem, add the following line anywhere in your program:
#
# extern "C" void __cxa_pure_virtual() {}
#
###################################################################################
# This script is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License,
# or (at your option) any later version.
#
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this script. If not, see .
###################################################################################
# Read the configuration file (if any):
CONFNAME="ardmake.conf"
for conf in "./$CONFNAME" "$HOME/.$CONFNAME"; do
if [ -r "$conf" ]; then
eval "$(awk -v FS== '{ sub(/^[ \t]*/, ""); sub(/^(set|export)[ ]*/, "");
if (/^A(RDUINO_|AVR)[A-Za-z0-9_]*=[^;<>`]*$/) {
print "[ -z \"$" $1 "\" ] && " $0
}
next }' "$conf")"
#echo "Loaded configuration file \"$conf\"."
fi
done
###################################################################################
# Defaults configuration:
# Path to avr tools (/usr/bin if installed from Linux distribution packages)
[ -z "$AVR_TOOLS_PATH" ] && AVR_TOOLS_PATH=/usr/bin
# Path to avrdude (/usr/bin if installed from Linux distribution packages)
[ -z "$AVRDUDE_PATH" ] && AVRDUDE_PATH=/usr/bin
# Path to search for additional Arduino libraries (separated by : colons).
# The "official" script directory at hardware/libraries is always searched!
if [ -z "$ARDUINO_LIBRARY_PATH" ]; then
ARDUINO_LIBRARY_PATH="../libraries:$HOME/sketchbook/libraries"
fi
# Try to figure out Arduino install directory (first from environment
# variable ARDUINO_DIR, then ~/arduino, then opt/arduino)
if [ -n "$ARDUINO_DIR" ]; then
INSTALL_DIR="$ARDUINO_DIR"
else
INSTALL_DIR="$HOME/arduino"
if [ ! -x "$INSTALL_DIR/arduino" -a -x '/opt/arduino/arduino' ]; then
INSTALL_DIR='/opt/arduino'
fi
fi
# Build locally unless a shared build is specifically requested:
if [ ! "$ARDUINO_BUILD" = "shared" ]; then
BUILD_LOCALLY=1
else
BUILD_LOCALLY=''
fi
# The extension for Arduino program files (.pde at the time of writing, but
# this is the same as for Processing - .ade would be more fitting)
EXT='pde'
# Command to reset serial port:
RESET_COMMAND="stty hupcl; sleep 0.1; true"
###################################################################################
basename="$(basename "$0")"
# Check the configuration:
PROGRAMMERFILE="$INSTALL_DIR/hardware/programmers.txt"
BOARDFILE="$INSTALL_DIR/hardware/boards.txt"
if [ ! -r "$BOARDFILE" ]; then
cat >&2 <>~/.$CONFNAME
Run "$basename help" for instructions.
EOF
exit 1
fi
[ ! -x "$AVRDUDE_PATH/avrdude" ] && AVRDUDE_PATH="$INSTALL_DIR/hardware/tools"
[ ! -x "$AVRDUDE_PATH/avrdude" ] && AVRDUDE_PATH="$(dirname $(which avrdude))"
if [ ! -x "$AVRDUDE_PATH/avrdude" ]; then
cat >&2 <&2 <. This script
is provided as free software under GPL with ABSOLUTELY NO WARRANTY.
Usage: $basename [target] [options for Make]
This script provides a command-line build environment for Arduino,
by wrapping the call to Make and setting parameters for it over those
defined in the Arduino Makefile. Supported features include library
autodetection from #include-directives (just like the graphical IDE),
custom library paths, all board types (including custom ones), external
programming devices, downloading programs from the microcontroller, etc.
Setup and installation:
1) Install Arduino, e.g.:
wget http://arduino.googlecode.com/files/arduino-0017.tgz
tar xvzf arduino-0017.tgz; ln -s arduino-0017 arduino
2) Install avrdude and GCC for AVR, e.g. on Ubuntu & Debian:
apt-get install avrdude gcc-avr
3) Configure your Arduino installation directory, e.g.
export ARDUINO_DIR=/path/to/arduino
3) Configure your Arduino board, e.g.:
export ARDUINO_BOARD=diecimila
4) Configure your Arduino serial port device, e.g.:
export ARDUINO_PORT=/dev/ttyUSB0
The variable ARDUINO_BOARD must be set to the short name of the
board you are using. To list available board types, use the
command "$basename boards".
By default, this script attempts to find an Arduino installation
in ~/arduino and /opt/arduino. If it's neither of these, specify
the variable ARDUINO_DIR accordingly.
The serial device defaults to /dev/ (e.g. /dev/arduino),
and to /dev/ttyUSB0 if that is not available. A specific port may
be configured by setting the variable ARDUINO_PORT. Linux admins
may wish to specify udev rules so that the port device is constant
(e.g. /dev/arduino); for details on that, see the comments at the top
of this script file, i.e. "$0".
Configuring variables:
The configuration variables for this script (as detailed above)
can be set in the file ~/.$CONFNAME, e.g.:
echo ARDUINO_BOARD=atmega328 >~/.$CONFNAME
To override all or part of this global configuration, a
program-specific configuration file called $CONFNAME can be
created inside each sketch directory, if desired. Any settings
found in the sketch directory $CONFNAME take precedence over
the settings in the user's ~/.$CONFNAME.
You may also configure any or all of these variables in the
environment. Variables configured in the environment take
precedence over those in any configuration file! This allows
you to specify variables directly on the command-line, e.g.:
ARDUINO_DIR=~/arduino-0014 $basename
To create and upload an Arduino sketch:
1) Create a directory for your program ("sketch"), e.g.
mkdir -p ~/sketchbook/Blink
2) Create your program .$EXT inside the directory, e.g.:
cd ~/sketchbook/Blink; vim Blink.$EXT
3) Compile your program by running this script:
$basename
4) After a succesful compilation, upload to your board:
$basename upload
Libraries are automatically detected from the #include-directives
used. The libraries installed together with your Arduino are
always available. Custom libraries are searched for in the
directory ~/sketchbook/libraries and in ../libraries (i.e. in
the directory libraries in the same directory as your sketch
directory is in).
The custom library locations can be overridden by specifying
the variable ARDUINO_LIBRARY_PATH as a colon-separated list of
directories, e.g.:
ARDUINO_LIBRARY_PATH=$HOME/arduino_libs:/opt/arduino_libs
There are also other make targets that you may use instead of
compile (the default) and upload. The target is specified as the first
command-line argument, and it can be any target in the Makefile. Special
targets handled by $basename are:
compile (default): Compile the applet (.hex file) ready for uploading.
Do this first after making changes to your program!
upload: Upload the applet to the microcontroller. See above
about the configuration variable ARDUINO_PORT. Usage:
$basename upload
To upload a pre-compiled file to the microcontroller,
you can specify a filename after upload on the
command line. The file must have the extension
.hex for Intel hex format, or the extension .bin
for raw binary format. For example:
$basename upload myprog.hex
isp:
dragon: Just like "upload", but an AVRISP (or clone) or
an AVRDragon device is used to upload instead. These
can be used to upload to a stand-alone microcontroller
in ISP mode.
The AVRDragon is an USB device and the port is
autodetected. For AVRISP and clones, the default
ports are /dev/avrisp and /dev/ttyUSB0, but the
port can be overridden by setting AVRISP_PORT.
programmer: Like "isp" and "dragon", except the target must
be followed by the short name of a programmer
defined in hardware/programmers.txt. This allows
you to use any external programmer define there, e.g.:
$basename programmer parallel
download: Download the microcontroller's flash memory to
the file specified as the next command line
parameter. The file name MUST have either the
extension .hex for Intel hex format, or the
extension .bin for raw binary format. For example:
$basename download backup.bin
External programmers can be used by specifying
"download" after the programmer, e.g.:
$basename isp download backup.hex
serial: Start a serial monitor on the serial port. The port
device defaults to the programming port (ARDUINO_PORT),
but it can be specified on the command line. The
speed can often be autodetected from the program in
the current directory if it uses Serial.begin(speed),
but the speed defaults to 9600 and can be specified
on the command line. Examples:
$basename serial
$basename serial 19200
$basename serial /dev/ttyS0
$basename serial /dev/ttyUSB1 2400
boards: List available board types.
programmers: List available external programmer types.
coff: Build an applet .cof file for debugging/etc.
lss: Build an applet .lss file to show annotated assembler.
Targets for burning a bootloader (requires a programming device!):
bootloader: Program the fuses and burn a bootloader. The
filenames and settings are obtained from the
file ARDUINO_DIR/harware/boards.txt according
to the board type (ARDUINO_BOARD).
The bootloader can only be burned with an external
programmer. If the settings in boards.txt are not
applicable to your programmer device (as is
probably the case), you can specify the external
programmer at the end of the command line.
For example, to burn the ADABoot bootloader for
ATMega168 using an AVRISP device, you would set
ARDUINO_BOARD="ADABoot168" and then run:
$basename bootloader isp
You can also follow the bootloader target with
a .bin or .hex filename to burn a custom
bootloader without entering it into boards.txt, e.g.:
$basename bootloader boot.hex programmer parallel
fuses: Just program the fuses and set the lock bits
to unlock. For example:
$basename fuses isp
EOF
exit 0
fi
# Display list of available board types if requested:
if [ "$1" = "boards" -o "$1" = "programmers" ]; then
if [ "$1" = "boards" ]; then
file="$BOARDFILE"
else
file="$PROGRAMMERFILE"
fi
echo "Available $1 in $file:"
awk -v FS== '$1 ~ /\.name/ {
sub(/\.name$/, "", $1)
printf("\t%-15s\t\"%s\"\n", $1, $2);
}' "$file"
if [ "$1" = "boards" ]; then
cat <
or /dev/ttyUSB0 if that is not available).
EOF
fi
exit 0
fi
# Die if no board type is set:
if [ -z "$ARDUINO_BOARD" ]; then
cat <&2
ERROR: The variable ARDUINO_BOARD must be set to the type of Arduino
board you are using. Accepted values are those appearing in Arduino's
hardware/boards.txt, e.g. "diecimila", "mega", "lilypad", etc.
The names are case-sensitive.
To save a certain board type as your default, put the setting
in ~/.$CONFNAME, e.g.:
echo ARDUINO_BOARD=diecimila >>~/.$CONFNAME
To configure a project-specific board type, put the setting
in the file $CONFNAME in the sketch directory, e.g.:
cd ~/sketchbook/MyProg
echo ARDUINO_BOARD=mega >>$CONFNAME
Run "$basename help" for instructions.
EOF
exit 1
fi
# Try to read the hardware configuration for this board:
eval $(awk -v FS== -v board="$ARDUINO_BOARD" '$1 ~ /\.name$/ {
if (boardname) { exit 0 }
sub(/\.name$/, "", $1)
if (board == $1 || board == $2) {
boardname = $2
speed=0; core=""; mcu=""; protocol="";
f_cpu=0; lfuse=""; hfuse=""; efuse="";
unlock_bits=""; lock_bits="";
bootloader_dir=""; bootlader_file="";
}
next
}
!boardname { next }
$1 ~ /\.upload\.protocol$/ { protocol = $2; next }
$1 ~ /\.upload\.speed$/ { speed = $2; next }
$1 ~ /\.upload\.maximum_size$/ {
max_size = $2; next
}
$1 ~ /\.build\.core$/ { core = $2; next }
$1 ~ /\.build\.f_cpu$/ { f_cpu = $2; next }
$1 ~ /\.build\.mcu$/ { mcu = $2; next }
$1 ~ /\.bootloader\.low_fuses$/ {
lfuse = $2; next
}
$1 ~ /\.bootloader\.high_fuses$/ {
hfuse = $2; next
}
$1 ~ /\.bootloader\.extended_fuses$/ {
efuse = $2; next
}
$1 ~ /\.bootloader\.unlock_bits$/ {
unlock_bits = $2; next
}
$1 ~ /\.bootloader\.lock_bits$/ {
lock_bits = $2; next
}
$1 ~ /\.bootloader\.path$/ {
bootloader_dir = $2; next
}
$1 ~ /\.bootloader\.file$/ {
bootloader_file = $2; next
}
END {
if (boardname) {
print "BOARDNAME=\"" boardname "\""
if (speed) {
gsub(/[^0-9]/, "", speed)
print "UPLOAD_RATE=\"" speed "\""
}
if (f_cpu) {
gsub(/[^0-9]/, "", f_cpu)
print "F_CPU=\"" f_cpu "\""
}
if (core) {
gsub(/[^a-zA-Z0-9_.:-]/, "", core)
print "CORE=\"" core "\""
}
if (mcu) {
gsub(/[^a-zA-Z0-9_.:-]/, "", mcu)
print "MCU=\"" mcu "\""
}
if (protocol) {
gsub(/[^a-zA-Z0-9_.:-]/, "", protocol)
print "AVRDUDE_PROGRAMMER=\"" \
protocol "\""
}
if (max_size) {
gsub(/[^0-9]/, "", max_size)
print "MAX_SIZE=\"" max_size "\""
}
if (hfuse != "") {
gsub(/[^0-9xA-Fa-f]/, "", hfuse)
print "BL_HFUSE=\"" hfuse "\""
}
if (lfuse != "") {
gsub(/[^0-9xA-Fa-f]/, "", lfuse)
print "BL_LFUSE=\"" lfuse "\""
}
if (efuse != "") {
gsub(/[^0-9xA-Fa-f]/, "", efuse)
print "BL_EFUSE=\"" efuse "\""
}
if (lock_bits != "") {
gsub(/[^0-9xA-Fa-f]/, "", lock_bits)
print "BL_LOCK=\"" lock_bits "\""
}
if (unlock_bits != "") {
gsub(/[^0-9xA-Fa-f]/, "", unlock_bits)
print "BL_UNLOCK=\"" unlock_bits "\""
}
if (bootloader_dir && bootloader_file) {
gsub(/["]/, "\\\"", bootloader_dir)
gsub(/["]/, "\\\"", bootloader_file)
print "BL_PATH=\"" bootloader_dir "/" \
bootloader_file "\""
}
}
}' "$BOARDFILE")
# Die if the board configuration was not found:
if [ -z "$F_CPU" ]; then
cat <&2
ERROR: The board "$ARDUINO_BOARD" was not found in the configuration
file "$BOARDFILE". The variable ARDUINO_BOARD
must be set to the (case-sensitive) short name of the board,
e.g. "diecimila" or "atmega328".
Run "$basename boards" to list known board types, or
"$basename help" for general instructions.
EOF
exit 1
fi
# Some defaults for board types, e.g. if the user has placed a custom
# board in boards.txt and didn't define everything:
[ -z "$CORE" ] && CORE=arduino
[ -z "$MAX_SIZE" ] && MAX_SIZE=14336
[ -z "$MCU" ] && MCU="$ARDUINO_BOARD"
[ -z "$AVRDUDE_PROGRAMMER" ] && AVRDUDE_PROGRAMMER=stk500v1
[ -z "$UPLOAD_RATE" ] && UPLOAD_RATE=19200
UPLOAD_DELAY=""
# Set some helper variables based on the Arduino location:
MAKEFILE="$INSTALL_DIR/hardware/cores/$CORE/Makefile"
[ ! -e "$MAKEFILE" ] && MAKEFILE="$INSTALL_DIR/hardware/cores/arduino/Makefile"
ARDUINO="$INSTALL_DIR/hardware/cores/$CORE"
LIBRARY_DIR="$INSTALL_DIR/hardware/libraries"
# Check for the wiring_serial.c bug in some versions of Arduino:
if grep -q -s -F 'wiring_serial.c' "$MAKEFILE"; then
if [ ! -e "$ARDUINO/wiring_serial.c" ]; then
echo '/* Empty file created due to bug in Arduino Makefile */' \
>"$ARDUINO/wiring_serial.c"
if [ ! -e "$ARDUINO/wiring_serial.c" ]; then
cat <&2
WARNING: The file "$ARDUINO/wiring_serial.c" is referred to in
the Makefile, but it does not exist. This is a bug in some Arduino
versions, and will probably lead to failed builds. To remedy, please
create the file (it can be empty) or remove the reference from the
Makefile ("$MAKEFILE").
EOF
fi
fi
fi
# Correct the programmer "stk500" specified for pretty much every
# Arduino board to "stk500v1" (which is the correct, more specific
# option for avrdude):
[ "$AVRDUDE_PROGRAMMER" = "stk500" ] && AVRDUDE_PROGRAMMER='stk500v1'
# Configure the programmer port location:
if [ -n "$ARDUINO_PORT" ]; then
PORT="$ARDUINO_PORT"
else
PORT="/dev/$CORE"
if [ ! -e "$PORT" ]; then
PORT="/dev/$ARDUINO_BOARD"
if [ ! -e "$PORT" ]; then
PORT='/dev/avr'
[ ! -e "$PORT" ] && PORT='/dev/ttyUSB0'
fi
fi
fi
# Serial monitor:
if [ "$1" = "serial" ]; then
shift
if [ -c "$1" ]; then
PORT="$1"
shift
fi
if [ ! -e "$PORT" ]; then
cat >&2 < 0) { baud = $0; exit }
}
END { print baud }' "$TARGET")
else
SPEED="${ARDUINO_BAUD:-9600}"
fi
if tty >/dev/null 2>&1; then
cat >&2 <"$PORT"
exit $?
else
echo "Error: Serial device \"$PORT\" does not exist!" >&2
exit 1
fi
fi
# Display verification that the correct board was selected:
cat <&2 <&2
exit 1
fi
cat </dev/null 2>&1
fi
# Change the target "dragon" to "upload", but perform the upload using
# the AVRDragon in ISP mode instead of the instead of the typical Arduino
# programming method (e.g. for DIY projects using the same microprocessor
# as an Arduino but not having the programming capability themselves).
#
# Similarly change the target "isp" to "upload", but perform the upload
# using an AVRISP (or clone thereof).
if [ -n "$1" ]; then
if [ "$1" = "dragon" ]; then
# Uploading with the AVR Dragon:
AVRDUDE_PROGRAMMER='dragon_isp'
PORT='usb'
UPLOAD_RATE=''
target='upload'
if [ "$2" = "download" ]; then target="$2"; shift; fi
elif [ "$1" = "isp" ]; then
# Uploading via AVRISP with the stk500v2 protocol:
if [ -n "$AVRISP_PORT" ]; then
PORT="$AVRISP_PORT"
elif [ -e '/dev/avrisp' ]; then
PORT='/dev/avrisp'
else
PORT='/dev/ttyUSB0'
fi
AVRDUDE_PROGRAMMER='stk500v2'
UPLOAD_RATE="$AVRISP_BAUD"
target='upload'
if [ "$2" = "download" ]; then target="$2"; shift; fi
elif [ "$1" = "programmer" ]; then
shift
AVRDUDE_PROGRAMMER="$1"
if [ -z "$AVRDUDE_PROGRAMMER" ]; then
echo "ERROR: No programming device specified on the command-line!" >&2
exit 1
fi
if [ ! -r "$PROGRAMMERFILE" ]; then
echo "ERROR: Could not read \"$PROGRAMMERFILE\"!" >&2
exit 1
fi
# Read custom configuration for an external programming device:
eval $(awk -v FS== -v prog="$AVRDUDE_PROGRAMMER" '$1 ~ /\.name$/ {
if (progname) { exit 0 }
sub(/\.name$/, "", $1)
if (prog == $1 || prog == $2) {
progname = $2; communication="";
protocol=""; delay=""; port="";
}
next
}
!progname { next }
$1 ~ /\.communication$/ { communication = $2; next }
$1 ~ /\.protocol$/ { protocol = $2; next }
$1 ~ /\.delay$/ { delay = $2; next }
END {
if (progname) {
print "AVRDUDE_PROGRAMMER_NAME=\"" progname "\""
gsub(/[^a-zA-Z0-9_.:-]/, "", communication)
print "AVR_COMMUNICATION=\"" communication "\""
gsub(/[^0-9]/, "", delay)
print "UPLOAD_DELAY=\"" delay "\""
if (protocol) {
gsub(/[^a-zA-Z0-9_.:-]/, "", protocol)
print "AVRDUDE_PROGRAMMER=\"" protocol "\""
}
}
}' "$PROGRAMMERFILE")
# Die if the specified programmer was not found in programmers.txt:
if [ -z "$AVRDUDE_PROGRAMMER_NAME" ]; then
cat >&2 <&1 | grep -q -s '^ *arduino *= .*conf'; then
# Use the "arduino" programmer with autoreset built in,
# if it's available in the avrdude version we are using.
AVRDUDE_PROGRAMMER='arduino'
target="$1"
else
target="$1_autoreset"
fi
else
target="$1"
fi
shift
else
target='compile'
fi
AVRDUDE_FLAGS="$AVRDUDE_FLAGS${PORT:+ -P $PORT} -c $AVRDUDE_PROGRAMMER${UPLOAD_RATE:+ -b $UPLOAD_RATE}${UPLOAD_DELAY:+ -i $UPLOAD_DELAY}"
# Show the configuration:
cat </dev/null
fi
exec "$AVRDUDE_PATH/avrdude" ${AVRDUDE_CONFIG:+-C "$AVRDUDE_CONFIG"} \
$AVRDUDE_FLAGS -U "flash:w:$1:a"
fi
# Download flash to file (.hex or .bin, Intel Hex or raw binary format):
if [ '(' "$target" = "download" -o "$target" = "download_autoreset" ')' \
-a -n "$1" ] && \
echo "$1" | grep -E -q -s '\.(hex|bin)$' ; then
echo "Downloading flash memory to file '$1'..."
if [ "$target" = "download_autoreset" ]; then
( eval "$RESET_COMMAND" ) <"$PORT" 2>/dev/null
fi
exec "$AVRDUDE_PATH/avrdude" ${AVRDUDE_CONFIG:+-C "$AVRDUDE_CONFIG"} \
$AVRDUDE_FLAGS \
-U "flash:r:$1:$(echo "$1" | sed 's/^.*hex$/i/; s/^.*bin$/r/')"
fi
# Escape AVRDUDE_CONFIG path for the Makefile:
[ -n "$AVRDUDE_CONFIG" ] && AVRDUDE_FLAGS="-C \"$AVRDUDE_CONFIG\" $AVRDUDE_FLAGS"
# Try to discover the program name:
TARGET=$(basename "$(pwd)")
for f in *.$EXT; do
TARGET=$(echo "$f" | sed "s/\.$EXT$//")
break
done
if [ ! -e "./$TARGET.$EXT" ]; then
cat >&2 <"]' '/^[ ]*#include [<"]/ { sub(/\.h[p]*$/, "", $2);
gsub(/[^a-zA-Z0-9_.:/-]/, "", $2);
print $2; next }' "$1" 2>/dev/null)
do
local found=''
local libpath=''
local libname="$lib"
local header="$ARDUINO/$libname.h"
local base=''
for libpath in $ARDUINO_LIBRARY_PATH "$LIBRARY_DIR"; do
local libdir="$libpath/$libname"
if [ -e "$libdir" ]; then
if check_for_libraries "$libdir/$libname.h" "$libname"; then
if [ "$libpath" = "$LIBRARY_DIR" ]; then
echo "Including Arduino library: $libname"
CINCS="$CINCS -I\$(LIBRARIES_DIR)/$libname"
[ -e "$libdir/utility" ] && \
CINCS="$CINCS -I\$(LIBRARIES_DIR)/$libname/utility"
else
echo "Including local library: $libname"
CINCS="$CINCS -I$libdir"
[ -e "$libdir/utility" ] && \
CINCS="$CINCS -I$libdir/utility"
fi
fi
check_header "$libname" "$libdir" "$libname"
found=1
break
fi
done
if [ -z "$found" ]; then
for base in "$ARDUINO" "$basedir" "$basedir/utility"; do
check_header "$libname" "$base" "$inlib" && break
done
fi
done
return 0
}
check_for_libraries "$TARGET.$EXT"
# Ensure the applet directory exists:
[ ! -d applet ] && mkdir applet
if [ -e 'applet/board' -a ! "$BOARDFILE" -nt "applet/board" ]; then
configured_board="$(head -n 1 'applet/board')"
else
configured_board=''
fi
[ ! "$configured_board" = "$ARDUINO_BOARD" ] && echo "$ARDUINO_BOARD" >'applet/board'
# Display library settings to the user:
old_CINCS="$CINCS"
CINCS="-I. -I./utility -I\$(ARDUINO)$CINCS"
if [ -n "$old_CINCS" ]; then
echo
echo "Includes = $CINCS"
#[ -n "$LIBSRC" ] && echo "LIBSRC =$LIBSRC"
#[ -n "$LIBASRC" ] && echo "LIBASRC =$LIBASRC"
#[ -n "$LIBCXXSRC" ] && echo "LIBCXXSRC =$LIBCXXSRC"
echo
fi
unset old_CINCS
# Set the compiler options to better match the IDE:
CTUNING='-ffunction-sections -fdata-sections -fshort-enums'
CFLAGS='$(CDEFS) $(CINCS) -O$(OPT) $(CWARN) $(CTUNING) $(CEXTRA) $(CDEBUG)'
CXXFLAGS='$(CDEFS) $(CINCS) -O$(OPT) -fno-exceptions $(CTUNING)'
# Create the Makefile:
if [ ! -e 'applet/Makefile' -o 'applet/board' -nt 'applet/Makefile' \
-o "$0" -nt 'applet/Makefile' ]; then
# Change the Make default target to our own:
echo 'compile: do_compile' >applet/Makefile
# Take the original Makefile, but remove the built-in dependency
# includes (so we can override them) and the original .elf target
# which we are replacing below:
sed '/^include $[(][^)]*\.d[)]/ d;
/^applet\/$[(]TARGET[)]\.elf: / d;
/^[ \t]*#/ d;
s/\.pde/\.$(EXT)/g' "$MAKEFILE" >>applet/Makefile
# Now the dirty parts, featuring some rather explicit Make:
echo -e 'do_compile: do_build show_size
do_build: applet/$(TARGET).hex
applet/$(TARGET).hex: applet/$(TARGET).elf
ARDMAKE_BOARD=applet/board
applet/$(TARGET).elf: applet/$(TARGET).cpp applet/core.a
\t$(CXX) $(ALL_CXXFLAGS) -Wl,--gc-sections $(LDFLAGS) -L. -Lapplet/ -o $@ $< applet/core.a
\t@chmod a-x $@ >/dev/null 2>&1 || true
applet/$(TARGET).cpp: $(TARGET).$(EXT) $(ARDUINO)/main.cxx $(ARDUINO)/WProgram.h $(ARDMAKE_BOARD)
\techo '\''#include "WProgram.h"'\'' >$@
\t@echo '\''#line 1 "$<"'\'' >>$@
\tcat $(TARGET).$(EXT) >>$@
\t@echo '\''#line 1 "$(ARDUINO)/main.cxx"'\'' >>$@
\tcat $(ARDUINO)/main.cxx >>$@
show_size:
\t@echo
\t@echo Program size:
\t@$(HEXSIZE) | awk -v m="$(MAX_SIZE)" '\''{print;if(NR^1){s=$$4}} \\
END {printf("\\n%d/%d bytes (%.1f%% of capacity, %d bytes left)\\n\\n",\\
s,m,s*100.0/m,m-s);}'\''
upload_autoreset: do_autoreset upload unreset
do_autoreset:
\t@echo Sending reset to prepare for upload...
\t( '"$RESET_COMMAND"' ) <$(PORT) 2>/dev/null
\t@echo
unreset:
\t@stty -hupcl <$(PORT) 2>/dev/null || true
$(OBJ): $(ARDMAKE_BOARD)
$(DEPS): $(ARDMAKE_BOARD)
$(APPC): applet/%.o: %.c
\t$(CC) -c $(ALL_CFLAGS) -o $@ $<
$(APPCXX): applet/%.o: %.cpp
\t$(CXX) -c $(ALL_CXXFLAGS) -o $@ $<
$(APPA): applet/%.o: %.S
\t$(CC) -c $(ALL_ASFLAGS) -o $@ $<
$(APPC:.o=.d): applet/%.d: %.c
\t$(CC) -M $(ALL_CFLAGS) $< | sed '\''s;^[^:]*:;applet/$*.o applet/$*.d:;'\'' >$@
$(APPCXX:.o=.d): applet/%.d: %.cpp
\t$(CXX) -M $(ALL_CXXFLAGS) $< | sed '\''s;^[^:]*:;applet/$*.o applet/$*.d:;'\'' >$@
$(APPA:.o=.d): applet/%.d: %.S
\t$(CC) -M $(ALL_ASFLAGS) $< | sed '\''s;^[^:]*:;applet/$*.o applet/$*.d:;'\'' >$@
applet/$(TARGET).d: applet/$(TARGET).cpp
vpath %.c applet/ $(sort $(dir $(OBJC)))
vpath %.cpp applet/ $(sort $(dir $(OBJCXX)))
vpath %.S applet/ $(sort $(dir $(OBJA)))
include $(DEPS)' >>applet/Makefile
# Ensure applet/core.a gets re-built every time, because otherwise
# we won't get the correct dependencies:
if [ -z "$target" -o "$target" = "compile" -o "$target" = "all" ]; then
if [ -w "applet/core.a" ]; then
echo "rm -f applet/core.a"
rm -f "applet/core.a"
fi
fi
fi
# Don't do autoreset if we don't have a serial port:
[ "$target" = "upload_autoreset" -a ! -c "$PORT" ] && target=upload
# Substitute the Makefile "clean" target:
if [ "$target" = "clean" ]; then
echo "Cleaning up..."
for ext in d o cpp h elf hex a s S lss cof; do
rm -f applet/*.$ext 2>/dev/null
done
rm -f applet/Makefile applet/board 2>/dev/null
# If we are building locally, do not try to clean inside Arduino dir:
[ -n "$BUILD_LOCALLY" ] && exit 0
fi
# Finally, execute Make:
exec make -f applet/Makefile \
MAKEFILE='applet/Makefile' LIBRARIES_DIR="$LIBRARIES_DIR" \
AVRDUDE_FLAGS="$AVRDUDE_FLAGS" AVRDUDE_PROGRAMMER="$AVRDUDE_PROGRAMMER" \
TARGET="$TARGET" PORT="$PORT" MCU="$MCU" F_CPU="$F_CPU" MAX_SIZE="$MAX_SIZE" \
AVR_TOOLS_PATH="$AVR_TOOLS_PATH" INSTALL_DIR="$INSTALL_DIR" EXT="$EXT" \
AVRDUDE_PATH="$AVRDUDE_PATH" UPLOAD_RATE="$UPLOAD_RATE" ARDUINO="$ARDUINO" \
LIBSRC="$LIBSRC" LIBASRC="$LIBASRC" LIBCXXSRC="$LIBCXXSRC" \
CINCS="$CINCS" CXXINCS="$CXXINCS" AVRDUDE='$(AVRDUDE_PATH)/avrdude' \
CTUNING="$CTUNING" CFLAGS="$CFLAGS" CXXFLAGS="$CXXFLAGS" \
OBJC='$(sort $(SRC:.c=.o) $(abspath $(LIBSRC:.c=.o)))' \
OBJCXX='$(sort $(CXXSRC:.cpp=.o) $(abspath $(LIBCXXSRC:.cpp=.o)))' \
OBJA='$(sort $(ASRC:.S=.o) $(abspath $(LIBASRC:.S=.o)))' \
OBJARDUINODIR='$(OBJC) $(OBJCXX) $(OBJA)' \
APPC='$(addprefix applet/,$(notdir $(OBJC)))' \
APPCXX='$(addprefix applet/,$(notdir $(OBJCXX)))' \
APPA='$(addprefix applet/,$(notdir $(OBJA)))' \
OBJAPPDIR='$(APPC) $(APPCXX) $(APPA)' \
OBJ='$(if $(BUILD_LOCALLY),$(OBJAPPDIR),$(OBJARDUINODIR))' \
DEPS='$(OBJ:.o=.d) applet/$(TARGET).d' LST='$(OBJ:.o=.lst)' \
$target ${BUILD_LOCALLY:+BUILD_LOCALLY=1} "$@"