2013-05-19 : I've let this languish for too long, mainly because I don't do much with Arduinos now. Happily Sudar Muthu has taken over maintainance of the code. His github repository is https://github.com/sudar/Arduino-Makefile/ though by the magic of github, the old URL continues to work.
Thanks to Sudar and everyone else who has helped for keeping this alive.
2012-09-17 : After letting this languish for many months there’s now version 0.10. This:
- Supports the Leonardo board.
- Moves the board reset code to Perl (this means you’ll need the Device::SerialPort module).
- Has new path handling code.
There are other small changes, for more details see the commit history.
The new path calculations mean that:
- Few, if any, paths need to specified in project specific Makefiles.
- Paths can be grabbed from the environment e.g. from .bashrc.
- It should be easier to move projects between e.g. Mac & Linux.
However, you’ll need to set up some new variables to make this work:
- Things which are included in this distribution e.g. ard-parse-boards
- Things which are always in the Arduino distribution e.g. boards.txt, libraries, &c.
- Things which might be bundled with the Arduino distribution, but might come from the system. Most of the toolchain is like this: on Linux it’s supplied by the system.
Thanks to Dan Villiom Podlaski Christiansen, Tom Hall, Scott Howard, Kalin Kozhuharov, Rickard Lindberg, Christopher Peplin, Marc Plano-Lesay, Jared Szechy, and Matthias Urlichs for patches and comments.
You can now grab the source for this from github. Besides my version there are several others under development which offer better integration with the IDE, support for Microchip’s ChipKIT boards, and other delights.
The Arduino has done much to popularize microcontrollers for the casual tinkerer. Its success suggests that there’s considerable value in combining a standard microcontroller (the ATmega) and a GCC based toolchain into an easily digesible package. For myself, it’s certainly easier to just install the latest release of the Arduino software than worry about building my own cross-compilers, particularly when it’s all new to me and consequently somewhat confusing.
After working through the toy tutorials though, I found myself wishing that writing code for the Arduino were more like writing other C programs. In my case, that means editing it with emacs then building it with make. I must emphasize that I’m not criticizing the Arduino IDE: there’s nothing wrong with it beyond it not being emacs...
It turns out that others have been along this path before: in the past the Arduino website had a hopeful sounding ‘Arduino from the Command Line’ article, but it’s gone now. There is still some information though it’s more limited.
Without an official Makefile, I wrote my own. You might wonder why I should embark on such a task. Well:
- I was keen that all of my objects and random other files were completely separate from the main Arduino stuff in the applet directory.
- Although I wanted to be able to build Arduino sketches, I also wanted a suitable jumping-off point for code which didn’t use wiring. In other words, to regard the Arduino software as a convenient way to get the AVR GCC toolchain.
- Rather than dumping a big Makefile in each sketch directory, I wanted to have a few definitions in the directory which then included a large project-independent file from elsewhere.
Finally, one of the things I enjoy about writing code for microcontrollers is the sense of continuity between the hardware datasheets published by the chip manufacturer and the code I write (by contrast if you’re writing code on Linux there’s a vast gulf between the code executing printf and stuff appearing on the screen). Writing my own Makefile seemed a good way to make sure I understood what was going on.
So to the Makefile. Obviously it owes a great debt to the people who wrote the Makefile shipped with the Arduino IDE and here’s the credit list from that file:
# Arduino 0011 Makefile # Arduino adaptation by mellis, eighthave, oli.keller
Thanks then to mellis, eighthavem and oli.keller.
If you’re using Debian or Ubuntu, then just grab the arduino-mk package.
You should then set up environment variables thus:
ARDUINO_DIR = /usr/share/arduino ARDMK_DIR = /usr AVR_TOOLS_DIR = /usr
Otherwise, you’ll need to download the tarball containing the Makefile, unpack it, and then copy the files somewhere sensible:
$ wget http://mjo.tc/atelier/2009/02/acli/arduino-mk_0.10.tar.gz $ tar xzvf arduino-mk_0.10.tar.gz $ cp arduino-mk-0.10/arduino-mk/Arduino.mk /usr/local/arduino/Arduino.mk $ cp arduino-mk-0.10/bin/* /usr/local/bin
The next step is to set up environment variables which point to the different files.
On the Mac you might want to set:
ARDUINO_DIR = /Applications/Arduino.app/Contents/Resources/Java ARDMK_DIR = /usr/local
On Linux, where the toolchain is installed in /usr, you might prefer:
ARDUINO_DIR = /usr/share/arduino ARDMK_DIR = /usr/local AVR_TOOLS_DIR = /usr
The final step is to create a small Makefile for the sketch you actually want to build. Let’s build the WebServer example from the Arduino distribution: it’s a good example because software-wise it’s as complicated as the stardard examples get, but you can just plug the hardware together.
Create a new directory and copy the WebServer.ino file into it.
Note: If you’re using version 1.0 of the Arduino software, you’ll need to make sure that the sketch’s name ends in .ino and not .pde.
Now this we’ll add a Makefile:
BOARD_TAG = uno ARDUINO_PORT = /dev/cu.usb* ARDUINO_LIBS = Ethernet Ethernet/utility SPI include /usr/local/arduino/Arduino.mk
Hopefully these will be self-explanatory but in case they’re not:
- A tag identifying which type of Arduino you’re using. This only works in version 0.6 and later.
- The port where the Arduino can be found (only needed when uploading) If this expands to several ports, the first will be used.
- A list of any libraries used by the sketch—we assume these are in $(ARDUINO_DIR)/hardware/libraries.
Until version 0.8 you had to specify a TARGET name which set the basename for the executables. You still can do this, but it’s not necessary: thanks to a patch from Daniele Vergini it now defaults to the name of the current directory.
In the past, the following options were used, and indeed you can still use them. However it’s probably better to use set BOARD_TAG and let the Makefile look up the values in boards.txt:
- The target processor (atmega168 for the Duemilanove).
- The target’s clock speed (16000000 for the Duemilanove).
- The protocol avrdude speaks—defaults to stk500v1.
- The rate at which we talk to the board—defaults to 19,200.
Makefiles before version 0.5 had to specify which processor and speed the target used. For standard boards, this information can be found in the boards.txt file, so it seemed sensible to use that instead.
Now, one need only define BOARD_TAG to match the target hardware and it should work. Internally the Makefile invokes ard-parse-boards—a small Perl utility included with the software—which parses board.txt.
If you’re not sure which board tag you need, ard-parse-board will dump a full list:
$ ard-parse-boards --boards Tag Board Name atmega168 Arduino NG or older w/ ATmega168 atmega328 Arduino Duemilanove or Nano w/ ATmega328 atmega8 Arduino NG or older w/ ATmega8 bt Arduino BT w/ ATmega168 bt328 Arduino BT w/ ATmega328 diecimila Arduino Diecimila, Duemilanove, or Nano w/ ATmega168 fio Arduino Fio lilypad LilyPad Arduino w/ ATmega168 lilypad328 LilyPad Arduino w/ ATmega328 mega Arduino Mega (ATmega1280) mega2560 Arduino Mega 2560 mini Arduino Mini pro Arduino Pro or Pro Mini (3.3V, 8 MHz) w/ ATmega168 pro328 Arduino Pro or Pro Mini (3.3V, 8 MHz) w/ ATmega328 pro5v Arduino Pro or Pro Mini (5V, 16 MHz) w/ ATmega168 pro5v328 Arduino Pro or Pro Mini (5V, 16 MHz) w/ ATmega328 uno Arduino Uno
If you don’t set it, BOARD_TAG defaults to uno.
You can, of course, continue to set F_CPU and MCU directly should you prefer that.
Early (up to and including version 0.4) of this Makefile didn’t really support this (despite claims to the contrary). Happily various kind people sorted out the problem, one of whom patched the Debian and Ubuntu version.
In the official IDE, it’s enough to select the library from a menu: this puts the relevant #include into the Sketch and adds the necessarily linker tweaks too.
In this Makefile, you’ll need to both add the #include yourself and append the directories which contain the library to the ARDUINO_LIBS variable. Often these will both have the same name, though it’s worth noting that the #include refers to a single file, but the ARDUINO_LIBS entry refers to an entire directory of source files.
However, care is needed if the library’s source files aren’t in a single directory. For example, the webserver example uses the Ethernet library and we needed to include both Ethernet and Ethernet/utility in ARDUINO_LIBS.
If you omit the .../utility library, you’ll get messy looking link errors from the bowels of the Ethernet library. The SPI and Wire libraries are like this too!
If you’re used to Unix then this is easy:
$ make ...
The output is pretty verbose, but I think it should be obvious if it worked. After building you’ll see a new directory has been created which contains all the object files: build-uno. Since version 0.10, if you rebuild the software with a different BOARD_TAG, you’ll get a different directory name.
$ $ ls -lR total 16 -rw-r--r-- 1 mjo staff 263 12 Feb 11:06 Makefile -rw-r--r-- 1 mjo staff 2308 12 Feb 10:57 WebServer.ino drwxr-xr-x 28 mjo staff 952 12 Feb 11:07 build-uno
Let’s peek inside the build-uno directory:
$ ls -l build-uno total 2136 -rw-r--r-- 1 mjo staff 2292 12 Feb 11:07 CDC.o -rw-r--r-- 1 mjo staff 2292 12 Feb 11:07 HID.o -rw-r--r-- 1 mjo staff 23452 12 Feb 11:07 HardwareSerial.o -rw-r--r-- 1 mjo staff 16008 12 Feb 11:07 IPAddress.o -rw-r--r-- 1 mjo staff 40012 12 Feb 11:07 Print.o -rw-r--r-- 1 mjo staff 21068 12 Feb 11:07 Stream.o -rw-r--r-- 1 mjo staff 16580 12 Feb 11:07 Tone.o -rw-r--r-- 1 mjo staff 2300 12 Feb 11:07 USBCore.o -rw-r--r-- 1 mjo staff 6048 12 Feb 11:06 WInterrupts.o -rw-r--r-- 1 mjo staff 7068 12 Feb 11:07 WMath.o -rw-r--r-- 1 mjo staff 79196 12 Feb 11:07 WString.o -rw-r--r-- 1 mjo staff 2329 12 Feb 10:57 WebServer.cpp -rw-r--r-- 1 mjo staff 1920 12 Feb 11:06 WebServer.d -rw-r--r-- 1 mjo staff 11324 12 Feb 11:06 WebServer.o -rwxr-xr-x 1 mjo staff 193852 12 Feb 11:07 WebServer.elf -rw-r--r-- 1 mjo staff 28572 12 Feb 11:07 WebServer.hex -rw-r--r-- 1 mjo staff 1920 12 Feb 11:08 depends.mk -rw-r--r-- 1 mjo staff 541002 12 Feb 11:07 libcore.a drwxr-xr-x 4 mjo staff 136 12 Feb 10:57 libs -rw-r--r-- 1 mjo staff 3616 12 Feb 11:07 main.o <-rw-r--r-- 1 mjo staff 5544 12 Feb 11:07 new.o -rw-r--r-- 1 mjo staff 9780 12 Feb 11:06 wiring.o -rw-r--r-- 1 mjo staff 7024 12 Feb 11:06 wiring_analog.o -rw-r--r-- 1 mjo staff 9704 12 Feb 11:06 wiring_digital.o -rw-r--r-- 1 mjo staff 7056 12 Feb 11:06 wiring_pulse.o -rw-r--r-- 1 mjo staff 5736 12 Feb 11:06 wiring_shift.o ./build-uno/libs: total 0 drwxr-xr-x 9 mjo staff 306 12 Feb 11:07 Ethernet drwxr-xr-x 3 mjo staff 102 12 Feb 11:07 SPI ./build-uno/libs/Ethernet: total 392 -rw-r--r-- 1 mjo staff 24836 12 Feb 11:07 Dhcp.o -rw-r--r-- 1 mjo staff 23112 12 Feb 11:07 Dns.o -rw-r--r-- 1 mjo staff 33008 12 Feb 11:07 Ethernet.o -rw-r--r-- 1 mjo staff 42000 12 Feb 11:07 EthernetClient.o -rw-r--r-- 1 mjo staff 19420 12 Feb 11:07 EthernetServer.o -rw-r--r-- 1 mjo staff 41244 12 Feb 11:07 EthernetUdp.o drwxr-xr-x 4 mjo staff 136 12 Feb 11:07 utility ./build-uno/libs/Ethernet/utility: total 152 -rw-r--r-- 1 mjo staff 40480 12 Feb 11:07 socket.o -rw-r--r-- 1 mjo staff 34840 12 Feb 11:07 w5100.o ./build-uno/libs/SPI: total 16 -rw-r--r-- 1 mjo staff 6812 12 Feb 11:07 SPI.o
Most of the files in here are object files for the wiring library. What about the others ?
- This is the .pde sketch file with a small main program prepended and a suitable #include prepended.
- This tracks the dependencies used by WebServer.pde
- This is executable produced by the linker
- This is a hex dump of (the code part) of the executable in a format understood by the Arduino’s bootloader.
- The object file we got by compiling WebServer.cpp.
- A single file containing all the dependency relations (it’s the concatentation of all the .d files).
- Rather than link all the system supplied objects directly, we build them into this library first, then link against it.
This is easy:
$ make upload
Uploading via ISP
If you’re using target hardware which doesn’t have a bootloader then you might want to use ISP to upload the code. Though you’ll obviously need some extra hardware to do this.
Assuming that avrdude supports your programmer though, you’ll only need to make a few changes to the Makefile to tell avrdude where it can find the programmer and how to talk to it:
ISP_PORT = /dev/ttyACM0 ISP_PROG = -c stk500v2
Then to upload:
$ make ispload
You might need to change the fuse settings when programming, though some care needs to be taken here or you might irreversibly damage the chip.
Normally the fuse settings are chosen from the boards.txt file to match the value of BOARD_TAG (assuming you’re running version 0.6 or higher), but you can set them yourself:
ISP_LOCK_FUSE_PRE = 0x3f ISP_LOCK_FUSE_POST = 0xcf ISP_HIGH_FUSE = 0xdf ISP_LOW_FUSE = 0xff ISP_EXT_FUSE = 0x01
Growing the project
There a couple of obvious things to do now. You might want to edit the sketch. That’s easy: just edit the .ino file and run make again.
Alternatively you might want to add some more source files to the project. That’s easy too: the Makefile understands C, C++ and assembler files in the source directory (with .c, .cpp, and .s extensions). Everything should just work.
Finally you might want to develop code which isn’t linked against the Wiring library. There’s some scope for this: just set NO_CORE in the Makefile e.g.
NO_CORE = 1
Bugs and problems
- The Makefile isn’t very elegant.
- When compiling the sketch file, the compiler actually sees the .cpp file derived from it. Accordingly the line numbers of any errors will be wrong (but not by that much).
- The Makefile doesn’t do some of the things that the Makefile distributed with the Arduino software does e.g. generating COFF files. I worry that some of these might be important.
- This hasn’t been used very much yet, even by me. I’m writing this now as much for my benefit as anyone else’s, though I’d be delighted to know if anyone else finds it useful.
2010-05-21, version 0.3
- Tidied up the licensing, making it clear that it’s released under LGPL 2.1.
- Philip Hands sent me some code to reset the Arduino by dropping DTR for 100ms, and I added it.
- Tweaked the Makefile to handle version 0018 of the Arduino software which now includes main.cpp. Accordingly we don’t need to—and indeed must not—add main.cxx to the .pde sketch file. The paths seem to have changed a bit too.
2010-05-24, version 0.4
- Tweaked rules for the reset target on Philip Hands’ advice.
2011-06-23, version 0.5
- Imported changes from Debian/Ubuntu, which incorporate a patch from Stefan Tomanek so that libraries would be compiled too.
Note: Many other people sent me similar patches, but I didn’t get around to using them. In the end, I took the patch from Debian and Ubuntu: there seems merit in not forking the code and using a tested version. So, thanks and apologies to Nick Andrew, Leandro Coletto Biazon, Thibaud Chupin, Craig Hollabaugh, Johannes H. Jensen, Fabien Le Lez, Craig Leres, and Mark Sproul.
2011-06-23, version 0.6
- Added ard-parse-boards. Mark Sproul suggested doing something like this ages ago, but I’ve only recently looked at it in detail.
- Fabien Le Lez reported that one needs to link with -lc to avoid linker errors.
Unreleased, version 0.7
- Added -lm to the linker options, and -F to stty.
2012-02-12, version 0.8
- Patches for version 1.0 of the Arduino IDE. Older versions might still work, but I’ve not tested it.
- A change to the build process: rather than link all the system objects directly into the executable, bundle them in a library first. This should make the final executable smaller.
- If TARGET isn’t explicitly set, default to the current directory name. Thanks to Daniele Vergini for this patch.
- Add support for .c files in system libraries: Dirk-Willem van Gulik and Evan Goldenberg both reported this and provided patches in the same spirit.
- Added a size target as suggested by Alex Satrapa.
Please consult the commit history on github.
It’s not a derivative of this, but Alan Burlison has written a similar thing.
Alan’s Makefile was used in a Pragmatic Programmer’s article.
Rei Vilo wrote to tell me that he’s using the Makefile ina Xcode 4 template called embedXcode. Apparently it supports many platforms and boards, including AVR-based Arduino, AVR-based Wiring, PIC32-based chipKIT, MSP430-based LaunchPad and ARM3-based Maple.