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.
Debugging the Program

A Makefile target that is interesting, whether for sanity checking, optimization, low-level debugging or simply to get to know the AVR internals, is disasm. Running: $ make disasm prints some information concerning the program, such as its text/data/bss size, to the console and creates helloavr.s. This file contains a disassembled version of the executable, intermixed with the original C source code. A peek inside reveals AVR Libc and avr-gcc's work behind the scenes, initializing the interrupt vector table and data, followed by the Assembly and C versions of the program.

Now we use GDB as a source-level debugger with simulavr running as a remote target. To do so, launch simulavr in the background and create a suitable gdbinit file:


$ simulavr --gdbserver --device at90s8515  &
$ make gdbinit

Running make in this manner creates gdbinit-helloavr, a file containing instructions for setting up GDB correctly, such that it connects to a simulavr, loads the compiled program, inserts a breakpoint and begins execution. Launch avr-gdb using the command:

$ avr-gdb -x gdbinit-helloavr

and you are presented with the GDB prompt; program execution is halted before the first instruction in main(). Set a breakpoint on line 71, using b 71, and enter C (continue) a few times. Every time you step over the instruction on line 71:

71       PORTB = ~currentValue;

~currentValue is output through PORTB. You should see a message to that effect, for example, writing 0xff to 0x0038. When you are done, issue a quit and kill the simulavr process, which is running in the background.

Installing the Program

If you've built or purchased the programmer hardware, you can install and test the software on a real AT90S8515 chip. Configure the avrdude section in the Makefile by setting the AVRDUDE_PROGRAMMERID and AVRDUDE_PORT variables, as explained in the comments above. Use:

AVRDUDE_PROGRAMMERID=stk500
AVRDUDE_PORT=/dev/ttyS0

for an STK500 programmer connected to the first serial port. Ensure that the programmer is connected to the appropriate port, insert the microcontroller in the programmer, apply power and type make writeflash. This generates the hex file used by AVRDUDE and writes its contents to the chip's Flash program memory.

Figure 4. HelloAVR Program Test Circuit

For those using the STK500 development kit, simply connect PORTB to the eight onboard LEDs using the ten-wire cable (as illustrated in Figure 3), and watch das blinkenlights. You can set up your own test hardware by constructing the schematic shown in Figure 4, which connects LEDs with suitable limiting resistors such that each pin of PORTB can activate one by going low and sinking current.

Where to from Here?

You've seen the flashing LEDs? Congratulate yourself; you are ready to begin creating your own AVR designs and software. See Resources for loads of AVR-related information and projects. There's almost no limit to what you can do.

Atmel shares a number of interesting project ideas through its AVR application notes, where it details implementation of stepper motor controllers, IR remote control receivers and transmitters and even an embedded Web server. One amazing project, ContikiOS (see Resources), distributes an open-source Internet-enabled, multitasking, graphical operating system that has been ported to the AVR and uses a version of the VNC server instead of a regular screen.

Enjoy experimenting with these versatile microcontrollers, be sure to share your discoveries and good luck building that robot horde!

Resources for this article: www.linuxjournal.com/article/7920.

Patrick Deegan was converted to IT and free software after being in contact with Linux while a student in the joint Math and Physics program at McGill U. Torn between working in physics, electronics and software development, Pat was bound to cofound Psychogenic, where he now gets to spend many days (and nights) playing with all three.

______________________

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

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState