Eclipse is a free, powerful, and full-featured development environment that can be set up to work with AVR and Arduino. This page is very much a work-in-progress, please free to add to it or improve it.

Installation

Install the software

Eclipse and additional plugins

  • Eclipse IDE for C/C++ Developers: http://www.eclipse.org/downloads You can also just add the C Development Toolkit to an existing installation. This tutorial have been tested successfully with Europa 3.3.2 Winter and Ganymede 3.4.1.
  • AVR Plugins for Eclipse. Use the most recent version (2.3.1 as of this writing). You will have to manually copy this "plugin" into the the Eclipse installation. Move the folder which begins with the name "de.innot.avreclipse" to the "dropins" folder located within the eclipse folder. Restart Eclipse if you have it already running. There should now be a new "AVR" toolbar button in your workspace.
  • Target Management Project. This is optional, it is used to monitor the serial board directly within Eclipse for debugging purposes.

GNU AVR-GCC toolchain

Creating an "Arduino Library" project

There are four ways to get the Arduino core library: using a precompiled binary, getting an existing library from an Arduino IDE project, compiling the library directly into your project, compiling your own library file.

The library source code is included in the Arduino IDE download, in the hardware/cores/arduino directory. At the very least, you will need the header files from that directory accessible to your Eclipse project:

    * HardwareSerial.h
    * WProgram.h
    * wiring.h
    * WConstants.h
    * binary.h
    * pins_arduino.h
    * wiring_private.h

You will also need the .a static library.

Using a precompiled library

Click here for a link to a precompiled core library (including header files). If this doesn't work, you will have to use one of the methods outlined below.

Including the library from an Arduino IDE project

From any Arduino IDE project, get the core.a file in the applet subdir and add it, too. I personally renamed it to diecimilia_core.a to prevent mistaking it for a library for another Arduino core. To generate this file you will need to create and compile a project in the Arduino IDE.

Note: You must use the upload feature to get the applet directory (in which core.a is located) into your sketch folder. Clicking on 'verify' in the IDE will compile and remove the files in a temporary location.

Compiling the library code directly into the Eclipse application project

You can copy the entire contents of the hardware/cores/arduino directory into your Eclipse project so that it is compiled into the application every time. But this requires that you use C++ projects, and the projects will take a bit longer to compile. This generally isn't a very good idea unless you're hacking the core code.

Compiling your own static library

You can compile your own static library in Eclipse (this author found this method more straightforward than using the Arduino IDE to generate the core library).

  • In Eclipse, click File->New->C++ Project.
  • Select the Empty Project option under AVR Cross Target Static Library (not "AVR Cross Target Application").
  • Enter the project name (e.g. "DiecimilaCore"). Click Next. Click next again.
  • Set the MCU type and frequency to the desired target architecture. For example, for the Diecimila, use ATmega168 running at 16000000 Hz. For new versions of the Duemilanove, use ATmega328P running at 16000000 Hz. If you look closely at the Arduino board, you can see the target platform written on the main processor. Click Finish.
  • In Eclipse, click Project->Properties.
  • Select C/C++ Build and expand the category (e.g., click the diamond to the left of "C/C++ Build").
  • Select Settings under C/C++ Build.
  • In the right pane, Click AVR Compiler and then Debugging.
  • Set "Generate Debugging Info" to No debugging info.
  • In the right pane, Click AVR Compiler, then Optimization
  • Set the Optimization Level to "Size Optimizations".
  • In the right pane, Click AVR C++ Compiler and then Debugging.
  • Set "Generate Debugging Info" to No debugging info.
  • In the right pane, Click AVR C++ Compiler, then Optimization
  • Set the Optimization Level to "Size Optimizations".
  • Copy the contents of the Arduino IDE's hardware/cores/arduino directory, except for main.cxx, into the Eclipse project.
  • Build the project by clicking the hammer in the tool bar, or by selecting Project->Build All. It will build a file called libProjectName.a (e.g. libDiecimilaCore.a). This is the core library file.

Your first Arduino project

  • Go to File -> New -> C Project
  • Select Empty Project under AVR Cross Target Application
  • Give the project a name. Click next, and then next
  • Select the appropriate MCU type and clock frequency. For the Diecimila, use ATmega168 with a frequency of 16000000. New versions of the Duemilanove use the ATmega328P.
  • Select Project->Properties
    • Select C/C++ Build->Settings in the left pane
    • Select Additional Tools in Toolchain in the right pane
      • Check Generate HEX file for Flash memory
      • Check Print Size.
      • If you want Eclipse to upload the firmware to the Arduino after every build, check AVRDude.
    • Select AVR Assembler and then Debugging
      • Set Generate Debugging Info to No debugging info
    • Select AVR Compiler and then Directories
      • If the core header files are not in the project directory, add the path to the header files.
      • Example: /Applications/Arduino.app/Contents/Resources/Java/hardware/cores/arduino
    • Select AVR Compiler and then Debugging
      • Set Generate Debugging Info to No debugging info
    • Select AVR Compiler and then Optimization
      • Set Optimization Level to Size Optimizations (-Os)
    • Select AVR C++ Compiler and then Directories
      • If the core header files are not in the project directory, add the path to the header files.
      • Example: /Applications/Arduino.app/Contents/Resources/Java/hardware/cores/arduino
    • Select AVR C++ Compiler and then Debugging
      • Set Generate Debugging Info to No debugging info
    • Select AVR C++ Compiler and then Optimization
      • Set Optimization Level to Size Optimizations (-Os)
    • Select AVR C/C++ Linker
      • If you're using C++ then the hex file can get really big. In the linker menu, change the command to avr-gcc and the command line pattern to the following:
      • ${COMMAND} --cref -s -Os ${OUTPUT_FLAG}${OUTPUT_PREFIX}${OUTPUT} ${INPUTS} -lm ${FLAGS}
    • Select AVR C/C++ Linker and then Libraries
      • Select Libraries Path and add the path to the core library
      • Select Libraries and add the core library name
      • For example, if the library is in the project under ${ProjectHome}/arduino/libDiecimilaCore.a, the path will be "${workspace_loc:/ProjectName/arduino}" and the library name will be "DiecimilaCore".

A couple more things to do...

You may need to add the following function when using the C++ headers:

extern "C" void __cxa_pure_virtual()

{

cli();

for (;;);

}

Add this function to your main application file (main.c). It may work in a header file as well.

The contents of the function can be any error handling code; this function will be called whenever a pure virtual function is called.


The Arduino environment issues a reset command prior to uploading. Newer versions of AVRDude (such as the one shipped with CrossPack) will also do this. If you have an older version and are using Windows, you can overcome this problem with this little app. Attach:newavrdude.exe

To use this, you need to:

1) rename the avrdude.exe that is currently being used by eclipse to "realavrdude.exe" 2) Make sure that in the eclipse configuration for avrdude, you specify the port override. This should add something like "-P//./COM7" and it needs to be argument #3 (if this causes a problem for anyone, let me know and I'll build in some configuration stuff). 3) copy newavrdude.exe into the same directory as realavrdude.exe, and rename it to avrdude.exe

Writing code

When using the Arduino IDE, you just have to define setup() and loop() functions and the IDE already has a main functional defined in the core library that calls setup() and loop() functions.

Eclipse does not define the main() function for you. However, you can use Arduino's default main() function by copying the main.cxx file from the hardware/cores/arduino directory in the Arduino IDE package. You might have to rename this file to main.c for Eclipse to recognize it as a code file. Linking in main.cxx will allow you just to define the setup() and loop() functions like you would in the Arduino IDE.

You must #include "WProgram.h" in your application program to gain access to the Arduino API.

If you choose to define your own main function, you must NEVER return from main(). I mean, you-MUST-NEVER-return-from-main. In human language, this could be translated as "the main() function must contain some kind of endless loop, because if it ends, the Arduino won't stop the program but will just keep reading random data as code". You must also always add a call to init(); as your first instructions. Not doing this will prevent any time-related functions from working.

So, your basic code will usually look like :

int main(void) { init();

/* Add setup code here */

for (;;) { /* write main loop here */ }

}

Uploading to the Arduino

Configuring AVRDude

Go back to your project settings, and go the AVR/AVRDude page. Create a new programmer configuration. You only need to do this once; for other Arduino projects, you can reuse this configuration.

For Diecimila, the programmer model is "ATMEL STK500 version 1.X firmware". Probably the same for other Arduino boards.

Enter the name of your serial port in the "Override default port" field. Usually this is something like "/dev/cu.usbserial*" in *NIX and something like "//./COM*" in Windows. Override the default baud rate to 19200 for Arduinos based on the ATmega168, or 57600 for Arduinos based on the ATmega328p.

On Windows, the FTDI virtual COM port driver doesn't register its COM port until the Arduino board is plugged in. With the board plugged into USB, check a terminal program or device manager to find out what COM port the FTDI driver is using for the Arduino. This COM port will usually be the same on a machine even if you reboot or unplug the Arduino, but might differ on different machines.

Once you've configured the programmer, save it, then go to the "Advanced" tab and check "Disable device signature check".

If you are using WinAVR-20090313 or earlier on Windows, then you will need to replace the default avrdude.exe and avrdude.conf in the WinAVR directory with the ones from the following directories in the Arduino IDE folder:

hardware\tools\avr\bin\avrdude.exe hardware\tools\avr\etc\avrdude.conf

Uploading

If you've checked "AVR Dude" in the Additional tool in toolchain panel of the C/C++ Build Settings, your program will get upload at each build. Else, you can just right click your project in the Explorer and choose "AVR/Upload Project to Device" or click the AVR upload button.

Serial port monitor

If you have Target Management Project installed, just go to the Window menu / Show View / Other.. And choose Terminal/Terminal. Open it with a serial link and choose the serial port your Arduino is connected to. Notice you'll probably have to disconnect the terminal while uploading.

As the RXTX library is a troublemaker for many people, there is a workaround : Just start a SSH or Telnet (please be aware this may be a potential security leak) server on your computer and open it in the Eclipse terminal. Then, on any unix, just type :

cat /dev/your_serial_port/

External References

TO-DO list

  • Create an Arduino project template to avoid doing a complete config of each project.
  • Find a workaround for the C++ Linker problem.
  • Make this easier to read ^_^
  • Add Windows-specific stuff, of which I don't know a thing.