Hello world

From OpenAVR

Jump to: navigation, search

This is in progress and not yet finished! Do not use!

Contents

[edit] First Approach

We will use the classic hello world program to demonstrate the required steps of creating an embedded AVR application with the GNU Toolchain. You will later see, that this procedure can be simplified a lot. However, walking through the details will hopefully provide a better understanding of what happens behind the curtain.

[edit] Creating the source file

Any text editor like vi for Linux or notepad for Windows may be used to create a C source file. Just make sure, that the result is in plain text format. Let's copy the hello world program by Brian Kernighan. Here's the original version:

main() {
    printf("hello, world");
}

Store this text in a file named hello.c.

[edit] Compiling the source file

On a command line enter

avr-gcc -c -mmcu=atmega2561 hello.c

If you are compiling for a different AVR device, replace atmega2561 with its name. The compiler will respond with the following output

hello.c: In function 'main':
hello.c:2: warning: incompatible implicit declaration of built-in function 'printf'

At the time Brian created the program, C compilers had been quite relaxed. Today's modern C compilers a much more picky in order to guide the programmer towards better code. Thus the warning message, which we will ignore for now.

Apart from the message, the compiler created a new file named hello.o. It already contains the binary code of our simple program for the AVR. This code is still incomplete. It lacks all absolute addresses and, most important, the required parts of the C runtime library.

[edit] Linking the object file

In order to create a full binary, we need to link hello.o with runtime initialization code (C startup) and the C standard libraries. On the command line enter:

avr-ld -T/winavr/avr/lib/ldscripts/avr6.x /winavr/avr/lib/avr6/crtm2561.o hello.o -L/winavr/lib/gcc/avr/4.1.2 -lc -lgcc

If no error occurs, there will be no response. This is quite normal for UNIX/Linux tools. The linker will have created a new file a.out, which contains all binary code of the executable. However, it is not in the hex or bin format, which we need for uploading it to the target.

[edit] Creating the hex file

The tool avr-objcopy can translate the binary created by linker to a hex file with the following command:

avr-objcopy -O ihex a.out

Now check the contents of a.out with your text editor. Indeed it contains the hex code of our hello world program.

[edit] Uploading the hex file to the target

avrdude is one of several tools, which are available to program the flash memory of AVR devices. As far as we can say, avrdude is the best supported.

Up to know we needed to modify the commands to fit our target. Things are becoming more complicated now, because many different programming adapters exist and most of them are supported by avrdude.

avrdude -p atmega2561 -P com1 -c stk500v2 -U flash:w:a.out


Personal tools