You are on page 1of 10

WHICH C-COMPILER SHOULD I CHOOSE?

U N O F F I C I A L C O M PA R I S O N O F A VA I L A B L E C - C O M P I L E R S F O R T H E AV R
ADAM JOHNSON
MARCH 2002

TABLE OF CONTENTS
What this document is and what it isn't.................................................................................................2 Meet the compilers .................................................................................................................................2 Setting up a new project (examples) ....................................................................................................4 Impressions and conclusions ................................................................................................................7

UNOFFICIAL COMPARISON OF AVAILABLE C-COMPILERS FOR THE AVR, ADAM JOHNSON - AVRFREAKS.NET 1

What this document is and what it isn't


This document is meant to act as a comparison from the (relatively) unbiased view of the author. It is not an official comparison, and it is based on my own experience only. It was accurate to the best of my knowledge at the time of writing, but no guarantees are made. It is intended to assist AVR newbies to choose a C compiler from the commonly used ones, not to say which is best. No offence is intended to any parties especially the people involved in working on these fine compilers. My personal thanks to the people involved with GCC AVR (too many to name individually), Richard Man, and Pavel Haiduc for their excellent work on making the AVR family more friendly to the end users! Only three of the major C compilers will be compared at this time. The reason for this is that these are the only ones which I have had experience with. The reviewed compilers are Codevision AVR (http://infotech.ir.ro), Imagecraft (http://www.imagecraft.com C Compiler for AVR (ICC-AVR), and GCC-AVR (GCC stands for the GNU compiler collection. For more information about GNU see http://www.gnu.org). The IAR compiler will not be reviewed at this stage, as this document is intended for beginners to AVR, and quite frankly the IAR compiler is out of the price range of most beginners (and some businesses!). It is however reputed to be a good compiler and those interested should check out IAR at http://www.iar.com.

Meet the compilers


What follows is a brief description of the compilers and their features. The headings link to places where the compilers can be downloaded from and more specific information can be found.

Codevision AVR

Also known as CVAVR or simply Codevision. This compiler is written by Pavel Haiduc of HP Infotech S.R.L. It is a full development system for the AVR series, available in two versions - the full version which generates code for the entire AVR Classic and Mega series', and the 'Light' version which will only generate code for the AVR Classic series (ie. part numbers beginning with AT90S....). There is support for devices with no SRAM (ie Tiny AVRs and the AT90S1200). There is an evaluation version of Codevision available free for download from the Codevision website which is limited in the size of program which it will compile. The size limit still allows a fair evaluation. The IDE is friendly and very easy to use. It is project based, and includes an automatic code generator called Codewizard AVR which generates all the necessary initialisation code for the AVR's onboard peripherals, as well as some external peripherals (using the included libraries). The supplied libraries provide support for a large number of commonly used internal and external devices as well eg. LCD screens, Real Time Clocks, Temperature Sensors, the UART, the SPI, etc. The compiled code can be downloaded into the target device using the built-in ISP (In System Programming) feature. This can be configured to automatically program the target after a successful compilation of the code. There is also a terminal program supplied as part of Codevision. The terminal can send and recieve files and also has the feature of displaying the recieved data in either hexadecimal or ASCII, and sending hexadecimal values one at a time.

UNOFFICIAL COMPARISON OF AVAILABLE C-COMPILERS FOR THE AVR, ADAM JOHNSON - AVRFREAKS.NET 2

On a slightly more technical note, Codevision can produce object code in COFF format. What that means to you and me is that it can be used, with full debugging capabilities, with the AVRStudio Debugger. It can also produce output in OBJ format, and Intel HEX for use with other FLASH and EEPROM Programming tools. Technical support is from Pavel himself (the author of CV). In my experience his support is always polite and helpful, and prompt responses make sure that you're not in the dark for too long. Pavel is also a regular on the AVRFreaks forum, so you know he is up with the people who use his product, and he offers a lot of technical help to the the AVRFreaks community.

GCC-AVR

The GNU C compiler for AVR is a port of the popular GNU C compiler to the AVR platform. It runs under MSDOS or a MSDOS shell under win32, or under Linux. GCC-AVR does not come with an IDE or most of the tools which accompany the other two compilers, it is just a C compiler. GCC-AVR has one decided advantage over the other compilers however: like all GNU products, GCC-AVR is entirely free, and the sourcecode for the compiler can be obtained so that it can be compiled fairly easily to run on almost any platform (especially any Linux machine!). GCC-AVR, like all of the GNU compilers is supplied with the standatd UNIX style Make program. This is used for building projects. Make uses Makefiles to tell it what to do and how to do it. Thankfully for the AVR community, Volker Oth has written a standard set of makefiles which can be easily modified to make compiling most AVR projects much easier. For those of you thinking that all this command-line driven compiling without an IDE sounds decidedly unlike your idea of a good time, even if it is free, there is hope yet. AVR studio can be used as an IDE for any external command line driven C compiler. That (thankfully) includes GCC. AVR Studio even provides source hilighting. There is still the matter of managing your own makefiles however, but most people manage with no problem. GCC cannot nativly produce COFF output files for use with AVR Studio for debugging and simulating, however there is now a program which will convert the ELF binary output to COFF available (follow the link at the beginning of this section). Technical support for GCC is a little more difficult than the others because it is not written by just one person or even one company. That's why the AVRFreaks AVRGCC forum exists. This is a forum specially for people who need help with AVRGCC, as a sort of surrogate for a tech help service. And it works well.

Imagecraft C Compiler (AVR)

Commonly abbreviated to ICCAVR or just ICC (not to be confused with GCC!) this fully ANSI C compatible compiler by Imagecraft Creations Inc. Imagecraft have been manufacturing a range of compilers for various embedded architectures (including Motorola 68HC11 and 68HC12, Atmel AVR and Tiny-AVR, PSoC, and others) since 1994. ICCAVR is a fully integrated development environment for the AVR series available in two versions: the Standard version, which will generate code for the entire AVR classic and MegaAVR ranges, and the Professional version, which has some extra features (most notably the Code Compressor which claims to reduce the generated binary size by up to 20%). Support for devices without SRAM (ie Tiny AVRs and the AT90S1200) is covered by a seperate product "ICC-Tiny". A 30 day trial version of the compiler is available
UNOFFICIAL COMPARISON OF AVAILABLE C-COMPILERS FOR THE AVR, ADAM JOHNSON - AVRFREAKS.NET 3

which also demonstrates the "Code Compressor" for a limited number of uses, as does the standard version. After 30 days the user must register the trial version or it becomes useless (ie refuses to run). The IDE is very easy to use. It is project based, and includes an automatic code generator called Application Wizard which generates initialisation code for the AVR's onboard peripherals. The supplied libraries provide support for the most commonly used internal peripherals ie UART, SPI, EEPROM, and also Stack checking functions. There is a terminal program supplied as part of the ICC IDE. It is a simple ASCII terminal and does not have any extra features other than the ability to send and recieve ASCII files. Imagecraft C can produce object code in COFF format. This is necessary for use with AVR Studio with full debugging capabilities. It can also generate Intel Hex format for ISP programming of flash and EEPROM. Technical support and enquiries from Imagecraft Creations inc. is excellent. Richard Man (who is the programmer behind ICCAVR) handles the tech support himself, so you know he knows what he's talking about, and his responses are always prompt and accurate. Richard is also a regular on the AVRFreaks forums, so you know he has his finger on the pulse too. Imagecraft really do listen to, and look after their customers!

Setting up a new project (examples)


In this section I will demonstrate how a simple LED flasher program would be written using each of the three compilers. This includes setting up the program and project, and any help which is lent by build in code generators is used. Step by step instructions are given so that the steps involved can be compared. The target processor is an AT90S8515 with the LED connected to PortC.0, running at 8MHz (because that's how my development board is set up). I should note here that most development boards (ie STK200, STK300, STK500, etc) run at 4MHz and have LEDs connected to PortB.0 - PortB.7. If you want to use these programs with these boards, then the LED port must be changed from port C to port B, and if you want the flash rate to be the same as I have, change the counter in the interrupt from 20 to 10 counts.

Codevision AVR (using CodeWizard)


1. 2. 3. 4.

The code produced by this example is here in cvtest.c. (included in files folder) Open the Codevision AVR IDE Click the File|New command (ie. "New" under the "File" dropdown menu. Choose to start a new project. Choose "Yes" to use CodeWizard to automatically generate initialisation code.

UNOFFICIAL COMPARISON OF AVAILABLE C-COMPILERS FOR THE AVR, ADAM JOHNSON - AVRFREAKS.NET 4

5. Under the "Chip" tab in CodeWizard select the target processor (AT90S8515). It is important to also select the correct clock frequency for your board. (8.0000 MHz in my case). 6. Under the "Ports" tab set the data direction for PortC.7 to be an output and the value to be a 1. 7. Under the "Timers" tab select Timer 0 to have an input frequency of 7.813KHz and turn the overflow IRQ on. (This will give a frequency of (7813 / 256) = 30.6Hz. If we use this to toggle the LED then it will flash too fast to see, so we use a software counter to divide it down to about 1.5Hz. 8. Select "File|Generate,Save and Exit". You will now have to save and name 3 files: the project, the .C file, and the codewizard file. 9. Now have a look at the generated code. You will notice a section with the comment // Timer 0 overflow interrupt service routine and just below it // Place your code here Where it says to place your code is where we put the lines static char a; a ++; if (a == 20) { PORTC ^= 0x80; a=0; } Just a note that the alternative notation PORTC.7 = !PORTC.7; could have been used instead of PORTC ^= 0x80; but this will only work in Codevision and is not compatible with the other compilers described here, so I have used the more portable version of the code :) 10. Now click "Project|Make" or press Shift+F9 to compile the program. 11. All that remains is to program the processor using the built-in chip programmer, and watch that LED go!

GCCAVR (using AVR Studio)

This assumes that you have AVR Studio and GCC-AVR set up properly already. There is another article (i hope??) which covers this setup. 1. Open AVR Studio 2. Select Project|New from the menu. 3. Give the project a name and location, and select the "Third Party C Compiler" option Now for GCC there is no application wizard, so all the code will have to be hand written. This is not a major problem. The code to be used is included here as gcctest.c. (included in files folder)
UNOFFICIAL COMPARISON OF AVAILABLE C-COMPILERS FOR THE AVR, ADAM JOHNSON - AVRFREAKS.NET 5

4. Enter the code shown in gcctest.c into a new file and save it as gcctest.c. (you could just copy this file for test purposes...). This file is created by right-clicking on the "Source Files" item in the project manager and selecting "Create New File". 5. Now a makefile must be created. Most of this has been done already and you can simply modify the makefile from one of the existing projects in the gcc-test suite of demo programs. 6. Once the makefile has been created it can be invoked using the make command. Assuming that you have AVR Studio set up to work with GCC then this can be done using the Project|Build command. 7. Now the program you have eritten must be downloaded into the AVR itself. AVR Studio offers tools to do this if you have an STK500, AVR-ISP, a JTAG ICE, or any of the other ATMEL ICE tools (I think...). For the rest of us (ie. if you are using the standard parallel dongle...), another program must be used for programming. I personally use PonyProg but there are lots of other tools available such as Atmel's AVR-ISP software (this is different from the AVR-ISP hardware mentioned earlier). Anyways, it's simply a matter of connecting the relevant device and loading the .HEX file which was created by GCCAVR and using your chosen programmer software/hardware to upload it to the AVR's FLASH memory. Once this is done, the LED should start flashing!

Imagecraft C Compiler
The code produced by this example is here in icctest.c. (included in files folder) 1. 2. 3. 4. Open the Imagecraft C IDE Click the Project|New menu item. Select a place to save your new project when prompted. Next click the Application Builder button on the toolbar. It's the one with a picture of a purple wizards hat on it.

UNOFFICIAL COMPARISON OF AVAILABLE C-COMPILERS FOR THE AVR, ADAM JOHNSON - AVRFREAKS.NET 6

5. Under the CPU tab select the chip and clock frequency (AT90S8515 @ 8MHz) 6. Under the Timer0 tab select to enable timer0, enable the overflow interrupt, and a generated rate of 30.5Hz (this is the slowest that timer0 can go at 8MHz) 7. A main() function (which is required in a C program) will need to be added which calls the init_devices() function and then goes into an infinite loop [while(1);]. The following code should be added inside the timer_ovf_isr() function: static char a; a++; if (a==20) { PORTC ^= 0x80; a = 0; } 8. Now save the source file. After it has been saved with the extension .c the source should become highlited in standard C source hilighting colors. 9. Next the source file needs to be added to the project. This is most easily done by making sure that it is the currently viewed file in the IDE and clicking the Project|Add Topmost Open File menu item. 10. Now make the project by pressing F9 or Project|Make Project . If the binary output is not automatically programmed into the chip (depends on your preferences settings) then click the chip programmer icon (the one with lots of 1's and 0's) on the toolbar and program the .hex file with the same name as your project into the chip. 11. All things being well and good, the LED should flash!

Impressions and conclusions


In the course of this review and my job, I have had a chance to use the three compilers reviewed in this article for some more complex projects than the one detailed in part 3. During the course of this work I have developed my opinions of the products. I have not attempted to do a comparison of code-size (of the ASM generated by the compilers) because I think that this is difficult if not impossible to do fairly, and there are new versions and revisions of each package being produced all the time, which means that such tests would only be accurate until the next release anyway.

Ease of Use

I think the issue of "Which one is easiest to use?" is very much a case of different strokes for different folks. The two commercial offerings (ICC and CV) are an outof-the-box sort of 'install it and go' type of thing. You just run the installation program, start up the built-in IDE, and start work (assuming you have licenced it...). GCC requires a bit more effort to get it running in the first place, but once it is configured it is not much harder to use. The included IDEs for CV and ICC are very easy to use, with Codevision's being slightly friendlier to the beginner, but both being equally powerful and manageable. Imagecraft has the Project Manager window on the right, and Codevision has it on the left, which can get a little annoying when swapping back and forth between them, but that would not be an issue for most people who only use one. The Imagecraft system is set out more like a traditional C development system (think Borland C++), whereas Codevison is set out in a manner which seems slightly more aimed at hardware people who do software as a means to an end. This is evidenced by the number of libraries included in Codevision to support various hardware devices quickly and easily. GCC on the other hand has the primary advantage of being free. If you download GCC, AVR Studio, and a programming software of your choice, then you have a complete AVR C development system for the cost of the internet time needed to
UNOFFICIAL COMPARISON OF AVAILABLE C-COMPILERS FOR THE AVR, ADAM JOHNSON - AVRFREAKS.NET 7

obtain it. This comes with a trade-off. The user interface is not good, especially if you don't integrate it into an IDE. Studio is not the greatest IDE either. It is sufficient, but it's project management tends to get cluttered and the windows tend to get lost behind one another before long. A major advancement would be a fixed project manager bar (like the two commercial IDEs have) down the side of the window which cannot be hidden behind other windows, but alas without this large projects can get hard. I have a friend (really!) who uses GCC almost exclusively and he tells me that using another editor for source file editing, and just using studio for compilation and project managing relieves this problem though.

Generated ASM, List, and Map files

I said at the beginning of this page that I did not compare the generated code sizes between the compilers. I have however had a cursory look at the quality of the generated ASM. It is interesting to note the different 'styles' of programming evident in each of the compilers. The generated ASM of each compiler has it's own style, and it's own way of going about things like register management and allocation. I would be stuck to comment on which one is most efficient though, because that sort of thing (compiler design etc.) is getting a bit beyond my league (hence they write the compilers, while I only review them!). The generated ASM from all the compilers is fairly tight. More interesting (and useful) to look at is the styles of the List and Map files generated to show you that ASM and how it relates to your C code. (For those who don't know: a List file is a listing of the generated ASM code from the C compiler complete with addresses of where things are put in memory etc, and a Map file is a list of all the global variables and data used by the program and where it is stored. They are both used for debugging purposes.) The Codevision compiler take all source files and compiles them into one big ASM file before assembly. This ASM file is interspersed with the relevant C code and memory addresses to become the list file (*.lst). C source lines are listed alternately with the ASM to which the lines are translated. The list file takes the same name as the project. The Map file is fairly basic, showing only the location and size of any and all global variables used. It appears that the map also shows statics, but I may be mistaken. The Imagecraft compiler generates a separate list file for each source file, with the same name as the source file and the extension *.lis. It also generates a global list file with the same name as the project and the extension *.lst. The listing is of the same format as Codevision ie. one C source line followed by the translated ASM code, then the next source line and so on. The map files from Imagecraft are very comprehensive. The addresses of global variables and functions are shown, including the start and end addresses of all segments as well. It appears that the locations of statics are not shown. The GCC list file is a little confusing to read. The compiler places lots of comments which sometimes hurt the readability more than they help. It is a complete listing though, and still useful for debugging. The map is similar. At the beginning is a sort of "normal" map file, followed by what seems to be the linker script. I'm not quite sure why they decided to put that there, but I guess it might come in useful if there were some pre-compiled object files missing or something like that.

UNOFFICIAL COMPARISON OF AVAILABLE C-COMPILERS FOR THE AVR, ADAM JOHNSON - AVRFREAKS.NET 8

So the verdict is:

Those users who are programmers who also do hardware will likely prefer Imagecraft C. It behaves most like any other (especially windows) C compiler that you are likely to be used to. The professional version with it's additional options is probably the most powerful compiler previewed here, and if IAR compatability is neccesary then Imagecraft is your best choice. Hardware people who also do programming will be more likely to like Codevision. It eases the strain of programming hardware for people who just want it to work without getting too involved in how, so long as your hardware is supported by the default libraries of course. Probably the best choice for those who are not yet 100% comfortable with the C language. GCC is not for the faint of heart, but can be made manageable for those on a budget of nothing squared. It has some slightly more quirky ways of doing things, born of the fact that GCC was not originally written for programming microcontrollers, but these are not difficult to get used to. Not reccomended for people who aren't already comfortable with the ANSI C language, as well as being happy with managing their own projects and makefiles.

UNOFFICIAL COMPARISON OF AVAILABLE C-COMPILERS FOR THE AVR, ADAM JOHNSON - AVRFREAKS.NET 9

You might also like