Top Banner
ARM® IAR C/C++ Development Guide Compiling and linking for Advanced RISC Machines Ltd’s ARM Cores
382
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • DARM-1

    ARM IAR C/C++ Development Guide

    Compiling and linking

    for Advanced RISC Machines LtdsARM Cores

  • DARM-1

    COPYRIGHT NOTICE Copyright 19992007 IAR Systems. All rights reserved.

    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.

    DISCLAIMERThe information in this document is preliminary and has not been checked for correctness. It does not represent a commitment on any part of IAR Systems. 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.

    TRADEMARKSIAR, IAR Systems, IAR Embedded Workbench, IAR MakeApp, C-SPY, visualSTATE, From Idea To Target, IAR KickStart Kit and IAR PowerPac are trademarks or registered trademarks owned by IAR Systems AB.

    ARM, Thumb, and Cortex are registered trademarks of Advanced RISC Machines Ltd.

    Microsoft and Windows are registered trademarks of Microsoft Corporation.

    All other product names are trademarks or registered trademarks of their respective owners.

    EDITION NOTICE

    First edition: June 2007

    Part number: DARM-1

    This guide applies to version 5.x of ARM IAR Embedded Workbench.

    The ARM IAR C/C++ Development Guide replaces all versions of the ARM IAR C/C++ Compiler Reference Guide and the IAR Linker and Library Tools Reference Guide.

    Internal reference: ISUD.

  • DARM-1

    iii

    Brief contentsTables .................................................................................................................... xvii

    Preface ................................................................................................................... xix

    Part 1. Using the IAR build tools ........................................ 1Introduction to the IAR build tools .......................................................... 3

    Developing embedded applications ........................................................... 9

    Data storage ...................................................................................................... 25

    Functions ............................................................................................................... 29

    Linking using ILINK ........................................................................................ 37

    Linking your application ................................................................................ 45

    The DLIB runtime environment .............................................................. 57

    Assembler language interface ................................................................... 85

    Using C++ ............................................................................................................ 99

    Application-related considerations ....................................................... 107

    Efficient coding for embedded applications ...................................... 117

    Part 2. Reference information ........................................... 135External interface details ............................................................................ 137

    Compiler options ........................................................................................... 145

    Linker options .................................................................................................. 175

    Data representation ...................................................................................... 195

    Compiler extensions .................................................................................... 207

    Extended keywords ....................................................................................... 217

  • DARM-1

    ivARM IAR C/C++ Development GuideCompiling and linking

    Pragma directives ............................................................................................ 227

    Intrinsic functions ........................................................................................... 243

    The preprocessor ........................................................................................... 261

    Library functions ............................................................................................. 267

    The linker configuration file ...................................................................... 275

    Section reference ............................................................................................ 295

    Implementation-defined behavior .......................................................... 299

    IAR utilities ........................................................................................................ 311

    Glossary .............................................................................................................. 319

    Index ..................................................................................................................... 335

  • DARM-1

    v

    ContentsTables .................................................................................................................... xvii

    Preface ................................................................................................................... xix

    Who should read this guide ..............................................................xixHow to use this guide .........................................................................xixWhat this guide contains ................................................................... xxOther documentation ..........................................................................xxi

    Further reading .................................................................................xxiiDocument conventions .....................................................................xxiii

    Typographic conventions ................................................................xxiii

    Part 1. Using the IAR build tools ........................................ 1Introduction to the IAR build tools .......................................................... 3

    The IAR build toolsan overview ..................................................... 3IAR C/C++ Compiler ........................................................................... 3IAR assembler ...................................................................................... 4The IAR ILINK Linker ........................................................................ 4External tools ....................................................................................... 4

    IAR language overview ........................................................................... 5Device support ........................................................................................... 5

    Supported ARM devices ...................................................................... 5Preconfigured support files .................................................................. 6Examples for getting started ................................................................ 6

    Special support for embedded systems .......................................... 6Extended keywords .............................................................................. 6Pragma directives ................................................................................. 7Predefined symbols .............................................................................. 7Special function types .......................................................................... 7Accessing low-level features ............................................................... 7

  • DARM-1

    viARM IAR C/C++ Development GuideCompiling and linking

    Developing embedded applications ........................................................... 9

    Developing embedded software using IAR build tools ............ 9Mapping of internal and external memory ........................................... 9Communication with peripheral units ................................................ 10Event handling ................................................................................... 10System startup .................................................................................... 10Real-time operating systems .............................................................. 10Interoperability with other build tools ............................................... 11

    The build processan overview ...................................................... 11The translation process ....................................................................... 12The linking process ............................................................................ 13After linking ....................................................................................... 14

    Application executionan overview ............................................. 15The initialization phase ...................................................................... 15The execution phase ........................................................................... 19The termination phase ........................................................................ 19

    Basic project configuration ................................................................. 19Processor configuration ...................................................................... 20Optimization for speed and size ......................................................... 21Runtime environment ......................................................................... 22

    Data storage ...................................................................................................... 25

    Introduction ............................................................................................. 25The stack and auto variables ............................................................. 25Dynamic memory on the heap ........................................................ 27

    Functions ............................................................................................................... 29

    Function-related extensions .............................................................. 29ARM and Thumb code ........................................................................ 29Execution in RAM ................................................................................... 30Primitives for interrupts, concurrency, and OS-related programming ............................................................................................ 30

    Interrupt functions .............................................................................. 31Installing exception functions ............................................................ 31

  • DARM-1

    Contents

    vii

    Interrupts and fast interrupts .............................................................. 32Nested interrupts ................................................................................ 33Software interrupts ............................................................................. 33Interrupt operations ............................................................................ 34Interrupts for ARM Cortex-M ............................................................ 34C++ and special function types ......................................................... 35

    Linking using ILINK ........................................................................................ 37

    Linkingan overview ............................................................................ 37Modules and sections ............................................................................ 38The linking process ................................................................................ 39Placing code and datathe linker configuration file .............. 40

    A simple example of a configuration file ........................................... 41Initialization at system startup ......................................................... 43

    The initialization process ................................................................... 43

    Linking your application ................................................................................ 45

    Linking considerations .......................................................................... 45Choosing a linker configuration file .................................................. 45Defining your own memory areas ...................................................... 46Placing sections .................................................................................. 48Reserving space in RAM ................................................................... 49Keeping modules ................................................................................ 49Keeping symbols and sections ........................................................... 50Application startup ............................................................................. 50Setting up the stack ............................................................................ 50Setting up the heap ............................................................................. 50Setting up the atexit limit ................................................................... 51Changing the default initialization ..................................................... 51Interaction between ILINK and the application ................................. 53Standard library handling ................................................................... 54Other output formats than ELF/DWARF ........................................... 54Veneers ............................................................................................... 54

    Hints for troubleshooting .................................................................. 55Relocation errors ................................................................................ 55

  • DARM-1

    viiiARM IAR C/C++ Development GuideCompiling and linking

    The DLIB runtime environment .............................................................. 57

    Introduction to the runtime environment .................................. 57Runtime environment functionality ................................................... 57Library selection ................................................................................ 58Situations that require library building .............................................. 59Library configurations ....................................................................... 59Low-level interface for debug support ............................................... 60

    Using a prebuilt library ........................................................................ 60Library filename syntax ..................................................................... 61Customizing a prebuilt library without rebuilding ............................ 62

    Choosing formatters for printf and scanf ..................................... 63Choosing printf formatter ................................................................... 63Choosing scanf formatter .................................................................. 64

    Overriding library modules ............................................................... 65Building and using a customized library ....................................... 67

    Setting up a library project ................................................................. 67Modifying the library functionality .................................................... 67Using a customized library ................................................................ 68

    System startup and termination ...................................................... 68System startup .................................................................................... 69System termination ............................................................................ 71

    Customizing system initialization ................................................... 72__low_level_init ............................................................................... 72Modifying the file cstartup.s ............................................................. 73

    Standard streams for input and output ........................................ 73Implementing low-level character input and output .......................... 73

    Configuration symbols for printf and scanf ................................. 75Customizing formatting capabilities .................................................. 76

    File input and output ............................................................................. 76Locale ........................................................................................................... 77

    Locale support in prebuilt libraries .................................................... 77Customizing the locale support .......................................................... 77Changing locales at runtime .............................................................. 78

  • DARM-1

    Contents

    ix

    Environment interaction ..................................................................... 79Signal and raise ........................................................................................ 80Time ............................................................................................................. 80Strtod ........................................................................................................... 80Assert ........................................................................................................... 81Atexit ........................................................................................................... 81C-SPY Debugger runtime interface .............................................. 81

    Low-level debugger runtime interface ............................................... 82The debugger terminal I/O window ................................................... 82

    Checking module consistency ........................................................... 83Runtime model attributes ................................................................... 83Using runtime model attributes .......................................................... 84

    Assembler language interface ................................................................... 85

    Mixing C and assembler ....................................................................... 85Intrinsic functions .............................................................................. 85Mixing C and assembler modules ...................................................... 86Inline assembler ................................................................................ 87

    Calling assembler routines from C ................................................. 88Creating skeleton code ....................................................................... 88Compiling the code ............................................................................ 89

    Calling assembler routines from C++ ............................................ 90Calling convention .................................................................................. 91

    Function declarations ........................................................................ 92Using C linkage in C++ source code ................................................. 92Preserved versus scratch registers ...................................................... 92Function entrance .............................................................................. 93Function exit ..................................................................................... 95Examples ............................................................................................ 96

    Call frame information ......................................................................... 96

    Using C++ ............................................................................................................ 99

    Overview .................................................................................................... 99Standard Embedded C++ ................................................................... 99Extended Embedded C++ ................................................................ 100

  • DARM-1

    xARM IAR C/C++ Development GuideCompiling and linking

    Enabling C++ support ...................................................................... 101Feature descriptions ............................................................................ 101

    Classes .............................................................................................. 101Functions .......................................................................................... 102Templates ........................................................................................ 102Variants of casts ............................................................................... 102Mutable ............................................................................................ 102Namespace ...................................................................................... 103The STD namespace ........................................................................ 103Pointer to member functions ............................................................ 103Using interrupts and EC++ destructors ............................................ 103

    C++ language extensions ................................................................... 104

    Application-related considerations ....................................................... 107

    Stack considerations ........................................................................... 107Exception stacks ............................................................................... 107

    Heap considerations ............................................................................ 108Interaction between the tools and your application ............. 109Checksum calculation ......................................................................... 111

    Calculating a checksum ................................................................... 111Adding a checksum function to your source code ........................... 112Things to remember ......................................................................... 114

    AEABI compliance ............................................................................... 114Linking AEABI compliant modules using the IAR ILINK Linker ..................................................................... 115Linking AEABI compliant modules using a linker from adifferent vendor ................................................................................ 115Enabling AEABI compliance in the compiler ................................. 116

    Efficient coding for embedded applications ...................................... 117

    Selecting data types ............................................................................. 117Using efficient data types ................................................................. 117Floating-point types ......................................................................... 118Alignment of elements in a structure ............................................... 119Anonymous structs and unions ........................................................ 119

  • DARM-1

    Contents

    xi

    Controlling data and function placement in memory .......... 121Data placement at an absolute location ............................................ 122Data and function placement in sections .......................................... 123

    Controlling compiler optimizations ............................................. 124Scope for performed optimizations .................................................. 124Optimization levels .......................................................................... 124Speed versus size ............................................................................. 125Fine-tuning enabled transformations ............................................... 126

    Writing efficient code ......................................................................... 128Saving stack space and RAM memory ............................................ 129Function prototypes .......................................................................... 129Integer types and bit negation .......................................................... 130Protecting simultaneously accessed variables .................................. 131Accessing special function registers ................................................ 131Passing values between C and assembler objects ............................ 132Non-initialized variables .................................................................. 133

    Part 2. Reference information ........................................... 135External interface details ............................................................................ 137

    Invocation syntax .................................................................................. 137Compiler invocation syntax ............................................................. 137ILINK invocation syntax ................................................................. 137Passing options ................................................................................. 138Environment variables ..................................................................... 139

    Include file search procedure .......................................................... 139Compiler output ................................................................................... 140ILINK output .......................................................................................... 141Diagnostics .............................................................................................. 142

    Message format for the compiler ..................................................... 142Message format for the linker .......................................................... 143Severity levels .................................................................................. 143Setting the severity level .................................................................. 144Internal error .................................................................................... 144

  • DARM-1

    xiiARM IAR C/C++ Development GuideCompiling and linking

    Compiler options ........................................................................................... 145

    Options syntax ....................................................................................... 145Types of options ............................................................................... 145Rules for specifying parameters ....................................................... 145

    Compiler options summary ............................................................. 148Descriptions of options ...................................................................... 150

    Linker options .................................................................................................. 175

    Linker options summary ................................................................... 175Descriptions of linker options ......................................................... 176

    Data representation ...................................................................................... 195

    Alignment ................................................................................................ 195Alignment in the ARM IAR C/C++ Compiler ................................ 196

    Byte order ................................................................................................ 196Basic data types .................................................................................... 196

    Integer types ..................................................................................... 196Floating-point types ........................................................................ 199

    Pointer types .......................................................................................... 201Function pointers .............................................................................. 201Data pointers .................................................................................... 201Casting ............................................................................................. 201

    Structure types ...................................................................................... 202Alignment ......................................................................................... 202General layout ................................................................................. 202Packed structure types ..................................................................... 203

    Type qualifiers ........................................................................................ 204Declaring objects volatile ................................................................ 204Declaring objects const .................................................................... 205

    Data types in C++ ................................................................................. 205

    Compiler extensions .................................................................................... 207

    Compiler extensions overview ....................................................... 207Enabling language extensions .......................................................... 208

  • DARM-1

    Contents

    xiii

    C language extensions ........................................................................ 208Important language extensions ......................................................... 208Useful language extensions .............................................................. 210Minor language extensions .............................................................. 214

    Extended keywords ....................................................................................... 217

    General syntax rules for extended keywords ........................... 217Type attributes .................................................................................. 217Object attributes .............................................................................. 219

    Summary of extended keywords ................................................... 220Descriptions of extended keywords ............................................. 220

    Pragma directives ............................................................................................ 227

    Summary of pragma directives ...................................................... 227Descriptions of pragma directives ................................................ 228

    Intrinsic functions ........................................................................................... 243

    Intrinsic functions summary ............................................................ 243Descriptions of intrinsic functions ................................................. 246

    The preprocessor ........................................................................................... 261

    Overview of the preprocessor ........................................................ 261Descriptions of predefined preprocessor symbols ................. 262Descriptions of miscellaneous preprocessor extensions ..... 265

    Library functions ............................................................................................. 267

    Introduction ............................................................................................ 267Header files ...................................................................................... 267Library object files ........................................................................... 267Reentrancy ....................................................................................... 268

    IAR DLIB Library .................................................................................. 268C header files ................................................................................... 269C++ header files ............................................................................... 270Library functions as intrinsic functions ........................................... 272Added C functionality ...................................................................... 272

  • DARM-1

    xivARM IAR C/C++ Development GuideCompiling and linking

    The linker configuration file ...................................................................... 275

    Overview .................................................................................................. 275Defining memories and regions ..................................................... 276Regions ...................................................................................................... 277Section handling .................................................................................... 280Section selection ................................................................................... 286Using symbols, expressions, and numbers ................................ 289Structural configuration .................................................................... 292

    Section reference ............................................................................................ 295

    Summary of sections ......................................................................... 295Descriptions of sections and blocks .............................................. 296

    Implementation-defined behavior .......................................................... 299

    Descriptions of implementation-defined behavior ................ 299Translation ....................................................................................... 299Environment ..................................................................................... 300Identifiers ......................................................................................... 300Characters ......................................................................................... 300Integers ............................................................................................. 302Floating point ................................................................................... 302Arrays and pointers .......................................................................... 303Registers ........................................................................................... 303Structures, unions, enumerations, and bitfields ............................... 303Qualifiers .......................................................................................... 304Declarators ....................................................................................... 304Statements ........................................................................................ 304Preprocessing directives ................................................................... 304IAR DLIB Library functions ............................................................ 306

    IAR utilities ........................................................................................................ 311

    The IAR Archive Builderiarchive .............................................. 311Invocation syntax ............................................................................. 311Summary of iarchive options ........................................................... 312Descriptions of options .................................................................... 312

  • DARM-1

    Contents

    xv

    Diagnostic messages ........................................................................ 313The IAR Checksum Calculatorichecksum ............................ 314

    Invocation syntax ............................................................................. 314Summary of ichecksum options ....................................................... 315Descriptions of options .................................................................... 315

    Glossary .............................................................................................................. 319

    Index ..................................................................................................................... 335

  • DARM-1

    xviARM IAR C/C++ Development GuideCompiling and linking

  • DARM-1

    xvii

    Tables1: Typographic conventions used in this guide ...................................................... xxiii2: Command line options for specifying library and dependency files ..................... 233: Sections holding initialized data ........................................................................... 434: Description of a relocation error ........................................................................... 555: Library configurations ........................................................................................... 596: Customizable items ............................................................................................... 627: Formatters for printf .............................................................................................. 648: Formatters for scanf .............................................................................................. 659: Descriptions of printf configuration symbols ....................................................... 7510: Descriptions of scanf configuration symbols ...................................................... 7511: Low-level I/O files .............................................................................................. 7612: Functions with special meanings when linked with debug info ......................... 8113: Example of runtime model attributes .................................................................. 8314: Registers used for passing parameters ................................................................ 9415: Registers used for returning values ..................................................................... 9516: Call frame information resources defined in a names block ............................... 9717: Exception stacks ................................................................................................ 10818: Compiler optimization levels ............................................................................ 12419: Compiler environment variables ....................................................................... 13920: ILINK environment variables ........................................................................... 13921: Error return codes .............................................................................................. 14122: Compiler options summary ............................................................................... 14823: Linker options summary ................................................................................... 17524: Integer types ...................................................................................................... 19625: Floating-point types .......................................................................................... 19926: Extended keywords summary ........................................................................... 22027: Pragma directives summary .............................................................................. 22728: Intrinsic functions summary .............................................................................. 24329: Predefined symbols ........................................................................................... 26230: Values for specifying different CPU core families in __TID__ ....................... 26431: Traditional standard C header filesDLIB ...................................................... 269

  • DARM-1

    xviiiARM IAR C/C++ Development GuideCompiling and linking

    32: Embedded C++ header files .............................................................................. 27033: Additional Embedded C++ header filesDLIB ............................................... 27034: Standard template library header files ............................................................... 27135: New standard C header filesDLIB ................................................................ 27136: Section summary ............................................................................................... 29537: Message returned by strerror()IAR DLIB library ......................................... 30938: iarchive parameters ........................................................................................... 31139: iarchive options summary ................................................................................. 31240: ichecksum parameters ....................................................................................... 31441: ichecksum options summary ............................................................................. 315

  • DARM-1

    xix

    PrefaceWelcome to the ARM IAR C/C++ Development Guide. The purpose of this guide is to provide you with detailed reference information that can help you to use the ARM IAR C/C++ build tools 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 guideYou should read this guide if you plan to develop an application using the C or C++ language for the ARM core and need to get detailed reference information on how to use the ARM IAR C/C++ build tools. In addition, you should have a working knowledge of the following:

    ! The architecture and instruction set of the ARM core. Refer to the documentation from Advanced RISC Machines Ltd for information about the ARM core

    ! The C or C++ programming language! Application development for embedded systems! The operating system of your host computer.

    How to use this guide When you start using the ARM IAR C/C++ compiler and linker, you should read Part 1. Using the IAR build tools in this guide.

    When you are familiar with the compiler and linker and have already configured your project, you can focus more on Part 2. Reference information.If you are new to using the IAR Systems build tools, we recommend that you first study the ARM IAR Embedded Workbench IDE User Guide. This guide contains a product overview, tutorials that can help you get started, conceptual and user information about the IAR Embedded Workbench IDE and the IAR C-SPY Debugger, and corresponding reference information.

  • DARM-1

    xx

    What this guide contains

    ARM IAR C/C++ Development GuideCompiling and linking

    What this guide contains Below is a brief outline and summary of the chapters in this guide.

    Part 1. Using the IAR build tools

    ! Introduction to the IAR build tools gives an introduction to the IAR build tools, which includes an overview of the tools, the programming languages, the available device support, and extensions provided for supporting specific features of the ARM core.

    ! Developing embedded applications gives the information you need to get started developing your embedded software using the IAR build tools.

    ! Data storage describes how data can be stored in memory. ! Functions gives a brief overview of function-related extensionsmechanisms for

    controlling functionsand describes some of these mechanisms in more detail.! Linking using ILINK describes the linking process using the IAR ILINK Linker and

    the related concepts.! Linking your application lists a number of aspects that you must consider when

    linking your application, including using ILINK options and tailoring the linker configuration file.

    ! 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.

    ! 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++.

    ! Application-related considerations discusses a selected range of application issues related to using the compiler and linker.

    ! Efficient coding for embedded applications gives hints about how to write code that compiles to efficient code for an embedded application.

    Part 2. Reference information

    ! External interface details provides reference information about how the compiler and linker interact with their environmentthe invocation syntax, methods for passing options to the compiler and linker, environment variables, the include file search procedure, and the different types of compiler and linker output. The chapter also describes how the diagnostic system works.

  • DARM-1

    Preface

    xxi

    ! Compiler options explains how to set options, gives a summary of the options, and contains detailed reference information for each compiler option.

    ! Linker options gives a summary of the options, and contains detailed reference information for each linker option.

    ! Data representation describes the available data types, pointers, and structure types. This chapter also gives information about type and object attributes.

    ! Compiler extensions gives a brief overview of the compiler extensions to the ISO/ANSI C standard. More specifically the chapter describes the available C language extensions.

    ! Extended keywords gives reference information about each of the ARM-specific keywords that are extensions to the standard C/C++ language.

    ! Pragma directives gives reference information about the pragma directives.! Intrinsic functions gives reference information about the functions that can be used

    for accessing ARM-specific low-level features.! The preprocessor gives a brief overview of the preprocessor, including reference

    information about the different preprocessor directives, symbols, and other related information.

    ! Library functions gives an introduction to the C or C++ library functions, and summarizes the header files.

    ! The linker configuration file describes the purpose of the linker configuration file and describes its contents.

    ! Section reference gives reference information about the use of sections.! Implementation-defined behavior describes how the ARM IAR C/C++ Compiler.

    handles the implementation-defined areas of the C language standard.! IAR utilities describes the IAR utilities iarchive and ichecksum.

    Glossary

    The glossary contains definitions of programming terms.

    Other documentationThe complete set of IAR Systems development tools for the ARM core 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 ARM IAR Embedded Workbench IDE User Guide

    ! Programming for the ARM IAR Assembler, refer to the ARM IAR Assembler Reference Guide

  • DARM-1

    xxii

    Other documentation

    ARM IAR C/C++ Development GuideCompiling and linking

    ! Using the IAR DLIB Library functions, refer to the online help system! Porting application code and projects created with a previous ARM IAR Embedded

    Workbench IDE, refer to ARM IAR Embedded Workbench Migration Guide! Developing safety-critical applications using the MISRA C guidelines, refer to the

    IAR Embedded Workbench MISRA C Reference 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:

    ! ARM Architecture Reference Manual. ARM Limited.! Barr, Michael, and Andy Oram, ed. Programming Embedded Systems in C and

    C++ . OReilly & Associates. ! Furber, Steve, ARM System-on-Chip Architecture. Addison-Wesley.! 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 Jose Lajoie. C++ Primer. Addison-Wesley.! Mann, Bernhard. C fr Mikrocontroller. Franzis-Verlag. [Written in German.]! Sloss, Andrew N. et al, ARM System Developer's Guide: Designing and Optimizing

    System Software. Morgan Kaufmann.! Stroustrup, Bjarne. The C++ Programming Language. Addison-Wesley.We recommend that you visit the following web sites:

    ! The Advanced RISC Machines Ltd web site, www.arm.com, contains information and news about the ARM core, as well as information about the ARM Embedded Application Binary Interface (AEABI).

    ! The IAR Systems web site, www.iar.com, holds application notes and other product information.

    ! The web site www.SevensAndNines.com, maintained by IAR Systems, provides an online user community and resource site for ARM developers.

    ! Finally, the Embedded C++ Technical Committee web site, www.caravan.net/ec2plus, contains information about the Embedded C++ standard.

  • DARM-1

    Preface

    xxiii

    Document conventions 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:

    Style Used for

    computer Text that you enter or that appears on the screen.

    parameter A label representing the actual value you should enter as part of a command.

    [option] An optional part of a command, where [] is part of the described syntax.

    {option} A mandatory part of a command, where {} is part of the described syntax.

    [option] An optional part of a command.{option} A mandatory part of a command.a|b|c Alternatives in a command.bold Names of menus, menu commands, buttons, and dialog boxes that

    appear on the screen.

    reference A 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.

    Table 1: Typographic conventions used in this guide

  • DARM-1

    xxiv

    Document conventions

    ARM IAR C/C++ Development GuideCompiling and linking

  • DARM-1

    1

    Part 1. Using the IAR build toolsThis part of the ARM IAR C/C++ Development Guide includes the following chapters:

    ! Introduction to the IAR build tools

    ! Developing embedded applications

    ! Data storage

    ! Functions

    ! Linking using ILINK

    ! Linking your application

    ! The DLIB runtime environment

    ! Assembler language interface

    ! Using C++

    ! Application-related considerations

    ! Efficient coding for embedded applications.

  • DARM-1

    2

  • DARM-1

    Part 1. Using the IAR build tools 3

    Introduction to the IAR build toolsThis chapter gives an introduction to the IAR build tools for the ARM core, which means you will get an overview of:

    ! The IAR build toolsthe build interfaces, compiler, assembler, and linker

    ! The programming languages

    ! The available device support

    ! The extensions provided by the ARM IAR C/C++ Compiler to support specific features of the ARM core.

    The IAR build toolsan overviewIn the IAR product installation you can find a set of tools, code examples, and user documentation, all suitable for developing software for ARM-based embedded applications. The tools allow you to develop your application in C, C++, or in assembler language.

    For a more detailed product overview, see the ARM IAR Embedded Workbench IDE User Guide. There you can also read about the C-SPY debugger.

    IAR Embedded Workbench is a very powerful Integrated Development Environment (IDE) that allows you to develop and manage complete embedded application projects. It provides an easy-to-learn and highly efficient development environment with maximum code inheritance capabilities, comprehensive and specific target support. IAR Embedded Workbench promotes a useful working methodology, and thus a significant reduction of the development time.

    The compiler, assembler, and linker can also be run from a command line environment, if you want to use them as external tools in an already established project environment.

    IAR C/C++ COMPILER

    The ARM IAR C/C++ Compiler is a state-of-the-art compiler that offers the standard features of the C and C++ languages, plus extensions designed to take advantage of the ARM-specific facilities.

  • DARM-1

    4

    The IAR build toolsan overview

    ARM IAR C/C++ Development GuideCompiling and linking

    IAR ASSEMBLER

    The ARM IAR Assembler is a powerful relocating macro assembler with a versatile set of directives and expression operators. The assembler features a built-in C language preprocessor and supports conditional assembly.

    The ARM IAR Assembler uses the same mnemonics and operand syntax as the Advanced RISC Machines Ltd ARM Assembler, which simplifies the migration of existing code. For detailed information, see the ARM IAR Assembler Reference Guide.

    THE IAR ILINK LINKER

    The IAR ILINK Linker is a powerful, flexible software tool for use in the development of embedded controller applications. It is equally well suited for linking small, single-file, absolute assembler programs as it is for linking large, relocatable, multi-module, C/C++, or mixed C/C++ and assembler programs.

    EXTERNAL TOOLS

    For information about how to extend the tool chain in the IAR Embedded Workbench IDE, see the ARM IAR Embedded Workbench IDE User Guide.

    Included GNU utilities

    Because ILINK both uses and produces industry-standard ELF and DWARF as object format, additional utilities that handle these formats can be used. Specifically, the GNU binary utilities:

    ! ar: Creates, modifies, and extracts from archives, that is, libraries! nm: Lists symbols from object files! objcopy: Copies and translates object files, specifically from ELF to the Intel-hex

    or Motorola S-record formats! objdump: Displays information from object files! readelf: Displays the contents of ELF format files! size: Lists section sizes and total size! c++filt: Filter to demangle encoded C++ symbols! addr2line: Converts addresses to file and line.

    For information about these utilities, see GNU binutils manual available from the Help menu alternatively in the arm\doc\binutils directory.

  • DARM-1

    Part 1. Using the IAR build tools

    Introduction to the IAR build tools

    5

    IAR language overviewThere are two high-level programming languages you can use with the ARM IAR C/C++ Compiler:

    ! C, the most widely used high-level programming language used in the embedded systems industry. Using the ARM 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 Embedded C++, with additional features such as full template support, multiple inheritance, 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. For more details, see the chapter Compiler extensions.

    It is also possible to implement parts of the application, or the whole application, in assembler language. See the ARM IAR Assembler Reference Guide.

    For more information about the Embedded C++ language and Extended Embedded C++, see the chapter Using C++.

    Device supportTo get a smooth start with your product development, the IAR product installation comes with wide range of device-specific support.

    SUPPORTED ARM DEVICES

    The ARM IAR C/C++ Compiler supports several different ARM cores and derivatives based on the instruction sets version 4, 5, 6, and 7. The object code that the compiler generates is not always binary compatible between the cores. Therefore it is crucial to specify a processor option to the compiler. The default core is ARM7TDMI.

  • DARM-1

    6

    Special support for embedded systems

    ARM IAR C/C++ Development GuideCompiling and linking

    PRECONFIGURED SUPPORT FILES

    The IAR product installation contains a vast amount of preconfigured files for supporting different devices.

    Header files for I/O

    Standard peripheral units are defined in device-specific I/O header files with the filename extension h. The product package supplies I/O files for all devices that are available at the time of the product release. You can find these files in the arm\inc\ directory. Make sure to include the appropriate include file in your application source files. If you need additional I/O header files, they can be created using one of the provided ones as a template. For detailed information about the header file format, see EWARM_HeaderFormat.pdf located in the arm\doc\ directory.

    Device description files

    The IAR C-SPY Debugger handles several of the device-specific requirements, such as definitions of peripheral registers and groups of these, by using device description files. These files are located in the arm\config directory and they have the filename extension ddf. To read more about these files, see the ARM IAR Embedded Workbench IDE User Guide and EWARM_DDFFormat.pdf located in the arm\doc\ directory.

    EXAMPLES FOR GETTING STARTED

    The arm\examples directory contains several hundreds of examples of working applications to get a smooth start with your development. The complexity of the examples ranges from simple LED blink to USB mass storage controllers. There are examples provided for most of the supported devices.

    Special support for embedded systemsThis section briefly describes the extensions provided by the ARM IAR C/C++ Compiler to support specific features of the ARM core.

    EXTENDED KEYWORDS

    The ARM IAR C/C++ Compiler provides a set of keywords that can be used for configuring how the code is generated. For example, there are keywords for declaring special function types.

    By default, language extensions are enabled in the IAR Embedded Workbench IDE.

  • DARM-1

    Part 1. Using the IAR build tools

    Introduction to the IAR build tools

    7

    The command line option -e makes the extended keywords available, and reserves them so that they cannot be used as variable names. See, -e, page 157 for additional information.

    For detailed descriptions of the extended keywords, see the chapter Extended keywords.

    PRAGMA DIRECTIVES

    The pragma directives control the behavior of the compiler, for example how it allocates memory, whether it allows extended keywords, and whether it issues warning messages.

    The pragma directives are always enabled in the ARM IAR C/C++ Compiler. They are consistent with ISO/ANSI C, and are very useful when you want to make sure that the source code is portable.

    For detailed descriptions of the pragma directives, see the chapter Pragma directives.

    PREDEFINED SYMBOLS

    With the predefined preprocessor symbols, you can inspect your compile-time environment, for example the CPU mode and time of compilation.

    For detailed descriptions of the predefined symbols, see the chapter The preprocessor.

    SPECIAL FUNCTION TYPES

    The special hardware features of the ARM core are supported by the compilers special function types: software interrupts, interrupts, and fast interrupts. You can write a complete application without having to write any of these functions in assembler language.

    For detailed information, see Primitives for interrupts, concurrency, and OS-related programming, page 30.

    ACCESSING LOW-LEVEL FEATURES

    For hardware-related parts of your application, accessing low-level features is essential. The ARM IAR C/C++ Compiler supports several ways of doing this: intrinsic functions, mixing C and assembler modules, and inline assembler. For information about the different methods, see Mixing C and assembler, page 85.

  • DARM-1

    8

    Special support for embedded systems

    ARM IAR C/C++ Development GuideCompiling and linking

  • DARM-1

    Part 1. Using the IAR build tools 9

    Developing embedded applicationsThis chapter provides the information you need to get started developing your embedded software for the ARM core using the IAR build tools.

    First, you will get an overview of the tasks related to embedded software development, followed by an overview of the build process, including the steps involved for compiling and linking an application.

    Next, the program flow of an executing application is described.

    Finally, you will get an overview of the basic settings needed for a project.

    Developing embedded software using IAR build toolsTypically, embedded software written for a dedicated microcontroller is designed as an endless loop waiting for some external events to happen. The software is located in ROM and execute on reset. There are a number of hardware and software factors that you must consider when writing this kind of software.

    MAPPING OF INTERNAL AND EXTERNAL MEMORY

    Embedded systems typically contain various types of memory, such as on-chip RAM, external DRAM or SRAM, ROM, EEPROM, or flash memory.

    As an embedded software developer, you must understand the features of the different memory types. For example, on-chip RAM is often faster than other types of memories, and variables that are accessed often would in time-critical applications benefit from being placed here. Conversely, some configuration data may be accessed seldom but must maintain their value after power off, so they should be saved in EEPROM or flash memory.

    For efficient memory usage, the compiler provides several mechanisms for controlling placement of functions and data objects in memory. For an overview see Controlling data and function placement in memory, page 121. The linker places sections of code in memory according to the directives you specify in the linker configuration file, see Placing code and datathe linker configuration file, page 40.

  • DARM-1

    10

    Developing embedded software using IAR build tools

    ARM IAR C/C++ Development GuideCompiling and linking

    COMMUNICATION WITH PERIPHERAL UNITS

    An embedded system does not work without interaction with the external environment. If external devices are connected to the microcontroller, you may need to initialize and control the signalling interface, for example by using chip select pins, and detect and handle external interrupt signals. Typically, this must be initialized and controlled at runtime. The normal way to do this is to use special function registers, or SFRs. These are typically available at dedicated addresses, containing bits that control the chip configuration.

    Standard peripheral units are defined in device-specific I/O header files with the filename extension h. See Device support, page 5. For an example, see Accessing special function registers, page 131.

    EVENT HANDLING

    In embedded systems, using interrupts is a method for handling external events immediately; for example, detecting that a button has been pressed. In general, when an interrupt occurs in the code, the core simply stops executing the code it runs, and starts executing an interrupt routine instead.

    The ARM IAR C/C++ Compiler supports the following processor exception types: interrupts, software interrupts, and fast interrupts, which means that you can write your interrupt routines in C, see Interrupt functions, page 31.

    SYSTEM STARTUP

    In all embedded systems, system startup code is executed to initialize the systemboth the hardware and the software systembefore the main function of the application is called. The CPU imposes this by starting execution from a fixed memory address.

    As an embedded software developer, you must ensure that the startup code is located at the dedicated memory addresses, or can be accessed using a pointer from the vector table. This means that startup code and the initial vector table must be placed in non-volatile memory, such as ROM, EPROM, or flash.

    A C/C++ application further needs to initialize all global variables. This initialization is handled by the linker and the system startup code in conjunction. For more information, see Application executionan overview, page 15.

    REAL-TIME OPERATING SYSTEMS

    In many cases, the embedded application is the only software running in the system. However, there are some advantages for using an RTOS.

  • DARM-1

    Part 1. Using the IAR build tools

    Developing embedded applications

    11

    For example, the timing of high-priority tasks is not affected by other parts of the program which are executed in lower priority tasks. This typically makes a program more deterministic and can reduce power consumption by using the CPU efficiently and putting the CPU in a lower-power state when idle.

    Using an RTOS can make your program easier to read and maintain, as well as smaller in many cases. Application code can be cleanly separated in tasks which are truly independent of each other. This makes teamwork easier, as the development work can be easily split into separate tasks which are handled by one developer or a group of developers.

    Finally, using an RTOS reduces the hardware dependence and creates a clean interface to the application, making it easier to port the program to different target hardware.

    INTEROPERABILITY WITH OTHER BUILD TOOLS

    The IAR compiler and linker provide support for AEABI, the Embedded Application Binary Interface for ARM. For more information about this interface specification, see the www.arm.com web site.

    The advantage of this interface is the interoperability between vendors supporting it; an application can be built up of libraries of object files produced by different vendors and linked with a linker from any vendor, as long as they adhere to the AEABI standard.

    AEABI specifies full compatibility for C and C++ object code, and for the C library. The AEABI does not include specifications for the C++ library.

    For more information about the AEABI support in the IAR build tools, see AEABI compliance, page 114.

    The ARM IAR build tools version 5.xx are not fully compatible with earlier versions of the product, see the ARM IAR Embedded Workbench Migration Guide for more information.

    The build processan overviewThis section gives an overview of the build process; how the various build toolscompiler, assembler, and linkerfit together, going from source code to an executable image.

    To get familiar with the process in practice, you should run one or more of the tutorials available in the ARM IAR Embedded Workbench IDE User Guide.

  • DARM-1

    12

    The build processan overview

    ARM IAR C/C++ Development GuideCompiling and linking

    THE TRANSLATION PROCESS

    There are two tools in the IAR Embedded Workbench IDE that translate application source files to intermediary object files. The IAR C/C++ Compiler and the IAR relocatable assembler. Both produces relocatable object files in the industry-standard format ELF, including the DWARF format for debug information.

    Note: The compiler can also be used for translating C/C++ source code into assembler source code. If required, you can modify the assembler source code which then can be assembled into object code. For more information about the IAR Assembler, see the ARM IAR Assembler Reference Guide.

    The following illustration shows the translation process:

    Figure 1: The build process before linking

    After the translation, you can choose to pack any number of modules into an archive, or in other words, a library. The important reason you should use libraries is that you can collect your object files in logical entities. Each module in a library is conditionally linked in the application, or in other words, is only included in the application if the module is used directly or indirectly by a module supplied as an object file. Optionally, you can create a library; then use the IAR utility iarchive alternatively the GNU binary utility ar.

  • DARM-1

    Part 1. Using the IAR build tools

    Developing embedded applications

    13

    THE LINKING PROCESS

    The relocatable modules, in object files and libraries, produced by the IAR compiler and assembler cannot be executed as is. To become an executable application, they need to be linked.

    Note: Modules produced by a toolset from another vendor can be included in the build as well. Be aware that this also might require a compiler utility library from the same vendor.

    The IAR ILINK Linker (ilinkarm.exe) is used for building the final application. Normally, ILINK requires the following information as input:

    ! A number of object files and possibly certain libraries! A program start label (set by default)! The linker configuration file that describes placement of code and data in the

    memory of the target system.

    The following illustration shows the linking process:

    Figure 2: The linking process

    Note: The standard C/C++ library contains support routines for the compiler as well as the implementation of the C/C++ standard library functions.

  • DARM-1

    14

    The build processan overview

    ARM IAR C/C++ Development GuideCompiling and linking

    During the linking, ILINK might produce error messages and logging messages on stdout and stderr. The log messages are useful for understanding why an application was linked the way it was, for example, why a module was included or a section removed.

    For an in-depth description of the procedure performed by ILINK, see The linking process, page 39.

    AFTER LINKING

    The IAR ILINK Linker produces an absolute object file in ELF format that contains the executable image. After linking, the produced absolute executable image can be used for:

    ! Loading into the IAR C-SPY Debugger or any other external debugger that reads ELF and DWARF.

    ! Programming to a flash/PROM using a flash/PROM-programmer. Before this is possible, the actual bytes in the image need to be converted into the standard Motorola 32-bit S-record format or the Intel Hex-32 format. For this, use the GNU utility objcopy, see GNUs online documentation for further information.

    The following illustration shows the possible uses of the absolute output ELF/DWARF file:

    Figure 3: Possible uses of the absolute output ELF/DWARF file

  • DARM-1

    Part 1. Using the IAR build tools

    Developing embedded applications

    15

    Application executionan overviewThis section gives an overview of the execution of an embedded application divided into three phases:

    ! Initialization phase! Execution phase! Termination phase.

    THE INITIALIZATION PHASE

    The initialization phase is executed when an application is started (the CPU is reset) but before the main function has been entered. The initialization phase can for simplicity be divided into:

    ! Hardware initialization, which generally at least initializes the stack pointer.The hardware initialization is typically performed in the system startup code cstartup.s and if required, by an extra low-level routine that you provide. It might include resetting/starting the rest of the hardware, setting up the CPU, etc, in preparation for the software C/C++ system initialization.

    ! Software C/C++ system initializationTypically, this includes assuring that every global (statically linked) C/C++ symbol receives its proper initialization value before the main function is called.

    ! Application initializationThis depends entirely on your application. Typically, it can include setting up an RTOS kernel and starting initial tasks for an RTOS-driven application. For a bare-bone application, it can include setting up various interrupts, initializing communication, initializing devices, etc.

    For a ROM/flash-based system, constants and functions are already placed in ROM. All symbols placed in RAM have to be initialized before the main function is called. The linker has already divided available RAM into different areas for variables, stack, heap, etc.

  • DARM-1

    16

    Application executionan overview

    ARM IAR C/C++ Development GuideCompiling and linking

    The following sequence of illustrations give a simplified overview of the different stages of the initialization.

    1 When an application is started, the system startup code first performs hardware initialization, such as initialization of the stack pointer to point at the predefined stack area:

    Figure 4: Initializing hardware

  • DARM-1

    Part 1. Using the IAR build tools

    Developing embedded applications

    17

    2 Then, memories that should be zero-initialized are cleared, in other words, filled with zeroes:

    Figure 5: Zero-initializing variables

    Typically, this is data referred to as zero-initialized data; variables declared as, for example, int i = 0;

  • DARM-1

    18

    Application executionan overview

    ARM IAR C/C++ Development GuideCompiling and linking

    3 For initialized data, data declared, for example, like int i = 6; the initializers are copied from ROM to RAM:

    Figure 6: Initializing variables

    4 Finally, the main function is called:

    Figure 7: Calling main

  • DARM-1

    Part 1. Using the IAR build tools

    Developing embedded applications

    19

    For a detailed description about each stage, see System startup and termination, page 68. For more details about initialization of data, see Initialization at system startup, page 43.

    THE EXECUTION PHASE

    The software of an embedded application is typically implemented as a loop which is either interrupt-driven or uses polling for controlling external interaction or internal events. For an interrupt-driven system, the interrupts are typically initialized at the beginning of the main function.

    In a system with real-time behavior and where responsiveness is critical, a multi-task system might be required. This means that your application software should be complemented with a real-time operating system. In this case, the RTOS and the different tasks must also be initialized at the beginning of the main function.

    THE TERMINATION PHASE

    Typically, an embedded application should never end. If it does, you must have defined a proper end behavior.

    To terminate an application in a controlled way, either call one of the standard C library functions exit, _Exit, or abort, or return from main. If you return from main, the exit function is executed, which means that C++ destructors for static and global variables are called (C++ only) and all open files are closed.Of course, in case of incorrect program logic, the application might terminate in an uncontrolled and abnormal waya system crash.

    To read more about this, see System termination, page 71.

    Basic project configurationIn the command line interface, the following line compiles the source file myfile.c into the object file myfile.o using the default settings:iccarm myfile.c

    On the command line, the following line can be used for starting ILINK:

    ilinkarm myfile.o myfile2.o -o a.out --config xxx.icf

    In this example, myfile.o and myfile2.o are object files, and xxx.icf is the linker configuration file. The option -o specifies the name of the output file.

    Note: By default, the label where the application starts is __iar_program_start. You can change this by using the --entry command line option.

  • DARM-1

    20

    Basic project configuration

    ARM IAR C/C++ Development GuideCompiling and linking

    However, you need to specify some additional options. 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 ARM device you are using. You can specify the options either from the command line interface or in the IAR Embedded Workbench IDE.

    You need settings for:

    ! Processor configuration, that is, processor variant, CPU mode, interworking, VFP and floating-point arithmetic, and byte order

    ! Optimization settings! Runtime environment! Customizing the ILINK configuration, see the chapter Linking your application! AEABI compliance, see AEABI compliance, page 114.

    In addition to these settings, there are many other options and settings available for fine-tuning the result even further. For details about how to set options and for a list of all available options, see the chapter Compiler options, and the ARM IAR Embedded Workbench IDE User Guide, respectively.

    PROCESSOR CONFIGURATION

    To make the compiler generate optimum code, you should configure it for the ARM core you are using.

    Processor variant

    The ARM IAR C/C++ Compiler supports several different ARM cores and devices based on the instruction sets version 4, 5, 6, and 7. All supported cores support Thumb instructions and 64-bit multiply instructions. The object code that the compiler generates is not always binary compatible between the cores. Therefore it is crucial to specify a processor option to the compiler. The default core is ARM7TDMI.

    See the ARM IAR Embedded Workbench IDE User Guide for information about setting the Processor variant option in the IAR Embedded Workbench.

    Use the --cpu option to specify the ARM core; see --cpu, page 151 for syntax information.

    CPU mode

    The ARM IAR C/C++ Compiler supports two CPU modes: ARM and Thumb.

    All functions and function pointers will compile in the mode that you specify, except those explicitly declared __arm or __thumb.

    See the ARM IAR Embedded Workbench IDE User Guide for information about setting the Processor variant or Chip option in the IAR Embedded Workbench.

  • DARM-1

    Part 1. Using the IAR build tools

    Developing embedded applications

    21

    Use the --arm or --thumb option to specify the CPU mode for your project; see --arm, page 150 and --thumb, page 174, for syntax information.

    Interworking

    When code is compiled with the --interwork option, ARM and Thumb code can be freely mixed. Interworking library functions can be called from both ARM and Thumb code. Interworking is default for devices based on the instruction sets version 5, 6, and 7, or when using the --aeabi compiler option. The interworking libraries are slightly larger than non-interworking libraries.

    See the ARM IAR Embedded Workbench IDE User Guide for information about setting the Generate interwork code option in the IAR Embedded Workbench.

    Use the --interwork option to specify interworking capabilities for your project; see --interwork, page 161, for syntax information.

    VFP and floating-point arithmetic

    If you are using an ARM core that contains a Vector Floating Point (VFP) coprocessor, you can use the --fpu option to generate code that carries out floating-point operations utilizing the coprocessor, instead of using the software floating-point library routines.

    See the ARM IAR Embedded Workbench IDE User Guide for information about setting the FPU option in the IAR Embedded Workbench.

    Use the --fpu option to use the coprocessor for floating-point operations; see --fpu, page 159, for syntax information.

    Byte order

    The ARM IAR C/EC++ Compiler supports the big-endian and little-endian byte order. All user and library modules in your application must use the same byte order.

    See the ARM IAR Embedded Workbench IDE User Guide for information about setting the Endian mode option in the IAR Embedded Workbench.

    Use the --endian option to specify the byte order for your project; see --endian, page 158, for syntax information.

    OPTIMIZATION FOR SPEED AND SIZE

    The ARM IAR C/C++ Compiler is a state-of-the-art compiler with an optimizer that performs, among other things, dead-code elimination, constant propagation, inlining, common subexpression elimination, static clustering, instruction scheduling, and precision reduction. It also performs loop optimizations, such as unrolling and induction variable elimination.

  • DARM-1

    22

    Basic project configuration

    ARM IAR C/C++ Development GuideCompiling and linking

    You can decide between several optimization levels and for the highest level you can choose between different optimization goalssize, speed, or balanced. Most optimizations will make the application both smaller and faster. However, when this is not the case, the compiler uses the selected optimization goal to decide how to perform the optimization.

    The optimization level and goal can be specified for the entire application, for individual files, and for individual functions. In addition, some individual optimizations, such as function inlining, can be disabled.

    For details about compiler optimizations and for more information about efficient coding techniques, see the chapter Efficient coding for embedded applications.

    RUNTIME ENVIRONMENT

    To create the required runtime environment you should choose a runtime library and set library options. You may also need to override certain library modules with your own customized versions.

    The runtime library provided is the IAR DLIB Library, which supports ISO/ANSI C and C++. This library also supports floating-point numbers in IEEE 754 format and it can be configured to include different levels of support for locale, file descriptors, multibyte characters, etc.

    The runtime library you choose can be one of the prebuilt libraries, or a library that you have customized and built yourself. The IAR Embedded Workbench IDE provides a library project template that you can use for building your own library version. This gives you full control of the runtime environment. If your project only contains assembler source code, there is no need to choose a runtime library.

    Note: Some tailoring might be required, for example to meet hardware requirements.

    For detailed information about the runtime environment, see the chapter The DLIB runtime environment.

    The way you set up a runtime environment and locate all the related files differs depending on which build interface you are usingthe IAR Embedded Workbench IDE or the command line.

    Setting up for the runtime environment in the IAR Embedded Workbench IDE

    Based on which library configuration you choose and your other project settings, the correct library file is used automatically. For the device-specific include files, a correct include path is set up.

  • DARM-1

    Part 1. Using the IAR build tools

    Developing embedded applications

    23

    Note that for the DLIB library there are different configurationsNormal, and Fullwhich include different levels of support for locale, file descriptors, multibyte characters, etc. See Library configurations, page 59, for more information.

    Setting up for the runtime environment from the command line

    Use the following command line options to explicitly specify the library and the dependency files:

    Normally, it is not needed to specify a library file explicitly, as ILINK automatically uses the correct library file.

    For a description of the prebuilt library object files for the IAR DLIB Library, see Using a prebuilt library, page 60. The table also shows how the object files correspond to the dependent project options, and the corresponding configuration files. Make sure to use the object file that matches your other project options.

    Setting library and runtime environment options

    You can set certain options to reduce the library and runtime environment size:

    ! The formatters used by the functions printf, scanf, and their variants, see Choosing formatters for printf and scanf, page 63.

    ! The size of the stack and the heap, see Setting up the stack, page 50, and Setting up the heap, page 50, respectively.

    Command line Description

    -I arm\inc Specifies the include paths--dlib_config

    C:\...\configfile.hSpecifies the library configuration file, either DLib_Config_Normal.h or DLib_Config_Full.h

    Table 2: Command line options for specifying library and dependency files

  • DARM-1

    24

    Basic project configuration

    ARM IAR C/C++ Development GuideCompiling and linking

  • DARM-1

    Part 1. Using the IAR build tools 25

    Data storage This chapter gives a brief introduction to the memory layout of the ARM core and the fundamental ways data can be stored in memory: on the stack, in static (global) memory, or in heap memory. Finally, detailed information about data storage on the stack and the heap is provided.

    Introduction The ARM core can address 4 Gbytes of continuous memory, ranging from 0x00000000 to 0xFFFFFFFF. Different types of physical memory can be placed in the memory range. A typical application will have both read-only memory (ROM) and read/write memory (RAM). In addition, some parts of the memory range contain processor control registers and peripheral units.

    In a typical application, data can be stored in memory in three different ways:

    ! On the stack. This is memory space that can be used by a function as long as it is executing. When the function returns to its caller, the memory space is no longer valid.

    ! Static memory. This kind of memory is allocated once and for all; it remains valid through the entire execution of the application. Variables that are either global or declared static are placed in this type of memory. The word static in this context means that the amount of memory allocated for this type of variable does not change while the application is running. The ARM core has one single address space and the compiler supports full memory addressing.

    ! On the heap. Once memory has been allocated on the heap, it remains valid until it is explicitly released back to the system by the application. This type of memory is useful