|
|
| 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 ProjectIn 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:
![]() 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. ![]()
The Project Concept and the GNU make UtilityWe 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 ProjectWe 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". ![]() ![]() The file count.c is now added to the project by AVR Studio and placed in the "Source Files" folder in the Project Window. ![]() 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". ![]() The file makefile is now added to the project by AVR Studio and placed in the "Other Files" folder in the Project window.
The MakefileThe template makefile by Volker Oth looks like this:
The only lines you might need to change are:
The line you must change is:
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 TogetherWe 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:
So, the question is: How do we get AVR Studio to use avr-gcc? What we have to do is:
Hence,
The procedure will differ slightly for Win9x and Win2000 users.
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 OptionsSet the target options by right clicking "Target: Debug" in the Project window and select "Settings...".![]() You will then get the "Target Options" dialog box.
Press the 'OK' button to continue. ![]() Important: Save your project now by selecting "Save" from the "Project" menu.
Building the Project with AVR Studio and avr-gccRight click "Target: Debug" in the Project window and select "Build".![]() 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. ![]()
Cleaning Up the ProjectWhen 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. ![]() ![]() The target "Clean" will now be available from the "Target" drop-down in the Project window. Select "Target - Clean". ![]() We need to change the target options for this target. Right click "Target - Clean" in the Project window and select "Settings...". ![]() 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. ![]() 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 FilesThe .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.
Project DevelopmentDevelopment 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:
|