This tutorial describes how to set up Eclipse for Arduino (AVR) C/C++ development. Eclipse is a full-featured Integrated Development Environment with modern editor features such as syntax highlighting, code completion and error checking. I became interested in using Eclipse for Arduino development as I’m often simultaneously developing firmware for the chip and visualization software in a Java / Processing applet. Since I’m already using Eclipse for Java / Processing, it’s nice to be able to work in the same environment for both.
The Arduino site has a decent walkthrough, but I found some of the steps to be confusing or outdated. Hopefully, this will get you up and running quickly.
Contents |
Overview
Most of this setup will have to happen once, then starting new projects is typically as easy as duplicating a simple, skeleton project. Getting the paths and configuration options correct for this initial project was the most confusing for me, but I haven’t needed to tinker with them again.
To begin, download Eclipse for C/C++ Developers, or install the C/C++ plugin for your existing Eclipse version (these instructions assume Eclipse 3.5 Galileo). To install an Eclipse plugin:
- Select CDT from the drop down menu. If CDT is not present, then click the add button and paste the link to the plugin update site in the Location field: http://download.eclipse.org/tools/cdt/releases/galileo
- Select Eclipse C/C++ Development Tools, then click “Next >” and “Finish” on the following screen. This may prompt you to restart Eclipse, however select No so we can install the next plugin.
- Return to the Install New Software dialog. Paste in the location for the AVR Plugin and select it and click Next to install. You may have to agree to license terms. http://avr-eclipse.sourceforge.net/updatesite/
Setup Eclipse
After the plugins have been installed they need to be configured. Specifically, the paths to the AVR Tools need to be verified.
- Open the Eclipse Preferences: Eclipse > Preferences. Twirl down the AVR category and select “Paths”. Verify that the paths have located the correct AVR toolkit (CrossPack-AVE on OS X). You can edit these fields as necessary.
- Select the AVRDude category. I’ve found that I need to add a Programmer configuration for each Arduino (or USB to Serial adapter) that I use. Please let me know if you can get a better solution working with wildcards.
- Give this configuration a descriptive name. I’m using “Arduino Diecimila”.
- Select “Arduino” as the Programmer Hardware
- Enter the correct port for your Arduino. You can display this by plugging the Arduino into the computer, opening the Terminal then typing: /dev/tty.usbserial then hitting the Tab key. The remainder of the port name should appear.
- Set the baud rate to 19200
- Click OK. Then close the preferences.
Create an Arduino project
For the first project you’ll need to manually set up the configuration. I’d *highly* recommend making a basic blink sketch, then duplicate it for each new project. This is what the tutorial will set up. Alternatively, you can try importing this basic project, but may have to set up the paths for your system anyway. Eclipse Blink Sketch
- Go to File -> New -> C Project
- Select Empty Project under AVR Cross Target Application and name the project “Hello_Blink”.
- Click Finish.
- Select the “Hello_Blink” project in the Project Explorer panel, then navigate to Project > Properties menu.
- Twirl down the AVR category and select AVRDude.
- Select your Arduino from the Programmer configuration menu
- Select Target Hardware at left.
- Ensure that your Arduino is plugged into your computer via USB (and is powered on).
- Click “Load from MCU”. This should query the Arduino for it’s type and detect it correctly.
- If this doesn’t work, typically the Diecimila is an ATmega168 while the Duemilanove is an ATmega328
Each Ardiuno project needs to include the Ardunio core library. This file is compiled automatically by the Arduino IDE. While you could compile it yourself in Eclipse, the easiest way to get it is to build a simple sketch in the Arduino IDE, then copy the “core.a” file into your Eclipse project.
- Start the Arduino IDE
- Open the Blink sketch (File > Examples > Digital > Blink)
- Click the “Verify” button to compile the sketch
- Select Sketch > Show Sketch Folder
- Drag core.a into your Eclipse Hello_Blink project to copy it in.
- Right-click on core.a, select Rename and change it to “libcore.a”
Now we set up the correct building settings:
- Twirl down C/C++ Build and select Settings
- Select “Additional Tools in Toolchain”
- Check the following:
- Generate HEX for Flash Memory
- Print Size
- AVRDude
- Uncheck Generate Extended Listing
- Select AVR Compiler
- Change the command from avr-gcc to avr-g++
- Select AVR Compiler > Directories
- Add an Include Path to the header files within the Arduino.app bundle (look at the screenshot)
- You can right-click on Arduino.app and select Show Package Contents, then navigate to the Contents/Resources/Java/hardware/cores/arduino folder
- Select AVR Compiler > Debugging
- Set “Generate Debugging info” to “No debugging info”
- Select AVR Compiler
- Set the Optimization level to Size Optimizations
- Select AVR Assembler > Debugging
- Set “Generate Debugging info” to “No debugging info”
- Select AVR C Linker > Libraries
- Add a library named “core”
- Add a library path. If libcore.a is in the Hello_Blink project folder, the path will be “${workspace_loc:/Hello_Blink}”
Add a new main.c source file
This file will contain your typical Arduino sketch code. There are additional bits of code that are typically added automatically by the Arduino IDE which need to be explicitly written in Eclipse. Feel free to copy the below code.
- Go to File > New… > C Source File. Name this file main.c
- Paste in the following for a basic blink sketch:
#include "WProgram.h" // prototypes void blink(int n, int d); // variables int ledPin = 13; // LED connected to digital pin 13 void setup(){ Serial.begin(9600); // opens serial port, sets data rate to 9600 bps pinMode(ledPin, OUTPUT); // sets the digital pin as output Serial.println("------------------"); Serial.println(" hello world"); Serial.println("------------------"); } void loop(){ blink(3, 50); // blink led (n times, ms interval) delay(1000); // wait 1 sec } void blink(int n, int d){ for (int i=0; i<n; i++) { digitalWrite(ledPin, HIGH); delay(d/2); digitalWrite(ledPin, LOW); delay(d/2); } } int main(void) { init(); setup(); for (;;) loop(); return 0; }
Building
When you’re ready to build and upload to the Arduino, click the Hammer icon. This should compile your program and then run avrdude, which uploads to the Arduino.
New projects
Duplicating a project in Eclipse is as simple as selecting the source project in the Project Explorer panel, copying it (Edit > Copy) then paste. Eclipse will prompt you for a new name , then will create a new, duplicate project in the workspace.
If you keep the blink sketch intact, you should be able to duplicate it each time and avoid the above setup.
Follow up
You should be all set to work on Arduino projects in Eclipse. Note that Eclipse uses various “perspectives” for different modes and programming languages. Typically, you’ll be working in the C/C++ perspective while working on Arduino code. If you’d like to switch back to the Java perspective for working with Processing, select it from the perspectives at the upper right of the main window, or Navigate to Window > Open Perspective > Other…
Leave a Reply
You must be logged in to post a comment.