[go: up one dir, main page]

0% found this document useful (0 votes)
22 views8 pages

Using AR

Yf can

Uploaded by

mohammedmokhimer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views8 pages

Using AR

Yf can

Uploaded by

mohammedmokhimer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Application Note 74

Using ARM C++ in Embedded Systems

Document number: ARM DAI 0074


Issued: March 1999
Copyright ARM Limited 1999

Copyright © 1999 ARM Limited. All rights reserved


Application Note 74
Using ARM C++ in Embedded Systems
Copyright © 1999 ARM Limited. All rights reserved.

Release information
The following changes have been made to this Application Note.

Change history

Date Issue Change

March 1999 A First release

Proprietary notice
ARM, the ARM Powered logo, Thumb and StrongARM are registered trademarks of ARM Limited.
The ARM logo, AMBA, Angel, ARMulator, EmbeddedICE, ModelGen, Multi-ICE, ARM7TDMI, ARM9TDMI, TDMI
and STRONG are trademarks of ARM Limited.
All other products, or services, mentioned herein may be trademarks of their respective owners

Confidentiality status
This document is Open Access. This document has no restriction on distribution.

Feedback on this Application Note


If you have any comments on this Application Note, please send email to errata@arm.com giving:
• the document title
• the document number
• the page number(s) to which your comments refer
• an explanation of your comments.
General suggestions for additions and improvements are also welcome.

ARM web address


http://www.arm.com

ii Copyright © 1999 ARM Limited. All rights reserved Application Note 74


ARM DAI 0074A
Table of Contents

Table of Contents

1 Introduction.................................................................................................................2

2 Using C++ with the Embedded C Library.................................................................3


2.1 Code structure.....................................................................................................3
2.2 Sample code .......................................................................................................4
3 C and C++ Linkage Conventions ..............................................................................6

Application Note 74 Copyright © 1999 ARM Limited. All rights reserved 1


ARM DAI 0074A
Introduction

1 Introduction
This application note gives a brief introduction to the use of ARM C++ in embedded
systems. It provides additional information on system initialization and should be used in
conjunction with the chapter Writing Code for ROM in the ARM Software Development
Toolkit User Guide (ARM DUI 0040). All information in this document relates to the use of
SDT 2.50 and C++ 1.10.
The ARM C++ libraries are used in conjunction with one of the following:
• the semihosted ANSI C library
• the Embedded C library.
This application note discusses the use of the ARM C++ libraries with the Embedded C
Library.
By default, the ARM C++ library makes use of the semihosted ANSI C library. When using
this library, all system initialization is performed by code within the library before your own
code is executed.
Although the semihosted ANSI C library is suitable for early development work, the
Embedded C library has a number of advantages:
• It is significantly smaller.
• It makes no assumptions about the underlying hardware.
• It is simpler to use with your own initialization code started from the reset vector.
Note Only a subset of ANSI C functions is present in the Embedded C library; functions that
depend on the underlying hardware or operating system are omitted. However, it is still
possible to use semihosted operations by invoking semihosted SWIs directly.

2 Copyright © 1999 ARM Limited. All rights reserved Application Note 74


ARM DAI 0074A
Using C++ with the Embedded C Library

2 Using C++ with the Embedded C Library


When using the ARM C++ library with the Embedded C Library, your application code
should be structured in the following way:
1 Carry out any low-level initialization.
2 Initialize the Embedded Heap Manager.
3 Call constructors for top-level objects.
4 Execute the main application code.
5 Call destructors for top-level objects.
These steps are described below and the suggested order must be followed.

2.1 Code structure

2.1.1 Carrying out any low-level initialization

You need to write a small amount of assembler code to perform low-level initialization
tasks before jumping to your C++ code. As an absolute minimum, this low-level
initialization must set up the stack pointer for all modes used.
A full discussion of this task is beyond the scope of this document. This code is not C++
specific and further information with sample code can be found in the chapter Writing
Code for ROM in the ARM Software Development Toolkit User Guide (ARM DUI 0040).

2.1.2 Initializing the Embedded Heap Manager

For all but the simplest C/C++ programs, you will need to make use of the heap to
dynamically allocate memory. Functions that use the heap include malloc() and the new
operator.
The Embedded C Library provides heap management. As the location and size of the
heap varies between targets, your code must pass this information to the heap manager.

Initialization function
You initialize the embedded heap manager by calling the following function with the size
and location of the block of memory to be used as the heap:
struct Heap_Descriptor *__rt_embeddedalloc_init(void *base, size_t size);

This function returns a pointer to a heap descriptor, and this result should be stored.

Call-back function
The Embedded C Library is designed to be fully re-entrant, and so it contains no static
data. For this reason, your code must provide a call-back function:
struct Heap_Descriptor *__rt_heapdescriptor(void)

This allows the library to locate the heap descriptor when required. This function passes
back the result of the __rt_embeddedalloc_init() function.

Application Note 74 Copyright © 1999 ARM Limited. All rights reserved 3


ARM DAI 0074A
Using C++ with the Embedded C Library

2.1.3 Calling constructors for top-level objects


The following function in the C++ library calls all the constructors for top-level objects:
void __cpp_initialise(void)

When using the C++ libraries with the Embedded C Library, your code must call this
function in order to initialize top-level objects properly.

2.1.4 Main application code


You have now performed all necessary initialization of the libraries and you can run your
code.

2.1.5 Calling destructors for top-level objects

Before your application terminates, you should call the following function to call all the
destructors for top-level objects:
void __cpp_finalise(void)
Note This will not be needed in embedded systems that never exit the main application code.

2.2 Sample code


The following code contains a routine called C_Entry that carries out the above steps.

Note that the C_Entry, __rt_embeddedalloc_init, __cpp_initialise and


__cpp_finalise functions use C linkage to facilitate calling from non-C++ code (see
Section 3, C and C++ Linkage Conventions on page 6 for more details).
#include <stddef.h>

extern "C" struct Heap_Descriptor *__rt_embeddedalloc_init(void *base,


size_t size);
extern "C" void __cpp_initialise(void);
extern "C" void __cpp_finalise(void);

void ApplicationCode(void);

struct Heap_Descriptor *hp;

extern "C" struct Heap_Descriptor *__rt_heapdescriptor(void)


{
return hp;
}

extern "C" void C_Entry(void)


{
/* Initialise the Embedded Heap Manager */
/* You will need to change the location to match that of your */
/* target system and set the size appropriately. */
/* The values below are in the SRAM of the ARM Development Board */
/* (PID7T) */

hp = __rt_embeddedalloc_init((void *)0x2030000,0x20000);

/* Call constructors for top level objects */


__cpp_initialise();

/* Execute your code */


ApplicationCode();

4 Copyright © 1999 ARM Limited. All rights reserved Application Note 74


ARM DAI 0074A
Using C++ with the Embedded C Library

/* In embedded systems, you may never exit your own code. */


/* If this is the case the following code is not needed. */

/* Call destructors for top level objects */


__cpp_finalise();

This code is included in the associated example, which is downloadable from the
application notes area of the ARM website.

Application Note 74 Copyright © 1999 ARM Limited. All rights reserved 5


ARM DAI 0074A
C and C++ Linkage Conventions

3 C and C++ Linkage Conventions


C++ compilers normally mangle the names of functions before linking, so that they include
information about the types of any parameters passed to or from the function. The main
use of name mangling is to allow functions to be overloaded. C does not support
overloading, so does not mangle function names.
In C++, extern “C” can be used to indicate that a function uses C and not C++ linkage.
This is illustrated in the code on page 4.
Note that:
• If a C++ function is to be called from C or from the ARM assembler, it should be
declared to use C linkage.
• If a C or ARM assembler function is to be called from C++, the C++ code should
include a function prototype with the C/ASM function marked as having C linkage.
Note For further information please refer to the chapter called Mixed Language
Programming in the ARM Software Development Toolkit User Guide (ARM DUI
0040).

6 Copyright © 1999 ARM Limited. All rights reserved Application Note 74


ARM DAI 0074A

You might also like