Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer to a specific fixed address

Tags:

c

pointers

How do you assign a specific memory address to a pointer?

The Special Function Registers in a microcontroller 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.

like image 384
Peter Luke Avatar asked Mar 05 '10 19:03

Peter Luke


People also ask

How do you reference a pointer in an address?

A reference must be initialized on declaration while it is not necessary in case of pointer. A reference shares the same memory address with the original variable but also takes up some space on the stack whereas a pointer has its own memory address and size on the stack.

How do you assign the address of a variable to a pointer?

To assign an address of a variable into a pointer, you need to use the address-of operator & (e.g., pNumber = &number ). On the other hand, referencing and dereferencing are done on the references implicitly.

Does a pointer point to an address?

As just seen, a variable which stores the address of another variable is called a pointer. Pointers are said to "point to" the variable whose address they store.


1 Answers

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
like image 199
Carl Norum Avatar answered Oct 08 '22 01:10

Carl Norum