Developing for the Atmel AVR Microcontroller on Linux

You'll enjoy the programming ease and built-in peripherals of the new generation of microcontrollers. Best of all, with these tools you can develop for the popular AVR series using a Linux host.
GDB and SimulAVR

Using avr-gdb and simulavr in tandem, you can run your software on a choice of AVR microcontrollers through the simulator, while using GDB to step through and observe the executing code. Acquire the simulavr source from the project site and perform the installation:

$ ./configure --prefix=/usr/local/AVR \
  --with-avr-includes=/usr/local/AVR/avr/include
$ make # make install

Install GDB, built for AVR targets, by compiling the source as follows:

$ ./configure --target=avr \
     --prefix=/usr/local/AVR
$ make
# make install

AVRDUDE

When you finally have a program ready for testing on actual hardware, you need some way to upload the data and write it to the microcontroller's Flash program memory. AVRDUDE and a compatible hardware programmer are the last components of the development kit. Grab a copy of the AVRDUDE source and install it with:

$ ./configure --prefix=/usr/local/AVR
$ make
# make install

You now have installed every software component required for a complete AVR development environment. All you need is the physical means to transfer programs to microcontrollers.

AVRDUDE supports a number of different hardware programmer configurations. The simplest systems are described on the AVRDUDE site and are comprised of little more than a parallel port connector, a ceramic oscillator and a DIP socket. These are powered directly off the computer's port and may not work for everyone.

Figure 2. Home-Brew PPI Programmer PCB

A step up in complexity, independently powered, buffered in-system programmers can be built easily (see Resources). Two programmers requiring only a few parts are discussed on the Psychogenic Web page, which describes the schematics, provides artwork and has complete instructions on creating your own printed circuit boards (as depicted in Figure 2) for the programmers.

Figure 3. Atmel STK500 Development Kit

A number of commercial solutions also are available. If you're interested in easily programming a wide selection of the AVR family, go with Atmel's STK500 kit. More than a simple programmer, the STK500 is a starter kit that allows you to program the microcontrollers and easily prototype new designs. It includes a number of LEDs and switches, an oscillator, RS-232 interface and other niceties that easily can be interfaced with your target chip.

A Complete AVR Project

Our focus here is on the development system rather than on programming for the AVR platform. The AVR Libc documentation is a good place to start for information on programming AVRs in Assembly, C and C++.

The Hello World program of the microcontroller universe is the classic flashing LEDs. A slightly different take on this theme, which Knight Rider fans should appreciate, is available on the Linux Journal FTP site, where you can download C (Listing 2) or C++ (Listing 3) versions of an example program that cycles each of eight light-emitting diodes (LEDs) back and forth.

Building the Program

Create a project directory—for instance, ~/helloavr/—and retrieve the program, saving Listing 2 as ~/helloavr/kr.c and Listing 3 as ~/helloavr/kitt.cpp. Also, copy the Makefile template, /usr/local/AVR/Makefile.tpl, to ~/helloavr/Makefile.

Using this Makefile is easy and makes compilation a snap. Open the Makefile in your favourite editor and modify the configuration section, near the top of the file, so that the MCU, PROJECTNAME and PRJSRC variables are set as shown in Listing 4. The MCU variable determines the AVR family member for which we are compiling the program, and the PRJSRC variable lists all the Assembly, C and C++ source files used in the project.

Once you've configured the Makefile, compiling and linking the program is as simple as typing make.

You can perform the compilation and linking steps manually instead, by issuing:


  $ avr-gcc -I.  -g -mmcu=at90s8515 -Os       \
             -fpack-struct -fshort-enums               \
             -funsigned-bitfields -funsigned-char \
             -Wall -Wstrict-prototypes -c kr.c

  $ avr-gcc -o helloavr.out kr.o

The most notable difference is the addition of the required -mmcu command-line argument, used to specify the target microcontroller. Either method compiles kr.c and creates the helloavr.out ELF-format program. This file cannot be executed on your development station but is used later during the debugging stage.

You also can build the C++ version of the program by doing a make clean, changing the Makefile PRJSRC variable to kitt.cpp and then issuing another make.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

AVR microcontrollers

tom11's picture

AVR microcontrollers is definitely user friendly since the combination of onboard reprogrammable Flash program memory and the in-system programming interface keeps the process of transferring software to the microcontroller simple and cheap.
______
texas criminal defense lawyer

Hi I run Ubuntu 8.10 but got

zainka's picture

Hi

I run Ubuntu 8.10 but got troubles when making bin utilities. Giving message "format not a string literal and no format arguments"

Tried to google but one of the thread said it would be dificult to omit. I guess they are wrong but how to solve it is out of my mind. I understand it have something to do with 8.10 using -Wformat=2 and that this flag may give false positives. Any sugestions???


libtool: compile: gcc -DHAVE_CONFIG_H -I. -I.././opcodes -I. -I. -I.././opcodes -I../bfd -I.././opcodes/../include -I.././opcodes/../bfd -W -Wall -Wstrict-prototypes -Wmissing-prototypes -Werror -g -O2 -c avr-dis.c -o avr-dis.o
cc1: warnings being treated as errors
avr-dis.c: In function 'avr_operand':
avr-dis.c:112: error: format not a string literal and no format arguments
avr-dis.c:152: error: format not a string literal and no format arguments
avr-dis.c:161: error: format not a string literal and no format arguments
avr-dis.c:172: error: format not a string literal and no format arguments
make[4]: *** [avr-dis.lo] Error 1
make[4]: Leaving directory `/home/zainka/Downloads/binutils-2.19.1/opcodes'
make[3]: *** [all-recursive] Error 1
make[3]: Leaving directory `/home/zainka/Downloads/binutils-2.19.1/opcodes'
make[2]: *** [all] Error 2
make[2]: Leaving directory `/home/zainka/Downloads/binutils-2.19.1/opcodes'
make[1]: *** [all-opcodes] Error 2
make[1]: Leaving directory `/home/zainka/Downloads/binutils-2.19.1'
make: *** [all] Error 2

AVR libc

FatsDominoTheory's picture

Maybe the configure process for AVR Libc has changed since this was written; "./doconf" is not present in the archive I downloaded.

The INSTALL file tells me to:

./configure --build=`./config.guess` --host=avr

To follow the directory conventions in this article, append that with a "--prefix" option like this:

./configure --build=`./config.guess` \
--host=avr --pref \
--prefix="/usr/local/AVR"

Then make it with:

make
make install

AVR STK500

marcus's picture

I have an AVR STK500 and I am using Ubuntu8.04. I am trying to download the update necessary to run the my AVR STK500 but I can't. Are these packages free?

Problem with optimization in the sample file kr.c from psychogen

mlcy's picture

I had a problem with gcc just skipping the busywait() function until i switched off optimization in the Makefile.
This resulted in all LEDs glowing since they were each turned on with approximately 30 clock cycles in between.

Lovely guide anyway!

Wrinkle (& resolution) using gcc-4.2.2 sources to build avr-gcc

Jason Clark's picture

I ran into a problem with the above instructions, using the gcc-4.2.2 sources. The build died with a long string of errors, starting with:

../../../gcc-4.2.0/libssp/ssp.c: In function '__guard_setup':
../../../gcc-4.2.0/libssp/ssp.c:70: warning: implicit declaration of function 'open'
../../../gcc-4.2.0/libssp/ssp.c:70: error: 'O_RDONLY' undeclared (first use in this function)

A little research showed this popping up for a few other folks trying to build cross-compilers with gcc 4.x. This message on the gcc-help list suggests that libssp is not likely to be needed when compiling for embedded systems, and suggests the expedient of disabling it during configure. The following worked for me:

$ ./configure --prefix=/usr/local/AVR \
        --target=avr --enable-languages="c,c++" \
        --disable-nls --disable-libssp
$ make
# make install

Simulavr

Anonymous's picture

Hi,

I am using simulavr version 0.1.2.2 in current Debian testing. In the article you stated, regarding output from simulavr, that "You should see a message to that effect, for example, writing 0xff to 0x0038." I don't get that output from simulavr, I only get the following message for each time through line 71:

Breakpoint 2, main () at kr.c:71
71 PORTB = ~currentValue;
(gdb) c
Continuing.
decoder.c:737: MESSAGE: BREAK POINT: PC = 0x00000045: clock = 194

Am I doing something wrong or could it be different versions of simulavr causing the problem?

Re: Simulavr

Anonymous's picture

Ok,

I figured it out. I had to use the following changes to the simulavr command in one terminal:

simulavr --gdbserver --device at90s8515 -X -P simulavr-disp

Before I ran this command in another terminal:

The -X -P simulavr-disp invokes the display program, that shows all the register info for simulavr, and puts it in a normal terminal window instead of an xterm.

Re: Simulavr

Anonymous's picture

The command I ran in the other terminal that I forgot to put above is the same as the one in the article:

avr-gdb -x gdbinit-helloavr

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix