Press "Enter" to skip to content

Arduino on eclipse! – Part II – Projects

How to create projects, reference them and maintain a complex hierarchy instead of a bunch of sketches.

This tutorial is based on a Windows 64 bit system. But the fact that eclipse is multi-platform should ease the things.

Creating and managing projects

To work with Arduino you will need at least 2 different projects: ArduinoCore and myArduinoProject. Here I will show how to configure and build both. But the division is essential to accelerate your work and to make things clear.

Creation of well configured projects is the base for a good coding practices. So… Read a book about it.

Arduino Core library

Creating Arduino core library project

Why? Because you control when the core libraries are compiled. Is easy to maintain dependent projects. And looks fancy!

In eclipse navigate to File>New>C++ project.
For curious minds: Yes, Arduino code is mainly written in C++, but you write in a language similar to C with the Arduino IDE! How is this possible? Think about it, and just as a clue, the compiler has something to do in this.

  1. In the C++ Project window set the field Project name to ‘ArduinoCore.
    You will understand why this is the appropriate name in a moment.
  2. Check Use default location.
    Defaults are good.
  3. Set the Project type to AVR Cross Target Static Library>Empty Project.
  4. Click on Next > button and deselect Debug configuration.
    You will erase this configuration afterwards.
  5. Click on Next > button and select the appropriate MCU Type and MCU Frequency.
    In next table you can find for some boards some of the values.

    Arduino MCU Type MCU Frequency
    Uno/Duemilanove/Nano ATmega328P 16 000 000 (six zeroes)
    Diecimila/Duemilanove/Nano ATmega168 16 000 000
    Mega2560/MegaADK ATmega2560 16 000 000
    Mega1280 ATmega1280 16 000 000
    Leonardo/Esplora/Micro ATmega32u4 16 000 000

    CHECK THE CHIP FOR THE PROPER MCU TYPE. Some boards have different MCU between versions.
    This table has few Arduino models, but you can find all the rest on the file ‘<PathToArduino>\hardware\arduino\boards.txt’ you just need to search for the parameters <MyBoard>.build.mcu and <MyBoard>.build.f_cpu
  6. Click on Finish button.

Adding source files

Lets add some sources!

  1. On eclipse look to the Project explorer and find the ArduinoCore project just created. Right click on it and select Import...
  2. On the Import dialog select File System option.
  3. Click on Next > button and navigate or paste to the Arduino Core source directory.
    C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino
  4. Check the ‘arduino‘ folder that appears on the left list.
  5. In the Into folder field put ‘ArduinoCore\src‘.
    Source folder will help you maintain your projects ordered. For sure source are not the only files that you will use! (doc, include, bin, etc…)
  6. Click on Finish button to import the files.

Configuring Arduino core library project

All this steps configures parameters that are passed to AVR tools when building. You can obtain this parameter configuration activating verbose mode in Arduino IDE and reading executed commands when compiling.

  1. Lets create a configuration: Right click on the ArduinoCore project on Project Explorer. Go Build Configurations>Manage…
  2. Click on New… and name the new configuration as your MCU, in my case ‘mega_ATmega2560_16Mhz’ click Finish button.
    Each configuration should target a different MCU architecture with a clock frequency and a hardware configuration. So is good to name them using those parameters.
    The clock frequency is an input parameter to configure timers having, in that way, a time basis, essential for communication and other processes.
    Hardware configuration variants are listed further in this post.
  3. Now Delete other configurations.
    You don’t need them.
  4. Open the project properties (Alt+enter when you have selected the ArduinoCore project on the Project Explorer).
  5. Navigate to section AVR and check Enable individual settings for Build Configurations.
  6. Navigate to C/C++ Build>Settings.

    • AVR Compiler
      • Directories
        Add eclipse-AddPathButton next 2 paths:
        <PathToArduino>\hardware\arduino\variants\<hwConfiguration>”
        “${workspace_loc:/${ProjName}/src}”
        This second path will include the /src folder that you have created on the project.
        Arduino <hwConfiguration>
        Uno Duemilanove Diecimila standard
        Nano eightanaloginputs
        Mega mega
        Leonardo Esplora leonardo

        Again, you can find the rest of the boards on the file ‘<PathToArduino>\hardware\arduino\boards.txt’ you just need to search for the parameter <MyBoard>.build.variant

        "C:\Program Files (x86)\Arduino\hardware\arduino\variants\mega"
        "${workspace_loc:/${ProjName}/src}"
        
      • Debugging
        Set Generate Debuggin Info to Standard debugging info (-g2)
        Leave Debug Info Format to stabs
        Debugging options are useful at build time, this option does not affect to the final binary, but will become useful because Arduino standard platforms  have no debugger. It is possible to use it, but not via the USB connection from the board.
      • Optimization
        Set Optimization Level to Size Optimizations.
        Uncheck Pack structs and Short enums.
        Check Each funtion in its own section and Each data item in its own section.
        Set Other Optimization Flags to ‘Format c:’… Don’t. This is only a joke, but is good to point that if you don’t understand what you are doing is better to ask!
      • Language Standar
        Uncheck char is unsigned and bitfields are unsigned.
        I disabled them for the ArduinoCore library because those params are not present on the Arduino IDE building command. At low level is preferable to be specific.
    • AVR C++ Compiler
      • Directories
        Add eclipse-AddPathButton next 2 paths:
        <PathToArduino>\hardware\arduino\variants\<hwConfiguration>”
        “${workspace_loc:/${ProjName}/src}”
      • Debugging
        Set Generate Debuggin Info to Standard debugging info (-g2)
        Leave Debug Info Format to stabs
      • Optimization
        Set Optimization Level to Size Optimizations.
        Uncheck Pack structs and Short enums.
        Check Each funtion in its own section and Each data item in its own section.
      • Language Standar
        Uncheck char is unsigned and bitfields are unsigned.
        Check Do not use Exceptions.
  7. Apply the configuration and press OK button.

Compile ArduinoCore library

Now project is well configured. You can build it.

  1. Having the ArduinoCore project selected on Project Explorer build eclipse-buildButton it. You have the button on eclipse toolbar.
  2. You should see on the Project Explorer that a new folder is created under ArduinoCore named as the configuration that you have chosen for your configuration.
    In my case ‘mega_ATmega2560_16Mhz’
  3. Check file ‘ArduinoCore\mega_ATmega2560_16MHz\libArduinoCore.a‘ exists. If you don’t see it maybe you need to Refresh (F5) your Project Explorer.
    This file contains the compiled code of the Arduino core libraries specialized for ATmega2560. Linker will use this file when you build your own project.
    I realized that Arduino IDE compiles the core library each time that you perform a Verify/Upload, so using eclipse will decrease the building time. Good!

Template project, ‘myArduinoProject’

Lets configure one template project… Parameters are almost the same than for the ArduinoCore project, but I will write down here all the steps (short format) again so you don’t need to go up’n down. xD
New things and modifications are bold.

Creating template project

  1. File>New>C++ project.
  2. Field Project name to ArduinoTemplate.
  3. Check Use default location.
  4. Set Project type to AVR Cross Target Application>Empty Project.
  5. Next > and deselect Debug.
  6. Next > and select the appropriate MCU Type and MCU Frequency. 
    Arduino MCU Type MCU Frequency
    Uno/Duemilanove/Nano ATmega328P 16 000 000 (six zeroes)
    Diecimila/Duemilanove/Nano ATmega168 16 000 000
    Mega2560/MegaADK ATmega2560 16 000 000
    Mega1280 ATmega1280 16 000 000
    Leonardo/Esplora/Micro ATmega32u4 16 000 000
  7. Click on Finish.

Adding source files

  1. Right click on the project and select New>Source folder call it ‘src’.
  2. Right click on the src folder and select New>Source file call it ‘blink.c’.
  3. Open blink.c and copy some code:
    #include "Arduino.h"
    
    int led = 13;
    
    void setup() { 
     pinMode(led, OUTPUT); 
    }
    
    void loop() {
     digitalWrite(led, HIGH); 
     delay(1000);
     digitalWrite(led, LOW);
     delay(1000);
    }
    

    Note that you are including ‘Arduino.h’. This file is automatically included by the Arduino IDE when you compile an sketch. But in eclipse you will need to include it on the files where you use the Arduino fucntions.

Configuring template project

  1. Right click on the ArduinoTemplate project Go Build Configurations>Manage…
  2. New… and name it ‘mega_ATmega2560_16Mhz’. Click Finish button.
  3. Delete other configurations.
  4. Open the project properties.
  5. On AVR section check Enable individual settings for Build Configurations.
  6. C/C++ Build>Settings.

    • AVR Compiler
      • Directories
        Add eclipse-AddPathButton 2 paths:
        <PathToArduino>\hardware\arduino\variants\<hwConfiguration>”
        “${workspace_loc:/${ProjName}/src}”

        Arduino <hwConfiguration>
        Uno Duemilanove Diecimila standard
        Nano eightanaloginputs
        Mega mega
        Leonardo Esplora leonardo
      • Debugging
        Set Generate Debuggin Info to Standard debugging info (-g2)
        Leave Debug Info Format to stabs
      • Optimization
        Optimization Level to Size Optimizations.
        Uncheck Pack structs and Short enums.
        Check Each funtion in its own section and Each data item in its own section.
      • Language Standar
        Uncheck char is unsigned and bitfields are unsigned.
    • AVR C++ Compiler
      • Directories
        Add eclipse-AddPathButton 2 paths:
        <PathToArduino>\hardware\arduino\variants\<hwConfiguration>”
        “${workspace_loc:/${ProjName}/src}”
      • Debugging
        Set Generate Debuggin Info to Standard debugging info (-g2)
        Leave Debug Info Format to stabs
      • Optimization
        Set Optimization Level to Size Optimizations.
        Uncheck Pack structs and Short enums.
        Check Each funtion in its own section and Each data item in its own section.
      • Language Standar
        Uncheck char is unsigned and bitfields are unsigned.
        Check Do not use Exceptions.
    • AVR C++ Linker
      • Libraries
        Add
         ‘${workspace_loc:/ArduinoCore/mega_ATmega2560_16Mhz}’ to Libraries Path (-L).
        Add ‘ArduinoCore’ and ‘m’ to Libraries (-l).
        This configuration will tell to the linker to use the file libArduinoCore.a file created for our platform, in my case ‘mega_ATmega2560_Mhz’.
        The library ‘m’ is a variation on the standard ‘libc/math’ library. The reason to include it is because Arduino IDE do the same thing. You can read more about ‘libm.a‘.
  7. Apply the configuration and press OK button.

Compile template project

Now project is well configured. You can build it.

  1. Build eclipse-buildButton ‘ArduinoTemplate‘.

Duplicating template project

  1. Navigate to your workspace with the Windows explorer.
  2. Copy ‘ArduinoTemplate‘ folder and rename it.
  3. On eclipse navigate on the menus to File>Import… and select General>Existing Projects into Workspace.
  4. As root directory select your workspace and check the new project you just created.
  5. Click on Finish.

If you have configured properly both projects you should see on the Console how the process is going on. At the end you will see a report with the size of your program and how much it occupies on your Arduino.
Now you have the binary code in your computer, the last step is to upload it into your board!
Continue with Developing for Arduino from eclipse (Part III)” to know how.

2 Comments

  1. […] From now eclipse and AVR plugin knows where to find the magic tools that allow you to compile small pieces of code for your Arduino. But wait! You still need to make some configurations in your projects to tell to those tools witch architecture do they need to compile for. How? Continue with “Developing for Arduino from eclipse (Part II)“ […]

  2. Arduino on eclipse! | Roadmap Arduino on eclipse! | Roadmap 23rd October 2014

    […] Developing for Arduino from eclipse (Part II) In this article you can find how to setup the different projects in Eclipse. […]

Leave a Reply