Update News
2011-06-23 : After letting this languish for a while, there are now a couple of new versions.
- Version 0.51 simply incorporates a change made by Debian and Ubuntu which compiles library files. Thanks the package maintainers for doing this, and to Stefan Tomanek who sent them the patch.
- Version 0.62 includes a new program ard-parse-board which finds out e.g. the processor type by looking in the standard distribution's boards.txt file. It's a bigger change and thus more likely to contain bugs.
Introduction
The Arduino3 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: the Arduino website has a hopeful sounding Arduino from the Command Line4 article. In turn this points you at the Makefile shipped as part of the Arduino software distribution: hardware/cores/arduino/Makefile.
This didn't really fit quite what I wanted to do though, so I wrote my own Makefile. 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.
Installation instructions
If you're using Debian or Ubuntu, then just grab the arduino-core package.
You'll need to download the tarball containing the Makefile,5, unpack it, and then copy the Makefile somewhere sensible:
$ wget http://mjo.tc/atelier/2009/02/acli/arduino-mk_0.6.tar.gz
$ tar xzvf arduino-mk_0.6.tar.gz
$ cp arduino-mk-0.6/Arduino.mk /path/to/my/arduino/stuff/Arduino.mk
$ cp arduino-mk-0.6/ard-parse-boards /usr/local/bin
The next step is to create a small Makefile for the sketch you actually want to build. The typical Arduino sketch directory looks like this:
$ ls -ltr
total 8
drwxr-xr-x 4 mjo staff 136 15 Feb 19:53 applet
-rw-r--r--@ 1 mjo staff 384 17 Feb 16:11 CLItest.pde
To this we'll add a Makefile:
ARDUINO_DIR = /Applications/Arduino.app/Contents/Resources/Java
TARGET = CLItest
BOARD_TAG = uno
ARDUINO_PORT = /dev/cu.usb*
ARDUINO_LIBS = LiquidCrystal
include /path/to/my/arduino/stuff/Arduino.mk
Hopefully these will be self-explanatory but in case they're not:
- ARDUINO_DIR
- Where you installed the Arduino software. On the Mac this has to point deep inside the Arduino installation /Applications.
- TARGET
- The basename used for the final files. Canonically this would match the .pde file, but you can choose anything!
- ARDUINO_LIBS
- A list of any libraries used by the sketch—we assume these are in $(ARDUINO_DIR)/hardware/libraries.
- BOARD_TAG
- A tag identifying which type of Arduino you're using. This only works in version 0.6 and later.
- ARDUINO_PORT
- The port where the Arduino can be found (only needed when uploading) If this expands to several ports, the first will be used.
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:
- MCU
- The target processor (atmega168 for the Duemilanove).
- F_CPU
- The target's clock speed (16000000 for the Duemilanove).
- AVRDUDE_ARD_PROGRAMMER
- The protocol avrdude speaks—defaults to stk500v1.
- AVRDUDE_ARD_BAUDRATE
- The rate at which we talk to the board—defaults to 19,200.
If you're using the toolchain provided by the system rather than bundled with the Arduino software (as I think is the case on Linux) then you'll have to add some more paths:
AVR_TOOLS_PATH = /usr/bin
AVRDUDE_CONF = /etc/avrdude.conf
BOARD_TAG
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.
ARDUINO_LIBS
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, to use the SD library6 you'll need to:
- add #include
to your sketch; - add SD and SD/utility to ARDUINO_LIBS.
If you omit the SD/utility library, you'll get messy looking link errors from the bowels of the SD library.
Building
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-cli.
$ ls -l
total 32
-rw-r--r--@ 1 mjo staff 384 17 Feb 16:11 CLItest.pde
-rw-r--r--@ 1 mjo staff 202 17 Feb 16:39 Makefile
drwxr-xr-x 4 mjo staff 136 15 Feb 19:53 applet
drwxr-xr-x 21 mjo staff 714 17 Feb 17:52 build-cli
build-cli
Let's peek inside the build-cli directory:
$ ls -l build-cli
total 392
-rw-r--r-- 1 mjo staff 491 17 Feb 17:52 CLItest.cpp
-rw-r--r-- 1 mjo staff 583 17 Feb 17:52 CLItest.d
-rwxr-xr-x 1 mjo staff 60971 17 Feb 17:52 CLItest.elf
-rw-r--r-- 1 mjo staff 3994 17 Feb 17:52 CLItest.hex
-rw-r--r-- 1 mjo staff 5932 17 Feb 17:52 CLItest.o
-rw-r--r-- 1 mjo staff 6920 17 Feb 17:52 HardwareSerial.o
-rw-r--r-- 1 mjo staff 12708 17 Feb 17:52 Print.o
-rw-r--r-- 1 mjo staff 6852 17 Feb 17:52 WInterrupts.o
-rw-r--r-- 1 mjo staff 4680 17 Feb 17:52 WMath.o
-rw-r--r-- 1 mjo staff 611 17 Feb 17:52 depends.mk
-rw-r--r-- 1 mjo staff 5900 17 Feb 17:52 pins_arduino.o
-rw-r--r-- 1 mjo staff 8476 17 Feb 17:52 wiring.o
-rw-r--r-- 1 mjo staff 7224 17 Feb 17:52 wiring_analog.o
-rw-r--r-- 1 mjo staff 8244 17 Feb 17:52 wiring_digital.o
-rw-r--r-- 1 mjo staff 6544 17 Feb 17:52 wiring_pulse.o
-rw-r--r-- 1 mjo staff 7424 17 Feb 17:52 wiring_serial.o
-rw-r--r-- 1 mjo staff 5308 17 Feb 17:52 wiring_shift.o
Most of the files in here are object files for the wiring library. What about the others ?
- CLItest.cpp
- This is the .pde sketch file with a small main program prepended and a suitable #include prepended.
- CLItest.d
- This tracks the dependencies used by CLItest.pde
- CLItest.elf
- This is executable produced by the linker
- CLItest.hex
- This is a hex dump of (the code part) of the executable in a format understood by the Arduino's bootloader.
- CLItest.o
- The object file we got by compiling CLItest.cpp.
- depends.mk
- A single file containing all the dependency relations (it's the concatentation of all the .d files).
You may recall the TARGET variable we specified in the Makefile above: it's that which sets the base name for e.g. CLItest.elf. However CLItest.cpp, CLItest.o and CLItest.d take their name from the .pde sketch file.
Uploading code
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
Fuses
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 .pde 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.
Wiring-less development
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.
Changelog
2010-05-21, version 0.37
- Tidied up the licensing, making it clear that it's released under LGPL 2.1.
- Philip Hands8 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.49
- Tweaked rules for the reset target on Philip Hands' advice.
2011-06-23, version 0.510
- 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.611
- 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.12
Similar work
It's not a derivative of this, but Alan Burlison has written a similar thing.13
Alan's Makefile was used in a Pragmatic Programmer's article.14
References
- 1. http://mjo.tc/atelier/2009/02/acli/arduino-mk_0.5.tar.gz
- 2. http://mjo.tc/atelier/2009/02/acli/arduino-mk_0.6.tar.gz
- 3. http://www.arduino.cc/
- 4. http://www.arduino.cc/en/Hacking/CommandLine
- 5. http://mjo.tc/atelier/2009/02/acli/arduino-mk_0.6.tar.gz
- 6. http://www.arduino.cc/en/Reference/SD
- 7. http://mjo.tc/atelier/2009/02/acli/arduino-mk_0.3.tar.gz
- 8. http://hands.com/~phil/
- 9. http://mjo.tc/atelier/2009/02/acli/arduino-mk_0.4.tar.gz
- 10. http://mjo.tc/atelier/2009/02/acli/arduino-mk_0.5.tar.gz
- 11. http://mjo.tc/atelier/2009/02/acli/arduino-mk_0.6.tar.gz
- 12. http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1290294587
- 13. http://bleaklow.com/2010/06/04/a_makefile_for_arduino_sketches.html
- 14. http://pragprog.com/magazines/2011-04/advanced-arduino-hacking
![Atom Feed [ Atom Feed ]](../../atom.png)
![[ facebook ]](../../social/facebook.png)
![[ delicious ]](../../social/delicious.png)
![[ digg ]](../../social/digg.png)
![[ reddit ]](../../social/reddit.png)
![[ stumbleupon ]](../../social/stumbleupon.png)