AVR-GCC ABC [1]

This time I decided to lay down some information on using the AVR-GCC compiler. I will not rewrite what’s in documentation is already written, but write down some issues that can help you start using this amazing tool.

Introduction

Most AVR programmers probably had a question in their mind about what compiler to use for their designs. The choice is huge. You can use commercial ones like IAR, CodeVision, ImageCraft. These compilers come with convenient GUI’s, code generators. But you have to buy a license to use them. So if you are going to do a low-budget project, the best choice is to use the open-source compiler GCC. This is the most successful open-source compiler in the world. The AVR platform is called AVR-GCC. The compiler itself has no graphical interface. Everything can be done in the MSDOS command line or the Linux command line. To make it easier to compile a project, there are makefiles used. GNU compilers usually are using UNIX style make files. This file can be run with the make.exe program. In reality, you don’t have to write makefiles – there are ready templates to use.
Everyone may agree that using a compiler in the command line is not convenient, or for somebody, it may seem very complicated. The good thing is that compiler can be used in any IDE. For example, an open-source GUI and editor are a Programmers Notepad. Another good choice is AVRStudio4. The creators of AVRStudio made the possibility to integrate the GCC compiler. Even makefiles are generated automatically. You can program without bothering about other processes that can be done automatically.
So in this article, I will try to review the following:

  • 1. WINAVR – AVR- GCC distribution (toolset) and usage of them;
  • 2. Makefile basics;
  • 3. AVR-GCC usage in AVRStudio4 environment;
  • 4. AVRLib library usage in your projects; 5. Real example.

WINAVR

WINAVR is a windows OS toolset for making AVR microcontroller based projects. This package includes an AVR-GCC C/C++ compiler. Official site https://winavr.sourceforge.net/index.html. There you can download everything. For this article, I will use WinAVR20050214 distribution as it is mostly a tested and working version. Of course, there is already a newer one, but this is still fresh and not tested well. You can use it as it has a few new features and fixes. The main tools and utilities in WINAVR are:
1. GNU Binutils 2.15 – Utilities (assembler, linker, make, and other.)
1) ld – GNU linker;
2) as – GNU assmebler;
Also (seein https://sources.redhat.com/binutils/):
3) addr2line;
4) ar;
5) c++filt
6) gprof;
7) nlmconv;
8) nm;
9) objcopy;
10) objdump;
11) ranlib;
12) readelf;
13) size;
14) strings;
15) strip;
16) windres.
2. https://www.gnu.org/software/gcc/ – GNU GCC 3.4.3 – C and C++ compiler for AVR.
3. avr-libc 1.2.3 – AVR standard function library.
4. avrdude 4.4.0 – open source program for flashing chips. You can configure or add a new chip or programmer by configuring files.
5. uisp 20050207 – other open-source programmer software.
6. avrdude-gui 0.2.0 – avrdude GUI (alpha version).
7. GNU Debugger (GDB) 6.1 – command-line debugger.
8. Insight 6.1 – GDB graphical interface.
9. avarice 2.3 – a program that allows interconnecting Atmel JTAG ICE and GDB in order to enable debugging AVR’s.
10. simulavr 0.1.2.1 – together with GDB it acts as a simulator.
11. SRecord 1.21 – Toolset for manipulating EEPROM. Reads writes etc.
12. Mfile – automatically make file generator.
13. Programmers Notepad 2.0.5.32 – editor and GUI for writing programs.
14. Other utilities (make, bach,…);
15 Good documentation in different formats.
16. Makefile template.

Installing WINAVR

First of all, download WinAVR20050214. You can download the newer version – the installation is the same. The instructions for installing the toolset is there. Let’s say, by default; we are installing in the C:\WinAVR directory. Then GNU Binutils will be in C:\WinAVR\Bin. Because GCC is adapted to the AVR platform, every utility name begins with avr-, for example, avr-as.exe, avr-gcc.exe, etc.

Writing Program

It is convenient to use pre-configured Programmers Notepad (PN) to write programs. Of course, you can use other GUI’s. PN can call any commands from which usually runs in the command line and display results. It is convenient to call make utility, which of course calls the compiler. To set these tools, go to Options->Tools. All tools you set will appear in the Tools main menu:

But let’s go from the beginning. I think the best point to start is by describing WINAVR install instruction and PN configuration and how to start to create a program. PN program can be launched from c:\winavr\pn\pn.exe or in the start menu.

In order to configure tools press the Tools-Options menu. You’ll see a window:

As you can see there are already predefined most common tools: Make All, Make Clean, Program. But to understand how this works let’s go through it. Open the Make All tool and press Edit.

Tool fields:
Name – the name for your tool;
Command – executed command. In this case, make.exe;
Folder – directory where is makefile. %d- shows the default project directory;
Parameters – parameters needed to execute the command. This means that the whole project will be compiled.
Shortcut- leave it none;
Save – it is good to choose all files. Then the program, before compiling, will save all files. Otherwise, the compilation will start without saving current project files.
Other fields leave as they are.
Bellow, there is a table of most common commands:

Write them all to Tools Options, and you will see them in the Tools menu.
After everything is configured, let’s start the first project by pressing File->New-Project. Chose a folder where it will be saved and name it.

Then create a new C file by going to File->New C/C++. Write a program to the project directory. After the file is created, press the right mouse button on the project name in the project tree and add files. In the file, menu select your c file.
The last important step is the creation of a makefile. There are several ways to create a makefile: you can write it yourself or use a template and edit it to meet your project settings.
Let’s say our program is:

#include
#include
#include
int main (void)
{
unsigned char counter;
DDRB = 0xFF;
while(1)
{
PORTB |=1<<2;
counter=0;
while (counter !=5)
{
_delay_loop_2(30000);
counter++;
}
PORTB &=~(1<<2);
counter=0;
while(counter !=5)
{
_delay_loop_2(30000);
counter++;
}
return 1;
}
}

You should see like this:

To compile the program, let’s create a makefile. Open Mfile program from Start->Programs:

In the text window, you already see the makefile template. If you want to compile a project successfully – you have to configure it. Press on the menu item Makefile you’ll see the list of tools. There is a list of everyone:

  • 1. Code generation – not implemented;
  • 2. Main file name… – chose the main program name without “.” end;
  • 3. MCU type – select microcontroller type;
  • 4. Output format – select output format. By default use ihex (Intel hex);
  • 5. Optimization level – select the optimization level. Just remember when the optimization level is higher there is a bigger probability for compilation errors to appear;
  • 6. Debug format – debug file format is chosen for debugging software you use;
  • 7. C standard level – chose c language standard. By default it is GNU99;
  • 8. C/C++ source file(s) – select all C and C++ source files included in the project. If these files are in the same project directory – you just need to write their names. Otherwise, you have to show the full path to them;
  • 9. Assembler source files(s) – select all asm source files included in your project;
  • 10. printf() – ability to select printf() function type. You can choose with less resource demand or with floating pint support, which takes a lot of support of jour MCU;
  • 11. scanf() – the same as printf() but for number input;
  • 12. External RAM options – selected if there is external ram used;
  • 13. AVRdude – not implemented;
  • 14. Programmer – select programmer type;
  • 15. Port – select port;
  • 16. Miscellaneous – others not implemented;
  • 17. Enable Editing of Makefile – you can select this if you want manually edit the makefile. You can enter all information manually.

When makefile is configured, just save it in the project directory without Extention. If you save it in a different location, you will have to show it in the PM Tools menu.
Now you can compile your project by pressing in PN Tools->Make All. If there are no errors, you should see messages like this:

> “make.exe” all
——– begin ——–
avr-gcc (GCC) 3.4.3
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Compiling: main.c
avr-gcc -c -mmcu=atmega8 -I. -gdwarf-2 -DF_CPU=8000000UL -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Wstrict-prototypes -Wa,-adhlns=main.lst -std=gnu99 -MD -MP -MF .dep/main.o.d main.c -o main.o
Linking: main.elf
avr-gcc -mmcu=atmega8 -I. -gdwarf-2 -DF_CPU=8000000UL -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Wstrict-prototypes -Wa,-adhlns=main.o -std=gnu99 -MD -MP -MF .dep/main.elf.d main.o –output main.elf -Wl,-Map=main.map,–cref -lm
Creating load file for Flash: main.hex
avr-objcopy -O ihex -R .eeprom main.elf main.hex
Creating load file for EEPROM: main.eep
avr-objcopy -j .eeprom –set-section-flags=.eeprom=”alloc,load” \
–change-section-lma .eeprom=0 -O ihex main.elf main.eep
Creating Extended Listing: main.lss
avr-objdump -h -S main.elf > main.lss
Creating Symbol Table: main.sym
avr-nm -n main.elf > main.sym
Size after:
main.elf :
section size addr
.text 148 0
.data 0 8388704
.bss 0 8388704
.noinit 0 8388704
.eeprom 0 8454144
.debug_aranges 20 0
.debug_pubnames 27 0
.debug_info 214 0
.debug_abbrev 156 0
.debug_line 250 0
.debug_str 138 0
Total 953
AVR Memory Usage:
—————–
Device: atmega8
Program: 148 bytes (1.8% Full)
(.text + .data + .bootloader)
Data: 0 bytes (0.0% Full)
(.data + .bss + .noinit)
——– end ——–
> Process Exit Code: 0

There you can see the results of compilation: the size of binaries and so on.

Makefile principals

We ran through makefile creation in the earlier paragraph. But this is a vital topic to talk about. Many of you are using GUIs without thinking about how programs are compiled and how you don’t know what files to include. If your project is effortless, you don’t need a makefile; you just can run a command like this “AVR-GCC myprj.c” and this will generate object files and hex. But when there are many source files included, like 10 or 100 without makefile, your life would be hard J. Makefiles are convenient, including multiple source files or additional libraries like AVRLIB. Still, it can help you to save tons of time. For example, if your earlier source files were compiled and changed something in your main file, then only your modified file will be recompiled when recompiling your project. Imagine if your project has 1000 source files – it can save tons of time. If you want to learn all about makefile, you should read a perfect tutorial about it here. We go through what is actual for compiling AVR projects.
Makefile is a file containing all information on how to compile your project. There can be variables and instructions that make this tool a mighty thing. As you noticed, makefile has no extension, and if its name is a makefile, you even do not have to show to make.exe program which files to use. So you can run the make.exe command from the project directory, and all compilation is done. Makefile can have parameters, as you saw All and Clean when configuring PN. So you can run d:\myproj\make all.
Very often, when working with projects where are many source files, and you will change something in one of them, but others were compiled in early stages, the make program will check if there were changes done to other files; if not, then compilation will be done only on changed files. But if you change such parameter as optimization level, then all files will be recompiled. As you already know, after compilation, there is a hex file created that is burned to flash. But there are other files generated like coff files needed for debugging with such programs as AVRStudio. For the newer AVRStudio version, there is used extended coff – extcoff. It will help if you read in the documentation of debugging software about supported formats to select the right one. Let’s open an example makefile from the c:\winavr\sample folder.
It can be scary the first time. But let’s go through important parts for us:

# MCU name -(after sign # everything is comments);
MCU = atmega128

There is MCU selected;
Then we define the main C file without extension:

TARGET = myproj

Then if there are more source files just add them:

SRC = $(TARGET).c otherfile.c

You see that instead of the main file there is a $(TARGET) variable which equals to myproj. If there are more files in other directories – you have to write a full path to them. For example:

SRC = $(TARGET).c c:otherfolderothercource.c

If there are asm files used in your project they have to be defined too:

ASRC = asm_file.s

ASM files in AVR-GCC have .s extension.
Optimization level is defined as follows:

OPT=s

As you know that in projects often used header files, then they have to be defined as follows:

EXTRAINCDIRS=c:includes d:myprojinc A:includes

Dependencies of c files are generated automatically. If this doesn’t help just finds parameter CFLAGS and in new line type in:

CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))

This will show to make a program to use these directories. The same has to be done with assembler includes:

AFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))

I think it is enough to start your own projects with AVR-GCC. Of source, you can define to flash your program after compilation or do another task. How to do this you can find in the documentation.

Leave a Reply