Tutorial

From http://www.itee.uq.edu.au/~comp2300/pracs/GNU_C_Tutorial/gcctute.htm

Updated - 23 April, 2002

Contents

Starting AVR Studio
Creating a New Project
Cleaning Up the Project
Debugging with Coff Files
Project Development

Starting AVR Studio

Start the AVR Studio program by clicking on:
Start->Programs->ATMEL AVR Tools->AVR Studio 3.53

Once the program has started, you will be looking at a screen like this.

 

Back to Contents

Creating a New Project

In this tutorial we will make a simple C program that decreases the value of one of the PORT registers, making a binary down counter.

To create a new project, go to the "Project" menu and select "New". The dialog box shown in the next figure appears. In this dialog box you should enter the project name. We choose the name count here, but this could of course be an arbitrary name.

Next you'll have to select the project location. This is is the location where AVR Studio and avr-gcc will store all files associated with the project. We have used the location c:\code as the folder. If the folder does not exist, AVR Studio will automatically create it without any further notification.

NOTE: If c:\code already exists, delete this folder and its contents.

Now we have to select the Project type:
AVR Assembler: This informs AVR Studio that is should use the built-in Assembler when compiling the project. No further configuration is required by the user.

 

Generic 3rd party C compiler: This option enables the user to manually configure AVR Studio to use an external compiler when compiling and linking the project. We'll use this option in the following example.
Press the 'OK' button to continue.

fig2.jpg

The Project manager will now appear and look as shown in the picture below. In this view, you'll see all files associated with your project. It will be empty at this point.

fig3.jpg

 

The Project Concept and the GNU make Utility

We will work with the concept of using a new makefile for each project, and hence, stay with using AVR Studio's "projects" for each project.

All of the project files should be kept in the same folder. In this tutorial, this will be the c:\code folder. This is necessary for AVR Studio’s "Generic 3rd party C compiler" support to work properly.

The GNU tool make that comes with the avr-gcc distribution will use the makefile’s rules to compile and link our project into a .hex file ready to program into the device. This is a neat, clean and comprehensible way of organizing your work.

 

Adding Files to the Project

We now have to add a C file to the project. Download and save count.c to the c:\code folder.

Add the file by right clicking the "Source Files" folder in the Project window and select "Add File...". You will then get the "Add Files to Project" dialog box. Navigate to the c:\code folder, click on count.c and then press "Open".

fig4.jpg

fig5.jpg

The file count.c is now added to the project by AVR Studio and placed in the "Source Files" folder in the Project Window.

fig6.jpg

Now add the makefile for this project. A template for your makefile is supplied with the avrgcc distribution. Copy the file c:\avrgcc\avrfreaks\makefile to the c:\code folder.

Right click the "Other Files" folder in the Project window and select "Add File...". You will then get the "Add Files to Project" dialog box. Navigate to the c:\code folder, double-click on makefile and then press "Open".

fig7.jpg

The file makefile is now added to the project by AVR Studio and placed in the "Other Files" folder in the Project window.

 

The Makefile

The template makefile by Volker Oth looks like this:

 

# Simple Makefile by Volker Oth (c) 1999

# edited by AVRfreaks.net nov.2001



########### change this lines according to your project ##################



#put the name of the target mcu here (at90s8515, at90s8535, attiny22, atmega603 etc.)

	MCU	= at90s8515



#put the name of the target file here (without extension)

	TRG	= gcctest1



#put your C sourcefiles here 

	SRC	= $(TRG).c



#put additional assembler source file here

	ASRC    =



#additional libraries and object files to link

	LIB	=



#additional includes to compile

	INC	= 



#compiler flags

	CPFLAGS	= -g -Os -Wall -Wstrict-prototypes -Wa,-ahlms=$(<:.c=.lst)



#linker flags

	LDFLAGS = -Wl,-Map=$(TRG).map,--cref



########### you should not need to change the following line #############

include $(AVR)/include/avr_make



###### dependecies, add any dependencies you need here ###################

$(TRG).o : $(TRG).c

The only lines you might need to change are:
MCU = at90s8515

This should be edited to reflect which AVR device you are using. We do not need to change it for this tutorial.

 

TRG = gcctest1

This is the name of your target. The makefile is written so that the source file has the same name as the target. If it doesn’t, make simply won’t find the source file. You can see this from the next line in the makefile: SRC = $(TRG).c

We will be using count as our target so you will need to change the line to: TRG = count

NOTE: Do not use any extension/suffix for the target name!

 

The line you must change is:
include $(AVR)/include/avr_make

The path is incorrect, change it to: include $(AVR)/avrfreaks/avr_make

This line takes care of including more dependencies and commands for the make process from the file avr_make. This file is included in the avr-gcc distribution from AVRfreaks. For a quick introduction to what it contains, consult Appendix A of Using AVRGCC with AVR Studio.

Open the file makefile for edit by double-clicking on it in the project window. It will open in the built-in editor. Change the target name and include path as specified above and save the changes.

Tying it all Together

We now have an open project, a source file and a makefile ready to go. But some work still remains to get all of these things working together. When we have finished these steps, you will have a method that you can use for other projects and should be fairly simple to maintain.

There are a few things that we have to realize at this point:
When we started out, AVR Studio knew nothing about avr-gcc, what it is or where it is.

 

Selecting "Generic 3rd party C compiler" as the project type did not initiate any magic event in your computer. AVR Studio did not suddenly know which compiler to use or how to use it.

 

Avr-gcc is run from the command-line, as well as the other GNU avr tools like the assembler, linker and the unix tools. They don’t have nice user interfaces. All of these are most effectively controlled by the program make and are run from the command line.

So, the question is: How do we get AVR Studio to use avr-gcc?

What we have to do is:
Get AVR Studio to become aware of the path to the GNU tools via environment parameters.

 

Run make and feed it with the right makefile.

Hence,
Write a .bat batch file that points out the right directories and runs make.

 

Let AVR Studio know that it should take a look at that batch file.

The procedure will differ slightly for Win9x and Win2000 users.

For Win9x Users

The batch file is supplied with the avrgcc distribution. It has the name gcc_cmp.bat and you will find it in the c:\avrgcc\avrfreaks folder. It looks like this:

 

@echo -------- begin --------

@set AVR=c:\avrgcc

@set CC=avr-gcc

@set PATH=c:\avrgcc\bin

make %1

@echo --------  end  --------

Copy c:\avrgcc\avrfreaks\gcc_cmp.bat to a folder on your computer that you know is included in the Windows path, i.e. c:\windows

This file will set some important environment parameters for the make utility, then make will be run. The "%1" argument is the project folder path, provided by AVR Studio. The make utility, by default, looks for the file called "makefile" in this folder.

 

For Win2000 or WinXP Users

You will need a "start" file to kick off the compilation. This file is supplied with the avrgcc distribution. It has the name gcc_cmp.bat and you will find it in the c:\avrgcc\avrfreaks\win2000 folder. It looks like this:

 

@echo -------- begin --------

@start /MIN /wait cmd /c gcc_cmp2.bat %1

@type c:\tmpout.txt

@del c:\tmpout.txt

@echo --------  end  --------

Copy c:\avrgcc\avrfreaks\win2000\gcc_cmp.bat to a folder on your computer that you know is included in the Windows path, i.e. c:\winnt

The batch file is also supplied with the avrgcc distribution. It has the name gcc_cmp2.bat and you will find it in the c:\avrgcc\avrfreaks\win2000 folder. It looks like this:

 

@set AVR=c:\avrgcc

@set CC=avr-gcc

@set PATH=c:\avrgcc\bin

make.exe %1 >c:\tmpout.txt 2>&1

Copy c:\avrgcc\avrfreaks\win2000\gcc_cmp.bat to the same folder that you copied gcc_cmp.bat.

 

This setup will start make.exe, which is instructed to direct its screen output to a file. The contents of this file is displayed in the "compiler output" window in AVR Studio and then the file is deleted.

 

Setting the Target Options

Set the target options by right clicking "Target: Debug" in the Project window and select "Settings...".

fig8.jpg

You will then get the "Target Options" dialog box.
Uncheck the "Run 'compile' on each file in Source Files group" checkbox.

 

Check the "Run linker/build stage tools" checkbox.

 

Enter the name of your batch file gcc_cmp.bat in the "Command line:" edit box.

 

Go to the "Run Stage Settings" and select "Run code".

 

In the first edit box, type "Errors: none" and in the second edit box, type "obj" for the extension of the object file.

Press the 'OK' button to continue.

fig9.jpg

Important: Save your project now by selecting "Save" from the "Project" menu.

 

Building the Project with AVR Studio and avr-gcc

Right click "Target: Debug" in the Project window and select "Build".

fig10.jpg

The "Project Output" window will now pop up, showing information from the make output. If all goes well, this project should build with no errors.

fig11.jpg

 

Cleaning Up the Project

When we want to rebuild the entire project, we must delete the project output files from the previous build. Otherwise, make will decide that a rebuild is not necessary. We will not delete these files manually, instead we will add a new target in AVR Studio that performs the cleaning up for us.

Right click "Target: Debug" in the Project window and select "Targets" then "Add...". The "Add New Target" dialog box appears. Enter the name "Clean" for the new target name and select "Debug" from the "Copy settings from:" drop-down. Press the 'OK' button to continue.

fig12.jpg

fig13.jpg

The target "Clean" will now be available from the "Target" drop-down in the Project window. Select "Target - Clean".

fig14.jpg

We need to change the target options for this target. Right click "Target - Clean" in the Project window and select "Settings...".

fig15.jpg

You will then get the "Target Options" dialog box. You can see that it has inherited the settings from the "Debug" target. Add the word clean after gcc_cmp.bat in the "Command line:" edit box. Press the 'OK' button to continue.

fig16.jpg

Important: You have just made changes to your project so you will need to save the project again.

Now, build the "Clean" target by right clicking "Target: Clean" in the Project window and selecting "Build". This will clean out the products from the make process. Why does this work? "Clean" is a defined target in the avr_make makefile included in the avr-gcc distribution.

Change the target back to "Target - Debug" and build the project again. If you now try to build the "Debug" project one more time without making any changes, make doesn't do anything. This is because the makefile, which make uses, is set up to only rebuild the parts of the project that need to be rebuilt at any given time.

When make determines that the source file has a later timestamp than the project output files, as a result of editing the source file, it decides to rebuild the project.

The makefile file itself is not included as one of the dependencies. So a change in this file will not automatically initiate a rebuild.

 

Debugging with Coff Files

The .o object file that avr-gcc generates does not contain the necessary information to let AVR Studio watch variables during debugging. The standard gcc .elf file does include this information but AVR Studio does not support the ELF format. It does, however, support the COFF format with variable watch.

If you have just built the project with "Target - Clean" as the target, you will have to change the project target back to "Target - Debug" and build the project again.

After successfully building the "Target - Debug" project target, we will open the count.cof file which is created in the project directory.

 
Select "Open..." from the "File" menu. You will then get the "Open" dialog box. Navigate to the c:\code folder, click on count.cof and press "Open".

fig17.jpg

 

The project window disappears and the "Simulator Options" dialog box appears. Select AT90S8515 as the device, select the frequency of 4.000000 MHz and press "OK" to continue.

fig18.jpg

 

A new window with the name gcrt1.S appears showing lots of directives and lines of assembly code.

 

Reopen your project by selecting "Open..." from the "Project" menu. In the "Open" dialog box, click on count.apr and press "Open".

 

Add the gcrt1.S file to the "Source Files" folder in the Project window. By doing this, each time the project is closed, this file gets closed as well.

 

Open your C source file by double-clicking on count.c in the Project window.

 

Place the cursor on the outp(0xFF, DDRB); line and press <F9> to set a breakpoint.

 

Open the "Watches" window by selecting "Watch" from the "View" menu. Find the variable value in the code, double click on it and drag it into the "Watches" window.

Important: You have just made some more changes to your project so you will need to save the project again.

 

By pressing <Shift+F5> or "Reset" from the "Debug" menu the program debugging will reset and the focus will change to the gcrt1.S window.

 

Press <F5> or "Go" from the "Debug" menu to start the program running. Focus changes to the count.c window and the program breaks (stops) at the line with the breakpoint.

fig19.jpg

 

Single-step the program by pressing <F10> and watch value decrement.

fig20.jpg

 

 

Back to Contents

Project Development

Development of the project is a cycle of making changes to the source code, rebuilding the project and debugging the code. Every time the project is rebuilt after making changes to the source code, the .cof has to be reopened. These are the steps that you should follow:

 
Ensure that any changes to the source code have been saved.
Build the project target "Target - Debug".
Reopen the .cof file that is created in your project folder. Press "Yes" in the "AvrDebug" message dialog box to close all project documents.
Reopen the project.
Open the C source file.
Start debugging again.

 

Back to Contents