0% found this document useful (0 votes)
157 views

About Software Tools

This document discusses setting up an AVR development environment on Linux using Eclipse. It provides instructions for installing AVR GCC, AVRDude, Eclipse, and the AVR Eclipse plugin. It also describes preparing a target hardware board with an ATtiny2313 microcontroller and connecting an Olimex AVR-ISP500 programmer. The document confirms that AVR GCC and AVRDude are functioning properly by compiling and programming a simple "hello world" program to the target board.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
157 views

About Software Tools

This document discusses setting up an AVR development environment on Linux using Eclipse. It provides instructions for installing AVR GCC, AVRDude, Eclipse, and the AVR Eclipse plugin. It also describes preparing a target hardware board with an ATtiny2313 microcontroller and connecting an Olimex AVR-ISP500 programmer. The document confirms that AVR GCC and AVRDude are functioning properly by compiling and programming a simple "hello world" program to the target board.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 40

I introduced

development environment for microchip PIC controllers on Linux in different article. Now
would like to write something about AVR development tools (on Linux) as well. A lot of
hobbyists are using excellent open-source AVR development software like gcc crosscompiler and avrdude downloader. They are all CUI-based software tools and it may be
hard for beginners like me to begin with, while GUI-based development is so easy.
In this article, I would like to explore installation of GUI-based avr-gcc on Eclipse
platform.

About software tools


Following software tools are used for development suite for AVR on Linux (Fedora 12).

AVR GCC
AVR GCC is a cross-compiler for Atmel AVR microcontrollers based on GNU C
Compiler. It is called "C compiler", but can also handle direct compilation from
assembler source files. Inside of it, GNU Assembler is invoked after the C
Compiler converts C source files into assembly language files, and then the
linker gathers those object files into a downloadable/executable binary file. The
following picture shows simplified process and relationship of each software and
input/output files.

AVR GCC toolchain (From AVR Freaks)


For more information on AVR GCC toolchain, you can check
AVR GCC/AVR GCC Tool Collection (AVR Freaks)
About details on AVR LIBC,
AVR LIBC User's Manual (http://www.nongnu.org/avr-libc/user-manual/)
will be helpful.

AVRDude
AVRDude is an open-source software to program Atmel AVR microcontrollers.
The original AVRDude is a CUI-based tool although various GUI versions have
been developed. In this article AVRDude is used from Eclipse to download
executable binary files to AVR MCU.
You can access AVRDude homepage at here:
http://savannah.nongnu.org/projects/avrdude/

Eclipse

Eclipse is well-known open-source software development platform. It is an


excellent Java development suite and a lot of plugins are available for
development with C/C++ or other languages. In this article I would like to install
Eclipse and a plugin called "AVR Eclipse plugin" to enable AVR software
development.
You can access Eclipse homepage at http://www.eclipse.org/

AVR Eclipse Plugin

"AVR Eclipse plugin" is open-source software to enable cross development of


AVR software on Eclipse. You can install it easily by using Eclipse's own update
feature. It becomes a very powerful AVR development tool as you can utilize
every Eclipse original feature. Only basic features are introduced in the article,
but it will help you a lot to maintain the project without writing makefile by
yourself.
You can check AVR Eclipse Plugin homepage at "The AVR Eclipse Plugin"

Install AVR-GCC and AVRDude


First, install basic AVR compiler-related packages and AVRDude from [Add software]
from Linux (Fedora 12) system menu. Following packages have been installed.

avr-binutils-2.20-2.fc12

avr-gcc-4.4.2-2.fc12

avr-gdb-6.6-11.fc12

avr-libc-1.6.7-2.fc12

avra-1.2.3-4.fc12

avrdude-5.10-1.fc12

Actually, that's minimum required software tools for development. You just need to write
a short script or makefile before you can start AVR software development on command
line. (You may want to add other useful programming/debugging packages, but we don't
use them this time.)

AVR GCC is ready?


Let's check if you can use avr-gcc with no problem. I write up a small, simple C source
file, useless but for testing. (It will continuously put out 8-bit data to port B.) Here, you
can compile it into a HEX file by typing short commands like the following example. The
example shows that avr-gcc compiles the source, links into an ELF binary, avr-objcopy
converts it into HEX, and finally avr-objdump puts out disassembly code for
confirmation. (Test of AVRDude is written later in the article with the preparation of
target hardware.)
C source file to check installation of avr-gcc (avrgcctest.c)

Example of manual build


$ avr-gcc -O2 -Os -mmcu=attiny2313 -o avrgcctest.elf avrgcctest.c
$ avr-objcopy -R .eeprom -O ihex avrgcctest.elf avrgcctest.hex
$ cat avrgcctest.hex
:1000000012C02AC029C028C027C026C025C024C0CD
:1000100023C022C021C020C01FC01EC01DC01CC0E4
:100020001BC01AC019C011241FBECFEDCDBF10E0F8
:10003000A0E6B0E0EAE6F0E002C005900D92A0363E
:10004000B107D9F710E0A0E6B0E001C01D92A036DC
:10005000B107E1F702D007C0D3CF8FEF87BB80E0B5
:0A0060008F5F88BBFDCFF894FFCF3F
:00000001FF

$ avr-objdump -d avrgcctest.elf

avrgcctest.elf:

file format elf32-avr

Disassembly of section .text:

00000000 <__vectors>:
0: 12 c0

rjmp

.+36

; 0x26 <__ctors_end>

2: 2a c0

rjmp

.+84

; 0x58 <__bad_interrupt>

4: 29 c0

rjmp

.+82

; 0x58 <__bad_interrupt>

6: 28 c0

rjmp

.+80

; 0x58 <__bad_interrupt>

----------------------------------------------------------(... continues until the end)

If your project has only one source file, that's all you should do (typing avr-gcc and avrobjcopy with some options) before making the final binary file.
Usually in CUI-based development everyone maintains projects by using "make" and
"makefile". However, it is sometimes bothersome for hasty beginners (for instance, the
author of this article) who want quick start of AVR development. Don't worry. Eclipse
helps you.

Install Eclipse platform


I'm using Fedora 12, in which all I did was installing the Eclipse package from repository
and Eclipse basic features were ready for Java and C/C++ development. If you are
using other distribution, some more steps might be necessary, but I believe that
installation of Eclipse and CDT would not be so difficult (I didn't confirm, though). On
installation of Eclipse, I didn't switch OpenJDK to Sun Java because there seemed to be
no problem at all for my purpose.
Please allow me to skip details about Eclipse and CDT installation as there are plenty of
good reference, how-to's and examples.
The version of the Eclipse I installed from Fedora repository was

eclipse-platform-1:3.5.1-22.fc12 (Galileo / 3.5.1)

After installation is done, start Eclipse to designate your workspace. No need to change
its location from default "workspace" although I did change it to "eclipse_workspace"
because I already had a directory with the same name.

Now let's check if Eclipse CDT is ready. When you install a new development tool, it
may be a ritual step to build "Hello, world" project. That act seems waste of time, but it is
important to see if Eclipse and GCC are working correctly. You can quickly create and
build "Hello, world" project only with few steps.
1. Start Eclipse, and from its menu select [Window] [Open perspective]
[Other...] [C/C++]
2. [File] [New] [C Project] to launch the project wizard.
Fill in/choose settings like following;
[Project Name]:
[Project type]:
[Toolchains]: Linux GCC
3. [Next] [Finish] and done
Very quick!

Hello
[Executable] [Hello World ANSI C Project]
Linux GCC

Eclipse C/C++ "Hello world" (click to enlarge)


That is the example of Eclipse and GCC to build projects for PC software. Next, let's
install "AVR Eclipse plugin". It will allow you to build projects for Atmel AVR by adding
avr-gcc toolchain to Eclipse.

Install AVR Eclipse plugin


Following instructions at homepage of AVR Eclipse plugin, install from Eclipse's update
feature. The example shows the steps on Eclipse 3.5 (Galileo), and note that other
versions may have different menu or dialog labels.
1. Feature update
From menu of Eclipse, [Help] [Install New Software] to open [Install] dialog.
2. Add download site

By click of [Add...] button beside [Work with] box, and add new update site for
"AVR Eclipse plugin" as a download source.
(Note: The homepage didn't write about installation steps on Galileo (Eclipse 3.5
series). I tried the URL for 3.4 or 3.3 (http://avreclipse.sourceforge.net/updatesite/), and it seemed OK as Galileo recognized the
correct location.)

Eclipse feature update


3. Select AVR Eclipse Plugin
Select [AVR Eclipse Plugin] below [CDT Optional Features], and start
installation by following instruction of dialogs. When finished, restart Eclipse.

Install Eclipse AVR Plugin


4. Confirmation
After restart of Eclipse, switch perspectives to [C/C++], then you can see a new
menu item and a button for AVR.

Eclipse AVR Plugin: Button and Menu

That's the end of all installation steps (Installed version was "2.3.4.20100807PRD").
Now, we prepare a target hardware and create new project for it.

Prepare the target


Programmer
My AVR programmer in the article is "Olimex AVR-ISP500".
Reference: Olimex AVR-ISP500 (http://www.olimex.com/dev/avr-isp500.html)

Olimex AVR-ISP500
This programmer can perform only programming by ISP. I can connect it directly to a
USB port and use a serial port without additional drivers. According to the manual,
AVRStudio and AVRDude recognize it as "STK500 version2 firmware".

AVR-ISP500: ICSP6 connector


ISP lines are available through 2x5 or 2x3 connector. I'm using 2x3 type connector for
my target. The connector provides common SPI lines, supply lines and a reset signal
(see schematic for detail).

Target hardware
I prepared this small target for the article by using ATtiny2313 and LED's on bread
board.

Target schematic
Simply, eight LED lamps are connected to port-B and two LED's to port-D. ISP signals
are also connected to the connector. The system clock is from the internal RC oscillator.
Supply voltage is 3.0V from two AA batteries (shown as 3.3V in the schematic).
For details about ATtiny2313, please see Atmel ATtiny2313 page
(http://www.atmel.com/dyn/products/product_card.asp?part_id=3229).

Target hardware
I made a simple connection board to connect ISP signals to bread boards.

ISP connection board

Testing AVRDude
AVR Eclipse plugin communicates with target hardware with assistance of AVRDude.
Before using Eclipse, let's confirm if AVRDude is able to talk with your target hardware.
The following example shows the result of my test with Olimex AVR-ISP500. In the
example, I confirmed valid connection to the target and then programming the hex file
which is compiled during the test of avr-gcc. You may need to change some AVRDude
options to test with your own target and programmer.

Programmer (-c option)


Port (-P option)
Target device (-p
option)

"stk500v2" (STK500 version2 compatible)


"/dev/ttyACM0" (check availability by "ls
/dev/ttyACM*")
"t2313" (ATtiny2313)

$ ls -l /dev/ttyACM*
crw-rw----. 1 root dialout 166, 0 2010-09-03 08:58 /dev/ttyACM0

$ avrdude -c stk500v2 -P /dev/ttyACM0 -p t2313

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.03s

avrdude: Device signature = 0x1e910a

avrdude: safemode: Fuses OK

avrdude done.

Thank you.

$ avr-gcc -O2 -Os -mmcu=attiny2313 -o avrgcctest.elf avrgcctest.c


$ avr-objcopy -R .eeprom -O ihex avrgcctest.elf avrgcctest.hex
$ avrdude -p t2313 -c stk500v2 -P /dev/ttyACM0 -U flash:w:avrgcctest.hex:a

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.03s

avrdude: Device signature = 0x1e910a


avrdude: NOTE: FLASH memory has been specified, an erase cycle will be
performed

To disable this feature, specify the -D option.


avrdude: erasing chip
avrdude: reading input file "avrgcctest.hex"
avrdude: input file avrgcctest.hex auto detected as Intel Hex
avrdude: writing flash (106 bytes):

Writing | ################################################## | 100% 0.37s

avrdude: 106 bytes of flash written


avrdude: verifying flash memory against avrgcctest.hex:
avrdude: load data flash data from input file avrgcctest.hex:
avrdude: input file avrgcctest.hex auto detected as Intel Hex
avrdude: input file avrgcctest.hex contains 106 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.31s

avrdude: verifying ...


avrdude: 106 bytes of flash verified

avrdude: safemode: Fuses OK

avrdude done.

Thank you.

If you have no idea what options are good for your target, AVRDude can show available
choice to you. AVRDude will put out list of supported programmer or device names
when invalid names are given. You can look for a good option string for your settings by
trying with improbable names (for example, "avrdude -p foobar", "avrdude -p stk500v2
-c foobar".
Note:
AVRDude communicates with AVR-ISP500 programmer via USB CDC port
(/dev/ttyACM*). For programmers to communicate through a USB serial port, the user

needs to obtain read/write access permission to the device file. If you don't have proper
access rights, you need to add your account to the appropriate group who owns access
permission to the device file (In Fedora 12, /dev/ttyACM* files are available to the group
of "dialout").

Create AVR GCC project


Every necessary tools are ready now. Let's start making new AVR project on Eclipse.

Start New project (AVR-GCC toolchain)


1. Create new C Project
From Eclipse(C/C++ perspective) menu, [File] [New] [C Project] to launch
the project wizard.
2. Select AVR GCC toolchain
Among the choice of [Project type], select [AVR Cross Target Application]
[Empty Project]. [Toolchains] will be [AVR-GCC Toolchain].

Creating AVR Eclipse C project (1)


Fill in [Project name], and click [Next]. You will see "Debug" and "Release" build
configurations are made. After confirmation, click [Next].
3. Setting target device
Next, select target device.
Select device name in [MCU Type] (in my case [ATtiny2313]), and fill in system
clock frequency in [MCU Frequency(Hz)] (in my case [1000000], meaning the
default setting of 1MHz; internal 8MHz RC oscillator divided by 8). And then click
[Finish] to create the project.

Creating AVR Eclipse C project (2)

Making source files


1. Add source files
From [Project Explorer] at the left of the screen, select the newly created project.
And then from the menu, choose [File] [New] [Source File] to add your
source file. Note that C-language source files should have trailing ".c" and
assembly source files should have trailing ".S" in their file names.

Add AVR Eclipse C source file (1)


2. Writing source code
Open the new source file and start writing your own code. Eclipse will support
convenient programming in C language. In my example, the software uses a
timer0 interrupt to blink LED connected to the port.

Add AVR Eclipse C source file (2)


Blink LED: source code

Project settings
1. AVR programmer settings
From Eclipse menu click [Project] [Properties] to open the setting dialog, and
then from [Resource] in the left choose [AVR] [AVRDude] [Programmer].
There will be no available programmer settings unless you make one. Click
[New] button to make it for your own programmer.

AVR Eclipse : Setting AVRDude


2. Settings for AVR-ISP500
Fill in following information to use Olimex AVR-ISP500 from Eclipse. The
example should be changed for your environment. Please refer to the user
manual to know what values should be set for your programmer.

Eclipse: AVRDude: Setting AVR-ISP500 programmer


Configuration name:

Name of the settings. Should be easy to be identified.

Description:

Description of the settings. Should be concise to know


what it is for.

Programmer Hardware (c):

Choose programmer for this settings.

Override default port (-P):

Port name where the programmer is connected. ex:


dev/ttyACM0

Once you created programmer settings, you can select it again in another new
project. If you're using several different programmers, you need to create
different settings for each.

Select configuration
1. Select build configuration
Choose "Debug" or "Release" configuration to build. From the menu, go to
[Project] [Build Configurations] [Set Active], and choose [Debug] or
[Release].
If you build without changing those configurations, you may encounter a problem.
In "Debug" build, the last target is set to ELF binary and no downloadable HEX
files are generated. In "Release" build, it generates two HEX files for both Flash
memory and EEPROM while not every project needs EEPROM binary.
2. Change build settings
Choose configuration you want to change (here I explain the steps for Debug
build, but almost same for Release build). From the menu, go to [Project]
[Properties] to open the setting dialog. From [Resource] in the left side, choose
[C/C++ Build] [Settings]. Make sure that [Configuration] is pointing "Debug
[Active]" in the right top of the dialog, and then choose [Tool settings] tab and
[Additional Tools in Toolchain] to change settings to generate each HEX files.
You can enable or disable HEX files as you need.

AVR Eclipse: Setting of HEX files


You can also change other compiler options there. From [Resource] in the dialog,
choose [AVR Compiler] to see settings for avr-gcc. The example shows
optimization settings for smaller code size (-O2 -Os). Other tools (like assembler,
linker or avr-objcopy) are also available in the dialog for optimized settings.

AVR Eclipse: Settings for optimization

Build and Download


1. Build
After you completed and saved the source code, you can start build the project
from menu by choose [Project] [Build Project].
2. Download and Run
After the build is done with no errors, you can program your target from menu by
choose [AVR] [Upload Project to Target Device] (or "AVR" button).
Congratulations! That's the end of the steps to start up your AVR project. After that, you
will continue testing and debugging until your project is working successfully.

Appendix
About Fuses
To change fuse settings you need to specify direct hex values when using AVRDude
CUI. It is very stressful process because it may cause malfunction of target devices, or
at worst make it unusable. AVR Eclipse plugin helps you change fuses very easily.
1. Enable Fuse writing feature
After setting up Eclipse so that you can program your target device, go to
[Project] [Properties] dialog, choose [AVR] [AVRDude] and open [Fuses]
tab. Turn on the option of [direct hex values] in [Upload Fuse Bytes].

2. Read in current Fuses


Click [Load from MCU] button to read in current fuse values already programmed
in the device.

3. Start modifying fuses


Launch the fuse editor by [Start Editor] button.

4. Programming new fuse values.


Fuse editor shows current values in the device. You can change each fuse
values as you need. Be careful when editing, because a wrong fuse value may
disable ISP programming or even break the target.

Here let's try new values for CKDIV8 fuse bit to disable system clock prescaler.
Turn CKDIV8 "Yes" "No", which will change the main system clock to 8MHz,
currently 1MHz.

After completing new fuse values, close the fuse editor and then dialog by
clicking [OK]. During next downloading to the target, the new fuse values are
updated into the device, and it will start with new settings. In the example above,
the main system clock increased from "1MHz = 8MHz / 8" to "8MHz = 8MHz / 1"
and the speed of blinking LEDs.
5. Disable fuse writing feature
After setting fuses, disable the feature by the same dialog.

Reference

AVR Libc manual


How to write interrupt routines by avr-gcc (avr-libc)
Interrupts (http://www.nongnu.org/avr-libc/usermanual/group__avr__interrupts.html)

Avr Wiki: "Using Timer/Counter0" (Japanese)


I studied how to use timer/counter0 interrupt by avr-gcc with this reference. My
example in the article has almost same code structure with their examples. The
content shows many helpful examples like other interrupt usage, assembly

language usage etc.


Getting Started Notes - Timer0

So youve read my blog post Setting up Eclipse for AVR projects and now you want to write some code
but youre somewhat bewildered by all the Eclipse options.
Well not to worry, Ill walk you through your first project step-by-step. Well go ahead and recreate the
infamous Arduino blink example that simply flashes the LED thats attached to digital pin 13.
By the time youve run through this tutorial you will know how to create projects, add C++ files to your
project, build it and upload it to your Arduino. Even more importantly I will show you how to use all the
convenient functions of the Arduino library such as digitalWrite, digitalRead and the Serial class.

Prerequisites
You need to install my compiled Arduino libraries for Eclipse users. You can get them here, all nicely
packaged with a Windows installer.

Create a new project


Fire up Eclipse and Choose File->New->C++

project. You will get a form like this one:

Click image to magnify it.


Enter the project name, blink into the top field, ensure that the project type and toolchain fields are
selected as in the above image and click Next.

Click image to magnify it.


This Wizard screen allows you to change the project configurations. Eclipse allows you to maintain
independent build settings for different named configurations. By default you get Debug and Release
settings. These are appropriate for projects that target desktop computers but have no relevance for AVR
programming. Leave them as-is for now because we will make some suitable changes later.

Click image to magnify it.


Here we need to make some changes because the selected micro-controller is not the one on-board the
Arduino. Im going to assume that youre using a Duemilanova or Uno board with an ATmega328P.
Choose the MCU type from the drop-down and set the speed to 16000000 (be careful to get 6 zeros).
Click Finish and Eclipse will create your project.

Build configurations
This step is optional but you really should get used to doing it because your project will be a lot cleaner
and extendible should you decide to port to another AVR MCU.
Select the Project->Build Configurations->Manage menu item. You should get the following
screen:

Click on the New button. You will get the following screen. Fill in the fields as in my example.

Click image to magnify it.


I am using the build configuration name to identify the MCU and the frequency that I will be targeting with
this build.
Back at the Manage Configurations form, delete both the Debug andRelease configurations
because they are no longer needed. When youre done, come out of the form and back to the Eclipse
workspace.

Arduino library settings


Navigate to this blog post and follow the directions for setting up the compiler and linker settings. When
youre done, come back here.

Compiler and linker optimal settings


Navigate to this blog post and make the changes to the project settings that I describe. Doing this ensures
that you get the best possible code generation. Come back here when youre done.

Additional project settings


We need to make a few more small changes to the project settings to ensure that AVRDude knows which
device configuration to use, and so that your project actually outputs a .hex file to upload because by
default it will not do that important step.
Right-click on the project name in the Project Explorer and selectProperties. Select AVR>AVRDude from the menu on the left. You will get a screen like this one.

Click image to magnify it.


Ensure that Arduino is selected in the dropdown menu. If you do not have an Arduino option in that
menu then you have not followed the instructions in my blog post Setting up Eclipse for AVR projects.
Now select C/C++ Build from the menu on the left. The panel should default to the Additional Tools in
in Toolchain options shown below.

Click image to magnify it.


Ensure that the Generate HEX file for Flash memory button is checked.

The Project Explorer


The Project Explorer on the left is the main navigation window for your project files. If you have a play
around with it youll see that right away Eclipse has gone off and discovered the system include files and
made them available for browsing in the project explorer.

Click image to magnify it.

Add a new source file


We need to add a C++ source file where we will write the code to blink the LED. From the main Eclipse
window select File->New->Source File. You will get this form:

Click image to magnify it.


Fill in the fields to create a file called main.cpp in the blink folder and hit OK. Eclipse will create the file
and open it for you, ready for editing.

main.cpp
Here is the source code to our little example, go ahead and copy it from this page into your main.cpp file
and then save it.
01 /*

02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

* main.cpp
*
* Created on: 17 Oct 2010
*
Author: Andy
*/
#include <wiring.h>
extern "C" void __cxa_pure_virtual() {
for(;;);
}
int main(void) {
init();
setup();
for(;;)
loop();
return 0; // not reached
}
void setup() {
// setup pins
pinMode(13,OUTPUT);
}
void loop() {
digitalWrite(13,HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(1000);
}

Lets walk through this short code fragment to see whats going on.
8 #include <wiring.h>
wiring.h is a header file in the Arduino library. It contains the definitions for pinMode, digitalRead and
digitalWrite. If we didnt include this file then those useful functions would not be available to us.
10 extern "C" void __cxa_pure_virtual() {
for(;;);
11
}
12
This somewhat obscure piece of code is just boilerplate stuff. You dont need to know what its doing but
you do need to include it exactly once per AVR project that you create. For those that really must know,
this is the function that will get called if you screw up and call a virtual function at a point in the code
where the virtual function table has not yet been created, such as in a constructor. Since this is a hard
error that cannot be recovered from it is OK to just go into an infinite loop awaiting a reset.
14 int main(void) {

15
16
17
18
19
20
21
22

init();
setup();
for(;;)
loop();
return 0; // not reached
}

The main function is the entry point to every C++ program. If youve only been using the Arduino IDE up
until now then you wont have seen it because the IDE hides it by including it in the library code. If you
delve into the library source code you can find it and youll discover that it looks exactly like this.
init() can be found in the wiring.c library file and is responsible for setting up the interrupt that maintains
the timer. Fail to call this and timing functions such as millis() will always return zero.
You are expected to supply implementations of setup() and loop(). No doubt you are already used to this.
I wont bother explaining what setup() and loop() actually do since they are about as basic as they could
possibly be.

Build it
Select the Project->Build Project menu item or just press Ctrl-B. If all is well then the you will get lots
of feedback in the Console window down at the bottom of the workspace. Heres what I get when I build it:
**** Build of configuration ATmega328P-16 for project blink ****

make all
Building file: ../main.cpp
Invoking: AVR C++ Compiler
avr-g++ -I"D:Program FilesArduino Libraries020stdinclude" -Wall -g2
-gstabs -O0 -fpack-struct -fshort-enums -funsigned-char -funsigned-bitfields
-fno-exceptions -mmcu=atmega328p -DF_CPU=16000000UL -MMD -MP -MF"main.d"
-MT"main.d" -c -o"main.o" "../main.cpp"
Finished building: ../main.cpp

Building target: blink.elf


Invoking: AVR C++ Linker
avr-g++ -Wl,-Map,blink.map,--cref
-L"D:Program FilesArduino Libraries020stdlib" -mmcu=atmega328p
-o"blink.elf"

./main.o

-lard_0020_atmega328P_16

Finished building target: blink.elf

Invoking: AVR Create Extended Listing


avr-objdump -h -S blink.elf >"blink.lss"
Finished building: blink.lss

Invoking: Print Size


avr-size --format=avr --mcu=atmega328p blink.elf
AVR Memory Usage
---------------Device: atmega328p

Program:

1212 bytes (3.7% Full)

(.text + .data + .bootloader)

Data:

9 bytes (0.4% Full)

(.data + .bss + .noinit)

Finished building: sizedummy


Any errors will be reported in the console and Eclipse will helpfully place a red cross icon to the left of the
offending line. It will also place a red-cross over the name of the file in project explorer.

Upload it to your Arduino


After your successful build you need to upload it to your Arduino for testing. To do this, select the AVR>Upload Project to Target Devicemenu option. Watch the console for feedback as AVRDude
performs the upload.

Setting Eclipse
For many hobbyists the first AVR integrated development environment (IDE) that they will encounter will
be the Arduino IDE.
The Ardunio IDE goes out of its way to make it easy for you to quickly bash out test code and get it up
and running as fast as possible without having to worry too much about whats going on in the
background with compilers, linkers and the avrdude upload software. This is great and I still use it
regularly to try out quick what if scenarios. However, the Arduino IDE is completely unsuitable for real
project development due its ultra-basic multi-file support, complete lack of access to compiler options and
an editor that Im going to be polite and call limited.
For real work you will need to move on to either a command-line/makefile environment or choose a
compatible IDE.
Eclipse is a full-featured cross-platform IDE that has been around for a long time now. Originally written
for java programmers, it has been extended over the years to include a perfectly competent C/C++
environment. Best of all its completely free. The rest of this article will give you step-by-step instructions
on how to get Eclipse up and running for AVR development on a Windows computer. My environment is
Windows 7 x64 but the instructions should be portable to XP and Vista.

Step 1. Install a JRE


Skip this step if you know you already have a Java Runtime Environment (JRE) on your PC. Otherwise
surf to the java.com website and install the free JRE. The reason that youre doing this is because Eclipse
is written in java and needs a java virtual machine to run.

Step 2. Download and install Eclipse CDT


Take yourself off to the downloads page at Eclipse.org and download the latest version of the Eclipse IDE
for C/C++ Developers. It comes packaged in a zip file containing a folder called eclipse. There is no
fancy installation program, just drag the eclipse folder out of the zip file and into your Program Files
directory.
When thats done you will probably want to create a shortcut to the eclipse.exe program that you will find
in the eclipse directory that you just created.

Step 3. Download and install WinAVR


Eclipse knows how to organize and edit C/C++ files but it does not have a clue how to compile them, and
it does not come with all the header files and libraries that you need to be an effective AVR developer. For
this, we need WinAVR. So off you go to the WinAVR download page and install it. This one comes with its
own installer so you should have no problems installing it.
Heres a tip: dont be tempted to install it into a directory that has spaces in its name such as Program
Files. The tools it installs are ported from Unix and Unix tools are notoriously incompatible with directory
names that have spaces in them. Just accept the default suggestion and youll be fine.

Step 4. Install the Eclipse AVR plugin


Launch Eclipse using the shortcut to eclipse.exe that you created in step 2. If this is the first time that you
have launched Eclipse then it will ask you for a workspace location. This is the default location in which
Eclipse will create projects. Change it if you want, or just accept the default.
Go to the Help->Install New Software option and enter http://avr-eclipse.sourceforge.net/updatesite into
the Work with: field at the top. Hit enter and you should have a screen that looks like this.

Click image to magnify it.


Check the boxes as shown in the image above and then just go through the wizard until youve got the
plugin installed. Eclipse will want to restart at the end. Let it.

Step 5: Configure AVRDude


AVRDude is the program that we use to take your compiled masterpiece and upload it to the AVR board.
In this step we will assume that you are using an Arduino board and will show you how to configure
AVRDude in Eclipse to talk to it.
You will need to know which serial port (COMx) that your Arduino is plugged into. If you dont know, then
switch it on now and fire up the Arduino IDE. The IDE shows you all available serial ports under theTools>Serial Port menu option. It should be obvious which one is your Arduino.
Back to Eclipse

Select the Window->Preferences menu option. Expand the AVR option on the right and click on
AVRDude. The AVR plugin allows you to have multiple AVRDude configurations and we are going to
create just one for your Arduino board.
Click on the Add button and youll get a window like this one.

Click image to magnify it.


Fill in the fields like Ive done. Of course you dont have to use the same name and description as I have
but do note the syntax for entering the COM port (//./COMx) and the baud rate of 57600. I may be the odd
one out here, but on my system sometimes Windows gives me COM3 and sometimes COM4. Its highly
annoying because each time it does the port flip I have to come back here and change the AVRDude
configuration to match. Bear that in mind if you suddenly find that you cant upload code to your board
that was working perfectly well the previous day!
When youre done click OK to add this configuration to your list.

Step 6: Check the paths to the WinAVR tools


Select the Window->Preferences menu option. Expand the AVR option on the right and click on Paths.
You should get a window like this:

Click image to magnify it.


The AVR plugin automatically configures itself with the relevant paths but do just eyeball them to make
sure they look OK.

Thats it!
Congratulations you are now ready to create your first AVR project using an enterprise-class development
environment

You might also like