Taillieu.Info

More Than a Hobby..

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /customers/3/2/5/taillieu.info/httpd.www/templates/taillieuinfo_joomla3_rev1/functions.php on line 194

Eclipse and Arduino Under Windows

 

http://sun0.cs.uca.edu/~administrator/reu/arduino-eclipse-windows.html

http://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/juno/SR2/eclipse-cpp-juno-SR2-win32-x86_64.zip

http://arduino.cc/en/Main/Software

 


 

This guide will help you integrate the Arduino SDK into the Eclipse IDE. These directions are specific to Windows. There are several reasons why you might want to do this. First, the Arduino IDE is awful for any kind of lengthy coding, because it is designed for short "sketches" rather than complex programs. Further, the Arduino IDE uses a subset of the C language. By integrating the SDK into Eclipse you will be able to use the full capabilities of the C/C++ language. Finally, you can now more easily use a version control system in your project.

Unfortunately, getting Arduino to talk to the Eclipse IDE is an overly complex process. There are currently 146 individual directions to follow. Be sure to follow the directions carefully! This process is very brittle.

Please note, I have only tested these directions against an Arduino Uno. In theory the process should be identical for other boards. But I have no way of testing for that. I have included the websites I drew these directions from at the bottom of the page. That way if I missed anything, you can see if they have covered it.

Install Eclipse And The Plug-in

This document assumes that you don't have eclipse installed on your computer. If it is already installed, then you can skip the first part of these directions.

  1. Download Eclipse IDE for C/C++ Developers
  2. Unzip the application. Put the resulting eclipse folder somewhere you can find it. This is all that is required to install the IDE. You may want to create a shortcut to the eclipse executable on your desktop.
  3. Start Eclipse.
  4. Create the workspace wherever you want. Call the workspace Arduino.
  5. Close the Welcome screen.
  6. Click Help → Install new software in the menu bar.
  7. Click the Add button.
    1. Set the Name field to AVR
    2. Set the URL to http://avr-eclipse.sourceforge.net/updatesite
    3. Click OK
  8. Wait for the package list to download.
  9. Check the AVR Eclipse Plugin item, then click the Next button.
  10. It may take it a couple of minutes to calculate the dependencies. When it is finished click the Next button.
  11. Accept the TOS, then click the Finish button.
  12. You my have to authorize the install because it is not signed. When the installation has finished, restart Eclipse.
  13. Click Help → Install new software in the menu bar.
  14. Click the Add button.
    1. Set the Name field to RXTX
    2. Set the URL to http://rxtx.qbang.org/eclipse
    3. Click OK
  15. Wait for the package list to download.
  16. Check the newest RXTX Plugin item, then click Next button.
  17. It may take it a couple of minutes to calculate the dependencies. When it is finished click the Next button.
  18. Accept the TOS, then click the Finish button.
  19. You my have to authorize the install because it is not signed. When the installation has finished, restart Eclipse.
  20. Click Help → Install new software in the menu bar.
  21. Under Work With... select the name of your eclipse version, e.g. Juno.
  22. This will load up a bunch of packages, so be patient. Once the packages are listed, open the Mobile Device Development tree. Check the Target Management Terminal item.
  23. Install the item just like we did before. When it is finished, be sure to restart Eclipse again.

Get the SDK

In order to develop for Arduino you will need its SDK. The SDK will provide us not only with libraries, but also the entire compiler tool chain. These directions will have you to install the SDK within Eclipse's directory. This is done for simplicity. You will have to locate the directory many times in the sections below this, so placing it here makes it location easier to remember.

  1. Download the latest Arduino SDK for Windows. Be sure to download the ZIP file!!!
  2. Unzip the package into the eclipse folder. This should leave you with a folder called arduino-1.x.y (where x.y refers to the last part of the SDK's version). From now on the location of this folder will be referred to as %SDKPATH%.
  3. Plug your board in. Windows probably cannot find the drivers for the board. Luckily the SDK comes with the drivers. Start by opening Device Manager in Windows.
  4. Find the Unknown Device. Right-click and select Update Driver Software.
  5. Tell the system that you know where the driver is (usually referred to as browsing).
  6. Next select the %SDKPATH%\drivers directory.
  7. Tell your system to install the drivers. That should be it.

Creating The Arduino Workspace

If you did not create an Arduino workspace above, then start by creating a new workspace that will be used just for Arduino development. You can create a new workspace by doing the following:

  1. Click File → Switch Workspace → Other... in the menu bar.
  2. Save the new workspace where ever you want.

Setup the AVR Plugin

At this point Eclipse knows nothing about your Arduino SDK. You must tell the plug-in where the various necessary tools are manually. This section guides you through this process.

  1. In eclipse click Window → Preferences in the menu bar.
  2. Select AVR in the left pane and open the list.
  3. Select the AVRDude item.
    1. Check the "Use custom configuration file for AVRDude" box.
    2. Click the Browse button.
    3. Use %SDKPATH%\hardware\tools\avr\etc\avrdude.conf as the path.
    4. Click the Apply button.
  4. Select the Paths item.
  5. Select AVR-GCC and click the Edit button.
    1. Set the Path Source to "Custom"
    2. Click Browse.
    3. Navigate to the %SDKPATH%\hardware\tools\avr\bin directory.
    4. Click the OK button to close the folder select dialog.
    5. Click the OK button to close the edit item dialog.
  6. Select GNU Make and click the Edit button.
    1. Set the Path Source to "Custom"
    2. Click Browse.
    3. Navigate to the %SDKPATH%\hardware\tools\avr\utils\bin directory.
    4. Click the OK button to close the folder select dialog.
    5. Click the OK button to close the edit item dialog.
  7. Select AVR Header Files and click the Edit button.
    1. Set the Path Source to "Custom"
    2. Click the Browse button. Use %SDKPATH%\hardware\tools\avr\avr\includeas the path.
    3. Click the OK button.
    4. Click the OK button.
  8. Select AVRDude and click the Edit button.
    1. Set the Path Source to "Custom"
    2. Click Browse.
    3. Navigate to the %SDKPATH%\hardware\tools\avr\bin directory.
    4. Click the OK button to close the folder select dialog.
    5. Click the OK button to close the edit item dialog.
  9. Click the Apply button.
  10. Click the OK button to close the Preferences dialog.

Creating The Arduino STD Library

By default the Arduino SDK does not include a standard library. Instead one is built on the fly when you use the Arduino IDE. We need this library. The library itself consists of a single file that will be static linked into our executables. This section will guide you on creating this library and placing it in a location the linker will be able to find.

  1. In Windows Explorer open the %SDKPATH% folder.
  2. Run the arduino.exe file you find there.
  3. Fill in the following as the sketch:
    void setup(){} void loop(){}
  4. Click the Verify button.
  5. Close the IDE, do not save the sketch.
  6. In the location bar in Windows Explorer, type: %TEMP%
  7. Find a directory called "build" followed by a bunch of random letters and numbers. For example: build6225406422258929134.tmp
  8. Inside this directory you will find a file called core.a, copy this file.
  9. You will need to paste the into the %SDKPATH%\hardware\tools\avr\avr\lib\directory.
  10. Give core.a a more linker friendly name by renaming it to libcore.a.

Create A Template Project

There are many build settings that you have to set in order to get a valid Arduino executable. To simplify the process, we are going to create a template project you can then later copy the settings from. This project will host all of the needed settings to get Eclipse to talk to Arduino.

  1. Click File → New Project in the menu bar.
  2. Select a C++ Project
  3. Give the project a name, e.g. Template, just be sure not to use any spaces or hyphens in the name.
  4. Set the Project Type to "AVR Cross Target Application"
  5. Click the Next button.
  6. Debug symbols are not currently supported. Hence, uncheck the Debug configuration, then click the Next button.
  7. These next settings define what type of microcontroller you have. That means these settings depend on what type of board you have.
    1. Go to the Arduino Hardware site.
    2. Select the board that matches yours.
    3. Look for the "Summary" section on the page. You need to find the Microcontroller item. Set the MCU Type in Eclipse to the microcontroller that is the closest match to the one listed on the website.
    4. In the "Summary" section there is a clock speed listed there. Set the MCU Frequency item in Eclipse to that value. Note, Eclipse uses hertz for the number. For example, for a clock speed of 16Mhz you will need to enter 16000000.
  8. Click the Finish button.

Defining The Project Properites

Getting Eclipse to build and flash an Arduino board is quite complex. We basically need to override almost all of the build settings so that they use our SDK. Follow the directions below quite carefully.

  1. Select the Template project item in the Package Explorer. Then right-click and select Properties.
  2. Open the AVR tree. Select the AVRDude item. These steps only ever need to be done once. They should then be used for every other project automatically.
    Note, there is a bug in the plug-in where it may give you an error and not show the Apply button when the dialog goes directly to the AVRDude item. To work around this, first click on any other item in the Properties dialog. Close the dialog, then reopen it. Click on the Target Hardware item in the AVR tree, then click on the AVRDude item.
    1. Click the New button on the Programmer tab.
    2. Change the Configuration name to Arduino.
    3. Set the Programmer Hardware to "Arduino."
    4. Start the Arduino IDE. Set the com port to what it says at the bottom of the IDE. Eclipse uses a strange syntax for this. For example, you would enter \\.\COM3 if your board uses the COM3 port.
    5. Set the Override default baudrate to 115200.
    6. Click the OK button to close the dialog.
  3. Now select the Arduino programmer from the drop down box.
  4. Select the tab labeled Other. In the other options field, put -v. This will turn on verbose output, and make any problems with flashing the board a little easier to discover.
  5. Click the Apply button.
  6. Open the C/C++ Build tree. Select the Settings item.
    1. For the Additional Tools in Toolchain item, check these:
      • Generate HEX file for Flash memory
      • Print Size
    2. Open the AVRCompiler tree. Select the Directories item. Add the following paths:
      • %SDKPATH%\hardware\arduino\cores\arduino
      • %SDKPATH%\libraries
      • There is one more directory you need. This directory contains thepins_arduino.h file. The location of this file is within one of the subdirectories inside the %SDKPATH%\hardware\arduino\variants\ directory. Which one to use depends on your board's setup. My Arduino Uno needs the "standard" pins file, so I would add the%SDKPATH%\hardware\arduino\variants\standard directory.
    3. Select the Optimization in the AVRCompiler tree. Make the following selections:
      • Set the Optimization Level to "Size Optimizations (-Os)"
      • Uncheck Pack Structs
      • Uncheck Short Enums
      • Add the these optimization flags: -ffunction-sections -fdata-sections
    4. Select the Miscellaneous item in the AVRCompiler tree. Just add --pedantic -c to the Other flags field.
  7. Make the exact same changes to the AVR C++ Compiler tree.
  8. Cick on the AVR Linker tree. Change the Command to avr-gcc
  9. Select the General item under the AVR Linker tree. Put the following in the Other arguments field: -Wl,--gc-sections
  10. Select the Libraries item under the AVR Linker tree. Add these libraries: c, m, printf_flt, scanf_flt, and core. You will need to add them one at a time.
  • Click the Apply button, then the OK button to close the dialog.
  • Eclipse does not always see the changes we made to the include directories right away. To fix this restart Eclipse.

Test Code

To check that we set all of the build properties correctly, we need to create a test application. We are going to create, build, and flash the Blink application from the Arduino tutorials.

  1. Right-click on the project and select New → File
  2. Set its name to main.cpp
  3. As an example, here is the code for blink:
    #include <Arduino.h>
    
    int ledPin =  13;
    
    void setup() {
      pinMode(ledPin, OUTPUT);
    }
    
    void loop() {
      digitalWrite(ledPin, HIGH);
      delay(1000);
      digitalWrite(ledPin, LOW);
      delay(1000);
    }
    
    int main(void) {
      init();
      setup();
    
      while(true) {
        loop();
      }
    }
    
  4. Now you need to run the program on the board
    1. Build the project.
    2. Select the project folder.
    3. Click AVR → Upload Project to Target Device from the menu bar.

Getting A Serial Console

The only way to communicate with your board is through the a serial console. In this section we are going to set up Eclipse to communicate with the board at run time, through the IDE.

  1. Make sure your board is plugged in. This process can crash Eclipse if it can't find your board.
  2. Click Window → Show View → Other in the menu bar.
  3. Scroll down and find the Terminal folder. Select the Terminal item in that folder. Note, sometimes there is another plug-in installed called Terminals. We want the one without the s.
  4. Click the OK button. This should have opened a new window down by the Console and Problems windows. Make the new window's tab active.
  5. Click the Connect button (it looks like a green sideways z inside the window).
  6. Make sure your board is plugged in and working!
    1. Change the title to Arduino Board
    2. Change the Connection Type to Serial.
    3. It probably autodetected the correct COM port, but if it did not put COM# (where # is the COM port's number).
    4. Make sure the Baud Rate is the same as what you set in Serial.begin().
    5. Click OK to close and save the changes.

If everything went okay, you should see any Serial.print() messages. To close the port, click on the Disconnect button (next to the Connect button).

Creating new Projects

To create a new project, you need to copy the settings from the Template project. Simply right-click on the Template project and select copy. Then right-click in the Package Explorer area, and select paste. Now just give it a name and you are done.

References