Update News
The current version of this software (0.4) won't work with versions of the Arduino software before 0018.
Introduction
The Arduino1 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 Line2 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
You'll need to download the tarball containing the Makefile,3, unpack it, and then copy the Makefile somewhere sensible:
$ wget http://mjo.tc/atelier/2009/02/acli/arduino-mk_0.4.tar.gz
$ tar xzvf arduino-mk_0.4.tar.gz
$ cp arduino-mk-0.4/Arduino.mk /path/to/my/arduino/stuff/Arduino.mk
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
MCU = atmega168
F_CPU = 16000000
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.
- MCU
- The target processor (atmega168 for the Duemilanove).
- F_CPU
- The target's clock speed (16000000 for the Duemilanove).
- 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.
A couple of other options might be useful:
- 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
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 change the chip. You can set the fuse settings thus (the values below are the defaults):
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.
- Handling Arduino libraries isn't very clean. For one thing I'm relying on them being compiled in a compatible way by the Arduino environment which is probably a bit risky. Secondly it's a bit lazy to get the user to specify which libraries are being used manually.
- 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).
- I fear that the Makefile makes unwarranted assumptions about the hardware: for example, all my Arduino experience has been ATmega 168 related.
- 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.3
- Tidied up the licensing, making it clear that it's released under LGPL 2.1.
- Philip Hands4 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.
References
- 1. http://www.arduino.cc/
- 2. http://www.arduino.cc/en/Hacking/CommandLine
- 3. http://mjo.tc/atelier/2009/02/acli/arduino-mk_0.4.tar.gz
- 4. http://hands.com/~phil/