Sunday, February 8, 2015

Using the Arduino environment with Eclipse

I've recently fallen in love with the AVR MCUs especially because of the Arduino and it's hugely successful Arduino IDE. It seems that if there is a piece of hardware, a sensor perhaps, then Arduino has a library for it that you can use. It's just great!

There's however one small problem: you need the IDE to build it, the IDE is very small in capabilities (there's no code insight help like in Eclipse for example) and everything seems to be a bitt ... I don't know how to call it.. "amateur" is the word I guess... There's nothing wrong with doing things this way - unless you're used to using something more effective than the notepad for coding - like I am.

So.. You have the Arduino IDE, you have UNO board (possibly a cheep chinese clone) and you have done the Blink example to the death. Now it's time to do some serious damage!

We're going to need the following:

- a couple of packages (sudo apt-get install avrdude avr-libc gcc-avr make openjdk-7-jdk)
- Eclipse for C/C++ developers
- AVR plugin for Eclipse CDT
- Arduino 1.0.6 IDE (may work for a newer one, haven't checked it yet)

I'm going to assume you have downloaded Eclipse, installed the AVR plugin as instructed on the
AVR plugin page and extracted the Arduino IDE into the /tmp folder so you have the /tmp/arduino-1.0.6 location with all the things in it).

First we need to create a programmer to make Eclipse happy. To do so click Window -> Preferences, expand the AVR node and select AVRDude. In the list of programmers click Add, enter "Arduino" as the name of the programmer, select Arduino from the Programmer Hardware list, optionally if you're using a Nano or Mini enter the proper port (I for example have had to enter /dev/ttyUSB0 and I'm using an Arduino Nano with Optiboot) and click OK.

Next we need a project that will use the Arduino environment. Let's create one:

File -> New -> C++ Project
Enter project name, select "Makefile project" and then "AVR GCC Toolchain" and click "Finish".

Next bit is a bit tricky so you need to follow it to the letter. In your freshly created project create a folder called "arduino" and copy there the entire content of the following directories:

/tmp/arduino-1.0.6/hardware/cores/arduino
/tmp/arduino-1.0.6/hardware/variants/standard (that's actually just one file)
/tmp/arduino-1.0.6/libraries/Wire
/tmp/arduino-1.0.6/libraries/Wire/utility
(and any other library you'd like to use)

That'll give you a mini version of the environment to use. Next we need a way to build it. That's quite easy - you just drop in this Makefile into the arduino folder and type "make clean all" and you're done. When you want to use more libraries just drop their files in there and make clean all again.

One last thing to enable Eclipse to understand Arduino libraries. We need to define the symbol ARDUINO with value 106. To do this select Project -> Properties, then C/C++ Build -> Build Variables, click Add, enter "ARDUINO" into "Variable name" and "106" into "Value", click OK, then OK the properties dialog and you're all set!

Now for the fun part - let's create a blinker!

For that we need to create a new file, let's call it sketch.cpp (File -> New -> File, enter the name, press enter, done). In that file we'll enter the following:

#include

void setup() {
pinMode(13, OUTPUT);
}

void loop() {
delay(500);
digitalWrite(13, LOW);
delay(500);
digitalWrite(13, HIGH);
}

This is basic Arduino thing with the addition of one extra include file at the beginning of the file. Nothing more. Now to build that we'll need this Makefile in your project's folder.

Now all you need to do is select "Project -> Make target -> Build..." which will open a list of known targets. Click "Add" and enter "clean load" (that's the set of commands to build and upload the sketch to your board). Unfortunately due to a bug you'll have to close this window and re-open it to see the target you just added - do that and then select it and press "Build". And presto! your Arduino blinks!

There's an added bonus: you can now build your project without any IDE, for example using some sort of continuous integration or something - and ride like a pro! :)

No comments: