cancel
Showing results for 
Search instead for 
Did you mean: 

RAM access by address

usafape
Associate II
Posted on September 12, 2012 at 08:10

Hi,

I want to have the ability to read/write to a specific address in RAM. How could I do that in C.

Any suggestions?

#really-bad-idea
10 REPLIES 10
frankmeyer9
Associate II
Posted on September 12, 2012 at 08:58

I suggest the stm32fxxx.h files as an example, like stm32f10x.h for the F1 family.

This file(s) are found in the standard peripheral libs for the specific part, and declare all fixed peripheral addresses/registers by laying C struct over them.

It is your turn, however, to make sure that you do not interfere with the compiler/linker.

konoppo
Associate II
Posted on September 12, 2012 at 09:15

Are you sure you can't do it in other way? Writing to a fixed address is often not good idea. The best way to do it is modification of the linker script. You have to:

- reduce the .bss section to make some RAM free

- make a new section in this place starting from YOUR_ADDRESS, call it for example .reserved-ram

Now you can simply read or write to/from the address which is in range of your section and you don't need to worry if there are any variables inside:

((uint8_t *) *(YOUR_ADDRESS + 0x01)) = i;

You can also declare the variable like that:

uint8_t ReservedRam[256] __attribute__((section(''.reserved-ram'')));

and this

i = ReservedRam[3];

reads the byte from YOUR_ADDRESS + 0x03. 

Best regards

Konoppo

usafape
Associate II
Posted on September 12, 2012 at 10:01

Looks like I found the solution:

uint32_t *pointer;
uint32_t pi,pii;
//SAVING DATA
pi = 0x20000000;//RECEIVE ADDRESS AND SAVE IT INTO A INTEGER
pointer = (uint32_t*)pi; //CONVERT IT INTO POINTER
*pointer = 100; //SAVE 100 INTO POINTER (0X20000000)
//RETRIEVING DATA
pi = 0x20000000;//RECEIVE ADDRESS AND SAVE IT INTO A INTEGER
pointer = (uint32_t*)pi; //CONVERT IT INTO POINTER
pii = *pointer;//READING ADDRESS THRU THE POINTER

My linker file will have allocated region for this so the compiler won't mess around (hopefully).
frankmeyer9
Associate II
Posted on September 12, 2012 at 13:50

Something I don't understand: what is your point in knowing the address ?

A variable, and it's name, are an abstraction explicitely provided for this issue. You refer to to a memory location by the name of the variable. You neither need to know, nor to care at which address it is actually located. That task  is left to the compiler and the linker.

usafape
Associate II
Posted on September 13, 2012 at 16:37

It has to do with giving flexibility to the system design. If I have access to an specific address, then I only have one command from an external domain to read/write to that address.

Also, I can use the same command to control other configuration registers and so on.

I basically have control over the processor's brain in real time ;)

Posted on September 13, 2012 at 19:43

Are you thinking of something like the old BASIC PEEK and POKE commands?

Posted on September 13, 2012 at 20:32

''I basically have control over the processor's brain in real time''

 

More to the point, there is a veryhigh risk that you will totally derange the processor by messing with its brain carelessly and/or inappropriately!!

Sure, it can be done - but that does not mean that it should be done!
emalund
Associate III
Posted on September 13, 2012 at 20:42

It has to do with giving flexibility to the system design

 

 

WRONG

It has to do with inflexibility to the system design.

you will have to dig out your hex calxulator every darn time you add a variable

Erik
Posted on September 14, 2012 at 01:07

//SAVING DATA

    *((uint32_t *)0x20000000) = 100;

//RETRIEVING DATA

    pii = *((uint32_t *)0x20000000);

If fixed/known addresses are required, perhaps it would be better to use definition files, or linker scripts

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..