Computing stuff tied to the physical world

Browsing the Arduino run-time code

In AVR, Software on Jan 10, 2012 at 00:01

The Arduino IDE is a thin wrapper around the avr-gcc compiler and the avr-libc run-time library. It also includes a fairly basic IDE, i.e. a text editor and conventions for managing projects, in the form of “sketches” and libraries.

I prefer to use my own programmer’s editor, because it supports multiple programming languages and has a lot more features for software development (such as Git integration, code folding, and save-on-lose-focus). The Arduino IDE supports external editors by disabling its own one – which is an option in the preferences:

External edit

Now I can simply use my own editor and switch to the Arduino IDE for compiling and uploading.

One advantage of using an external editor, is that you can look at other source code than just your own sketches. In the rest of this post, I’m going to describe how to look at one of the most interesting parts of the Arduino IDE: its run-time library, i.e. the Wiring code which adds supports for everything which makes an Arduino different from the ATmega’s on which it is based.

Note: what follows is specific for Mac OSX, but apart from the location of these files and the editor used, you should be able to transpose all of this to your own beloved computer environment.

The first task, is to figure out where the Arduino IDE’s run-time code is located. In Mac OSX, this code is located inside the Arduino application. To view this area, you can right-click on the Arduino app:

Screen Shot 2012 01 09 at 19 29 48

This leads to the following directory structure:

Screen Shot 2012 01 09 at 19 28 16

The interesting bits are inside the “hardware” folder:

Screen Shot 2012 01 09 at 19 48 09

Here are the first few lines of “Arduino.h”, for example (this used to be “WProgram.h”):

Screen Shot 2012 01 09 at 19 55 09

These source files are where everything specific to the Arduino IDE’s runtime happens. The “Serial” object, and the “millis()” code, for example. If you want to really understand what the Arduino is about, then it’s well worth going through some of these files. As you’ll find out, there’s no magic once you start looking in the right places.

Try it!

  1. What external editor are you using?

  2. When i started development, i immediately switched to Eclipse IDE with C/C++ plugin. There is a nice article explaining how to do that: http://arduino.cc/playground/Code/Eclipse

    Using the AVR plugin, you even have a nice integration of the avrdude programmer directly out of the IDE. Only thing no working for me is the serial terminal (on mac) – thats the only reason for me to start the arduino app at all :)

  3. On Linux and Windows these files are in the folder the Arduino IDE is installed into.

    The downside of TextMate is that it chages a lot of money. If you’re looking for other good (free) editors for Arduino sketches, try Geany, vim and/or emacs.

  4. In terms of Mac editors, what’s wrong with Xcode? It’s really convinient and free (!!!) It also has command line – see: man xed

  5. I’ve been using vim + Arscons http://code.google.com/r/kylecgordon-arscons/ which is a make file for arduino sketches :)

    (more documentation of the arscons see: https://code.google.com/p/arscons/ )

Comments are closed.