This is the mail archive of the crossgcc@cygnus.com mailing list for the crossgcc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Accessing fixed addresses


What is the best way to define fixed memory locations in C, such as
hardware device registers?  In particular, I'd like to set it up so
that I can easily define multiple groups of identical hardware
registers at different locations, and have the compiler optimize the 
accesses as much as possible.

Here's an example: suppose I have a chip in my system that has three
registers (A, B, C) mapped to sequential addresses BASE, BASE+4,
BASE+8, respectively, where BASE is defined elsewhere.  An easy way to
do this might be:

    #define REG_A (unsigned int)(BASE)
    #define REG_B (unsigned int)(BASE+4)
    #define REG_C (unsigned int)(BASE+8)

Using #define seemed a bit ugly, so I did the following:

    volatile unsigned int *const reg_A = (void *)(BASE);
    volatile unsigned int *const reg_B = (void *)(BASE+4);
    volatile unsigned int *const reg_C = (void *)(BASE+8);

and put these in a .h file.  This worked pretty well; the pointers
gave me a symbolic way to access the locations in the debugger, and
gcc optimized the code so that the memory locations were accessed
directly, rather than through the pointer, so there was no access 
overhead.

The problem was when I tried to use the .h file in more than one
object file, and then link them.  As one would expect, I got multiple
definition errors.

If I make the .h file only have extern declarations, and put the
actual pointers in a .c file, the compiler won't be able to optimize
to direct memory accesses (I assume).  Also, my .h file was actually a
bit more complicated than above: I had a macro to append a prefix to
the register name so I could define different prefixes and bases for
multiple chips.  With an extern-only .h file, I'd have to have a
different object file of pointers for each chip.

I'm sure others have gone down this path before.  Can anyone share
any neat little tricks?

Thanks.
--
John A. Breen
jab3@hotmail.com


______________________________________________________
Get Your Private, Free Email at http://www.hotmail.com
_______________________________________________
New CrossGCC FAQ: http://www.objsw.com/CrossGCC
_______________________________________________
To remove yourself from the crossgcc list, send
mail to crossgcc-request@cygnus.com with the
text 'unsubscribe' (without the quotes) in the
body of the message.