No part of this document may be reproduced without the prior written consent of IAR
Systems. The software described in this document is furnished under a license and may
only be used or copied in accordance with the terms of such a license.
DISCLAIMER
The information in this document is subject to change without notice and does not
represent a commitment on any part of IAR Systems. While the information contained
herein is assumed to be accurate, IAR Systems assumes no responsibility for any errors
or omissions.
In no event shall IAR Systems, its employees, its contractors, or the authors of this
document be liable for special, direct, indirect, or consequential damage, losses, costs,
charges, claims, demands, claim for lost profits, fees, or expenses of any nature or kind.
TRADEMARKS
IAR Systems, From Idea to Target, IAR Embedded Workbench, visualSTATE, IAR
MakeApp and C-SPY are trademarks owned by IAR Systems AB.
Atmel is a registered trademark of Atmel Corporation. AVR is a registered trademark of
Atmel Corporation.
Microsoft and Windows are registered trademarks of Microsoft Corporation.
All other product names are trademarks or registered trademarks of their respective
owners.
EDITION NOTICE
Fourth edition: February 2005
Part number: CAVR-4
This guide applies to version
4.x of AVR IAR Embedded Workbench®.
Brief contents
Tables ...................................................................................................................... xv
Preface ................................................................................................................... xix
Part 1. Using the compiler ......................................................... 1
Getting started .................................................................................................... 3
Data storage ...................................................................................................... 15
73: Traditional standard C header files—DLIB ...................................................... 245
74: Embedded C++ header files .............................................................................. 246
75: Additional Embedded C++ header files—DLIB ............................................... 246
76: Standard template library header files ............................................................... 247
77: New standard C header files—DLIB ................................................................ 247
78: IAR CLIB Library header files ......................................................................... 249
79: Miscellaneous IAR CLIB Library header files ................................................. 249
80: Message returned by strerror()—IAR CLIB library ......................................... 265
81: Message returned by strerror()—IAR DLIB library ......................................... 268
xvii
AVR® IAR C/C++ Compiler
xviii
Reference Guide
Preface
Welcome to the AVR® IAR C/C++ Compiler Reference Guide. The purpose
of this guide is to provide you with detailed reference information that can
help you to use the AVR IAR C/C++ Compiler to best suit your application
requirements. This guide also gives you suggestions on coding techniques so
that you can develop applications with maximum efficiency.
Who should read this guide
You should read this guide if you plan to develop an application using the C or C++
language for the AVR microcontroller and need to get detailed reference information on
how to use the AVR IAR C/C++ Compiler. In addition, you should have a working
knowledge of the following:
● The architecture and instruction set of the AVR microcontroller. Refer to the
documentation from Atmel® Corporation for information about the AVR
microcontroller
● The C or C++ programming language
● Application development for embedded systems
● The operating system of your host machine.
How to use this guide
When you start using the AVR IAR C/C++ Compiler, you should read Part 1. Using the
compiler in this guide.
When you are familiar with the compiler and have already configured your project, you
can focus more on Part 2. Compiler reference.
If you are new to using the IAR toolkit, we recommend that you first study the AV R® IAR Embedded Workbench™ IDE User Guide. This guide contains a product overview,
tutorials that can help you get started, conceptual and user information about IAR
Embedded Workbench and the IAR C-SPY Debugger, and corresponding reference
information. The AVR® IAR Embedded Workbench™ IDE User Guide also contains a
glossary.
xix
What this guide contains
What this guide contains
Below is a brief outline and summary of the chapters in this guide.
Part 1. Using the compiler
● Getting started gives the information you need to get started using the AVR IAR
C/C++ Compiler for efficiently developing your application.
● Data storage describes how data can be stored in memory, with emphasis on the
different memory models and memory type attributes.
● Functions describes the special function types, such as interrupt functions and
monitor functions.
● Placing code and data describes the concept of segments, introduces the linker
command file, and describes how code and data are placed in memory.
● The DLIB runtime environment describes the runtime environment in which an
application executes. It covers how you can modify it by setting options, overriding
default library modules, or building your own library. The chapter also describes
system initialization and introduces the file
cstartup, as well as how to use
modules for locale, and file I/O.
● The CLIB runtime environment gives an overview of the runtime libraries and how
they can be customized. The chapter also describes system initialization and
introduces the file cstartup.
● Assembler language interface contains information required when parts of an
application are written in assembler language. This includes the calling convention.
● Using C++ gives an overview of the two levels of C++ support: The
industry-standard EC++ and IAR Extended EC++.
● Efficient coding for embedded applications gives hints about how to write code that
compiles to efficient code for an embedded application.
Part 2. Compiler reference
● Data representation describes the available data types, pointers, and structure types.
This chapter also gives information about type and object attributes.
● Segment reference gives reference information about the compiler’s use of
segments.
● Compiler options explains how to set the compiler options, gives a summary of the
options, and contains detailed reference information for each compiler option.
● Extended keywords gives reference information about each of the AVR-specific
keywords that are extensions to the standard C/C++ language.
● Pragma directives gives reference information about the pragma directives.
● The preprocessor gives a brief overview of the preprocessor, including reference
information about the different preprocessor directives, symbols, and other related
information.
● Intrinsic functions gives reference information about the functions that can be used
for accessing AVR-specific low-level features.
AVR® IAR C/C++ Compiler
xx
Reference Guide
Other documentation
Preface
● Library functions gives an introduction to the C or C++ library functions, and
summarizes the header files.
● Implementation-defined behavior describes how the AVR IAR C/C++ Compiler.
handles the implementation-defined areas of the C language standard.
● IAR language extensions describes the IAR extensions to the ISO/ANSI standard
for the C programming language.
● Diagnostics describes how the compiler’s diagnostic system works.
The complete set of IAR Systems development tools for the AVR microcontroller is
described in a series of guides. For information about:
● Using the IAR Embedded Workbench™ IDE with the IAR C-SPY™ Debugger,
refer to the AVR® IAR Embedded Workbench™ IDE User Guide
● Programming for the AVR IAR Assembler, refer to the AVR® IAR Assembler
Reference Guide
● Using the IAR XLINK Linker™, the IAR XAR Library Builder™, and the IAR
XLIB Librarian™, refer to the IAR Linker and Library Tools Reference Guide
● Using the IAR DLIB Library functions, refer to the online help system
● Using the IAR CLIB Library functions, refer to the IAR C Library Functions
Reference Guide, available from the online help system.
● Porting application code and projects created with a previous AVR IAR Embedded
Workbench IDE, refer to AVR® IAR Embedded Workbench Migration Guide.
All of these guides are delivered in hypertext PDF or HTML format on the installation
media. Some of them are also delivered as printed books.
FURTHER READING
The following books may be of interest to you when using the IAR Systems
development tools:
● Barr, Michael, and Andy Oram, ed. Programming Embedded Systems in C and
C++. O’Reilly & Associates.
● Harbison, Samuel P. and Guy L. Steele (contributor). C: A Reference Manual.
Prentice Hall.
● Kernighan, Brian W. and Dennis M. Ritchie. The C Programming Language.
Prentice Hall. [The later editions describe the ANSI C standard.]
● Labrosse, Jean J. Embedded Systems Building Blocks: Complete and Ready-To-Use
Modules in C. R&D Books.
● Lippman, Stanley B. and Josée Lajoie. C++ Primer. Addison-Wesley.
● Mann, Bernhard. C für Mikrocontroller. Franzis-Verlag. [Written in German.]
● Stroustrup, Bjarne. The C++ Programming Language. Addison-Wesley.
xxi
Document conventions
Document conventions
We recommend that you visit the following websites:
● The Atmel® Corporation website, www.atmel.com, contains information and news
about the AVR microcontrollers.
● The IAR website, www.iar.com, holds application notes and other product
information.
● Finally, the Embedded C++ Technical Committee website,
www.caravan.net/ec2plus, contains information about the Embedded C++
standard.
When, in this text, we refer to the programming language C, the text also applies to C++,
unless otherwise stated.
TYPOGRAPHIC CONVENTIONS
This guide uses the following typographic conventions:
StyleUsed for
computerText that you enter or that appears on the screen.
parameterA label representing the actual value you should enter as part of a
command.
[option]An optional part of a command.
{a | b | c}Alternatives in a command.
boldNames of menus, menu commands, buttons, and dialog boxes that
appear on the screen.
refe renceA cross-reference within this guide or to another guide.
…An ellipsis indicates that the previous item can be repeated an arbitrary
number of times.
Identifies instructions specific to the IAR Embedded Workbench
interface.
Identifies instructions specific to the command line interface.
Identifies helpful tips and programming hints.
AVR® IAR C/C++ Compiler
xxii
Reference Guide
Table 1: Typographic conventions used in this guide
Part 1. Using the compiler
This part of the AVR® IAR C/C++ Compiler Reference Guide includes the
following chapters:
● Getting started
● Data storage
● Functions
● Placing code and data
● The DLIB runtime environment
● The CLIB runtime environment
● Assembler language interface
● Using C++
● Efficient coding for embedded applications.
1
2
IAR language overview
Getting started
This chapter gives the information you need to get started using the AVR IAR
C/C++ Compiler for efficiently developing your application.
First you will get an overview of the supported programming languages,
followed by a description of the steps involved for compiling and linking an
application.
Next, the compiler is introduced. You will get an overview of the basic settings
needed for a project setup, including an overview of the techniques that enable
applications to take full advantage of the AVR microcontroller. In the following
chapters, these techniques will be studied in more detail.
There are two high-level programming languages available for use with the AVR IAR
C/C++ Compiler:
● C, the most widely used high-level programming language used in the embedded
systems industry. Using the AVR IAR C/C++ Compiler, you can build
freestanding applications that follow the standard ISO 9899:1990. This standard is
commonly known as ANSI C.
● C++, a modern object-oriented programming language with a full-featured library
well suited for modular programming. IAR Systems supports two levels of the
C++ language:
● Embedded C++ (EC++), a subset of the C++ programming standard, which is
intended for embedded systems programming. It is defined by an industry
consortium, the Embedded C++ Technical committee. See the chapter Using C++.
● IAR Extended EC++, with additional features such as full template support,
namespace support, the new cast operators, as well as the Standard Template
Library (STL).
Each of the supported languages can be used in strict or relaxed mode, or relaxed with
IAR extensions enabled. The strict mode adheres to the standard, whereas the relaxed
mode allows some deviations from the standard.
It is also possible to implement parts of the application, or the whole application, in
assembler language. See the AVR® IAR Assembler Reference Guide.
Part 1. Using the compiler
3
Building applications—an overview
For more information about the Embedded C++ language and IAR Extended Embedded
C++, see the chapter Using C++.
Building applications—an overview
A typical application is built from a number of source files and libraries. The source files
can be written in C, C++, or assembler language, and can be compiled into object files
by the AVR IAR C/C++ Compiler or the AVR IAR Assembler.
A library is a collection of object files. A typical example of a library is the compiler
library containing the runtime environment and the C/C++ standard library. Libraries
can also be built using the IAR XAR Library Builder, the IAR XLIB Librarian, or be
provided by external suppliers.
The IAR XLINK Linker is used for building the final application. XLINK normally uses
a linker command file, which describes the available resources of the target system.
Below, the process for building an application on the command line is described. For
information about how to build an application using the IAR Embedded Workbench
IDE, see the AVR® IAR Embedded Workbench™ IDE User Guide.
COMPILING
In the command line interface, the following line compiles the source file myfile.c
into the object file myfile.r90 using the default settings:
iccavr myfile.c
In addition, you need to specify some critical options, see Basic settings for project
configuration, page 5.
LINKING
The IAR XLINK Linker is used for building the final application. Normally, XLINK
requires the following information as input:
● A number of object files and possibly certain libraries
● The standard library containing the runtime environment and the standard language
functions
● A program start label
● A linker command file that describes the memory layout of the target system
● Information about the output format.
On the command line, the following line can be used for starting XLINK:
In this example, myfile.r90 and myfile2.r90 are object files, lnkm128s.xcl is the
linker command file, and
the label where the application starts. The option -o specifies the name of the output
file, and the option -F can be used for specifying the output format. (The default output
format is
Motorola.)
The IAR XLINK Linker produces output according to your specifications. Choose the
output format that suits your purpose. You might want to load the output to a
debugger—which means that you need output with debug information. Alternatively,
you might want to load the output to a flash loader—in which case you need output
without debug information, such as Intel-hex or Motorola S-records.
Basic settings for project configuration
This section gives an overview of the basic settings for the project setup that are needed
to make the compiler generate the best code for the AVR device you are using. You can
specify the options either from the command line interface or in the IAR Embedded
Workbench IDE. For details about how to set options, see Setting command line options,
page 167, and the AVR® IAR Embedded Workbench™ IDE User Guide, respectively.
The basic settings available for the AVR microcontroller are:
● Processor configuration
● Memory model
● Size of double floating-point type
● Optimization for speed and size
● Runtime environment.
In addition to these settings, there are many other options and settings available for
fine-tuning the result even further. See the chapter Compiler options for a list of all
available options.
Getting started
cl3s-ec.r90 is the runtime library. The option -s specifies
PROCESSOR CONFIGURATION
To make the compiler generate optimum code you should configure it for the AVR
microcontroller you are using.
The --cpu option versus the -v option
There are two processor options that can be used for configuring the processor support:
--cpu=derivative and -vn
Your program may use only one processor option at a time, and the same processor
option must be used by all user and library modules in order to maintain consistency.
Part 1. Using the compiler
5
Basic settings for project configuration
Both options set up default behavior—implicit assumptions—but note that the
--cpu
option is more precise because it contains more information about the intended target
than the more generic
-v option. The --cpu option knows, for example, how much flash
memory is available in the given target and allows the compiler to optimize accesses to
code memory in a way that the
-v option does not. See Mapping of processor options
--cpu and -v, page 6.
The
--cpu=derivative option implicitly sets up all internal compiler settings needed
to generate code for the processor variant you are using. The following options are
implicitly controlled when you use the
--eeprom_size, --enhanced_core, --spmcr_address, -v and --64k_flash.
Because these options are automatically set when you use the
--cpu option: --eecr_address,
--cpu option, you cannot
set them explicitly. For information about implicit assumptions when using the -v
option, see Summary of processor configuration, page 8. To read more about the
generated code, see -v, page 197.
See the AVR® IAR Embedded Workbench™ IDE User Guide for information about
setting project options in IAR Embedded Workbench.
Use the --cpu or -v option to specify the AVR derivative; see the chapter Compiler options for syntax information.
Mapping of processor options --cpu and -v
The following table shows the mapping of processor options and which AVR
microcontrollers they support: