Thursday, May 5, 2011

[Winavr]+[Eclipse IDE]

Probably many of us (including me) are using Programmers Notepad or AVR Studio to set up AVR projects. Each of them has advantages and disadvantages. For instance WinAVR comes with Programmers Notepad GUI by default. It is very powerful editor, but there are lots of manual routines required to start compiling project – like setting up makefile, creating file dependencies, etc. AVR Studio is a great solution which generates makefiles automatically, and it has great simulator for immediate debugging. So why would we need another IDE? Actually Eclipse IDE is one of the best open source tools that is widely used by programmers. Eclipse has some nice features that makes it attractive like code writing with auto-complete functionality,  optimized for managing projects, subversion integration, code completion in editor.  So why not to give a try for it.

Download and Installation 
First of all make sure you have latest WinAVR tools installed. Latest WinAVR can be downloaded here and need to be installed.

Eclipse IDE is a Java based IDE, so we also need Java Runtime 5 (or higher). Java Runtime can be downloaded here and need to be installed. Then download Eclipse IDE for C/C++ for Windows platform  from here. Eclipse is self contained (does not require installation) and only needs to be unpacked (e.g. c:\eclipse).

Now you need to download AVR Eclipse plugin from here. This plugin is self contained (does not require installation) and only needs to be unpacked in Plugin folder (e.g. c:\eclipse\plugin)


How to run Eclipse IDE
Run Eclipse by double click.

Following screens show that everything gone fine.



This is default location of the workspace, press OK button. Following welcome page indicates that Eclipse IDE is working and we can start our winavr projects.

Dismiss the welcome page by clicking the cross on the tab to reveal the actual work environment. Now we can start new project.

First project with code
To start a new project go to File->New->C Project or C++ Project. Select Empty Project in AVR Cross Target Aplications and enter Project name which also serves as new project folder in desired location. In my case my project name is test.project01.


After pressing Next you will have to select configurations weather you want to debug or generate hex file. Lets leave both (debug and release) configurations selected.
Click Next..

Here we must select AVR microcontroller and its clock frequency. After selections are done click Finish button to prepare new project. Now our project is set up we can start writing program for our AVR. To add files to project we can create new or add existing source files. Simply go to File->New->Source File to add blank C or C++ file. We can use drag and drop feature to add files to project tree. Files also are copied to project folder physically.

How to compile
To compile the source code go to Project->Build Project or press following button in Eclipse IDE.

After compiling the few lines of source code I noticed that it is 21.5% full,

so we need to reduce the size of the compiled file with the help of following  settlings in Eclipse IDE. These settings will reduce the compiled file size dramatically to 1%.

Optimization to reduce code size
For optimization we have to select Project->Properties. Then select C/C++ Build->Settings in the left pane. Select Additional Tools in Toolchain in the right pane. Check Generate HEX file for Flash memory. Check Print Size.

Select AVR Assembler and then Debugging. Set Generate Debugging Info to No debugging info

Select AVR Compiler and then Debugging. Set Generate Debugging Info to No debugging info

Select AVR Compiler and then Optimization. Set Optimization Level to Size Optimizations (-Os)

Select AVR C++ Compiler and then Debugging. Set Generate Debugging Info to No debugging info

Select AVR C++ Compiler and then Optimization. Set Optimization Level to Size Optimizations (-Os)

Set Other Optimization Flags to following (this is to reduce hex file size):
-ffunction-sections -fdata-sections -Wl,--gc-sections

Select AVR C/C++ Linker.In the linker menu, change command line pattern to the following:
${COMMAND} --cref -s -Os ${OUTPUT_FLAG}${OUTPUT_PREFIX}${OUTPUT} ${INPUTS} -lm ${FLAGS}