Skip to content

Setting up Eclipse for AVR Arduino

Ethan Slattery edited this page Dec 22, 2015 · 7 revisions

Since we all are CS students and have learned good programming practices it doesn't take long to outgrow the Arduino IDE. It is light and basic, but many of the things we count on are not possible.

  • Only compiles files that are physically open, making large multi-file projects difficult
  • no code completion or parameter assistance
  • compiler errors are hidden or just displayed in a small terminal window
  • code refactoring and highlighting

There are many options for development, but this article is about Eclipse since that is what everyone knows from the CS1 series. The steps here can be used for any environment you want because most of the work is setting up the AVR-C libraries and compiling the Arduino libraries, only a small part is eclipse specific. This is also specific to a Windows user, but if you follow the reference links and read through this it should be fairly trivial to make it work on Linux or OSX or any other IDE besides eclipse.

Refrences

Downloads

STEP 1 - Install all the things

  1. Install minGW:

    • Run installer and then pick the components to install.
    • Make sure to install the msys tools
  2. Install WinAVR:

    • Run the installer
    • I recommend that you install to default location C:\WinAVR-20100110\
    • Adding to PATH is optional and personally I did not choose this option. I already have msys and minGW in my PATH and AVR has the same tools so you will get conflict. If you install to the default location, then eclipse will know where to look anyhow.
    • WINDOWS 7+ 64bit (so most everyone) find the file msys-1.0.dll in your minGW installation and copy it into the WinAVR installation.
      • C:\MinGW\msys\1.0\bin to C:\WinAVR-20100110\utils\bin if everything is in default locations
  3. Install Arduino

    • Install or unzip, it doesn't matter. We are just going to steal the libraries later. I usually keep the latest one "installed" since there are some other things I use that know where to find it and use the tools if I do.
  4. Install Eclipse

    • I have heard that the AVR plugin likes to break eclipse sometimes. My eclipse isn't "installed" just unzipped into a folder. So if it breaks I will just delete that folder and re-create it from the downloaded zip file. FOR SURE do not use the same workspace for your AVR/Arduino projects and your class assignments. that is a recipe for disaster.
    • As a note: There is an Arduino Eclipse plugin, but I am not using this because it makes FAR more changes to the eclipse install and it is not necessary. By using the AVR plugin and knowing the settings, we can use a larger variety of hardware in the future and not hide the stuff we should learn (compiler settings, fuses, etc).
  5. Install the AVR plugin

    • I used the built in Eclipse addon manager, but you can also install it manually if you want
    • Follow the directions here as they are good, and I shouldn't repeat it all here.

STEP 2 - Setting Up Eclipse

  1. again, use a dedicated workspace for AVR, so you don't accidently muck up your schoolwork!"
  2. Open Eclipse, create a new workspace somewhere, we all know this
  3. Create a new C++ project, there should be a new option for AVR Cross Target Application using the AVR-GCC Toolchain
  4. Goto Window->Preferences inside Eclipse
    • Goto the AVR section and the subsection PATHS
    • Make sure these are correct! If you installed WinAVR to the default directory, then these should be ok.
    • hit Apply then OK
  5. Right-click on your project and open the Properties.
    • Goto the C/C++ Build Section and the subsection Settings
    • NOTE: setting here only affect the build config selected at the top, so make sure you know which one you are changing (preferably the Release config)
    • under Additional Tools in Toolchain make sure only the following are checked:
      • Generate HEX file for flash memory
      • Print Size
      • AVRDude (optional, this will make it so your HEX is auto-uploaded after every build. YMMV)
    • hit Apply
    • Goto the AVR section and the subsection AVRDude
      • Create a new Programmer Configuration, which open a large secondary window
      • Give it a good Name and Description (I called mine Arduino Duemilanove since that's the board I use. Other boards may need different settings, so it's good to name it appropriately.)
      • In the HUGE list of programmer Hardware choose Arduino, duh :)
      • Override Default Port : the COM port of your Arduino's (check in the device manager after plugging it in and letting drivers load)
      • Override default baud rate : 57600 for the Arduino Uno and Duemilanove. otherwise, you will have to look this up.
      • Everything else is OK so hit OK
    • Goto the AVR section and the subsection Target Hardware
      • Click LOAD FROM MCU and the proper chip should show up in the MCU type.
      • DO NOT manually enter this. If this fails, then there is either something wrong with your programmer setup in the last step or your environment is bad (WinAVR is not in the right path, etc.)
      • If you are using an Uno or Duemilanove the type should be ATmega328P, otherwise...
      • It will not read the clock frequency, though, so make sure to set this. The vast majority of Arduinos are at 16000000 (16MHz) but if you are not using an Uno or Duemilanove, look it up!
    • hit Apply and OK

STEP 3 - AVR Hello World!

  1. Create a main.cpp just like you would for a normal C++ project
  2. Enter the code for an AVR hello world, or a light blink!
#include <avr/io.h>     // Basic AVR library
#include <util/delay.h> // AVR delay library
#define LED PB5         // AVR code likes to use Macros a lot for readability

int main(void) {
 DDRB |= (1 << LED);  // define LED pin as output

 while (1) {
   PORTB |=  (1 << LED);    // switch LED on
   _delay_ms(1000);
   PORTB &= ~(1 << LED);    // switch LED off
   _delay_ms(100);
 }
return 0;
}
  1. Right-click on the project and goto Index -> Rebuild, to rebuild the index and have eclipse recognize the AVR specific code. Get used to this! seems like a common problem, especially later when you start importing Arduino libraries
  2. Build It! Errors here are likely due to faulty toolchain or tools. I spent over an hour troubleshooting make errors only to find out that the msys in winAVR is outdated and stealing the dll from minGW like you should have done in step 1.2
  3. If your Arduino board is not plugged in AVRDude will throw an error, which looks like a compilation error in eclipse. Just FYI, one reason I personally don't check AVRDude in step 2.5 for auto-uploading
  4. Notice in the console the size report (if you checked Print Size in step 2.5), important to keep an eye on as microcontrollers fill up very fast if you start using lots of variables and strings!
  5. Upload to the board using AVRDude either with the button on the toolbar or in the menu AVR -> Upload
  6. OMG BLINKY LIGHT!

STEP 4 - Arduino Libraries

Even though straigh AVR code is smaller, faster, and more useful long term the goal here is a soft transition. Arduino libraries are so darn useful! They abstract away a lot of the hardware specifics and there are a lot of projects out there using them. By importing them into eclipse and setting it up for AVR we can write pure Arduino code, pure AVR-C code and any hybrid in between.

The official tutorial here is pretty comprehensive, I will just add the specifics to what I learned. So I recommend opening this link in another tab/window!

  1. Method 1 is definitely the fastest and Easiest way to get the libraries! just open up the Arduino IDE and compile any old example. key thing to remember:
    • Don't close the IDE till you get the file! it will delete the temporary build directory.
    • MAKE SURE YOU SET THE BOARD the compiled core.a file is Arduino specific, so this is very important.
    • Organization is good: I created a C:\lib\ directory to keep all my libraries in.
      • Inside libs I have ArduinoCore and inside that a include directory for .h files and a 328P_16MHz folder for my libcore file that I renamed libArduinoCore.a
      • remember that the .a file is board specific so this organization lets me create a directory for each board type (Uno and Duemilanove use the 328P chip at 16MHz) making including the right one later easy.
      • Microcontroller use A LOT of libraries. I have another folder parallel to ArduinoCore called ArduinoLibs for those. you can just mix them in with the arduino core and I have seen some online do that. but the arduino core gets updated often and I want to be able to update it without a ton of confusion from extra .h files and such. this also allows me to grab libraries through cloning their git repo which makes keeping those libs up to date easy too!

STEP 5 - Arduino Style Blinky

  1. Create a new project, just like before.

  2. Follow the instructions on setting up the project. Notice it is similar to the settings we did for the AVR hello world, except additional setting in Optimization and Language Standard, and linking in the directories for the Arduino libs.

    • I DID NOT change the linker settings like it says, it said to try without the change first and I did. it works fine without for me.
    • Make sure to add -Wl,--gc-sections to your Other Arguments in AVR C/C++ Linker -> General settings. This helps reduce code size by throwing out sections of libraries you aren't using. space is at a premium here!
    • I did not create a symbol table. meh.
    • Realize that we already made a program without all these settings! this is a lot of optimization and such that will make Arduino code work better, and at first just do it all but it is good to start thinking about what each setting is doing and why.
  3. Create main.cpp and enter the code below (Arduino blink example)

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
}
  1. Compile and Upload! BLINKY!!!!
  2. Notice there is no main function! this is pure Arduino code, but now it is inside our nice IDE.
  3. FYI - Another painful learning moment but if you want to mix Arduino code with normal style C++/AVR make be aware that the Arduino preprocessor creates a file like this:
void main() {
   init();     // Initialzies arduino stuff
   setup();    // your setup loop
   while (1)   // forever!
       loop(); // your loop!
}

some but not all Arduino libraries require that init() call, and errors on a microcontroller are not fun! it just kinda sits there quietly and does nothing. The code below is an example of a hybrid Arduino/C++ program. I wanted to write in normal C++ style but use the Arduino libraries to blink my LED. notice the call to init() before anything else, and then what normally goes in setup() below it like a normal C++ program. specifically the delay() function crashes if there is no init() call.

#include "Arduino.h"

int main() {
  init();  //
  pinMode(13, OUTPUT);

  while(true){
    digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(1000);              // wait
    digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
    delay(500);               // wait
  }
  return 0;
}

STEP 6 - Template, other Libraries, etc

I suggest using the blink as a template since as you now know there are more than a few command line parameters to set up and the Arduino libraries to link in. Unfortunately I do no think eclipse has a built-in way to create a default project setting. Or you can just grab one from the tutorial

The tutorial also talks about possible issues. several I already talked about. check it out or google your error. avrfreaks, stack exchange, and Arduino message boards are a lifesaver.

The tutorial talks about using extra libraries. You will need this soon if you don't now. They just copy the files into the actual project where as I said before I keep everything in a centralized location. either way works.

Serial Terminal in Eclipse

In the Arduino IDE there is a built-in serial terminal. This can be very important since the easiest way to debug your programs is to print debug messages to the serial port and watch them in a terminal. You can use any terminal you want but sometimes it is nice to have the view IN eclipse. It is not hard to do, just make sure if you are on windows that you not only install the two eclipse plug-ins but ALSO grab the RXTS libraries and put them into your java JRE folders as described at the link below.

Good tutorial here, no need to re-write everything

Also, Be warned that you are not able to upload to the board while a serial terminal is connected. This is one reason it is nice to have it in elipse. There are less keystrokes to start and stop it when uploading.

Atmel Studio

Someday you will probably want to upgrade to the official IDE. It is big and heavy, based on visual studio, and windows only. BUT it is the only place you will get a decent debugger for the AVR chips. Where you can step through code and see the actual bits on a chip flip on a simulated device on screen. If you end up struggling to understand registers this can be very helpful. Setting it up with Arduino libraries is very similar to what we did here (another good reason to keep the libraries centralized!) and detailed here: Part 1 - Programming an arduino board in atmel studio Part 2 - Bringing the Arduino Core into atmel studio Part 3 - Using 3rd party libs in atmel studio

Clone this wiki locally