Accessing fixed addresses
Brendan Simon
brendan@dgs.monash.edu.au
Wed Mar 3 18:33:00 GMT 1999
I've seen structures defined for devices with large register sets. Then
an instance of that structure is defined as extern for each device.
These go in a header file.
eg.
typedef struct device
{
unsigned short reg1;
unsigned long reg2;
}
extern device device1;
extern device device3;
extern device device2;
The address of each device is then defined/set in a linker command file.
(eg. linkcmds). The symbols can also be set by linker flags/switches on
the CC or LD command.
I think this will do what you want.
Brendan Simon.
John Breen wrote:
> 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.
More information about the crossgcc
mailing list