Engineer To Engineer Note EE-42
Notes on using Analog Devices’ DSP, audio, & video components from the Computer Products Division
Phone: (800) ANALOG-D or (781) 461-3881, FAX: (781) 461-3010, EMAIL: dsp.support@analog.com
C Programs on the ADSP-2106x
Last Modified: 6/25/96
This application note gives a brief introduction
in programming Analog Devices SHARC DSP in C
language. Attached to this note are two C examples files
irq1.c and linkdma.c. A brief description of their purpose
is given below. All programs have been written with the
development software ADDS-210XX-SW-PC release 3.3
and have been tested on the ADDS-2106x-EZ-LAB. Note
that there is a new C compiler on the FTP site
(ftp.analog.com).
Attached files:
21062c.ach
21060c.ach
def06xc.h
linkdma.c
irq1.c
1. Read The Release Note
There are some restrictions regarding the software tools.
Please read the release notes to be firm with the
restrictions of your development software.
2. Writing An Architecture File
The architecture file is used to describe your target system
on which the C program should run. There are two .ACH
files attached to this application note. 21062C.ACH could
be used to run C code on the EZ-LAB and the
21060c.ACH is for people who upgraded their EZ-LAB
with an ADSP-21060. The /CINIT, /CSTACK and
/CHEAP directives are only used for C programming and
are described in the C TOOLS MANUAL on page 3-3 ff.
The attached .ACH files are only examples to allow a
quick start and if you wish to divide up your memory in
another way refer to the USER’S MANUAL in chapter 5.
produce an optimized code use one of the following
switches.
-O, -O2, -O3
But remember that it is not possible to use the CBUG
feature for code that has been optimized.
Furthermore the iterators (ITER) of the numerical C
extension does not work with the -O3 switch.
Using any optimization switches often cause the
following problem. If a variable is checked in a loop but
is not updated in that loop the compiler removes the test
of this variable out of the loop to minimize the number of
instructions. This could cause problems when the value of
this variable is updated within an interrupt service routine
and should be evaluated in the main loop. To avoid this
behavior it is necessary to declare this variable as volatile.
e.g.
volatile int test_flag;
Sometimes customers wish to place C-Functions not in
the default segment seg_pmco, but in a different one. This
could be easily done with the
G21K.EXE compiler. e.g. There is a main program
(sum.c) and an external function (ext.c) both written in C
language. The main program calls the external function in
the alternative code segment alt_pmco.
/* PRG that calls an external c function in an alternate code segment */
extern int sum(int a,int b);
void main(void)
{
/* --- VAR --- */
int i=5;
int j=3;
/* --- MAIN --- */
i= sum(i,j);
idle();
}
/* PRG END */
3. Writing And Compiling C Code
After you wrote your c code the compiler should be
invoked in the following way:
g21k code.c -a 21062C.ach -o output -g
The -g switch is used to generate debuggable code for the
simulator and emulator. If you want the compiler to
The external functions could look like the
following code:
int sum(int a, int b)
{
return a+b;
}
To compile this example use the command lines
g21k sum.c -c -a 21062c.ach
g21k ext.c -c -mpmcode=alt_pmco
g21k sum.o ext.o -a 21062c.ach -o sum -map -g
macro could be used to define an IO port in C language.
The syntax is:
DEF_PORT( name, type, seg, mem)
The -mpmcode switch chooses the alternative code
segment. Variables placed in the dm, pm memory space
could be placed in an alternative segment by invoking the
compiler with a similar switch.
-mdmdata, -pmdata
4. Extensions to ANSI C
The Analog Devices C compiler is based on the GNU
software and supports ANSI C. Nevertheless there are
some extensions which are very useful. As it is possible
to store data either in the program or data memory of the
Analog Devices DSP an additional keyword indicates
where variables are placed by the linker. Use the PM
directive to place variables in program memory and the
DM directive to place them in data memory. e.g.
float dm source[16]
float pm dest[16]
When no directive is given the linker will place the
variable in the data memory by default. The default
memory segment in which the dm variables are placed is
labeled seg_dmda, and the default segment for variables
which reside in program memory space is called
seg_pmda. Often it is useful to place the variables in
different segments for example external memory space. An
easy way to do this is to use the delivered include file
“macros.h”. To use this comfortable feature add the
directive
#include<macros.h>
to your source code and use the following syntax:
DEF_VAR ( name, type, seg, mem)
name: variable name
type: the actual C type
seg: the segment in the architecture file.
mem: the memory that the variable exists in
pm,dm
Another useful macro is called “DEF_PORT” which has
nearly the same syntax as the above mentioned one. This
name: variable name
type: the actual C type
seg: the segment in the architecture file
mem: the memory that the variable exists in
pm,dm
The necessary segment declaration in the architecture file
could look like the following line.
.SEGMENT /PORT /BEGIN=0x00404000
/END=0x00404000 /DM /width=32 mafeadrs;
It is worthy to have a look at the macros.h file as there are
a lot of useful declarations, that are not documented yet.
Further extensions are described in the C TOOLS
MANUAL chapter 5.
5. Accessing IOP Registers
Most of the SHARC’s control registers are memory
mapped to the address range from 0x0000 to 0x0100. To
access these memory mapped IOP registers you could use
the following syntax in your C code.
#define SYSCON *(int*)0x000
Using this pointer assignment it is now possible to
change the content of the System Control register in the
C file with the following instruction:
SYSCON = 0x1234;
Attached to this note is a header file def06xc.h, that
already defines some IOP registers. This file has to be
included at the beginning of your program to grant this
comfortable way of accessing IOP control registers. The
syntax is:
#include<def06xc.h>
6. Embedded Assembly Language
6.1 Accessing Core Processor System Registers
The core processor system registers (MODE1, MODE2,
ASTAT, STKY, IRPTL, IMASK, IMASKP, USTAT1 &
EE-42 Page 2
Notes on using Analog Devices’ DSP, audio, & video components from the Computer Products Division
Phone: (800) ANALOG-D or (781) 461-3881, FAX: (781) 461-3010, EMAIL: dsp.support@analog.com