tags:

views:

212

answers:

1

How do you assign a specific memory address to a pointer ? The Special Function Registers in micro such AVR m128 has fixed addresses, the AVR gcc defines the SFR in the io.h header file but I want to handle it myself, thanks for the help in advance.

Cheers!

+5  A: 

Sure, no problem. You can just assign it directly to a variable:

volatile unsigned int *myPointer = (volatile unsigned int *)0x12345678;

What I usually do is declare a memory-mapped I/O macro:

#define mmio32(x)   (*(volatile unsigned long *)(x))

And then define my registers in a header file:

#define SFR_BASE    (0xCF800000)
#define SFR_1       (SFR_BASE + 0x0004)
#define SFR_2       (SFR_BASE + 0x0010)

And then use them:

unsigned long registerValue = mmio32(SFR_1); // read
mmio32(SFR2) = 0x85748312;                   // write
Carl Norum
I would have used a generic void pointer, unless the data at that location is an unsigned int.
Sean A.O. Harney
It's usually a good idea to declare memory-mapped I/O addresses as `volatile`.
Paul R
@Sean, he wants to access a memory mapped register. You can't dereference a `void` pointer, what use would that be?
Carl Norum
@Paul - right! Typing faster than thinking here.
Carl Norum
Well, you could make it volatile void * so that they can't access it casually except by using mmio32(). I wouldn't do it that way, perhaps that's what you want?
Southern Hospitality