About Software Tools
About Software Tools
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.
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.
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
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-objdump -d avrgcctest.elf
avrgcctest.elf:
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>
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.
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
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.)
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.
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".
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.
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.
$ ls -l /dev/ttyACM*
crw-rw----. 1 root dialout 166, 0 2010-09-03 08:58 /dev/ttyACM0
avrdude done.
Thank you.
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").
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.
Description:
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.
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].
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
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.
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.
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
./main.o
-lard_0020_atmega328P_16
Program:
Data:
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.
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.
Thats it!
Congratulations you are now ready to create your first AVR project using an enterprise-class development
environment