Wednesday, May 26, 2010

Setup Arduino with Eclipse

While I think the Arduino IDE is great to play around with an AVR microcontroller it reaches its limits when used with bigger projects. They often need a better source code organisation, esp. if one's using C++ classes. Because of that I mostly switch to an Eclipse based development environment after an initial kickoff phase.

The setup of the eclipse based development environment for Arduino is a bit of a challenge. Following my setup procedure, based upon Robert Carlsen's Blog on a Mac OS X Snow Leopard and a Boardiuno:
  • install XCode if not already done (else make will fail)
  • install Cross Pack for AVR development
  • install Arduino IDE
  • Download and install Eclipse for C/C++ developers
  • Install the AVR-Eclipse Plugin to Eclipse
    • Help -> Install new software
    • http://www.eclipse.org/downloads/ is the update site
    • restart eclipse
  • check the paths in Eclipse preferences / AVR / Paths
    • should point to the cross pack
    • Atmel Part Description Files is not needed
  • add a AVRDude config (Eclipse preferences / AVR / AVRDude -> Add)
    • Name: Boarduino
    • Programmer Hardware: STK500 Version 1.x
    • Baudrate: 57600
    • Set port (find the port by 'ls /dev/cu.*', i.e. /dev/cu.usbserial-FTELSJ9T)
  • create a new CPP project (or checkout my template)
    • New C++ Project
    • AVR Cross Target Application
    • don't press finish, but next
    • select hardware (ATMega 328P and 16 Mhz (16000000) for Boarduino)
    • finish
  • Open the properties of the newly created project
    • AVR / AVRDude: Select the programmer ("Boarduino")
    • under C/C++ Build/Settings (for both targets, Debug and Release)
      • under "Additional Tools in Toolchain" check only:
        • Generate HEX file for flash
        • Print Size
        • AVRDude (optional, directly uploads after compilation)
      • under AVR compiler
        • change command from "avr-gcc" to "avr-g++"
        • Directories: add "/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino"
        • Debugging: no debugging info
        • Optimization: size (-Os)
      • under AVR C++ compiler
        • change command from "avr-gcc" to "avr-g++"
        • Directories: add "/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino"
        • Debugging: no debugging info
        • Optimization: size (-Os)
      • under AVR C++ Linker
        • Add Library
          • Name: core
        • Add Library Path
          • Path: ${workspace_loc:/Blink/lib}   (if the project name is "Blink")
  • Open Arduino IDE and an example project
    • set your Arduino (in Tools/Board)
    • Click Verify/Compile in the toolbar while pressing shift
    • Extract the folder name from the log-panel (beginning with /var/folders/....)
    • copy the core.a from the folder into <eclipse-project>/lib/libcore.a
  • Compile the project in Eclipse


Notes:
you might have to define 
    extern "C" void __cxa_pure_virtual(void);
    void __cxa_pure_virtual(void) {}
in your projects main.
And also:
    #undef int
    #undef abs
    #undef double
    #undef float
    #undef round
if you get compile errors in math.h and several other system-files.


Versions used:
  • Eclipse 20100218-1602 (3.5 SR2)
  • AVR Eclipse 3.2.1
  • Arduino IDE 0018
  • CrossPack AVR 20100115

Good luck.

3 comments:

  1. Have you done any arduino coding in scala?

    ReplyDelete
  2. No not on the device itself. I believe that is not possible due to memory constraints on the AVR processor. I think the scala libraries will never fit in those few kb.
    But I'm communicating between a scala program and the arduino (code is in c++) over USB and over XBee's and that's working like a charm.

    ReplyDelete
  3. Mario - Thanks for the great info! I got this stack working after numerous failed attempts at approaches documented on other sites.

    One question for you though - I'm able to access the core library functionality (ex, delay, digitalWrite, analogWrite, etc), but not 'extended' types of functionality, such as String, LiquidCrystal, etc. (I haven't tried 3rd-party libraries as of yet.) When I try to declare a String instance, such as:

    String s = "This is a string";

    I get the error:

    main.cpp:(.text+0x1a): undefined reference to `String::String(char const*)'

    I'm a Java guy, not a C guy, so I'm trying to figure this part out, but any insight you might provide would be appreciated!

    BTW, I've done an include of WString.h in my main.cpp, and added an include path in the project of /Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/avr/include (contains stdlib.h, string.h and ctype.h, which are included in WString.h

    ReplyDelete